Skip to content

Commit

Permalink
server: fix zitadel user api's
Browse files Browse the repository at this point in the history
  • Loading branch information
shreeharsha-factly committed Jan 25, 2025
1 parent b640bec commit 63b4e64
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 57 deletions.
13 changes: 6 additions & 7 deletions server/service/core/action/author/list.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package author

import (
"log"
"net/http"

"github.com/factly/dega-server/config"
Expand Down Expand Up @@ -67,17 +66,15 @@ func list(w http.ResponseWriter, r *http.Request) {
}

// get users details from zitadel
zitadelUsers, err := zitadel.GetOrganisationUsers(r.Header.Get("Authorization"), authCtx.OrganisationID, uIDs, nil)
res, err := zitadel.GetOrganisationUsers(r.Header.Get("Authorization"), authCtx.OrganisationID, uIDs, nil)

if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}

log.Println(zitadelUsers)

for _, zitadelUser := range zitadelUsers {
for _, zitadelUser := range res.Result {
authors = append(authors, model.Author{
ID: zitadelUser.ID,
DisplayName: zitadelUser.Human.Profile.DisplayName,
Expand All @@ -87,6 +84,7 @@ func list(w http.ResponseWriter, r *http.Request) {
}

result.Nodes = authors
result.Total = res.Total

renderx.JSON(w, http.StatusOK, result)
}
Expand Down Expand Up @@ -125,9 +123,9 @@ func PublicList(w http.ResponseWriter, r *http.Request) {
}

// get users details from zitadel
zitadelUsers, _ := zitadel.GetOrganisationUsers(viper.GetString("ZITADEL_PERSONAL_ACCESS_TOKEN"), authCtx.OrganisationID, uIDs, nil)
res, _ := zitadel.GetOrganisationUsers(viper.GetString("ZITADEL_PERSONAL_ACCESS_TOKEN"), authCtx.OrganisationID, uIDs, nil)

for _, zitadelUser := range zitadelUsers {
for _, zitadelUser := range res.Result {
authors = append(authors, model.Author{
ID: zitadelUser.ID,
DisplayName: zitadelUser.Human.Profile.DisplayName,
Expand All @@ -137,6 +135,7 @@ func PublicList(w http.ResponseWriter, r *http.Request) {
}

result.Nodes = authors
result.Total = res.Total

renderx.JSON(w, http.StatusOK, result)
}
6 changes: 3 additions & 3 deletions server/service/core/action/policy/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ func create(w http.ResponseWriter, r *http.Request) {
SpaceID: authCtx.SpaceID,
}

users, err := zitadel.GetOrganisationUsers(r.Header.Get("Authorization"), authCtx.OrganisationID, policyReq.Users, nil)
res, err := zitadel.GetOrganisationUsers(r.Header.Get("Authorization"), authCtx.OrganisationID, policyReq.Users, nil)

if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
return
}

if len(users) != len(policyReq.Users) {
if int(res.Total) != len(policyReq.Users) {
errorx.Render(w, errorx.Parser(errorx.DecodeError()))
return
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func create(w http.ResponseWriter, r *http.Request) {
Users: []policyUser{},
}

for _, user := range users {
for _, user := range res.Result {
policyUser := policyUser{
UserID: user.ID,
DisplayName: user.Human.Profile.DisplayName,
Expand Down
6 changes: 3 additions & 3 deletions server/service/core/action/policy/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ func details(w http.ResponseWriter, r *http.Request) {
uIDs = append(uIDs, policyUser.UserID)
}

users, err := zitadel.GetOrganisationUsers(r.Header.Get("Authorisation"), authCtx.OrganisationID, uIDs, nil)
res, err := zitadel.GetOrganisationUsers(r.Header.Get("Authorisation"), authCtx.OrganisationID, uIDs, nil)

if err != nil {
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
return
}

if len(users) != len(uIDs) {
if int(res.Total) != len(uIDs) {
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
return
}
Expand Down Expand Up @@ -131,7 +131,7 @@ func details(w http.ResponseWriter, r *http.Request) {
Users: []policyUser{},
}

for _, user := range users {
for _, user := range res.Result {
result.Users = append(result.Users, policyUser{
UserID: user.ID,
DisplayName: user.Human.Profile.DisplayName,
Expand Down
2 changes: 1 addition & 1 deletion server/service/core/action/space/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ func usersList(w http.ResponseWriter, r *http.Request) {

var users []model.SpaceUser

zitadel.GetOrganisationUsers(r.Header.Get("authorization"), space.OrganisationID, []string{authCtx.UserID}, nil)
zitadel.GetOrganisationGrants(r.Header.Get("authorization"), space.OrganisationID)
renderx.JSON(w, http.StatusOK, users)
}
2 changes: 1 addition & 1 deletion server/service/core/action/space/users/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func list(w http.ResponseWriter, r *http.Request) {
users := make([]user, 0)

for _, id := range uIDs {
for _, u := range res {
for _, u := range res.Result {
if u.ID == id {
users = append(users, user{
ID: u.ID,
Expand Down
6 changes: 3 additions & 3 deletions server/service/core/action/space/users/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ func update(w http.ResponseWriter, r *http.Request) {
return
}

users, err := zitadel.GetOrganisationUsers(r.Header.Get("authorization"), authCtx.OrganisationID, req.IDs, nil)
orgsResult, err := zitadel.GetOrganisationUsers(r.Header.Get("authorization"), authCtx.OrganisationID, req.IDs, nil)

if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
return
}

if len(users) != len(req.IDs) {
if int(orgsResult.Total) != len(req.IDs) {
errorx.Render(w, errorx.Parser(errorx.DecodeError()))
return
}
Expand All @@ -54,7 +54,7 @@ func update(w http.ResponseWriter, r *http.Request) {

spaceUsers := make([]model.SpaceUser, 0)

for _, user := range users {
for _, user := range orgsResult.Result {
spaceUsers = append(spaceUsers, model.SpaceUser{
SpaceID: authCtx.SpaceID,
UserID: user.ID,
Expand Down
12 changes: 6 additions & 6 deletions server/service/core/action/user/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,24 @@ func list(w http.ResponseWriter, r *http.Request) {

// Get organisation ID

res, err := zitadel.GetOrganisationUsers(r.Header.Get("authorization"), authCtx.OrganisationID, []string{}, nil)
res, err := zitadel.GetOrganisationGrants(r.Header.Get("authorization"), authCtx.OrganisationID)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}

result := paging{
Total: int64(len(res)),
Total: res.Total,
}

users := make([]user, 0)

for _, u := range res {
for _, u := range res.Result {
users = append(users, user{
ID: u.ID,
DisplayName: u.Human.Profile.DisplayName,
Email: u.Human.Email.Email,
ID: u.UserID,
DisplayName: u.DisplayName,
Email: u.Email,
})
}

Expand Down
4 changes: 2 additions & 2 deletions server/util/authors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ func GetAuthors(token, orgID string, ids, userNames []string) (map[string]model.
if token == "" {
token = viper.GetString("ZITADEL_PERSONAL_ACCESS_TOKEN")
}
zitadelUsers, err := zitadel.GetOrganisationUsers(token, orgID, ids, userNames)
res, err := zitadel.GetOrganisationUsers(token, orgID, ids, userNames)

if err != nil {
return nil, err
}

// Adding author
authors := make(map[string]model.Author)
for _, zitadelUser := range zitadelUsers {
for _, zitadelUser := range res.Result {
author := model.Author{
ID: zitadelUser.ID,
DisplayName: zitadelUser.Human.Profile.DisplayName,
Expand Down
111 changes: 111 additions & 0 deletions server/util/zitadel/getOrganisationGrants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package zitadel

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"

"github.com/factly/x/loggerx"
"github.com/spf13/viper"
)

type OrganisationGrantsResponse struct {
Result []OrganisationGrant `json:"result"`
Details Details `json:"details"`
}

type Details struct {
TotalResult string `json:"totalResult"`
}

type OrganisationGrant struct {
UserID string `json:"userId"`
DisplayName string `json:"displayName"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Email string `json:"email"`
RoleKeys []string `json:"roleKeys"`
}

type OrganisationGrants struct {
Result []OrganisationGrant `json:"result"`
Total int64 `json:"total"`
}

func GetOrganisationGrants(token, orgID string) (OrganisationGrants, error) {
url := viper.GetString("zitadel_protocol") + "://" + viper.GetString("zitadel_domain") + "/management/v1/users/grants/_search"
method := "POST"

payload := ZitadelQueryPayload{
Query: Query{
Offset: 0,
Asc: true,
},
}
payload.Queries = make([]interface{}, 0)

payload.Queries = append(payload.Queries, map[string]interface{}{
"userTypeQuery": UserTypeQuery{
Type: "TYPE_HUMAN",
}})

allOrgs := OrganisationGrants{}
resp := OrganisationGrantsResponse{}

buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(payload)

client := &http.Client{}
req, err := http.NewRequest(method, url, buf)

if err != nil {
loggerx.Error(err)
return allOrgs, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
if token != "" {
req.Header.Add("Authorization", "Bearer "+getBearerToken(token))
} else {
req.Header.Add("Authorization", "Bearer "+getBearerToken(viper.GetString("ZITADEL_PERSONAL_ACCESS_TOKEN")))
}

req.Header.Add("x-zitadel-orgid", orgID)

res, err := client.Do(req)
if err != nil {
loggerx.Error(err)
return allOrgs, err
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return allOrgs, err
}

err = json.Unmarshal(body, &resp)
if err != nil {
loggerx.Error(err)
return allOrgs, err
}

allOrgs.Result = resp.Result

// covert string to int64
if resp.Details.TotalResult == "" {
return allOrgs, nil
}

total, err := strconv.ParseInt(resp.Details.TotalResult, 10, 64)
if err != nil {
return allOrgs, err
}
allOrgs.Total = int64(total)

return allOrgs, nil
}
Loading

0 comments on commit 63b4e64

Please sign in to comment.