Implementata la possibilitá di deautorizzare gli utenti per singoli gruppi

Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
2020-04-06 23:09:45 +02:00
parent c0bf3b2b36
commit dd3ab25fba
6 changed files with 114 additions and 23 deletions

View File

@@ -163,7 +163,7 @@ func authorizeUser(userID int, authorize bool) error {
return nil
}
func setUserGroups(userID int, groups ...userGroup) error {
func addUserGroups(userID int, groups ...userGroup) error {
if redisClient == nil {
return ErrNilPointer
}
@@ -186,6 +186,41 @@ func setUserGroups(userID int, groups ...userGroup) error {
return nil
}
func remUserGroups(userID int, newGroups []userGroup, remGroups ...userGroup) error {
if redisClient == nil {
return ErrNilPointer
}
sort.Slice(newGroups, func(i, j int) bool { return newGroups[i] < newGroups[j] })
for _, remGroup := range remGroups {
err := redisClient.SRem("ug"+strconv.Itoa(int(remGroup)), strconv.Itoa(userID)).Err()
if err != nil {
log.Printf("Error removing user from usergroup set: %v", err)
return ErrRedisAddSet
}
}
if len(newGroups) > 0 {
var csvGroups string
for _, group := range newGroups {
csvGroups += strconv.Itoa(int(group)) + ","
}
err := redisClient.HSet(usersGroups, strconv.Itoa(userID), csvGroups).Err()
if err != nil {
log.Printf("Error adding user groups to hash: %v", err)
return ErrRedisAddHash
}
} else {
err := redisClient.HDel(usersGroups, strconv.Itoa(userID)).Err()
if err != nil {
log.Printf("Error removing user from usersGroups hash: %v", err)
return ErrRedisAddHash
}
}
return nil
}
func getUserGroups(userID int) ([]userGroup, error) {
if redisClient == nil {
return nil, ErrNilPointer
@@ -271,6 +306,27 @@ func convertUserGroups(groups []userGroup) []string {
return stringGroups
}
func getGroupName(group userGroup) (string, error) {
switch group {
case ugSoprano:
return "Soprano", nil
case ugContralto:
return "Contralto", nil
case ugTenore:
return "Tenore", nil
case ugBasso:
return "Basso", nil
case ugCommissario:
return "Commissario", nil
case ugReferente:
return "Referente", nil
case ugPreparatore:
return "Preparatore", nil
default:
return "", ErrGroupInvalid
}
}
func getUserDescription(u *tb.User) (string, error) {
userGroups, err := getUserGroups(u.ID)
if err != nil {