diff --git a/src/main/kotlin/com/terraformation/backend/customer/api/AdminController.kt b/src/main/kotlin/com/terraformation/backend/customer/api/AdminController.kt index cdb91721ac56..ff06fbb39ca2 100644 --- a/src/main/kotlin/com/terraformation/backend/customer/api/AdminController.kt +++ b/src/main/kotlin/com/terraformation/backend/customer/api/AdminController.kt @@ -24,6 +24,7 @@ import com.terraformation.backend.db.default_schema.FacilityType import com.terraformation.backend.db.default_schema.InternalTagId import com.terraformation.backend.db.default_schema.OrganizationId import com.terraformation.backend.db.default_schema.ReportId +import com.terraformation.backend.db.default_schema.Role import com.terraformation.backend.db.default_schema.SubLocationId import com.terraformation.backend.db.default_schema.UserId import com.terraformation.backend.db.default_schema.UserType @@ -149,13 +150,17 @@ class AdminController( @GetMapping("/") fun getIndex(model: Model): String { val organizations = organizationStore.fetchAll().sortedBy { it.id.value } + val allOrganizations = organizationsDao.findAll().sortedBy { it.id!!.value } + model.addAttribute("allOrganizations", allOrganizations) + model.addAttribute("canAddAnyOrganizationUser", currentUser().canAddAnyOrganizationUser()) model.addAttribute("canImportGlobalSpeciesData", currentUser().canImportGlobalSpeciesData()) model.addAttribute("canManageInternalTags", currentUser().canManageInternalTags()) model.addAttribute("canSetTestClock", config.useTestClock && currentUser().canSetTestClock()) model.addAttribute("canUpdateAppVersions", currentUser().canUpdateAppVersions()) model.addAttribute("organizations", organizations) model.addAttribute("prefix", prefix) + model.addAttribute("roles", Role.entries.map { it to it.getDisplayName(Locale.ENGLISH) }) return "/admin/index" } @@ -1204,24 +1209,32 @@ class AdminController( return organization(organizationId) } - @PostMapping("/assignTerraformationContact") - fun assignTerraformationContact( + @PostMapping("/addOrganizationUser") + fun addOrganizationUser( @RequestParam organizationId: OrganizationId, - @NotBlank @RequestParam terraformationContactEmail: String, + @NotBlank @RequestParam email: String, + @RequestParam role: Role, redirectAttributes: RedirectAttributes, ): String { try { - val metadata = - organizationService.assignTerraformationContact( - terraformationContactEmail, organizationId) - redirectAttributes.successMessage = - "User $metadata assigned as Terraformation Contact in organization $organizationId." + if (role == Role.TerraformationContact) { + val userId = organizationService.assignTerraformationContact(email, organizationId) + redirectAttributes.successMessage = + "User $userId assigned as contact for organization $organizationId" + } else { + if (userStore.fetchByEmail(email) != null) { + val userId = organizationService.addUser(email, organizationId, role) + redirectAttributes.successMessage = "User $userId added to organization $organizationId" + } else { + redirectAttributes.failureMessage = "User $email does not exist" + } + } } catch (e: Exception) { - log.warn("Terraformation Contact assignment failed", e) - redirectAttributes.failureMessage = "Terraformation Contact assignment failed: ${e.message}" + log.warn("Failed to add user to organization", e) + redirectAttributes.failureMessage = "Adding user failed: ${e.message}" } - return organization(organizationId) + return adminHome() } @PostMapping("/deleteReport") diff --git a/src/main/kotlin/com/terraformation/backend/customer/model/DeviceManagerUser.kt b/src/main/kotlin/com/terraformation/backend/customer/model/DeviceManagerUser.kt index 2dcdadca889e..44eaee5c776c 100644 --- a/src/main/kotlin/com/terraformation/backend/customer/model/DeviceManagerUser.kt +++ b/src/main/kotlin/com/terraformation/backend/customer/model/DeviceManagerUser.kt @@ -122,6 +122,7 @@ data class DeviceManagerUser( organizationId in permissionStore.fetchOrganizationRoles(targetUserId) } + override fun canAddAnyOrganizationUser(): Boolean = false override fun canAddOrganizationUser(organizationId: OrganizationId): Boolean = false override fun canAddTerraformationContact(organizationId: OrganizationId): Boolean = false override fun canCountNotifications(): Boolean = false diff --git a/src/main/kotlin/com/terraformation/backend/customer/model/IndividualUser.kt b/src/main/kotlin/com/terraformation/backend/customer/model/IndividualUser.kt index ab40d99c3f18..bc3162c539b8 100644 --- a/src/main/kotlin/com/terraformation/backend/customer/model/IndividualUser.kt +++ b/src/main/kotlin/com/terraformation/backend/customer/model/IndividualUser.kt @@ -142,8 +142,10 @@ data class IndividualUser( override fun isCredentialsNonExpired() = true override fun isEnabled() = true + override fun canAddAnyOrganizationUser() = isSuperAdmin() + override fun canAddOrganizationUser(organizationId: OrganizationId) = - isAdminOrHigher(organizationId) + isSuperAdmin() || isAdminOrHigher(organizationId) override fun canAddTerraformationContact(organizationId: OrganizationId) = isSuperAdmin() @@ -346,7 +348,7 @@ data class IndividualUser( override fun canSendAlert(facilityId: FacilityId) = isAdminOrHigher(facilityId) override fun canSetOrganizationUserRole(organizationId: OrganizationId, role: Role) = - isAdminOrHigher(organizationId) + isSuperAdmin() || isAdminOrHigher(organizationId) override fun canSetTerraformationContact(organizationId: OrganizationId) = isSuperAdmin() diff --git a/src/main/kotlin/com/terraformation/backend/customer/model/SystemUser.kt b/src/main/kotlin/com/terraformation/backend/customer/model/SystemUser.kt index d55afa5fc8d9..36de2fd47911 100644 --- a/src/main/kotlin/com/terraformation/backend/customer/model/SystemUser.kt +++ b/src/main/kotlin/com/terraformation/backend/customer/model/SystemUser.kt @@ -111,6 +111,7 @@ class SystemUser( * manually by a system administrator. */ + override fun canAddAnyOrganizationUser(): Boolean = true override fun canAddOrganizationUser(organizationId: OrganizationId): Boolean = true override fun canAddTerraformationContact(organizationId: OrganizationId): Boolean = true override fun canCountNotifications(): Boolean = true diff --git a/src/main/kotlin/com/terraformation/backend/customer/model/TerrawareUser.kt b/src/main/kotlin/com/terraformation/backend/customer/model/TerrawareUser.kt index e0818b28865f..60b5be5475c5 100644 --- a/src/main/kotlin/com/terraformation/backend/customer/model/TerrawareUser.kt +++ b/src/main/kotlin/com/terraformation/backend/customer/model/TerrawareUser.kt @@ -79,6 +79,7 @@ interface TerrawareUser : Principal { * Permission checks. Each of these returns true if the user has permission to perform the action. */ + fun canAddAnyOrganizationUser(): Boolean fun canAddOrganizationUser(organizationId: OrganizationId): Boolean fun canAddTerraformationContact(organizationId: OrganizationId): Boolean fun canCountNotifications(): Boolean diff --git a/src/main/resources/templates/admin/index.html b/src/main/resources/templates/admin/index.html index c7036141d29a..2e5c411c07fa 100644 --- a/src/main/resources/templates/admin/index.html +++ b/src/main/resources/templates/admin/index.html @@ -66,5 +66,35 @@
+ If the role is "Terraformation Contact" and the organization already has a Terraformation + contact, the existing contact will be replaced by the new one. +
+ + +