work on adding group support
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:
@@ -15,7 +15,7 @@ import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type RegisterInput struct {
|
||||
type AddUserInput struct {
|
||||
UserName string `json:"userName" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
GroupId int `json:"groupId"`
|
||||
@@ -73,8 +73,8 @@ func DeleteUser(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterUser(c *gin.Context) {
|
||||
var input RegisterInput
|
||||
func AddUser(c *gin.Context) {
|
||||
var input AddUserInput
|
||||
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
|
34
controllers/controlGroups.go
Normal file
34
controllers/controlGroups.go
Normal file
@@ -0,0 +1,34 @@
|
||||
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) {
|
||||
|
||||
}
|
18
main.go
18
main.go
@@ -241,13 +241,23 @@ func main() {
|
||||
// API calls that only an administrator can make
|
||||
adminOnly := router.Group("/api/admin")
|
||||
adminOnly.Use(middlewares.JwtAuthAdminMiddleware())
|
||||
|
||||
// User functions for admin
|
||||
adminOnly.POST("/user/delete", controllers.DeleteUser)
|
||||
adminOnly.POST("/user/register", controllers.RegisterUser) // TODO deprecate
|
||||
adminOnly.POST("/user/add", controllers.RegisterUser)
|
||||
adminOnly.POST("/user/register", controllers.AddUser) // TODO deprecate
|
||||
adminOnly.POST("/user/add", controllers.AddUser)
|
||||
adminOnly.GET("/users", controllers.GetUsers)
|
||||
// TODO
|
||||
//adminOnly.POST("/user/update", controllers.UpdateUser)
|
||||
//adminOnly.GET("/groups/list", controllers.ListGroups)
|
||||
adminOnly.GET("/users", controllers.GetUsers)
|
||||
|
||||
// Group functions for admin
|
||||
adminOnly.GET("/groups", controllers.GetGroups)
|
||||
adminOnly.GET("/group/add", controllers.AddGroup)
|
||||
// TODO
|
||||
//adminOnly.GET("/group/update", controllers.UpdateGroup)
|
||||
//adminOnly.GET("/group/delete", controllers.DeleteGroup)
|
||||
|
||||
// Other functions for admin
|
||||
adminOnly.POST("/unlock", controllers.Unlock)
|
||||
|
||||
// Deprecated
|
||||
|
@@ -6,11 +6,11 @@ import (
|
||||
)
|
||||
|
||||
type Group struct {
|
||||
GroupId int `db:"GroupId"`
|
||||
GroupName string `db:"GroupName"`
|
||||
LdapGroup bool `db:"LdapGroup"`
|
||||
LdapDn string `db:"LdapDN"`
|
||||
Admin bool `db:"Admin"`
|
||||
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"`
|
||||
}
|
||||
|
||||
// GroupGetByName queries the database for the specified group name
|
||||
@@ -53,3 +53,30 @@ func GroupList() ([]Group, error) {
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// GroupAdd adds a new group definition to the database
|
||||
func (g *Group) GroupAdd() (*Group, error) {
|
||||
var err error
|
||||
|
||||
// Validate role not already in use
|
||||
_, err = GroupGetByName(g.GroupName)
|
||||
|
||||
if err != nil && err.Error() == "group not found" {
|
||||
log.Printf("GroupAdd confirmed no existing group, continuing with creation of group '%s'\n", g.GroupName)
|
||||
|
||||
result, err := db.NamedExec(("INSERT INTO groups (GroupName, LdapGroup, LdapDn, Admin) VALUES (:GroupName, :LdapGroup, :LdapDn, :Admin);"), g)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("GroupAdd error executing sql record : '%s'\n", err)
|
||||
return &Group{}, err
|
||||
} else {
|
||||
affected, _ := result.RowsAffected()
|
||||
id, _ := result.LastInsertId()
|
||||
log.Printf("GroupAdd insert returned result id '%d' affecting %d row(s).\n", id, affected)
|
||||
}
|
||||
} else {
|
||||
log.Printf("GroupAdd group name already exists : '%v'\n", err)
|
||||
}
|
||||
|
||||
return g, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user