implement group delete
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -2,21 +2,24 @@ package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"log"
|
||||
"net/http"
|
||||
"smt/models"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type AddGroupInput struct {
|
||||
type GroupInput struct {
|
||||
GroupId int `db:"GroupId" json:"groupId"`
|
||||
GroupName string `db:"GroupName" json:"groupName"`
|
||||
LdapGroup bool `db:"LdapGroup" json:"ldapGroup"`
|
||||
LdapDn string `db:"LdapDn" json:"ldapDn"`
|
||||
Admin bool `db:"Admin" json:"admin"`
|
||||
}
|
||||
|
||||
func GetGroups(c *gin.Context) {
|
||||
func GetGroupsHandler(c *gin.Context) {
|
||||
groups, err := models.GroupList()
|
||||
|
||||
if err != nil {
|
||||
@@ -29,8 +32,8 @@ func GetGroups(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "success", "data": groups})
|
||||
}
|
||||
|
||||
func AddGroup(c *gin.Context) {
|
||||
var input AddGroupInput
|
||||
func AddGroupHandler(c *gin.Context) {
|
||||
var input GroupInput
|
||||
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
@@ -53,6 +56,9 @@ func AddGroup(c *gin.Context) {
|
||||
g.LdapDn = input.LdapDn
|
||||
g.Admin = input.Admin
|
||||
|
||||
//remove leading/trailing spaces in groupname
|
||||
g.GroupName = html.EscapeString(strings.TrimSpace(g.GroupName))
|
||||
|
||||
// Check if role already exists
|
||||
testGroup, _ := models.GroupGetByName(g.GroupName)
|
||||
log.Printf("AddGroup checking if group '%s' already exists\n", g.GroupName)
|
||||
@@ -77,3 +83,46 @@ func AddGroup(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "group creation success"})
|
||||
}
|
||||
|
||||
func DeleteGroupHandler(c *gin.Context) {
|
||||
var input GroupInput
|
||||
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Input validation
|
||||
if input.GroupId == 0 && len(input.GroupName) == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "no group name or id specified"})
|
||||
return
|
||||
}
|
||||
|
||||
g := models.Group{}
|
||||
g.GroupId = input.GroupId
|
||||
g.GroupName = input.GroupName
|
||||
|
||||
//remove leading/trailing spaces in groupname
|
||||
g.GroupName = html.EscapeString(strings.TrimSpace(g.GroupName))
|
||||
|
||||
// Confirm user account exists
|
||||
testGroup, _ := models.GroupGetByName(g.GroupName)
|
||||
log.Printf("DeleteGroupHandler confirming group '%s' exists\n", g.GroupName)
|
||||
if (models.Group{} == testGroup) {
|
||||
errString := fmt.Sprintf("attempt to delete non-existing group '%s'", g.GroupName)
|
||||
log.Printf("DeleteGroupHandler %s\n", errString)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": errString})
|
||||
return
|
||||
} else {
|
||||
err := g.GroupDelete()
|
||||
|
||||
if err != nil {
|
||||
errString := fmt.Sprintf("error deleting group : '%s'", err)
|
||||
log.Printf("DeleteGroupHandler %s\n", errString)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": errString})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "group deletion success"})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user