Skip to content

Commit

Permalink
Add groups instead only super groups from Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscariremma committed Nov 12, 2024
1 parent f2ae0e6 commit 629de7c
Showing 1 changed file with 69 additions and 14 deletions.
83 changes: 69 additions & 14 deletions internal/pkg/services/auth/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ package auth

import (
"github.com/cthit/goldapps/internal/pkg/model"
"slices"
"strings"
)

type AuthSuperGroup struct {
Name string `json:"name"`
PrettyName string `json:"prettyName"`
Type string `json:"type"`
Groups []AuthGroup `json:"groups"`
UseManagedAccount bool `json:"useManagedAccount"`
}

type AuthGroup struct {
Name string `json:"name"`
PrettyName string `json:"prettyName"`
Type string `json:"type"`
Members []struct {
Post struct {
PostID string `json:"postId"`
Expand All @@ -17,8 +25,7 @@ type AuthSuperGroup struct {
EmailPrefix string `json:"emailPrefix"`
} `json:"post"`
User AuthUser `json:"user"`
} `json:"members"`
UseManagedAccount bool `json:"useManagedAccount"`
}
}

type AuthUser struct {
Expand Down Expand Up @@ -53,28 +60,76 @@ func (users AuthUsers) ToUsers() model.Users {
return usersList
}

func (superGroup AuthSuperGroup) ToGroup() model.Group {
group := model.Group{
func (superGroup AuthSuperGroup) ToGroups() model.Groups {
groups := model.Groups{}
outerGroup := model.Group{
Email: strings.ToLower(superGroup.Name) + "@chalmers.it",
Type: superGroup.Type,
Aliases: []string{},
}
for _, member := range superGroup.Members {
var memberEmail string
if superGroup.UseManagedAccount {
memberEmail = member.User.Cid + "@chalmers.it"
} else {
memberEmail = member.User.Email

superGroupPostGroups := make(map[string]model.Group)

for _, group := range superGroup.Groups {
memberGroup := model.Group{
Email: strings.ToLower(group.Name) + "@chalmers.it",
Type: superGroup.Type,
Aliases: []string{},
}
group.Members = append(group.Members, strings.ToLower(memberEmail))
memberGroupPostGroups := make(map[string]model.Group)

for _, member := range group.Members {

var memberEmail string
if superGroup.UseManagedAccount {
memberEmail = member.User.Cid + "@chalmers.it"
} else {
memberEmail = member.User.Email
}

if member.Post.EmailPrefix != "" {
memberGroupPostGroup, exists := memberGroupPostGroups[member.Post.EmailPrefix]
if !exists {
memberGroupPostGroup = model.Group{
Email: strings.ToLower(member.Post.EmailPrefix + "." + group.Name + "@chalmers.it"),
Type: superGroup.Type,
Aliases: []string{},
}

}
memberGroupPostGroup.Members = append(memberGroupPostGroup.Members, memberEmail)
memberGroupPostGroups[member.Post.EmailPrefix] = memberGroupPostGroup

postSuperGroup, exists := superGroupPostGroups[member.Post.EmailPrefix]
if !exists {
postSuperGroup = model.Group{
Email: strings.ToLower(member.Post.EmailPrefix + "." + superGroup.Name + "@chalmers.it"),
Type: superGroup.Type,
Aliases: []string{},
}
}
if !slices.Contains(postSuperGroup.Members, memberGroupPostGroup.Email) {
postSuperGroup.Members = append(postSuperGroup.Members, memberGroupPostGroup.Email)
superGroupPostGroups[member.Post.EmailPrefix] = postSuperGroup
}
}

memberGroup.Members = append(memberGroup.Members, memberEmail)
}
outerGroup.Members = append(outerGroup.Members, memberGroup.Email)
groups = append(groups, memberGroup)
}
for _, postGroup := range superGroupPostGroups {
groups = append(groups, postGroup)
}
return group
groups = append(groups, outerGroup)
return groups
}

func (superGroups AuthSuperGroups) ToGroups() model.Groups {
groupsList := model.Groups{}
for _, superGroup := range superGroups {
groupsList = append(groupsList, superGroup.ToGroup())
groupsList = append(groupsList, superGroup.ToGroups()...)
}
return groupsList
}

0 comments on commit 629de7c

Please sign in to comment.