diff --git a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt index 3cd3d02..927f7b8 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt @@ -7,6 +7,7 @@ import org.springframework.security.access.annotation.Secured import org.springframework.security.core.annotation.AuthenticationPrincipal import org.springframework.stereotype.Controller import org.springframework.ui.Model +import org.springframework.validation.BindingResult import org.springframework.web.bind.annotation.DeleteMapping import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.ModelAttribute @@ -24,7 +25,9 @@ import javax.validation.constraints.NotEmpty @Controller class BaseController @Autowired constructor( - val imageRepository: ImageRepository + val imageRepository: ImageRepository, + val memberRepository: MemberRepository, + val roleRepository: RoleRepository ) { @GetMapping("/") @@ -40,6 +43,23 @@ class BaseController model.addAttribute("redirect", redirect) return "login" } + + @GetMapping("/register") + fun showRegister(model: Model) : String { + val user = Member(name = "", email = "", password = "") + model.addAttribute("user_details", user) + model.addAttribute("submitted", false) + return "register" + } + + @PostMapping("/register") + fun doRegister(@Valid @ModelAttribute("user_details") newUser: Member, bindingResult: BindingResult): String { + if(bindingResult.hasErrors()) return "register" + newUser.password = passwordEncoder().encode(newUser.password) + newUser.roles.add(roleRepository.findByNameIs("USER")) + memberRepository.save(newUser) + return "login" + } } fun userCanEdit(userDetails: CustomUserDetails?, image: Image): Boolean = diff --git a/src/main/kotlin/uk/co/neviyn/booru/Entity.kt b/src/main/kotlin/uk/co/neviyn/booru/Entity.kt index f2f0147..e0743c9 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Entity.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Entity.kt @@ -12,14 +12,20 @@ import javax.persistence.JoinTable import javax.persistence.ManyToMany import javax.persistence.ManyToOne import javax.persistence.SequenceGenerator +import javax.validation.constraints.Email +import javax.validation.constraints.NotBlank import javax.validation.constraints.Pattern @Entity open class Member( @Column(unique = true) + @field:NotBlank(message = "Username is required") open var name: String = "", + @field:NotBlank(message = "Email address is required") + @field:Email(message = "Email address invalid") open var email: String = "", @JsonIgnore + @field:NotBlank(message = "Password is required") open var password: String = "", @JsonIgnore open var enabled: Boolean = true, diff --git a/src/main/kotlin/uk/co/neviyn/booru/Repository.kt b/src/main/kotlin/uk/co/neviyn/booru/Repository.kt index 06ce673..31f2b13 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Repository.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Repository.kt @@ -11,7 +11,9 @@ interface MemberRepository : CrudRepository { fun findByName(name: String): Member? } -interface RoleRepository : CrudRepository +interface RoleRepository : CrudRepository { + fun findByNameIs(name: String) : Role +} interface ImageRepository : JpaRepository { @Suppress("SqlResolve") diff --git a/src/main/resources/templates/fragments.html b/src/main/resources/templates/fragments.html index eef016f..d8be8d9 100644 --- a/src/main/resources/templates/fragments.html +++ b/src/main/resources/templates/fragments.html @@ -43,6 +43,7 @@ + Register