Skip to content
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

Don't panic if config is nil in community #3743

Merged
merged 2 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 80 additions & 80 deletions appdatabase/migrations/bindata.go

Large diffs are not rendered by default.

289 changes: 169 additions & 120 deletions appdatabase/migrationsprevnodecfg/bindata.go

Large diffs are not rendered by default.

34 changes: 16 additions & 18 deletions mailserver/migrations/bindata.go

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

77 changes: 42 additions & 35 deletions multiaccounts/migrations/bindata.go

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

32 changes: 15 additions & 17 deletions protocol/anonmetrics/migrations/migrations.go

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

2 changes: 1 addition & 1 deletion protocol/communities/community.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ func (o *Community) hasMemberPermission(member *protobuf.CommunityMember, permis
}

func (o *Community) hasPermission(pk *ecdsa.PublicKey, roles map[protobuf.CommunityMember_Roles]bool) bool {
if pk == nil || o.config.ID == nil {
if pk == nil || o.config == nil || o.config.ID == nil {
return false
}

Expand Down
38 changes: 38 additions & 0 deletions protocol/communities/community_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,44 @@ func (s *CommunitySuite) SetupTest() {

}

func (s *CommunitySuite) TestHasPermission() {
// returns false if empty public key is passed
community := &Community{}
ownerKey, err := crypto.GenerateKey()
s.Require().NoError(err)

nonMemberKey, err := crypto.GenerateKey()
s.Require().NoError(err)

memberKey, err := crypto.GenerateKey()
s.Require().NoError(err)

s.Require().False(community.hasPermission(nil, adminRolePermissions()))

// returns false if key is passed, but config is nil
s.Require().False(community.hasPermission(&nonMemberKey.PublicKey, adminRolePermissions()))

// returns true if the user is the owner

communityDescription := &protobuf.CommunityDescription{}
communityDescription.Members = make(map[string]*protobuf.CommunityMember)
communityDescription.Members[common.PubkeyToHex(&memberKey.PublicKey)] = &protobuf.CommunityMember{Roles: []protobuf.CommunityMember_Roles{protobuf.CommunityMember_ROLE_ADMIN}}

community.config = &Config{ID: &ownerKey.PublicKey, CommunityDescription: communityDescription}

s.Require().True(community.hasPermission(&ownerKey.PublicKey, adminRolePermissions()))

// return false if user is not a member
s.Require().False(community.hasPermission(&nonMemberKey.PublicKey, adminRolePermissions()))

// return true if user is a member and has permissions
s.Require().True(community.hasPermission(&memberKey.PublicKey, adminRolePermissions()))

// return false if user is a member and does not have permissions
s.Require().False(community.hasPermission(&memberKey.PublicKey, ownerRolePermission()))

}

func (s *CommunitySuite) TestInviteUserToOrg() {
newMember, err := crypto.GenerateKey()
s.Require().NoError(err)
Expand Down
4 changes: 4 additions & 0 deletions protocol/communities/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,10 @@ func (m *Manager) HandleCommunityAdminEvent(signer *ecdsa.PublicKey, adminEvent
return nil, err
}

if community == nil {
return nil, ErrOrgNotFound
}

if !community.IsMemberAdmin(signer) {
return nil, errors.New("user is not an admin")
}
Expand Down
Loading