All checks were successful
continuous-integration/drone/push Build is passing
103 lines
3.1 KiB
Go
103 lines
3.1 KiB
Go
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
|
|
}
|