Skip to content

Commit

Permalink
chore(boards2): simplify default permissions implementation (#3770)
Browse files Browse the repository at this point in the history
The default permissions implementation is an implementation of the
`Permissions` interfaces that is used by default when creating new
boards and also used as the default realm permissions.

The original idea is to be able to switch the implementation for a
custom one allowing boards to customize the board interaction rules, or
to be able to switch the realm implementation for a different one that
uses another DAO with different board creation rules for example. This
is an idea that still requires further discussions and refinement.

This PR removes the default permission creation options which at this
point makes no sense given that we don't have a boards2 package and
default permissions are only used within the realm.
  • Loading branch information
jeronimoalbi authored Feb 18, 2025
1 parent 871d13e commit 72c1407
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 230 deletions.
69 changes: 34 additions & 35 deletions examples/gno.land/r/nt/boards2/v1/board.gno
Original file line number Diff line number Diff line change
Expand Up @@ -153,40 +153,39 @@ func (board *Board) GetPostFormURL() string {
}

func createDefaultBoardPermissions(owner std.Address) *DefaultPermissions {
return NewDefaultPermissions(
commondao.New(),
WithSuperRole(RoleOwner),
WithRole(
RoleAdmin,
PermissionBoardRename,
PermissionBoardFlaggingUpdate,
PermissionMemberInvite,
PermissionMemberRemove,
PermissionThreadCreate,
PermissionThreadEdit,
PermissionThreadDelete,
PermissionThreadRepost,
PermissionThreadFlag,
PermissionReplyCreate,
PermissionReplyDelete,
PermissionReplyFlag,
PermissionRoleChange,
),
WithRole(
RoleModerator,
PermissionThreadCreate,
PermissionThreadEdit,
PermissionThreadRepost,
PermissionThreadFlag,
PermissionReplyCreate,
PermissionReplyFlag,
),
WithRole(
RoleGuest,
PermissionThreadCreate,
PermissionThreadRepost,
PermissionReplyCreate,
),
WithUser(owner, RoleOwner),
perms := NewDefaultPermissions(commondao.New())
perms.SetSuperRole(RoleOwner)
perms.AddRole(
RoleAdmin,
PermissionBoardRename,
PermissionBoardFlaggingUpdate,
PermissionMemberInvite,
PermissionMemberRemove,
PermissionThreadCreate,
PermissionThreadEdit,
PermissionThreadDelete,
PermissionThreadRepost,
PermissionThreadFlag,
PermissionReplyCreate,
PermissionReplyDelete,
PermissionReplyFlag,
PermissionRoleChange,
)
perms.AddRole(
RoleModerator,
PermissionThreadCreate,
PermissionThreadEdit,
PermissionThreadRepost,
PermissionThreadFlag,
PermissionReplyCreate,
PermissionReplyFlag,
)
perms.AddRole(
RoleGuest,
PermissionThreadCreate,
PermissionThreadRepost,
PermissionReplyCreate,
)
perms.AddUser(owner, RoleOwner)
return perms
}
33 changes: 0 additions & 33 deletions examples/gno.land/r/nt/boards2/v1/permission_options.gno

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"gno.land/r/demo/users"
)

// TODO: Move to a package or make it private?

// DefaultPermissions manages users, roles and permissions.
type DefaultPermissions struct {
superRole Role
Expand All @@ -22,16 +20,28 @@ type DefaultPermissions struct {

// NewDefaultPermissions creates a new permissions type.
// This type is a default implementation to handle users, roles and permissions.
func NewDefaultPermissions(dao *commondao.CommonDAO, options ...DefaultPermissionsOption) *DefaultPermissions {
dp := &DefaultPermissions{
func NewDefaultPermissions(dao *commondao.CommonDAO) *DefaultPermissions {
if dao == nil {
panic("default permissions require a DAO")
}

return &DefaultPermissions{
dao: dao,
roles: avl.NewTree(),
users: avl.NewTree(),
}
for _, apply := range options {
apply(dp)
}
return dp
}

// SetSuperRole assigns a super role.
// A super role is one that have all permissions.
// These type of role doesn't need to be mapped to any permission.
func (dp *DefaultPermissions) SetSuperRole(r Role) {
dp.superRole = r
}

// AddRole add a role with one or more assigned permissions.
func (dp *DefaultPermissions) AddRole(r Role, p Permission, extra ...Permission) {
dp.roles.Set(string(r), append([]Permission{p}, extra...))
}

// RoleExists checks if a role exists.
Expand Down Expand Up @@ -227,12 +237,11 @@ func (dp DefaultPermissions) handleRoleChange(args Args, cb func(Args)) {
}

func createDefaultPermissions(owner std.Address) *DefaultPermissions {
return NewDefaultPermissions(
commondao.New(),
WithSuperRole(RoleOwner),
WithRole(RoleAdmin, PermissionBoardCreate),
WithUser(owner, RoleOwner),
)
perms := NewDefaultPermissions(commondao.New())
perms.SetSuperRole(RoleOwner)
perms.AddRole(RoleAdmin, PermissionBoardCreate)
perms.AddUser(owner, RoleOwner)
return perms
}

func assertBoardNameIsNotAddress(s string) {
Expand Down
Loading

0 comments on commit 72c1407

Please sign in to comment.