forked from authentricity/authentricity
94 lines
1.9 KiB
Go
94 lines
1.9 KiB
Go
package webui
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
"go.e43.eu/authentricity/internal/models"
|
|
"go.e43.eu/authentricity/internal/store"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func (s *Service) actionAddGroup(
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
ent models.Entity,
|
|
md store.EntryMetadata,
|
|
) {
|
|
user, ok := ent.(*models.UserRecord)
|
|
if !ok {
|
|
s.renderBadRequest(w, r)
|
|
return
|
|
}
|
|
|
|
if !s.isAdmin(r.Context()) {
|
|
s.renderForbidden(w, r)
|
|
return
|
|
}
|
|
|
|
groupName := strings.TrimSpace(r.PostForm.Get("group"))
|
|
if groupName == "" {
|
|
s.renderEntity(w, r, ent, "No group specified")
|
|
}
|
|
|
|
grent, _, err := store.GetEntityByAnyName(r.Context(), s.store, groupName)
|
|
if err != nil {
|
|
zap.L().Error("Error finding group", zap.Error(err))
|
|
s.renderError(w)
|
|
return
|
|
} else if grent == nil {
|
|
s.renderEntity(w, r, ent, "Group not found")
|
|
return
|
|
} else if grent.Type() != models.TypeGroup {
|
|
s.renderEntity(w, r, ent, "Not a group")
|
|
return
|
|
}
|
|
|
|
err = s.store.AddUserToGroup(r.Context(), user.UUID, grent.ID())
|
|
if err != nil {
|
|
zap.L().Error("Error adding user to group", zap.Error(err))
|
|
s.renderError(w)
|
|
return
|
|
}
|
|
|
|
s.renderEntity(w, r, ent, "Group added")
|
|
}
|
|
|
|
func (s *Service) actionRemoveGroups(
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
ent models.Entity,
|
|
md store.EntryMetadata,
|
|
) {
|
|
user, ok := ent.(*models.UserRecord)
|
|
if !ok {
|
|
s.renderBadRequest(w, r)
|
|
return
|
|
}
|
|
|
|
if !s.isAdmin(r.Context()) {
|
|
s.renderForbidden(w, r)
|
|
return
|
|
}
|
|
|
|
toRemove := r.PostForm["group"]
|
|
for _, grp := range toRemove {
|
|
id, err := uuid.Parse(grp)
|
|
if err != nil {
|
|
zap.L().Error("Error parsing group ID", zap.Error(err))
|
|
s.renderError(w)
|
|
return
|
|
}
|
|
|
|
err = s.store.RemoveUserFromGroup(r.Context(), user.UUID, id)
|
|
if err != nil {
|
|
zap.L().Error("Error removing user from group", zap.Error(err))
|
|
s.renderError(w)
|
|
return
|
|
}
|
|
}
|
|
|
|
s.renderEntity(w, r, ent, "Groups removed")
|
|
}
|