This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
permission.go
137 lines (118 loc) · 3.39 KB
/
permission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package user
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/factly/dega-server/util/arrays"
"github.com/factly/x/renderx"
"github.com/go-chi/chi"
"github.com/factly/dega-server/service/core/action/policy"
"github.com/factly/dega-server/util"
"github.com/factly/dega-server/service/core/model"
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/middlewarex"
)
// userpermissions - Get user's permission
// @Summary Get user's permission
// @Description Get user's permission
// @Tags Users
// @ID get-users-permission
// @Consume json
// @Produce json
// @Param X-User header string true "User ID"
// @Param X-Space header string true "Space ID"
// @Param user_id path string true "User ID"
// @Success 200 {object} []model.Permission
// @Router /core/users/{user_id}/permissions [get]
func userpermissions(w http.ResponseWriter, r *http.Request) {
uID, err := middlewarex.GetUser(r.Context())
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.Unauthorized()))
return
}
sID, err := middlewarex.GetSpace(r.Context())
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.Unauthorized()))
return
}
oID, err := util.GetOrganisation(r.Context())
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.Unauthorized()))
return
}
userID := chi.URLParam(r, "user_id")
id, err := strconv.Atoi(userID)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InvalidID()))
return
}
var result []model.Permission
// check if the user is admin of organisation
isAdmin := util.CheckSpaceKetoPermission("all", uint(oID), uint(uID))
// fetch all the keto policies
policyList, err := policy.GetAllPolicies()
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
return
}
if isAdmin == nil {
// logged user is admin and user_id is also admin's
if id == uID {
allPermission := []model.Permission{
model.Permission{
Resource: "admin",
Actions: []string{"admin"},
},
}
renderx.JSON(w, http.StatusOK, allPermission)
return
}
result = GetPermissions(int(oID), int(sID), id, policyList)
} else {
// logged user not admin
errorx.Render(w, errorx.Parser(errorx.GetMessage("Not allowed", http.StatusUnauthorized)))
return
}
renderx.JSON(w, http.StatusOK, result)
}
// GetPermissions gets user's permissions
func GetPermissions(oID, sID, uID int, policyList []model.KetoPolicy) []model.Permission {
permissionsMap := make(map[string][]string)
spacePrefix := fmt.Sprint("id:org:", oID, ":app:dega:space:", sID, ":")
permissions := make([]model.Permission, 0)
for _, pol := range policyList {
if strings.HasPrefix(pol.ID, spacePrefix) {
var isPresent bool = false
for _, user := range pol.Subjects {
if user == fmt.Sprint(uID) {
isPresent = true
break
}
}
if isPresent {
polPermission := policy.GetPermissions(pol, uint(uID))
for _, per := range polPermission {
if _, found := permissionsMap[per.Resource]; !found {
permissionsMap[per.Resource] = make([]string, 0)
}
permissionsMap[per.Resource] = arrays.Union(permissionsMap[per.Resource], per.Actions)
}
}
}
}
for res, act := range permissionsMap {
perm := model.Permission{
Resource: res,
Actions: act,
}
permissions = append(permissions, perm)
}
return permissions
}