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 @@ - +
- Browse all + Browse All + | + Upload New + | + My Account