Skip to content

Commit f1f75c4

Browse files
committed
Fixed issues with user org swaps
1 parent 89502c4 commit f1f75c4

File tree

2 files changed

+97
-13
lines changed

2 files changed

+97
-13
lines changed

db-connector.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4619,6 +4619,18 @@ func GetUsersByOrg(ctx context.Context, orgId string) ([]User, error) {
46194619
nameKey := "Users"
46204620

46214621
users := []User{}
4622+
cacheKey := fmt.Sprintf("%s_orgusers_%s", nameKey, orgId)
4623+
if project.CacheDb {
4624+
cache, err := GetCache(ctx, cacheKey)
4625+
if err == nil {
4626+
cacheData := []byte(cache.([]uint8))
4627+
err = json.Unmarshal(cacheData, &users)
4628+
if err == nil {
4629+
return users, nil
4630+
}
4631+
}
4632+
}
4633+
46224634
if project.DbType == "opensearch" {
46234635
return users, errors.New("Not implemented")
46244636
} else {
@@ -4635,6 +4647,19 @@ func GetUsersByOrg(ctx context.Context, orgId string) ([]User, error) {
46354647
}
46364648
}
46374649

4650+
if project.CacheDb {
4651+
marshaled, err := json.Marshal(users)
4652+
if err != nil {
4653+
log.Printf("[WARNING] Failed marshalling users for cache: %s", err)
4654+
return users, nil
4655+
}
4656+
4657+
err = SetCache(ctx, cacheKey, marshaled, 1)
4658+
if err != nil {
4659+
log.Printf("[WARNING] Failed setting cache for users by org '%s': %s", cacheKey, err)
4660+
}
4661+
}
4662+
46384663
return users, nil
46394664
}
46404665

@@ -6904,7 +6929,7 @@ func GetPrioritizedApps(ctx context.Context, user User) ([]WorkflowApp, error) {
69046929
}
69056930

69066931
orgFound = true
6907-
log.Printf("[DEBUG] Found matching org %s in parent org %s", newApp.ReferenceOrg, parentOrg.Id)
6932+
//log.Printf("[DEBUG] Found matching org %s in parent org %s", newApp.ReferenceOrg, parentOrg.Id)
69086933
break
69096934
}
69106935

shared.go

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,10 +1015,21 @@ func HandleGetOrg(resp http.ResponseWriter, request *http.Request) {
10151015
}
10161016

10171017
if !userFound && !sanitizeOrg {
1018-
log.Printf("[ERROR] User '%s' (%s) isn't a part of org %s (get)", user.Username, user.Id, org.Id)
1019-
resp.WriteHeader(401)
1020-
resp.Write([]byte(`{"success": false, "reason": "User doesn't have access to org"}`))
1021-
return
1018+
found := false
1019+
for _, orgId := range user.Orgs {
1020+
if orgId == org.Id {
1021+
found = true
1022+
admin = false
1023+
break
1024+
}
1025+
}
1026+
1027+
if !found {
1028+
log.Printf("[ERROR] User '%s' (%s) isn't a part of org %s (get)", user.Username, user.Id, org.Id)
1029+
resp.WriteHeader(401)
1030+
resp.Write([]byte(`{"success": false, "reason": "User doesn't have access to org"}`))
1031+
return
1032+
}
10221033

10231034
}
10241035
}
@@ -1182,7 +1193,6 @@ func HandleGetOrg(resp http.ResponseWriter, request *http.Request) {
11821193
}
11831194
}
11841195

1185-
org.Users = []User{}
11861196
org.SyncConfig.Apikey = ""
11871197
org.SyncConfig.Source = ""
11881198

@@ -1215,6 +1225,8 @@ func HandleGetOrg(resp http.ResponseWriter, request *http.Request) {
12151225
}
12161226
}
12171227
}
1228+
} else {
1229+
org.Users = []User{}
12181230
}
12191231

12201232
// This is for sending branding information
@@ -9292,6 +9304,32 @@ func HandleGetUsers(resp http.ResponseWriter, request *http.Request) {
92929304
newUsers = append(newUsers, item)
92939305
}
92949306

9307+
if project.Environment == "cloud" {
9308+
orgUsers, err := GetUsersByOrg(ctx, user.ActiveOrg.Id)
9309+
if err != nil {
9310+
log.Printf("[WARNING] Failed getting org users for support access: %s", err)
9311+
} else {
9312+
for _, orgUser := range orgUsers {
9313+
found := false
9314+
for _, existingUser := range newUsers {
9315+
if existingUser.Id == orgUser.Id {
9316+
found = true
9317+
break
9318+
}
9319+
}
9320+
9321+
if found {
9322+
continue
9323+
}
9324+
9325+
//orgUser.Deleted = true
9326+
orgUser.LoginType = "DELETED"
9327+
orgUser.Role = "user"
9328+
newUsers = append(newUsers, orgUser)
9329+
}
9330+
}
9331+
}
9332+
92959333
deduplicatedUsers := []User{}
92969334
for _, item := range newUsers {
92979335
found := false
@@ -10703,7 +10741,7 @@ func DeleteUser(resp http.ResponseWriter, request *http.Request) {
1070310741
resp.Write([]byte(`{"success": true}`))
1070410742
}
1070510743

10706-
func HandleDeleteUsersAccount(resp http.ResponseWriter, request *http.Request) {
10744+
func HandleDeleteUsersAccountPermanent(resp http.ResponseWriter, request *http.Request) {
1070710745

1070810746
cors := HandleCors(resp, request)
1070910747
if cors {
@@ -10758,8 +10796,15 @@ func HandleDeleteUsersAccount(resp http.ResponseWriter, request *http.Request) {
1075810796
return
1075910797
}
1076010798

10761-
if !userInfo.SupportAccess && userInfo.Id != foundUser.Id {
10762-
log.Printf("Unauthorized user (%s) attempted to delete an account. Must be a user or have support access.", userInfo.Username)
10799+
if !userInfo.SupportAccess {
10800+
log.Printf("[INFO] Unauthorized user (%s) attempted to delete an account. Must be a user or have support access.", userInfo.Username)
10801+
resp.WriteHeader(401)
10802+
resp.Write([]byte(`{"success": false, "reason": "Unauthorize User. Must be a regular user or have support access"}`))
10803+
return
10804+
}
10805+
10806+
if userInfo.Id != foundUser.Id {
10807+
log.Printf("[INFO] Unauthorized user (%s) attempted to delete an account. Must be a user or have support access.", userInfo.Username)
1076310808
resp.WriteHeader(401)
1076410809
resp.Write([]byte(`{"success": false, "reason": "Unauthorize User. Must be a regular user or have support access"}`))
1076510810
return
@@ -11646,10 +11691,24 @@ func HandleChangeUserOrg(resp http.ResponseWriter, request *http.Request) {
1164611691
}
1164711692

1164811693
if !userFound && !user.SupportAccess {
11649-
log.Printf("[ERROR] User %s (%s) can't change to org %s (%s) (2)", user.Username, user.Id, org.Name, org.Id)
11650-
resp.WriteHeader(403)
11651-
resp.Write([]byte(`{"success": false, "reason": "No permission to change to this org (2). Please contact [email protected] if this is unexpected."}`))
11652-
return
11694+
11695+
// FIXME: This changes the source of truth from JUST org.Users to user.Orgs
11696+
// May be a problem in worst case scenarios, but only works for orgids
11697+
// you know, so chance of causing an issue is **VERY** low.
11698+
found := false
11699+
for _, orgId := range user.Orgs {
11700+
if orgId == org.Id {
11701+
found = true
11702+
break
11703+
}
11704+
}
11705+
11706+
if !found {
11707+
log.Printf("[ERROR] User %s (%s) can't change to org %s (%s) (2)", user.Username, user.Id, org.Name, org.Id)
11708+
resp.WriteHeader(403)
11709+
resp.Write([]byte(`{"success": false, "reason": "No permission to change to this org (2). Please contact [email protected] if this is unexpected."}`))
11710+
return
11711+
}
1165311712
}
1165411713

1165511714
if user.SupportAccess {

0 commit comments

Comments
 (0)