From 27441efde70e575aa16e00a2ff3cb78b4d5e0ad3 Mon Sep 17 00:00:00 2001 From: Leonid Date: Thu, 13 Feb 2025 15:23:23 +0300 Subject: [PATCH] =?UTF-8?q?-=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20Bootstrap=20=D0=B8=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=B2=D0=BD=D0=B5=D1=88=D0=BD=D0=B8=D0=B9=20?= =?UTF-8?q?=D0=B2=D0=B8=D0=B4=20=D1=84=D0=BE=D1=80=D0=BC=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.youtube.com/watch?v=a51jGwoTNmI&list=PLU2ftbIeotGpAYRP9Iv2KLIwK36-o_qYk&index=13 --- .../sweater/security/CustomUserDetails.java | 5 ++ src/main/resources/application.properties | 1 + src/main/resources/templates/greeting.ftl | 8 +- src/main/resources/templates/login.ftl | 4 +- src/main/resources/templates/main.ftl | 86 +++++++++++-------- src/main/resources/templates/parts/common.ftl | 11 ++- src/main/resources/templates/parts/login.ftl | 29 +++++-- src/main/resources/templates/parts/navbar.ftl | 29 +++++++ .../resources/templates/parts/security.ftl | 16 ++++ src/main/resources/templates/registration.ftl | 4 +- src/main/resources/templates/userEdit.ftl | 36 +++++--- src/main/resources/templates/userList.ftl | 43 ++++++---- 12 files changed, 189 insertions(+), 83 deletions(-) create mode 100644 src/main/resources/templates/parts/navbar.ftl create mode 100644 src/main/resources/templates/parts/security.ftl diff --git a/src/main/java/net/sytes/kashey/sweater/security/CustomUserDetails.java b/src/main/java/net/sytes/kashey/sweater/security/CustomUserDetails.java index 4dcacca..8413b67 100644 --- a/src/main/java/net/sytes/kashey/sweater/security/CustomUserDetails.java +++ b/src/main/java/net/sytes/kashey/sweater/security/CustomUserDetails.java @@ -1,5 +1,6 @@ package net.sytes.kashey.sweater.security; +import net.sytes.kashey.sweater.domain.Role; import net.sytes.kashey.sweater.domain.User; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; @@ -56,4 +57,8 @@ public class CustomUserDetails implements UserDetails { public User getUser() { return user; } + + public boolean isAdmin() { + return user.getRoles().contains(Role.ADMIN); + } } \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 0ad3c5c..2a17057 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -4,6 +4,7 @@ spring.datasource.username=user spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.freemarker.expose-request-attributes=true +spring.freemarker.expose-session-attributes=true spring.freemarker.suffix=.ftl upload.path=/home/kashey/soft/sweater/uploads diff --git a/src/main/resources/templates/greeting.ftl b/src/main/resources/templates/greeting.ftl index b19ebf4..dc0813e 100644 --- a/src/main/resources/templates/greeting.ftl +++ b/src/main/resources/templates/greeting.ftl @@ -1,7 +1,7 @@ <#import "parts/common.ftl" as com> <@com.page> -
Hello there
- Main page -
- Users page +
Hello, guest
+
+ This is a simple version of Twitter +
\ No newline at end of file diff --git a/src/main/resources/templates/login.ftl b/src/main/resources/templates/login.ftl index 64752ee..8d8525d 100644 --- a/src/main/resources/templates/login.ftl +++ b/src/main/resources/templates/login.ftl @@ -2,7 +2,5 @@ <#import "parts/login.ftl" as l> <@com.page> - Login page - <@l.login "/login" /> - Add new user + <@l.login "/login" false/> \ No newline at end of file diff --git a/src/main/resources/templates/main.ftl b/src/main/resources/templates/main.ftl index 1f1aa1c..a8d1478 100644 --- a/src/main/resources/templates/main.ftl +++ b/src/main/resources/templates/main.ftl @@ -1,44 +1,56 @@ <#import "parts/common.ftl" as com> -<#import "parts/login.ftl" as l> <@com.page> -
- <@l.logout /> - Users page -
- -
-
- - - - - -
-
-
-
- Tag to filer -
- - +
+ + +
-
Messages list
- <#list messages as message> -
- ${message.id} - ${message.text} - ${message.tag} - ${message.authorName} -
- <#if message.filename??> - - -
+ +
+
+
+
+ +
+
+ +
+
+
+ + +
+
+ +
+ +
+
- <#else> - No messages found - -
+
+
+ <#list messages as message> +
+
+ <#if message.filename??> + + +
+
+ ${message.text} + ${message.tag} +
+ +
+ <#else> + No messages found + +
\ No newline at end of file diff --git a/src/main/resources/templates/parts/common.ftl b/src/main/resources/templates/parts/common.ftl index 0df073a..17f81c9 100644 --- a/src/main/resources/templates/parts/common.ftl +++ b/src/main/resources/templates/parts/common.ftl @@ -5,10 +5,19 @@ Sweater + + + - + <#include "navbar.ftl"> +
<#nested> +
+ + + + diff --git a/src/main/resources/templates/parts/login.ftl b/src/main/resources/templates/parts/login.ftl index c3a6812..bc56ad1 100644 --- a/src/main/resources/templates/parts/login.ftl +++ b/src/main/resources/templates/parts/login.ftl @@ -1,15 +1,28 @@ -<#macro login path> -
-
-
-
- +<#macro login path isRegistrationForm=false> + +
+ +
+ +
+
+
+ +
+ +
+
+ <#if !isRegistrationForm> + Add new user + + value="Create" <#else> value="Sign in"/> +
<#macro logout>
- - + +
\ No newline at end of file diff --git a/src/main/resources/templates/parts/navbar.ftl b/src/main/resources/templates/parts/navbar.ftl new file mode 100644 index 0000000..fdd7c03 --- /dev/null +++ b/src/main/resources/templates/parts/navbar.ftl @@ -0,0 +1,29 @@ +<#include "security.ftl"> +<#import "login.ftl" as l> + + \ No newline at end of file diff --git a/src/main/resources/templates/parts/security.ftl b/src/main/resources/templates/parts/security.ftl new file mode 100644 index 0000000..3d6e821 --- /dev/null +++ b/src/main/resources/templates/parts/security.ftl @@ -0,0 +1,16 @@ +<#assign +known = SPRING_SECURITY_CONTEXT?? +> + +<#if known> + <#assign + user = SPRING_SECURITY_CONTEXT.authentication.principal + name = user.getUsername() + isAdmin = user.isAdmin() + > +<#else> + <#assign + name = "unknown" + isAdmin = false + > + \ No newline at end of file diff --git a/src/main/resources/templates/registration.ftl b/src/main/resources/templates/registration.ftl index 6b6b6e7..c6ddc2f 100644 --- a/src/main/resources/templates/registration.ftl +++ b/src/main/resources/templates/registration.ftl @@ -2,10 +2,10 @@ <#import "parts/login.ftl" as l> <@com.page> - Registration page +
Add new user

<#if message??> ${message} - <@l.login "/registration" /> + <@l.login "/registration" true/> diff --git a/src/main/resources/templates/userEdit.ftl b/src/main/resources/templates/userEdit.ftl index 3ed4e12..196fb44 100644 --- a/src/main/resources/templates/userEdit.ftl +++ b/src/main/resources/templates/userEdit.ftl @@ -1,14 +1,30 @@ <#import "parts/common.ftl" as com> <@com.page> -
- - <#list roles as role> -
- +
+ +
+ + +
+ Please provide a username. +
- - - - - +
+ + <#list roles as role> +
+ + +
+ +
+ + + + + +
\ No newline at end of file diff --git a/src/main/resources/templates/userList.ftl b/src/main/resources/templates/userList.ftl index 4fd5ed1..574d1f4 100644 --- a/src/main/resources/templates/userList.ftl +++ b/src/main/resources/templates/userList.ftl @@ -1,23 +1,30 @@ <#import "parts/common.ftl" as com> <@com.page> -
Users
- - - - - - - - - - <#list users as user> +
+
NameRole
+ - - - + + + - - -
${user.username}<#list user.roles as role>${role}<#sep>, editNameRoleActions
- Main page + + + <#list users as user> + + ${user.username} + + <#list user.roles as role> + ${role} + <#sep> + + + Edit + + + + + + Main page +
\ No newline at end of file