All checks were successful
continuous-integration/drone/push Build is passing
80 lines
2.0 KiB
Go
80 lines
2.0 KiB
Go
package controllers
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"smt/models"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type AddGroupInput struct {
|
|
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) {
|
|
groups, err := models.GroupList()
|
|
|
|
if err != nil {
|
|
errString := fmt.Sprintf("error retrieving groups : '%s'", err)
|
|
log.Printf("GetGroups %s\n", errString)
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": errString})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "success", "data": groups})
|
|
}
|
|
|
|
func AddGroup(c *gin.Context) {
|
|
var input AddGroupInput
|
|
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if len(input.GroupName) == 0 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "no group name specified"})
|
|
return
|
|
}
|
|
|
|
if input.LdapGroup && len(input.LdapDn) == 0 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ldapGroup is true but no ldapDn specified"})
|
|
return
|
|
}
|
|
|
|
g := models.Group{}
|
|
g.GroupName = input.GroupName
|
|
g.LdapGroup = input.LdapGroup
|
|
g.LdapDn = input.LdapDn
|
|
g.Admin = input.Admin
|
|
|
|
// Check if role already exists
|
|
testGroup, _ := models.GroupGetByName(g.GroupName)
|
|
log.Printf("AddGroup checking if group '%s' already exists\n", g.GroupName)
|
|
|
|
if (models.Group{} == testGroup) {
|
|
log.Printf("AddGroup confirmed no existing group name\n")
|
|
} else {
|
|
errorString := fmt.Sprintf("attempt to register conflicting groupname '%s'", g.GroupName)
|
|
log.Printf("Register error : '%s'\n", errorString)
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": errorString})
|
|
return
|
|
}
|
|
|
|
_, err := g.GroupAdd()
|
|
|
|
if err != nil {
|
|
errString := fmt.Sprintf("error creating group : '%s'", err)
|
|
log.Printf("AddGroup %s\n", errString)
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": errString})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "group creation success"})
|
|
}
|