New storage error and role deletion function

New error is for indicating actions that are not ever allowed, even for
admins
And you can now delete roles
This commit is contained in:
Melody Becker 2024-11-07 10:45:02 +01:00
parent 9169cec4bb
commit 575392d6d4
2 changed files with 14 additions and 0 deletions

View file

@ -12,3 +12,4 @@ var ErrEntryNotFound = errors.New("entry not found")
var ErrEntryAlreadyExists = errors.New("entry already exists") var ErrEntryAlreadyExists = errors.New("entry already exists")
var ErrNothingToChange = errors.New("nothing to change") var ErrNothingToChange = errors.New("nothing to change")
var ErrInvalidData = errors.New("invalid data") var ErrInvalidData = errors.New("invalid data")
var ErrNotAllowed = errors.New("action not allowed")

View file

@ -2,6 +2,7 @@ package storage
import ( import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"gitlab.com/mstarongitlab/goutils/sliceutils"
"gitlab.com/mstarongitlab/linstrom/util" "gitlab.com/mstarongitlab/linstrom/util"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -215,3 +216,15 @@ func (s *Storage) UpdateRole(role *Role) error {
defer util.Untrace(util.Trace(&log.Logger)) defer util.Untrace(util.Trace(&log.Logger))
return s.db.Save(role).Error return s.db.Save(role).Error
} }
func (s *Storage) DeleteRoleByName(name string) error {
// Prevent deletion of built-in roles
if sliceutils.Contains(
sliceutils.Map(allDefaultRoles, func(t *Role) string { return t.Name }),
name,
) {
return ErrNotAllowed
}
defer util.Untrace(util.Trace(&log.Logger))
return s.db.Where(&Role{Name: name, IsBuiltIn: false}).Delete(&Role{}).Error
}