/
get.go
73 lines (64 loc) · 1.73 KB
/
get.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
package rulesvc
import (
"context"
"github.com/common-fate/common-fate/pkg/identity"
"github.com/common-fate/common-fate/pkg/rule"
"github.com/common-fate/common-fate/pkg/storage"
)
func (s *Service) GetRule(ctx context.Context, ID string, user *identity.User, isAdmin bool) (*rule.GetAccessRuleResponse, error) {
q := storage.GetAccessRuleCurrent{ID: ID}
_, err := s.DB.Query(ctx, &q)
// Throw storage errors if they occur
if err != nil {
return nil, err
}
if canGet(user, q.Result, isAdmin) {
canRequest := false
// even though admins can see the rule, requesting access to rule is only possible
// if they are explicitly selected as request groups when creating the rule.
for _, group := range user.Groups {
for _, g := range q.Result.Groups {
if g == group {
canRequest = true
}
}
}
return &rule.GetAccessRuleResponse{
Rule: q.Result,
CanRequest: canRequest,
}, nil
}
// Otherwise not allowed
return nil, ErrUserNotAuthorized
}
// canGet checks if the user is in the list of groups,
// or if the user is an approver of the rule
func canGet(user *identity.User, rule *rule.AccessRule, isAdmin bool) bool {
// Admins can always access a rule
if isAdmin {
return true
}
// DE = User can see a rule they're an approver for
for _, au := range rule.Approval.Users {
if au == user.ID {
return true
}
}
// DE = User can see a rule they're an approver of (via groups)
for _, group := range user.Groups {
for _, g := range rule.Approval.Groups {
if g == group {
return true
}
}
}
// DE = User can see a rule they're assigned to (via the groups)
for _, group := range user.Groups {
for _, g := range rule.Groups {
if g == group {
return true
}
}
}
return false
}