Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
finish implied permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehelmick committed Dec 17, 2020
1 parent 1ef33f0 commit cde9287
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
25 changes: 17 additions & 8 deletions pkg/controller/user/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,14 @@ func TestUpdate(t *testing.T) {

for _, permission := range rbac.NamePermissionMap {
permission := permission
target := fmt.Sprintf(`input#permission-%d`, permission)
targets := []string{fmt.Sprintf(`input#permission-%d`, permission)}
// We also ned to remove permissions that imply this permission, or it will be added back in.
for _, superPerm := range rbac.ImpliedBy(permission) {
targets = append(targets, fmt.Sprintf(`input#permission-%d`, superPerm))
}

if err := chromedp.Run(taskCtx,
// Build the actions as an array prior to run since super-permissions actions aren't known until runtime.
actions := []chromedp.Action{
// Pre-authenticate the user.
browser.SetCookie(cookie),

Expand All @@ -74,14 +79,18 @@ func TestUpdate(t *testing.T) {

// Wait for render.
chromedp.WaitVisible(`body#users-edit`, chromedp.ByQuery),
}

// Fill out the form.
chromedp.RemoveAttribute(target, "checked", chromedp.ByQuery),
chromedp.Submit(`form#user-form`, chromedp.ByQuery),
// Fill out the form.
for _, target := range targets {
actions = append(actions, chromedp.RemoveAttribute(target, "checked", chromedp.ByQuery))
}
actions = append(actions, chromedp.Submit(`form#user-form`, chromedp.ByQuery))

// Wait for render.
chromedp.WaitVisible(`body#users-show`, chromedp.ByQuery),
); err != nil {
// Wait for render.
actions = append(actions, chromedp.WaitVisible(`body#users-show`, chromedp.ByQuery))

if err := chromedp.Run(taskCtx, actions...); err != nil {
t.Fatal(err)
}
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/rbac/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ func CompileAndAuthorize(actorPermission Permission, toUpdate []Permission) (Per
return permission, nil
}

// ImpliedBy returns any permissions that
func ImpliedBy(permission Permission) []Permission {
return impliedBy[permission]
}

// PermissionNames returns the list of permissions included in the given
// permission.
func PermissionNames(p Permission) []string {
Expand Down Expand Up @@ -166,8 +171,23 @@ var (
MobileAppWrite: {MobileAppRead},
UserWrite: {UserRead},
}

// This is the inverse of the above map, set by the init() func.
// Done in code to ensure it always stays in sync with requiredPermission.
impliedBy = make(map[Permission][]Permission)
)

func init() {
for has, needs := range requiredPermission {
for _, perm := range needs {
if _, ok := impliedBy[perm]; !ok {
impliedBy[perm] = make([]Permission, 0, 1)
}
impliedBy[perm] = append(impliedBy[perm], has)
}
}
}

// --
// Legacy permissions
// --
Expand Down

0 comments on commit cde9287

Please sign in to comment.