add permission definition
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-01-11 14:32:50 +11:00
parent 1ffa19d225
commit afec665759
6 changed files with 99 additions and 7 deletions

View File

@@ -84,7 +84,7 @@ func (g *Group) GroupAdd() (*Group, error) {
return g, nil
}
// GroupDelete removes a group definition to the database
// GroupDelete removes a group definition from the database
func (g *Group) GroupDelete() error {
var err error

View File

@@ -100,3 +100,45 @@ func (p *Permission) PermissionAdd() (*Permission, error) {
return p, nil
}
// PermissionDelete removes a permission definition from the database
func (p *Permission) PermissionDelete() error {
var err error
var permission Permission
// Validate permission exists
if p.PermissionId > 0 {
permission, err = PermissionGetById(p.PermissionId)
} else if len(p.Description) > 0 {
permission, err = PermissionGetByDesc(p.Description)
} else {
errString := "unable to identify permission with supplied parameters"
log.Printf("PermissionDelete %s\n", errString)
return errors.New(errString)
}
if err != nil && err.Error() == "permission not found" {
log.Printf("PermissionDelete unable to validate group exists : '%s'\n", err)
return err
}
// Make sure we have a group ID
if p.PermissionId == 0 {
p.PermissionId = permission.PermissionId
}
// Delete the group
log.Printf("PermissionDelete confirmed group exists, continuing with deletion of permission id %d, '%s'\n", p.PermissionId, p.Description)
result, err := db.NamedExec((`DELETE FROM permissions WHERE PermissionId = :PermissionId`), p)
if err != nil {
log.Printf("PermissionDelete error executing sql delete : '%s'\n", err)
return err
} else {
affected, _ := result.RowsAffected()
id, _ := result.LastInsertId()
log.Printf("PermissionDelete returned result id '%d' affecting %d row(s).\n", id, affected)
}
return nil
}

View File

@@ -37,8 +37,7 @@ const createUsers string = `
UserName VARCHAR,
Password VARCHAR,
Admin BOOLEAN DEFAULT 0,
LdapUser BOOLEAN DEFAULT 0,
FOREIGN KEY (GroupId) REFERENCES groups(GroupId)
LdapUser BOOLEAN DEFAULT 0
);
`
@@ -279,7 +278,6 @@ func CreateTables() {
Password VARCHAR,
Admin BOOLEAN DEFAULT 0,
LdapUser BOOLEAN DEFAULT 0
FOREIGN KEY (GroupId) REFERENCES groups(GroupId)
);
INSERT INTO users SELECT * FROM _users_old;
COMMIT;

View File

@@ -116,7 +116,8 @@ func LoginCheck(username string, password string) (string, error) {
u := User{}
// Query database for matching user object
err = db.QueryRowx("SELECT * FROM Users WHERE Username=?", username).StructScan(&u)
// Use IFNULL to handle situation where a user might not be a member of a group
err = db.QueryRowx("SELECT UserId, IFNULL(GroupId, 0) GroupId, UserName, Password, LdapUser, Admin FROM Users WHERE Username=?", username).StructScan(&u)
if err != nil {
if err == sql.ErrNoRows {