File tree 4 files changed +83
-5
lines changed
4 files changed +83
-5
lines changed Original file line number Diff line number Diff line change
1
+ package httpmw
2
+
3
+ //func Can(action rbac.Action, object rbac.Object) func(http.Handler) http.Handler {
4
+ // return func(next http.Handler) http.Handler {
5
+ // return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
6
+ //
7
+ // },
8
+ // }
9
+ //}
Original file line number Diff line number Diff line change
1
+ package rbac
2
+
3
+ import (
4
+ "strings"
5
+
6
+ "golang.org/x/xerrors"
7
+ )
8
+
9
+ const (
10
+ Admin = "admin"
11
+ Member = "member"
12
+
13
+ OrganizationMember = "organization-member"
14
+ OrganizationAdmin = "organization-admin"
15
+ )
16
+
17
+ // RoleByName returns the permissions associated with a given role name.
18
+ // This allows just the role names to be stored.
19
+ func RoleByName (name string ) (Role , error ) {
20
+ arr := strings .Split (name , ":" )
21
+ if len (arr ) > 2 {
22
+ return Role {}, xerrors .Errorf ("too many semicolons in role name" )
23
+ }
24
+
25
+ roleName := arr [0 ]
26
+ var scopeID string
27
+ if len (arr ) > 1 {
28
+ scopeID = arr [1 ]
29
+ }
30
+
31
+ // If the role requires a scope, the scope will be checked at the end
32
+ // of the switch statement.
33
+ var scopedRole Role
34
+ switch roleName {
35
+ case Admin :
36
+ return RoleAdmin , nil
37
+ case Member :
38
+ return RoleMember , nil
39
+ case OrganizationMember :
40
+ scopedRole = RoleOrgMember (scopeID )
41
+ case OrganizationAdmin :
42
+ scopedRole = RoleOrgAdmin (scopeID )
43
+ default :
44
+ // No role found
45
+ return Role {}, xerrors .Errorf ("role %q not found" , roleName )
46
+ }
47
+
48
+ // Scoped roles should be checked their scope is set
49
+ if scopeID == "" {
50
+ return Role {}, xerrors .Errorf ("%q requires a scope id" , roleName )
51
+ }
52
+
53
+ return scopedRole , nil
54
+ }
55
+
56
+ func RoleName (name string , scopeID string ) string {
57
+ if scopeID == "" {
58
+ return name
59
+ }
60
+ return name + ":" + scopeID
61
+ }
Original file line number Diff line number Diff line change @@ -22,8 +22,8 @@ func TestExample(t *testing.T) {
22
22
user := subject {
23
23
UserID : "alice" ,
24
24
Roles : []rbac.Role {
25
- rbac .RoleOrgAdmin ( "default" ),
26
- rbac .RoleMember ,
25
+ must ( rbac .RoleByName ( rbac . Member ) ),
26
+ must ( rbac .RoleByName ( rbac . RoleName ( rbac . OrganizationMember , "default" ))) ,
27
27
},
28
28
}
29
29
@@ -52,3 +52,10 @@ func TestExample(t *testing.T) {
52
52
require .NoError (t , err , "this user can read workspace '1234'" )
53
53
})
54
54
}
55
+
56
+ func must [T any ](value T , err error ) T {
57
+ if err != nil {
58
+ panic (err )
59
+ }
60
+ return value
61
+ }
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package rbac
2
2
3
3
import "fmt"
4
4
5
+ // Permission is the format passed into the rego.
5
6
type Permission struct {
6
7
// Negate makes this a negative permission
7
8
Negate bool `json:"negate"`
57
58
58
59
func RoleOrgDenyAll (orgID string ) Role {
59
60
return Role {
60
- Name : "org-deny-" + orgID ,
61
+ Name : RoleName ( "org-deny" , orgID ) ,
61
62
Org : map [string ][]Permission {
62
63
orgID : {
63
64
{
@@ -75,7 +76,7 @@ func RoleOrgDenyAll(orgID string) Role {
75
76
// organization scope.
76
77
func RoleOrgAdmin (orgID string ) Role {
77
78
return Role {
78
- Name : "org-admin-" + orgID ,
79
+ Name : RoleName ( "org-admin:" , orgID )
79
80
Org : map [string ][]Permission {
80
81
orgID : {
81
82
{
@@ -93,7 +94,7 @@ func RoleOrgAdmin(orgID string) Role {
93
94
// organization scope.
94
95
func RoleOrgMember (orgID string ) Role {
95
96
return Role {
96
- Name : "org-member-" + orgID ,
97
+ Name : RoleName ( "org-member:" , orgID ) ,
97
98
Org : map [string ][]Permission {
98
99
orgID : {},
99
100
},
You can’t perform that action at this time.
0 commit comments