new add role feature
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-01-04 09:43:59 +11:00
parent f7168d465a
commit 0619b497f7
5 changed files with 105 additions and 9 deletions

View File

@@ -2,6 +2,7 @@ package controllers
import (
"errors"
"fmt"
"html"
"log"
"net/http"
@@ -29,6 +30,13 @@ type DeleteInput struct {
UserName string `json:"userName" binding:"required"`
}
type AddRoleInput struct {
RoleName string `json:"roleName" binding:"required"`
LdapGroup string `json:"ldapGroup"`
ReadOnly bool `json:"readOnly"`
Admin bool `json:"admin"`
}
func DeleteUser(c *gin.Context) {
var input DeleteInput
@@ -63,7 +71,7 @@ func DeleteUser(c *gin.Context) {
}
}
func Register(c *gin.Context) {
func RegisterUser(c *gin.Context) {
var input RegisterInput
if err := c.ShouldBindJSON(&input); err != nil {
@@ -117,7 +125,45 @@ func Register(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{"message": "registration success"})
c.JSON(http.StatusOK, gin.H{"message": "user registration success"})
}
func AddRole(c *gin.Context) {
var input AddRoleInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// define the new role properties
r := models.Role{}
r.RoleName = input.RoleName
r.ReadOnly = input.ReadOnly
r.Admin = input.Admin
r.LdapGroup = input.LdapGroup
// Check if role already exists
testRole, _ := models.GetRoleByName(r.RoleName)
log.Printf("AddRole checking if role '%s' already exists\n", r.RoleName)
if (models.Role{} == testRole) {
log.Printf("AddRole confirmed no existing rolename\n")
} else {
errorString := fmt.Sprintf("attempt to register conflicting rolename '%s'", r.RoleName)
log.Printf("Register error : '%s'\n", errorString)
c.JSON(http.StatusBadRequest, gin.H{"error": errorString})
return
}
_, err := r.AddRole()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"Error creating role": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "role creation success"})
}
func Login(c *gin.Context) {