From f61fd78287ccb3586f6af24397fc9d33f0404767 Mon Sep 17 00:00:00 2001 From: neviyn Date: Wed, 31 Mar 2021 20:03:00 +0100 Subject: [PATCH] Add users to projects --- .../uk/co/neviyn/projectplanner/Entities.kt | 1 + .../neviyn/projectplanner/HtmlController.kt | 19 ++--- .../uk/co/neviyn/projectplanner/Requests.kt | 4 - .../resources/templates/addprojectuser.html | 26 ------- src/main/resources/templates/project.html | 73 +++++++++++++++---- 5 files changed, 65 insertions(+), 58 deletions(-) delete mode 100644 src/main/resources/templates/addprojectuser.html diff --git a/src/main/kotlin/uk/co/neviyn/projectplanner/Entities.kt b/src/main/kotlin/uk/co/neviyn/projectplanner/Entities.kt index e24bfc0..c22dd40 100644 --- a/src/main/kotlin/uk/co/neviyn/projectplanner/Entities.kt +++ b/src/main/kotlin/uk/co/neviyn/projectplanner/Entities.kt @@ -17,6 +17,7 @@ import javax.persistence.OneToMany open class User( var username: String = "INVALID", var email: String = "INVALID", + @JsonIgnore var password: String = "INVALID", @ManyToMany(cascade = [CascadeType.ALL]) @JsonIgnore diff --git a/src/main/kotlin/uk/co/neviyn/projectplanner/HtmlController.kt b/src/main/kotlin/uk/co/neviyn/projectplanner/HtmlController.kt index 19911b6..998f82b 100644 --- a/src/main/kotlin/uk/co/neviyn/projectplanner/HtmlController.kt +++ b/src/main/kotlin/uk/co/neviyn/projectplanner/HtmlController.kt @@ -99,7 +99,9 @@ class ProjectController @Autowired constructor(val projectRepository: ProjectRep @PreAuthorize("hasPermission(#id, 'Long', '')") fun getProject(@PathVariable id: Long, model: Model) : String { val project = projectRepository.findById(id).get() + val nonMembers = userRepository.findByIdNotIn(project.members.map { it.id!! }) model.addAttribute("project", project) + model.addAttribute("nonMembers", nonMembers) return "project" } @@ -109,27 +111,20 @@ class ProjectController @Autowired constructor(val projectRepository: ProjectRep return projectRepository.findById(id).get().events } - @GetMapping("/adduser") - @PreAuthorize("hasPermission(#id, 'Long', '')") - fun addUserToProjectForm(@PathVariable id: Long, model: Model) : String{ - val project = projectRepository.findById(id).get() - val users = userRepository.findByIdNotIn(project.members.map { it.id!! }).map { SimpleUser(it.id!!, it.username) } - model.addAttribute("available_users", users) - return "addprojectuser" - } - @PostMapping("/adduser") @PreAuthorize("hasPermission(#id, 'Long', '')") - fun addUserToProject(@PathVariable id: Long, @RequestBody u: UserID) { - val user = userRepository.findById(u.id).get() + fun addUserToProject(@PathVariable id: Long, @RequestParam("uid") uid: Long) : String { + val user = userRepository.findById(uid).get() val project = projectRepository.findById(id).get() project.members.add(user) + user.projects.add(project) projectRepository.save(project) + return "redirect:/project/$id" } @PostMapping("/removeuser") @PreAuthorize("hasPermission(#id, 'Long', '')") - fun removeUserFromProject(@PathVariable id: Long, @RequestParam("id") uid: Long, model: Model) : String{ + fun removeUserFromProject(@PathVariable id: Long, @RequestParam("id") uid: Long) : String{ val project = projectRepository.findById(id).get() // Don't allow projects to have no members if(project.members.size == 1) return "redirect:/project/$id" diff --git a/src/main/kotlin/uk/co/neviyn/projectplanner/Requests.kt b/src/main/kotlin/uk/co/neviyn/projectplanner/Requests.kt index 2313c73..0b4ffb0 100644 --- a/src/main/kotlin/uk/co/neviyn/projectplanner/Requests.kt +++ b/src/main/kotlin/uk/co/neviyn/projectplanner/Requests.kt @@ -2,10 +2,6 @@ package uk.co.neviyn.projectplanner import java.time.LocalDateTime -data class UserID(val id: Long) - -data class SimpleUser(val id: Long, val username: String) - data class DisplayUser(val id: Long, val username: String, val email: String, val password: String, val oldPassword: String) data class NewProject(val title: String) diff --git a/src/main/resources/templates/addprojectuser.html b/src/main/resources/templates/addprojectuser.html deleted file mode 100644 index edea7b6..0000000 --- a/src/main/resources/templates/addprojectuser.html +++ /dev/null @@ -1,26 +0,0 @@ - - - - - Add user to project | Project Planner - - - - - -
- - -
- - \ No newline at end of file diff --git a/src/main/resources/templates/project.html b/src/main/resources/templates/project.html index 05e9a57..e98beb5 100644 --- a/src/main/resources/templates/project.html +++ b/src/main/resources/templates/project.html @@ -4,8 +4,9 @@ Project Planner - - + + + + @@ -38,7 +47,9 @@
- +
Members:
@@ -46,32 +57,37 @@

username

-
- - + + +
- Add member +
-
+
- -