add permission definition
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:
144
models/permission.go
Normal file
144
models/permission.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Permission struct {
|
||||
PermissionId int `db:"PermissionId" json:"permissionId"`
|
||||
Description string `db:"Description" json:"description"`
|
||||
ReadOnly bool `db:"ReadOnly" json:"readOnly"`
|
||||
SafeId int `db:"SafeId" json:"safeId"`
|
||||
UserId int `db:"UserId" json:"userId"`
|
||||
GroupId int `db:"GroupId" json:"groupId"`
|
||||
}
|
||||
|
||||
// PermissionGetByDesc queries the database for a permission record matching the specified description
|
||||
func PermissionGetByDesc(description string) (Permission, error) {
|
||||
var p Permission
|
||||
|
||||
// Query database for matching group object
|
||||
err := db.QueryRowx("SELECT * FROM permissions WHERE Description=?", description).StructScan(&p)
|
||||
if err != nil {
|
||||
return p, errors.New("permission not found")
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// PermissionGetById queries the database for a permission record matching the specified permission id
|
||||
func PermissionGetById(id int) (Permission, error) {
|
||||
var p Permission
|
||||
|
||||
// Query database for matching group object
|
||||
err := db.QueryRowx("SELECT * FROM permissions WHERE PermissionId=?", id).StructScan(&p)
|
||||
if err != nil {
|
||||
return p, errors.New("permission not found")
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// PermissionList returns a list of all permissions in database
|
||||
func PermissionList() ([]Permission, error) {
|
||||
var results []Permission
|
||||
|
||||
// Query database for groups
|
||||
rows, err := db.Queryx("SELECT * FROM permissions")
|
||||
|
||||
if err != nil {
|
||||
log.Printf("PermissionList error executing sql record : '%s'\n", err)
|
||||
return results, err
|
||||
} else {
|
||||
// parse all the results into a slice
|
||||
for rows.Next() {
|
||||
var p Permission
|
||||
err = rows.StructScan(&p)
|
||||
if err != nil {
|
||||
log.Printf("PermissionList error parsing sql record : '%s'\n", err)
|
||||
return results, err
|
||||
}
|
||||
results = append(results, p)
|
||||
|
||||
}
|
||||
log.Printf("PermissionList retrieved '%d' results\n", len(results))
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// PermissionAdd adds a new permission definition to the database
|
||||
func (p *Permission) PermissionAdd() (*Permission, error) {
|
||||
var err error
|
||||
//var check Permission
|
||||
if len(p.Description) > 0 {
|
||||
_, err = PermissionGetByDesc(p.Description)
|
||||
} else {
|
||||
return &Permission{}, errors.New("unable to identify permission with supplied parameters")
|
||||
}
|
||||
|
||||
if err != nil && err.Error() == "permission not found" {
|
||||
log.Printf("PermissionAdd confirmed no existing permission, continuing with creation of permission '%s'\n", p.Description)
|
||||
|
||||
result, err := db.NamedExec(("INSERT INTO permissions (Description, SafeId, UserId, GroupId, ReadOnly) VALUES (:Description, :SafeId, :UserId, :GroupId, :ReadOnly);"), p)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("PermissionAdd error executing sql record : '%s'\n", err)
|
||||
return &Permission{}, err
|
||||
} else {
|
||||
affected, _ := result.RowsAffected()
|
||||
id, _ := result.LastInsertId()
|
||||
p.PermissionId = int(id)
|
||||
log.Printf("PermissionAdd insert returned result id '%d' affecting %d row(s).\n", id, affected)
|
||||
}
|
||||
} else {
|
||||
errString := "permission with identical description already exists"
|
||||
log.Printf("PermissionAdd %s\n", errString)
|
||||
return &Permission{}, errors.New(errString)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user