From 4e7643f976a83b81011784434d723efb2ef34ad7 Mon Sep 17 00:00:00 2001 From: neviyn Date: Fri, 7 May 2021 11:20:23 +0100 Subject: [PATCH] Added base profile page, navbar, changed some href binding --- .../kotlin/uk/co/neviyn/booru/Controller.kt | 33 +++++++++++ src/main/kotlin/uk/co/neviyn/booru/Request.kt | 12 ++++ .../kotlin/uk/co/neviyn/booru/Security.kt | 6 +- src/main/resources/templates/fragments.html | 30 +++++++++- src/main/resources/templates/gallery.html | 1 + src/main/resources/templates/landing.html | 8 ++- src/main/resources/templates/login.html | 2 +- src/main/resources/templates/upload.html | 3 +- src/main/resources/templates/user.html | 56 +++++++++++++++++++ 9 files changed, 145 insertions(+), 6 deletions(-) create mode 100644 src/main/kotlin/uk/co/neviyn/booru/Request.kt create mode 100644 src/main/resources/templates/user.html diff --git a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt index 8887e45..bb916d6 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt @@ -7,12 +7,14 @@ import org.springframework.security.core.annotation.AuthenticationPrincipal import org.springframework.stereotype.Controller import org.springframework.ui.Model import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.ModelAttribute import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.multipart.MultipartFile import java.security.MessageDigest import java.util.* +import javax.validation.Valid import javax.validation.constraints.NotEmpty @Controller @@ -78,6 +80,37 @@ class ImageController } +@Controller +@RequestMapping("/user") +class MemberController +@Autowired constructor( + val memberRepository: MemberRepository +) { + + @GetMapping + fun memberDetails(@AuthenticationPrincipal userDetails: CustomUserDetails, model: Model) : String { + val user = DisplayUser(userDetails.getId(), userDetails.username, userDetails.getEmail(), "", "") + model.addAttribute("userData", user) + return "user" + } + + @PostMapping + fun updateLoggedInUser(@Valid @ModelAttribute userData: DisplayUser, @AuthenticationPrincipal userDetails: CustomUserDetails, model: Model): String { + if (userData.id == userDetails.getId() && passwordEncoder().matches(userData.oldPassword, userDetails.password)) { + val user = memberRepository.findById(userDetails.getId()).get() + user.email = userData.email + if (userData.password.isNotEmpty()) user.password = passwordEncoder().encode(userData.password) + memberRepository.save(user) + model.addAttribute("message", "Your profile has been updated") + } else { + model.addAttribute("error", "Incorrect existing password") + } + model.addAttribute("userData", DisplayUser(userData.id, userData.username, userData.email, userData.password, "")) + return "user" + } + +} + @Controller @RequestMapping("/upload") class UploadController diff --git a/src/main/kotlin/uk/co/neviyn/booru/Request.kt b/src/main/kotlin/uk/co/neviyn/booru/Request.kt new file mode 100644 index 0000000..a26d2d9 --- /dev/null +++ b/src/main/kotlin/uk/co/neviyn/booru/Request.kt @@ -0,0 +1,12 @@ +package uk.co.neviyn.booru + +import javax.validation.constraints.NotBlank +import javax.validation.constraints.Positive + +data class DisplayUser( + @field:Positive val id: Long, + val username: String, + val email: String, + val password: String, + @field:NotBlank val oldPassword: String +) \ No newline at end of file diff --git a/src/main/kotlin/uk/co/neviyn/booru/Security.kt b/src/main/kotlin/uk/co/neviyn/booru/Security.kt index 6d17b2a..c5a9c03 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Security.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Security.kt @@ -21,7 +21,7 @@ class SecurityConfig val userDetailsService: CustomUserDetailsService ) : WebSecurityConfigurerAdapter() { override fun configure(http: HttpSecurity) { - http.authorizeRequests().antMatchers("/upload").hasAuthority("USER") + http.authorizeRequests().antMatchers("/upload/**", "/user/**").hasAuthority("USER") .anyRequest().permitAll().and() .formLogin().loginPage("/login").permitAll().and() .logout().logoutSuccessUrl("/").permitAll().and() @@ -50,6 +50,10 @@ constructor( override fun isCredentialsNonExpired(): Boolean = true override fun isEnabled(): Boolean = member.enabled + + fun getId(): Long = member.id + + fun getEmail(): String = member.email } @Service diff --git a/src/main/resources/templates/fragments.html b/src/main/resources/templates/fragments.html index d14d22f..7197617 100644 --- a/src/main/resources/templates/fragments.html +++ b/src/main/resources/templates/fragments.html @@ -1,5 +1,5 @@ - +
@@ -10,5 +10,33 @@
+
+
+ +
+ +
\ No newline at end of file diff --git a/src/main/resources/templates/gallery.html b/src/main/resources/templates/gallery.html index 46dad45..1e4f157 100644 --- a/src/main/resources/templates/gallery.html +++ b/src/main/resources/templates/gallery.html @@ -5,6 +5,7 @@ Gallery +
diff --git a/src/main/resources/templates/landing.html b/src/main/resources/templates/landing.html index 81ecaa1..dd49e91 100644 --- a/src/main/resources/templates/landing.html +++ b/src/main/resources/templates/landing.html @@ -14,12 +14,16 @@
-
+ diff --git a/src/main/resources/templates/login.html b/src/main/resources/templates/login.html index b8cbd86..dd94c28 100644 --- a/src/main/resources/templates/login.html +++ b/src/main/resources/templates/login.html @@ -39,7 +39,7 @@
- Home + Home
diff --git a/src/main/resources/templates/upload.html b/src/main/resources/templates/upload.html index aa74c3e..788ddeb 100644 --- a/src/main/resources/templates/upload.html +++ b/src/main/resources/templates/upload.html @@ -5,6 +5,7 @@ Upload +
@@ -12,7 +13,7 @@
- + diff --git a/src/main/resources/templates/user.html b/src/main/resources/templates/user.html new file mode 100644 index 0000000..a7520b1 --- /dev/null +++ b/src/main/resources/templates/user.html @@ -0,0 +1,56 @@ + + + + + My Account + + +
+
+
+
+

Profile

+
+
+ +
+
+ +
+ Username + +
+
+ Email + +
+
+ Password + +
+

Please enter your existing password to update your profile.

+
+ Existing Password + +
+
+
+
+
+ +
+
+ +
+
+ +
+
+
+
+ +
+
+
+ \ No newline at end of file