Skip to content

Commit

Permalink
Add my permissions method to the API
Browse files Browse the repository at this point in the history
  • Loading branch information
demdxx committed Mar 31, 2024
1 parent d887008 commit 128a9fd
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 58 deletions.
2 changes: 1 addition & 1 deletion acl/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@ func HaveAccountLink(ctx context.Context, obj any) bool {

// HasPermission returns `true` if the `user` have all permissions from the list (without custom check)
func HasPermission(ctx context.Context, permissions ...string) bool {
return IsNoPermCheck(ctx) || session.Account(ctx).HasPermission(ctx, permissions...)
return IsNoPermCheck(ctx) || session.Account(ctx).HasPermission(permissions...)
}
191 changes: 147 additions & 44 deletions example/api/internal/server/graphql/generated/exec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 3 additions & 10 deletions example/api/internal/server/graphql/resolvers/rbac.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion model/account_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,16 @@ func (acc *Account) CheckedPermissions(ctx context.Context, resource any, patter
return acc.Permissions.CheckedPermissions(ctx, resource, patterns...)
}

// ListPermissions for the account
func (acc *Account) ListPermissions(patterns ...string) []rbac.Permission {
if acc == nil || acc.Permissions == nil {
return nil
}
return acc.Permissions.Permissions(patterns...)
}

// HasPermission for the account
func (acc *Account) HasPermission(ctx context.Context, patterns ...string) bool {
func (acc *Account) HasPermission(patterns ...string) bool {
return acc.Permissions.HasPermission(patterns...)
}

Expand Down
9 changes: 9 additions & 0 deletions model/account_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type permissionChecker interface {
CheckedPermissions(ctx context.Context, resource any, patterns ...string) rbac.Permission
ChildRoles() []rbac.Role
ChildPermissions() []rbac.Permission
Permissions(patterns ...string) []rbac.Permission
HasPermission(patterns ...string) bool
}

Expand Down Expand Up @@ -71,6 +72,14 @@ func (groups groupPermissionChecker) ChildPermissions() []rbac.Permission {
return perms
}

func (groups groupPermissionChecker) Permissions(patterns ...string) []rbac.Permission {
var perms []rbac.Permission
for _, group := range groups {
perms = append(perms, group.Permissions(patterns...)...)
}
return perms
}

func (groups groupPermissionChecker) HasPermission(patterns ...string) bool {
for _, group := range groups {
if group.HasPermission(patterns...) {
Expand Down
5 changes: 5 additions & 0 deletions protocol/graphql/schemas/rbac.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ extend type Query {
List of the RBAC permissions
"""
listPermissions(patterns: [String!] = null): [RBACPermission!] @hasPermissions(permissions: ["permission.list"])

"""
List of the RBAC permissions for the current user
"""
listMyPermissions(patterns: [String!] = null): [RBACPermission!] @hasPermissions(permissions: ["permission.list"])
}

extend type Mutation {
Expand Down
Loading

0 comments on commit 128a9fd

Please sign in to comment.