fix: only save/send request to join when the permissions are satisfied (#3748)

This commit is contained in:
Jonathan Rainville 2023-07-14 13:06:37 -04:00 committed by GitHub
parent 64f7706567
commit 11a3f495bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 13 deletions

View File

@ -2890,7 +2890,17 @@ func (m *Manager) GetByIDString(idString string) (*Community, error) {
return m.GetByID(id)
}
func (m *Manager) RequestToJoin(requester *ecdsa.PublicKey, request *requests.RequestToJoinCommunity) (*Community, *RequestToJoin, error) {
func (m *Manager) SaveRequestToJoinAndCommunity(requestToJoin *RequestToJoin, community *Community) (*Community, *RequestToJoin, error) {
if err := m.persistence.SaveRequestToJoin(requestToJoin); err != nil {
return nil, nil, err
}
community.config.RequestedToJoinAt = uint64(time.Now().Unix())
community.AddRequestToJoin(requestToJoin)
return community, requestToJoin, nil
}
func (m *Manager) CreateRequestToJoin(requester *ecdsa.PublicKey, request *requests.RequestToJoinCommunity) (*Community, *RequestToJoin, error) {
community, err := m.persistence.GetByID(&m.identity.PublicKey, request.CommunityID)
if err != nil {
return nil, nil, err
@ -2914,12 +2924,6 @@ func (m *Manager) RequestToJoin(requester *ecdsa.PublicKey, request *requests.Re
requestToJoin.CalculateID()
if err := m.persistence.SaveRequestToJoin(requestToJoin); err != nil {
return nil, nil, err
}
community.config.RequestedToJoinAt = uint64(time.Now().Unix())
community.AddRequestToJoin(requestToJoin)
return community, requestToJoin, nil
}

View File

@ -3123,7 +3123,7 @@ func (s *MessengerCommunitiesSuite) TestCommunityBanUserRequesToJoin() {
request := &requests.RequestToJoinCommunity{CommunityID: community.ID()}
// We try to join the org
_, rtj, err := s.alice.communitiesManager.RequestToJoin(&s.alice.identity.PublicKey, request)
_, rtj, err := s.alice.communitiesManager.CreateRequestToJoin(&s.alice.identity.PublicKey, request)
s.Require().NoError(err)

View File

@ -578,6 +578,15 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TestBecomeMemberPermissions(
err = <-waitOnCommunityEncryptionErrCh
s.Require().NoError(err)
_, err = WaitOnMessengerResponse(
s.bob,
func(r *MessengerResponse) bool {
return len(r.Communities()) > 0
},
"no community",
)
s.Require().NoError(err)
// bob should be kicked from the community,
// because he doesn't meet the criteria
community, err = s.owner.communitiesManager.GetByID(community.ID())
@ -603,6 +612,18 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TestBecomeMemberPermissions(
s.Require().Error(err)
s.Require().ErrorContains(err, "no messages")
// bob tries to join, but he doesn't satisfy so the request isn't sent
passwdHash := types.EncodeHex(crypto.Keccak256([]byte(bobPassword)))
request := &requests.RequestToJoinCommunity{CommunityID: community.ID(), Password: passwdHash, AddressesToReveal: []string{bobAddress}}
_, err = s.bob.RequestToJoinCommunity(request)
s.Require().Error(err)
s.Require().ErrorContains(err, "permission to join not satisfied")
// make sure bob does not have a pending request to join
requests, err := s.bob.MyPendingRequestsToJoin()
s.Require().NoError(err)
s.Require().Len(requests, 0)
// make bob satisfy the criteria
s.makeAddressSatisfyTheCriteria(testChainID1, bobAddress, permissionRequest.TokenCriteria[0])

View File

@ -743,11 +743,7 @@ func (m *Messenger) RequestToJoinCommunity(request *requests.RequestToJoinCommun
return nil, err
}
community, requestToJoin, err := m.communitiesManager.RequestToJoin(&m.identity.PublicKey, request)
if err != nil {
return nil, err
}
err = m.syncCommunity(context.Background(), community, m.dispatchMessage)
community, requestToJoin, err := m.communitiesManager.CreateRequestToJoin(&m.identity.PublicKey, request)
if err != nil {
return nil, err
}
@ -777,6 +773,9 @@ func (m *Messenger) RequestToJoinCommunity(request *requests.RequestToJoinCommun
if err != nil {
return nil, err
}
if !response.Satisfied {
return nil, errors.New("permission to join not satisfied")
}
for _, accountAndChainIDs := range response.ValidCombinations {
revealedAccounts[accountAndChainIDs.Address].ChainIds = accountAndChainIDs.ChainIDs
@ -787,6 +786,15 @@ func (m *Messenger) RequestToJoinCommunity(request *requests.RequestToJoinCommun
}
}
community, _, err = m.communitiesManager.SaveRequestToJoinAndCommunity(requestToJoin, community)
if err != nil {
return nil, err
}
err = m.syncCommunity(context.Background(), community, m.dispatchMessage)
if err != nil {
return nil, err
}
payload, err := proto.Marshal(requestToJoinProto)
if err != nil {
return nil, err