fix: only save/send request to join when the permissions are satisfied (#3748)
This commit is contained in:
parent
64f7706567
commit
11a3f495bd
|
@ -2890,7 +2890,17 @@ func (m *Manager) GetByIDString(idString string) (*Community, error) {
|
||||||
return m.GetByID(id)
|
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)
|
community, err := m.persistence.GetByID(&m.identity.PublicKey, request.CommunityID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
@ -2914,12 +2924,6 @@ func (m *Manager) RequestToJoin(requester *ecdsa.PublicKey, request *requests.Re
|
||||||
|
|
||||||
requestToJoin.CalculateID()
|
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
|
return community, requestToJoin, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3123,7 +3123,7 @@ func (s *MessengerCommunitiesSuite) TestCommunityBanUserRequesToJoin() {
|
||||||
|
|
||||||
request := &requests.RequestToJoinCommunity{CommunityID: community.ID()}
|
request := &requests.RequestToJoinCommunity{CommunityID: community.ID()}
|
||||||
// We try to join the org
|
// 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)
|
s.Require().NoError(err)
|
||||||
|
|
||||||
|
|
|
@ -578,6 +578,15 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TestBecomeMemberPermissions(
|
||||||
err = <-waitOnCommunityEncryptionErrCh
|
err = <-waitOnCommunityEncryptionErrCh
|
||||||
s.Require().NoError(err)
|
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,
|
// bob should be kicked from the community,
|
||||||
// because he doesn't meet the criteria
|
// because he doesn't meet the criteria
|
||||||
community, err = s.owner.communitiesManager.GetByID(community.ID())
|
community, err = s.owner.communitiesManager.GetByID(community.ID())
|
||||||
|
@ -603,6 +612,18 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TestBecomeMemberPermissions(
|
||||||
s.Require().Error(err)
|
s.Require().Error(err)
|
||||||
s.Require().ErrorContains(err, "no messages")
|
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
|
// make bob satisfy the criteria
|
||||||
s.makeAddressSatisfyTheCriteria(testChainID1, bobAddress, permissionRequest.TokenCriteria[0])
|
s.makeAddressSatisfyTheCriteria(testChainID1, bobAddress, permissionRequest.TokenCriteria[0])
|
||||||
|
|
||||||
|
|
|
@ -743,11 +743,7 @@ func (m *Messenger) RequestToJoinCommunity(request *requests.RequestToJoinCommun
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
community, requestToJoin, err := m.communitiesManager.RequestToJoin(&m.identity.PublicKey, request)
|
community, requestToJoin, err := m.communitiesManager.CreateRequestToJoin(&m.identity.PublicKey, request)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
err = m.syncCommunity(context.Background(), community, m.dispatchMessage)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -777,6 +773,9 @@ func (m *Messenger) RequestToJoinCommunity(request *requests.RequestToJoinCommun
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if !response.Satisfied {
|
||||||
|
return nil, errors.New("permission to join not satisfied")
|
||||||
|
}
|
||||||
|
|
||||||
for _, accountAndChainIDs := range response.ValidCombinations {
|
for _, accountAndChainIDs := range response.ValidCombinations {
|
||||||
revealedAccounts[accountAndChainIDs.Address].ChainIds = accountAndChainIDs.ChainIDs
|
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)
|
payload, err := proto.Marshal(requestToJoinProto)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in New Issue