Pārlūkot izejas kodu

pisah changepass dan edit user

athrainsky 1 gadu atpakaļ
vecāks
revīzija
5a42c9dd8d

+ 54 - 21
src/main/kotlin/com/server/alb/controllers/BugController.kt

@@ -7,7 +7,6 @@ import com.server.alb.services.ProjectService
 import com.server.alb.services.UserService
 import jakarta.servlet.http.HttpServletRequest
 import jakarta.validation.Valid
-import jakarta.validation.Validator
 import org.springframework.security.authentication.AnonymousAuthenticationToken
 import org.springframework.security.core.Authentication
 import org.springframework.security.core.context.SecurityContextHolder
@@ -31,8 +30,7 @@ class BugController(
     private val bugService: BugService,
     private val userService: UserService,
     private val projectService: ProjectService,
-    private val passwordEncoder: PasswordEncoder,
-    private val validator: Validator
+    private val passwordEncoder: PasswordEncoder
 ) {
     val qcResultType = Status.QCResultType.values()
     val worList = Status.WorkedType.values()
@@ -81,7 +79,6 @@ class BugController(
         newUser.password = passwordEncoder.encode(userRole.password)
         newUser.roles = userRole.role
         newUser.isEnabled = true
-        println(newUser)
         return if (bindingResult.hasErrors()) {
             model.addAttribute("role", getRole())
             "reg"
@@ -94,7 +91,10 @@ class BugController(
 
     @RequestMapping(value = ["/userList"])
     fun viewUser(model: Model): String {
+        val myUserDetails = SecurityContextHolder.getContext().authentication.name
+        val userLogin = userRepository.findByUsername(myUserDetails)
         model.addAttribute("user", getUser())
+        model.addAttribute("user2", userService[userLogin.get().id])
         return "userList"
     }
 
@@ -119,7 +119,6 @@ class BugController(
         model: Model,
     ): String {
         return if (bindingResult.hasErrors()) {
-            println(project)
             "add_proj"
         } else {
             projectRepository.save(project)
@@ -186,7 +185,6 @@ class BugController(
                 bug.qcId = userLogin.get()
             }
         }
-        println(bug)
         return if (bindingResult.hasErrors()) {
             model.addAttribute("user", getUser())
             model.addAttribute("platform", getPlatform())
@@ -210,7 +208,6 @@ class BugController(
     ): String {
         val referer: String = request.getHeader("Referer")
         val url = referer.split("=")
-        val url2 = referer.split("8080/")
         if (bug.qcId == null) {
             val myUserDetails = SecurityContextHolder.getContext().authentication.name
             val userLogin = userRepository.findByUsername(myUserDetails)
@@ -219,7 +216,6 @@ class BugController(
             }
         }
         return if (bindingResult.hasErrors()) {
-            println(bug)
             model.addAttribute("listQc", getUser())
             model.addAttribute("listPro", getUser())
             model.addAttribute("platform", getPlatform())
@@ -229,7 +225,7 @@ class BugController(
             model.addAttribute("dStList", dStList)
             model.addAttribute("qcResultType", qcResultType)
             redirectAttributes.addFlashAttribute("successMessage", "invalid value")
-            "redirect:/" + url2.last()
+            "edit_bug"
         } else {
             bugService.save(bug)
             redirectAttributes.addFlashAttribute("successMessage", "Success save bug")
@@ -239,29 +235,51 @@ class BugController(
 
     @PostMapping(value = ["/saveUser"])
     fun saveUserEdit(
-        @Valid @ModelAttribute("userRole") userRole: UserRole,
-        bindingResult: BindingResult,
+        @ModelAttribute("userRole") userRole: UserRole,
         redirectAttributes: RedirectAttributes,
         model: Model
     ): String {
         val existUser = userService[userRole.id]
         val editUser = User()
-        editUser.id = userRole.id
+        editUser.id = existUser.id
         editUser.username = userRole.username
-        editUser.password = passwordEncoder.encode(userRole.password)
+        editUser.password = existUser.password
+        userRole.password = existUser.password
         editUser.roles = userRole.role
         editUser.isEnabled = true
-        println(editUser)
         model.addAttribute("role", getRole())
+        if (userRole.username.isEmpty()) {
+            redirectAttributes.addFlashAttribute("successMessage", "User Name tidak boleh kosong")
+            return "redirect:/userList/edit/" + userRole.id
+        }
+        userService.save(editUser)
+        redirectAttributes.addFlashAttribute("successMessage", "Success save user")
+        return "redirect:/userList"
+    }
+
+    @PostMapping(value = ["/saveChangePass"])
+    fun saveChangePass(
+        @ModelAttribute("userRole") userRole: UserRole,
+        redirectAttributes: RedirectAttributes,
+        model: Model
+    ): String {
+        val existUser = userService[userRole.id]
+        val editUser = User()
+        editUser.id = userRole.id
+        editUser.username = existUser.username
+        editUser.password = passwordEncoder.encode(userRole.password)
+        editUser.roles = existUser.roles
+        editUser.isEnabled = true
         val check = userService.checkIfValidOldPassword(userRole.oldPass, existUser.password)
         if (!check) {
             redirectAttributes.addFlashAttribute("successMessage", "old password not match")
-            return "redirect:/userList/edit/" + editUser.id
-        } else if (bindingResult.hasErrors()) {
-            redirectAttributes.addFlashAttribute("successMessage", "invalid value")
-            return "redirect:/userList/edit/" + editUser.id
+            return "redirect:/userList/chgPass/" + userRole.id
+        } else if (userRole.password.isBlank()) {
+            redirectAttributes.addFlashAttribute("successMessage", "Password tidak boleh kosong")
+            return "redirect:/userList/chgPass/" + userRole.id
         }
         userService.save(editUser)
+        redirectAttributes.addFlashAttribute("successMessage", "Success save user")
         return "redirect:/userList"
     }
 
@@ -273,9 +291,8 @@ class BugController(
         model: Model
     ): String {
         return if (bindingResult.hasErrors()) {
-            println(project)
             redirectAttributes.addFlashAttribute("successMessage", "invalid value")
-            "redirect:/proj/edit/" + project.id
+            "edit_proj"
         } else {
             projectService.save(project)
             "redirect:/proj"
@@ -303,8 +320,22 @@ class BugController(
     @GetMapping("/userList/edit/{id}")
     fun editUser(@PathVariable(name = "id") id: Long): ModelAndView {
         val mav = ModelAndView("edit_user")
+        val userRole = UserRole()
         val user: User = userService[id]
+        userRole.id = user.id
+        userRole.username = user.username
+        userRole.password = user.password
+        userRole.role = user.roles
+        mav.addObject("userRole", userRole)
+        mav.addObject("role", getRole())
+        return mav
+    }
+
+    @GetMapping("/userList/chgPass/{id}")
+    fun changePass(@PathVariable(name = "id") id: Long): ModelAndView {
+        val mav = ModelAndView("change_pass")
         val userRole = UserRole()
+        val user: User = userService[id]
         userRole.id = user.id
         userRole.username = user.username
         userRole.password = user.password
@@ -315,7 +346,9 @@ class BugController(
     }
 
     @GetMapping("/proj/edit/{id}")
-    fun editProject(@PathVariable(name = "id") id: Long): ModelAndView {
+    fun editProject(
+        @PathVariable(name = "id") id: Long//, @ModelAttribute("project") project: Project
+    ): ModelAndView {
         val mav = ModelAndView("edit_proj")
         val project: Project = projectService[id]
         mav.addObject("project", project)

+ 1 - 1
src/main/kotlin/com/server/alb/models/Project.kt

@@ -22,6 +22,6 @@ class Project
     var project: List<Platform> = mutableListOf()
 
     override fun toString(): String {
-        return ("Project [id=$id,name=$name, description=$description]")
+        return ("Project [name=$name, description=$description]")
     }
 }

+ 1 - 1
src/main/kotlin/com/server/alb/models/User.kt

@@ -40,7 +40,7 @@ class User() {
     }
 
     override fun toString(): String {
-        return ("User [id=$id,username=$username, password=$password, isEnabled=$isEnabled, roles=$roles]")
+        return ("User [id=$id, username=$username, password=$password, isEnabled=$isEnabled, roles=$roles]")
     }
 
 }

+ 3 - 3
src/main/kotlin/com/server/alb/models/UserRole.kt

@@ -1,7 +1,7 @@
 package com.server.alb.models
 
 import jakarta.validation.constraints.NotBlank
-import jakarta.validation.constraints.NotNull
+import jakarta.validation.constraints.NotEmpty
 
 class UserRole {
     var id: Long = 0
@@ -12,13 +12,13 @@ class UserRole {
     @NotBlank(message = "Password tidak boleh kosong")
     var password: String = ""
 
-    @NotNull(message = "Role tidak boleh kosong")
+    @NotEmpty(message = "Role tidak boleh kosong")
     var role: Set<Role> = HashSet()
     var isEnabled = false
 
     var oldPass: String = ""
 
     override fun toString(): String {
-        return ("UserRole [id=$id,username=$username, password=$password, isEnabled=$isEnabled, role=$role, oldPass=$oldPass]")
+        return ("UserRole [id=$id, username=$username, password=$password, isEnabled=$isEnabled, role=$role, oldPass=$oldPass]")
     }
 }

+ 0 - 16
src/main/kotlin/com/server/alb/services/BugValidationService.kt

@@ -1,16 +0,0 @@
-package com.server.alb.services
-
-import com.server.alb.models.ListBug
-import io.micrometer.common.util.StringUtils
-import org.springframework.stereotype.Service
-
-
-@Service
-class BugValidationService {
-    companion object {
-        fun isValid(bug: ListBug?): Boolean {
-            return (bug != null && StringUtils.isNotEmpty(bug.description) && StringUtils.isNotBlank(bug.link))
-        }
-    }
-
-}

+ 17 - 0
src/main/kotlin/com/server/alb/services/ValidationService.kt

@@ -0,0 +1,17 @@
+package com.server.alb.services
+
+import com.server.alb.models.Project
+import io.micrometer.common.util.StringUtils
+import org.springframework.stereotype.Service
+
+
+@Service
+class ValidationService {
+
+    companion object {
+        fun validProject(project: Project?): Boolean {
+            return (StringUtils.isNotEmpty(project!!.description) && StringUtils.isNotBlank(project.name))
+        }
+    }
+
+}

+ 42 - 0
src/main/resources/templates/change_pass.html

@@ -0,0 +1,42 @@
+<!DOCTYPE html>
+<html xmlns:th="http://www.thymeleaf.org"
+>
+<head>
+    <meta charset="ISO-8859-1">
+    <title>Change Password</title>
+    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet"
+          integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
+</head>
+<body>
+<div class="container py-5 h-100">
+    <h1>Change Password</h1>
+    <br/>
+    <form action="#" th:action="@{/saveChangePass}" th:object="${userRole}" method="post">
+        <table>
+            <tr>
+                <td>User ID:</td>
+                <td><input type="text" th:field="*{id}" class="form-control" readonly="readonly"/></td>
+            </tr>
+            <tr>
+                <td>User Name:</td>
+                <td><input type="text" th:field="*{username}" class="form-control" readonly="readonly"></td>
+            </tr>
+            <tr>
+                <td>Old Password:</td>
+                <td><input id="oldPass" type="password" th:field="*{oldPass}" class="form-control"></td>
+                <span th:text="${successMessage}" style="color:red"></span>
+            </tr>
+            <tr>
+                <td>New Password:</td>
+                <td><input id="password" type="password" th:field="*{password}" class="form-control"></td>
+            </tr>
+            <tr>
+                <td colspan="2">
+                    <button type="submit">Save</button>
+                </td>
+            </tr>
+        </table>
+    </form>
+</div>
+</body>
+</html>

+ 3 - 17
src/main/resources/templates/edit_user.html

@@ -10,7 +10,7 @@
 <body>
 <div class="container py-5 h-100">
     <h1>Edit User</h1>
-    <span th:text="${successMessage}" style="color:red"></span>
+
     <br/>
     <form action="#" th:action="@{/saveUser}" th:object="${userRole}" method="post">
         <table>
@@ -20,27 +20,13 @@
             </tr>
             <tr>
                 <td>User Name:</td>
-                <td><input type="text" th:field="*{username}" class="form-control">
-                <td th:if="${#fields.hasErrors('username')}" th:errors="*{username}" style="color:red">username Error</td>
-                </td>
-            </tr>
-            <tr>
-                <td>Old Password:</td>
-                <td><input id="oldPass" type="password" th:field="*{oldPass}" class="form-control">
-                <td th:if="${#fields.hasErrors('oldPass')}" th:errors="*{oldPass}" style="color:red">oldPass Error</td>
-                </td>
-            </tr>
-            <tr>
-                <td>New Password:</td>
-                <td><input id="password" type="password" th:field="*{password}" class="form-control">
-                <td th:if="${#fields.hasErrors('password')}" th:errors="*{password}" style="color:red">password Error</td>
-                </td>
+                <td><input type="text" th:field="*{username}" class="form-control"></td>
+                <span th:text="${successMessage}" style="color:red"></span>
             </tr>
             <tr>
                 <td>Role:</td>
                 <td><select th:field="*{role}">
                     <option th:each="role: ${role}" th:value="${role.id}" th:text="${role.name}"></option>
-                    <td th:if="${#fields.hasErrors('role')}" th:errors="*{role}" style="color:red">role Error</td>
                 </select></td>
             </tr>
             <tr>

+ 4 - 0
src/main/resources/templates/userList.html

@@ -17,12 +17,16 @@
             &nbsp;List User
             <a href="/proj">List Project</a>
         </div>
+        <div th:each="user2: ${user2}">
+            <a th:href="@{'/userList/chgPass/' + ${user2.id}}">Change Password</a>
+        </div>
 
         <form th:action="@{/logout}" method="post">
             <input type="submit" value="Logout"/>
         </form>
 
         <h1>List User</h1>
+        <span th:text="${successMessage}" style="color:green"></span>
     </div>
     <br/><br/>
     <table class="table table table-striped">