-
Notifications
You must be signed in to change notification settings - Fork 391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(boards2): member role management #3512
Merged
jeronimoalbi
merged 8 commits into
gnolang:devx/feature/boardsv2
from
gnostudio:feat/refine-roles-permissions
Jan 16, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b23d378
chore: change permissions WithUser option to add it as DAO member
jeronimoalbi b421d4e
chore: update TODOs
jeronimoalbi f78ce9c
feat: update role and permissions for super and boards DAOs
jeronimoalbi ddbd0c8
test: fix unit tests
jeronimoalbi abce625
feat: change Permissions interface to support changing user roles
jeronimoalbi c5d5187
test: change set user roles to check user exists
jeronimoalbi 1c384bb
feat: support changing member roles
jeronimoalbi bedfc6f
test: add file tests for `ChangeMemberRole()`
jeronimoalbi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,20 +32,6 @@ func NewDefaultPermissions(dao *admindao.AdminDAO, options ...DefaultPermissions | |
return dp | ||
} | ||
|
||
// Roles returns the list of roles. | ||
func (dp DefaultPermissions) Roles() []Role { | ||
var roles []Role | ||
if dp.superRole != "" { | ||
roles = append(roles, dp.superRole) | ||
} | ||
|
||
dp.roles.Iterate("", "", func(name string, _ interface{}) bool { | ||
roles = append(roles, Role(name)) | ||
return false | ||
}) | ||
return roles | ||
} | ||
Comment on lines
-36
to
-47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||
|
||
// RoleExists checks if a role exists. | ||
func (dp DefaultPermissions) RoleExists(r Role) bool { | ||
if dp.superRole != "" && r == dp.superRole { | ||
|
@@ -104,18 +90,18 @@ func (dp *DefaultPermissions) AddUser(user std.Address, roles ...Role) error { | |
return errors.New("user already exists") | ||
} | ||
|
||
for _, r := range roles { | ||
if !dp.RoleExists(r) { | ||
return errors.New("invalid role: " + string(r)) | ||
} | ||
} | ||
|
||
if err := dp.dao.AddMember(user); err != nil { | ||
return err | ||
} | ||
return dp.setUserRoles(user, roles...) | ||
} | ||
|
||
dp.users.Set(user.String(), append([]Role(nil), roles...)) | ||
return nil | ||
// SetUserRoles sets the roles of a user. | ||
func (dp *DefaultPermissions) SetUserRoles(user std.Address, roles ...Role) error { | ||
if !dp.users.Has(user.String()) { | ||
return errors.New("user not found") | ||
} | ||
return dp.setUserRoles(user, roles...) | ||
} | ||
|
||
// RemoveUser removes a user from permissions. | ||
|
@@ -125,6 +111,11 @@ func (dp *DefaultPermissions) RemoveUser(user std.Address) bool { | |
return removed | ||
} | ||
|
||
// HasUser checks if a user exists. | ||
func (dp DefaultPermissions) HasUser(user std.Address) bool { | ||
return dp.users.Has(user.String()) | ||
} | ||
|
||
// GetDAO returns the underlying DAO. | ||
// Returned value can be nil if the implementation doesn't have a DAO. | ||
func (dp DefaultPermissions) GetDAO() *admindao.AdminDAO { | ||
|
@@ -146,11 +137,24 @@ func (dp *DefaultPermissions) WithPermission(user std.Address, perm Permission, | |
dp.handleBoardRename(args, cb) | ||
case PermissionMemberInvite: | ||
dp.handleMemberInvite(args, cb) | ||
case PermissionRoleChange: | ||
dp.handleRoleChange(args, cb) | ||
default: | ||
cb(args) | ||
} | ||
} | ||
|
||
func (dp *DefaultPermissions) setUserRoles(user std.Address, roles ...Role) error { | ||
for _, r := range roles { | ||
if !dp.RoleExists(r) { | ||
return errors.New("invalid role: " + string(r)) | ||
} | ||
} | ||
|
||
dp.users.Set(user.String(), append([]Role(nil), roles...)) | ||
return nil | ||
} | ||
|
||
func (DefaultPermissions) handleBoardCreate(args Args, cb func(Args)) { | ||
name, ok := args[0].(string) | ||
if !ok { | ||
|
@@ -192,15 +196,63 @@ func (dp DefaultPermissions) handleMemberInvite(args Args, cb func(Args)) { | |
cb(args) | ||
} | ||
|
||
func (dp DefaultPermissions) handleRoleChange(args Args, cb func(Args)) { | ||
// Owners and Admins can change roles. | ||
// Admins should not be able to assign or remove the Owner role from members. | ||
caller := std.GetOrigCaller() | ||
if dp.HasRole(caller, RoleAdmin) { | ||
role, ok := args[2].(Role) | ||
if !ok { | ||
panic("expected a valid member role") | ||
} | ||
|
||
if role == RoleOwner { | ||
panic("admins are not allowed to promote members to Owner") | ||
} else { | ||
member, ok := args[1].(std.Address) | ||
if !ok { | ||
panic("expected a valid member address") | ||
} | ||
|
||
if dp.HasRole(member, RoleOwner) { | ||
panic("admins are not allowed to remove the Owner role") | ||
} | ||
} | ||
} | ||
|
||
cb(args) | ||
} | ||
|
||
func createDefaultPermissions(owner std.Address) *DefaultPermissions { | ||
// TODO: DAO should be a different realm or proposal and voting functions should be part of boards realm? | ||
// Permissions and DAO mechanics should be discussed and improved. Add `GetDAO()` to `Permissions`?? | ||
return NewDefaultPermissions( | ||
admindao.New(admindao.WithMember(owner)), | ||
admindao.New(), | ||
WithSuperRole(RoleOwner), | ||
WithRole(RoleAdmin, PermissionBoardCreate, PermissionMemberInvite), | ||
// TODO: Finish assigning all roles and permissions | ||
// WithRole(RoleModerator, permissions...), | ||
WithRole( | ||
RoleAdmin, | ||
PermissionBoardCreate, | ||
PermissionBoardRename, | ||
PermissionMemberInvite, | ||
PermissionMemberRemove, | ||
PermissionThreadCreate, | ||
PermissionThreadEdit, | ||
PermissionThreadDelete, | ||
PermissionThreadFlag, | ||
PermissionReplyDelete, | ||
PermissionReplyFlag, | ||
PermissionRoleChange, | ||
), | ||
WithRole( | ||
RoleModerator, | ||
PermissionThreadCreate, | ||
PermissionThreadEdit, | ||
PermissionThreadFlag, | ||
PermissionReplyFlag, | ||
), | ||
WithRole( | ||
RoleGuest, | ||
PermissionThreadCreate, | ||
PermissionThreadRepost, | ||
), | ||
WithUser(owner, RoleOwner), | ||
) | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed member initialization because
WithUser(owner, RoleOwner)
now adds the user to the DAO if it's not a member.