Skip to content

Commit d3f829a

Browse files
committed
Add GroupsForUser as a prelude to some seagull / gatekeeper
functionality.
1 parent 8e5598f commit d3f829a

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

permission/client/client.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ func (c *Client) GetUserPermissions(ctx context.Context, requestUserID string, t
4747
return permission.FixOwnerPermissions(result), nil
4848
}
4949

50+
// GroupsForUser returns what users have shared permissions with the user with an id of granteeUserID.
51+
// The GroupedPermissions are keyed by the id of the user who shared their permissions with granteeUserID.
52+
func (c *Client) GroupsForUser(ctx context.Context, granteeUserID string) (permission.GroupedPermissions, error) {
53+
if ctx == nil {
54+
return nil, errors.New("context is missing")
55+
}
56+
if granteeUserID == "" {
57+
return nil, errors.New("user id is missing")
58+
}
59+
60+
url := c.client.ConstructURL("access", "groups", granteeUserID)
61+
result := permission.GroupedPermissions{}
62+
if err := c.client.RequestData(ctx, "GET", url, nil, nil, &result); err != nil {
63+
if request.IsErrorResourceNotFound(err) {
64+
return nil, request.ErrorUnauthorized()
65+
}
66+
return nil, err
67+
}
68+
69+
return permission.FixGroupedOwnerPermissions(result), nil
70+
}
71+
5072
func (c *Client) HasMembershipRelationship(ctx context.Context, granteeUserID, grantorUserID string) (has bool, err error) {
5173
fromTo, err := c.GetUserPermissions(ctx, granteeUserID, grantorUserID)
5274
if err != nil {

permission/permission.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
type Permission map[string]interface{}
88
type Permissions map[string]Permission
9+
type GroupedPermissions map[string]Permissions
910

1011
const (
1112
Follow = "follow"
@@ -17,11 +18,19 @@ const (
1718

1819
type Client interface {
1920
GetUserPermissions(ctx context.Context, requestUserID string, targetUserID string) (Permissions, error)
21+
GroupsForUser(ctx context.Context, granteeUserID string) (GroupedPermissions, error)
2022
HasMembershipRelationship(ctx context.Context, granteeUserID, grantorUserID string) (has bool, err error)
2123
HasCustodianPermissions(ctx context.Context, granteeUserID, grantorUserID string) (has bool, err error)
2224
HasWritePermissions(ctx context.Context, granteeUserID, grantorUserID string) (has bool, err error)
2325
}
2426

27+
func FixGroupedOwnerPermissions(groupPermissions GroupedPermissions) GroupedPermissions {
28+
for key, perms := range groupPermissions {
29+
groupPermissions[key] = FixOwnerPermissions(perms)
30+
}
31+
return groupPermissions
32+
}
33+
2534
func FixOwnerPermissions(permissions Permissions) Permissions {
2635
if ownerPermission, ok := permissions[Owner]; ok {
2736
if _, ok = permissions[Write]; !ok {

0 commit comments

Comments
 (0)