From 1d2b508c20fd2831d5483871f3e18276ca9a0be7 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 10 Aug 2023 11:46:38 -0400 Subject: [PATCH] fix(communities_manager): fix joining community with admin permission (#3870) Fixes an issue where if a community had an admin permission, it would be impossible to join as a normal member because the admin permission wasn't satisfied. --- protocol/communities/manager.go | 3 +- ...nities_messenger_token_permissions_test.go | 144 ++++++++++++++++++ 2 files changed, 146 insertions(+), 1 deletion(-) diff --git a/protocol/communities/manager.go b/protocol/communities/manager.go index 15c89562c..2a33fc399 100644 --- a/protocol/communities/manager.go +++ b/protocol/communities/manager.go @@ -1671,7 +1671,7 @@ func (m *Manager) CheckPermissionToJoin(id []byte, addresses []gethcommon.Addres accountsAndChainIDs := combineAddressesAndChainIDs(addresses, allChainIDs) - if len(permissionsToJoin) == 0 { + if len(becomeMemberPermissions) == 0 || len(permissionsToJoin) == 0 { // There are no permissions to join on this community at the moment, // so we reveal all accounts + all chain IDs response := &CheckPermissionsResponse{ @@ -1682,6 +1682,7 @@ func (m *Manager) CheckPermissionToJoin(id []byte, addresses []gethcommon.Addres return response, nil } return m.checkPermissions(permissionsToJoin, accountsAndChainIDs, false) + } func (m *Manager) accountsSatisfyPermissionsToJoin(community *Community, accounts []*protobuf.RevealedAccount) (bool, protobuf.CommunityMember_Roles, error) { diff --git a/protocol/communities_messenger_token_permissions_test.go b/protocol/communities_messenger_token_permissions_test.go index f051f0617..ec1029c21 100644 --- a/protocol/communities_messenger_token_permissions_test.go +++ b/protocol/communities_messenger_token_permissions_test.go @@ -698,6 +698,150 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TestBecomeMemberPermissions( s.Require().Equal(msg.Text, response.Messages()[0].Text) } +func (s *MessengerCommunitiesTokenPermissionsSuite) TestJoinCommunityWithAdminPermission() { + community, _ := s.createCommunity() + + // setup become admin permission + permissionRequest := requests.CreateCommunityTokenPermission{ + CommunityID: community.ID(), + Type: protobuf.CommunityTokenPermission_BECOME_ADMIN, + TokenCriteria: []*protobuf.TokenCriteria{ + &protobuf.TokenCriteria{ + Type: protobuf.CommunityTokenType_ERC20, + ContractAddresses: map[uint64]string{testChainID1: "0x123"}, + Symbol: "TEST", + Amount: "100", + Decimals: uint64(18), + }, + }, + } + + response, err := s.owner.CreateCommunityTokenPermission(&permissionRequest) + s.Require().NoError(err) + s.Require().Len(response.Communities(), 1) + + s.advertiseCommunityTo(community, s.bob) + + // Bob should still be able to join even if there is a permission to be an admin + s.joinCommunity(community, s.bob, bobPassword, []string{}) + + // Verify that we have Bob's revealed account + revealedAccounts, err := s.owner.GetRevealedAccounts(community.ID(), common.PubkeyToHex(&s.bob.identity.PublicKey)) + s.Require().NoError(err) + s.Require().Len(revealedAccounts, 1) + s.Require().Equal(bobAddress, revealedAccounts[0].Address) +} + +func (s *MessengerCommunitiesTokenPermissionsSuite) TestJoinCommunityAsMemberWithMemberAndAdminPermission() { + community, _ := s.createCommunity() + + // setup become member permission + permissionRequestMember := requests.CreateCommunityTokenPermission{ + CommunityID: community.ID(), + Type: protobuf.CommunityTokenPermission_BECOME_MEMBER, + TokenCriteria: []*protobuf.TokenCriteria{ + &protobuf.TokenCriteria{ + Type: protobuf.CommunityTokenType_ERC20, + ContractAddresses: map[uint64]string{testChainID1: "0x123"}, + Symbol: "TEST", + Amount: "100", + Decimals: uint64(18), + }, + }, + } + response, err := s.owner.CreateCommunityTokenPermission(&permissionRequestMember) + s.Require().NoError(err) + s.Require().Len(response.Communities(), 1) + + // setup become admin permission + permissionRequestAdmin := requests.CreateCommunityTokenPermission{ + CommunityID: community.ID(), + Type: protobuf.CommunityTokenPermission_BECOME_ADMIN, + TokenCriteria: []*protobuf.TokenCriteria{ + &protobuf.TokenCriteria{ + Type: protobuf.CommunityTokenType_ERC20, + ContractAddresses: map[uint64]string{testChainID1: "0x124"}, + Symbol: "TESTADMIN", + Amount: "100", + Decimals: uint64(18), + }, + }, + } + response, err = s.owner.CreateCommunityTokenPermission(&permissionRequestAdmin) + s.Require().NoError(err) + s.Require().Len(response.Communities(), 1) + + // make bob satisfy the member criteria + s.makeAddressSatisfyTheCriteria(testChainID1, bobAddress, permissionRequestMember.TokenCriteria[0]) + + s.advertiseCommunityTo(community, s.bob) + + // Bob should still be able to join even though he doesn't satisfy the admin requirement + // because he satisfies the member one + s.joinCommunity(community, s.bob, bobPassword, []string{}) + + // Verify that we have Bob's revealed account + revealedAccounts, err := s.owner.GetRevealedAccounts(community.ID(), common.PubkeyToHex(&s.bob.identity.PublicKey)) + s.Require().NoError(err) + s.Require().Len(revealedAccounts, 1) + s.Require().Equal(bobAddress, revealedAccounts[0].Address) +} + +func (s *MessengerCommunitiesTokenPermissionsSuite) TestJoinCommunityAsAdminWithMemberAndAdminPermission() { + community, _ := s.createCommunity() + + // setup become member permission + permissionRequestMember := requests.CreateCommunityTokenPermission{ + CommunityID: community.ID(), + Type: protobuf.CommunityTokenPermission_BECOME_MEMBER, + TokenCriteria: []*protobuf.TokenCriteria{ + &protobuf.TokenCriteria{ + Type: protobuf.CommunityTokenType_ERC20, + ContractAddresses: map[uint64]string{testChainID1: "0x123"}, + Symbol: "TEST", + Amount: "100", + Decimals: uint64(18), + }, + }, + } + response, err := s.owner.CreateCommunityTokenPermission(&permissionRequestMember) + s.Require().NoError(err) + s.Require().Len(response.Communities(), 1) + + // setup become admin permission + permissionRequestAdmin := requests.CreateCommunityTokenPermission{ + CommunityID: community.ID(), + Type: protobuf.CommunityTokenPermission_BECOME_ADMIN, + TokenCriteria: []*protobuf.TokenCriteria{ + &protobuf.TokenCriteria{ + Type: protobuf.CommunityTokenType_ERC20, + ContractAddresses: map[uint64]string{testChainID1: "0x124"}, + Symbol: "TESTADMIN", + Amount: "100", + Decimals: uint64(18), + }, + }, + } + response, err = s.owner.CreateCommunityTokenPermission(&permissionRequestAdmin) + s.Require().NoError(err) + s.Require().Len(response.Communities(), 1) + + // make bob satisfy the admin criteria + s.makeAddressSatisfyTheCriteria(testChainID1, bobAddress, permissionRequestAdmin.TokenCriteria[0]) + + s.advertiseCommunityTo(community, s.bob) + + // Bob should still be able to join even though he doesn't satisfy the member requirement + // because he satisfies the admin one + s.joinCommunity(community, s.bob, bobPassword, []string{}) + + // Verify that we have Bob's revealed account + revealedAccounts, err := s.owner.GetRevealedAccounts(community.ID(), common.PubkeyToHex(&s.bob.identity.PublicKey)) + s.Require().NoError(err) + s.Require().Len(revealedAccounts, 1) + s.Require().Equal(bobAddress, revealedAccounts[0].Address) +} + func (s *MessengerCommunitiesTokenPermissionsSuite) TestViewChannelPermissions() { community, chat := s.createCommunity()