diff --git a/devtools/deployments/multi-tenancy/initialize_users.go b/devtools/deployments/multi-tenancy/initialize_users.go index 245a2569b3..ebbbbb1073 100644 --- a/devtools/deployments/multi-tenancy/initialize_users.go +++ b/devtools/deployments/multi-tenancy/initialize_users.go @@ -90,6 +90,7 @@ func main() { } resetAllUserPasswords() + setUserRoles() } func createUser(client *libregraph.APIClient, user libregraph.EducationUser) (string, error) { @@ -112,6 +113,34 @@ func createTenant(client *libregraph.APIClient, tenant libregraph.EducationSchoo return newTenant.GetId(), nil } +func setUserRoles() { + tls := tls.Config{InsecureSkipVerify: true} + restyClient := resty.New().SetTLSClientConfig(&tls) + client := gocloak.NewClient("https://keycloak.opencloud.test") + client.SetRestyClient(restyClient) + ctx := context.Background() + token, err := client.LoginAdmin(ctx, "kcadmin", "admin", "master") + if err != nil { + fmt.Printf("Failed to login: %s\n", err) + panic("Something wrong with the credentials or url") + } + + role, _ := client.GetRealmRole(ctx, token.AccessToken, "openCloud", "opencloudUser") + users, err := client.GetUsers(ctx, token.AccessToken, "openCloud", gocloak.GetUsersParams{}) + if err != nil { + fmt.Printf("%s\n", err) + panic("Oh no!, failed to list users :(") + } + for _, user := range users { + err := client.AddRealmRoleToUser(ctx, token.AccessToken, "openCloud", *user.ID, []gocloak.Role{ + *role, + }) + if err != nil { + fmt.Printf("Failed to assign role to user %s: %s\n", *user.Username, err) + } + } +} + func resetAllUserPasswords() { tls := tls.Config{InsecureSkipVerify: true} restyClient := resty.New().SetTLSClientConfig(&tls)