more refactors

This commit is contained in:
2026-02-08 19:57:12 +11:00
parent 1a2bdaf4cf
commit 97342e08fe
17 changed files with 334 additions and 308 deletions

View File

@@ -24,6 +24,10 @@ type Permission struct {
Roles []Role `bun:"m2m:role_permissions,join:Permission=Role"`
}
func (p Permission) isSystem() bool {
return p.IsSystem
}
// GetPermissionByName queries the database for a permission matching the given name
// Returns nil, nil if no permission is found
func GetPermissionByName(ctx context.Context, tx bun.Tx, name permissions.Permission) (*Permission, error) {
@@ -150,26 +154,5 @@ func DeletePermission(ctx context.Context, tx bun.Tx, id int) error {
if id <= 0 {
return errors.New("id must be positive")
}
// Check if permission is system permission
perm, err := GetPermissionByID(ctx, tx, id)
if err != nil {
return errors.Wrap(err, "GetPermissionByID")
}
if perm == nil {
return errors.New("permission not found")
}
if perm.IsSystem {
return errors.New("cannot delete system permission")
}
_, err = tx.NewDelete().
Model((*Permission)(nil)).
Where("id = ?", id).
Exec(ctx)
if err != nil {
return errors.Wrap(err, "tx.NewDelete")
}
return nil
return DeleteWithProtection[Permission](ctx, tx, id)
}