Perform token criteria checks on existing members

When a community permission is edited, we need to revalidate
the token criteria with the existing member list, as members might
no longer fulfill the requirements.

This commit runs the checks in a go routine after the permission has
been updated.
This commit is contained in:
Pascal Precht 2023-03-20 11:36:32 +01:00 committed by Follow the white rabbit
parent 2cbced95c5
commit 128f82df09
2 changed files with 50 additions and 0 deletions

View File

@ -369,6 +369,15 @@ func (o *Community) Color() string {
return ""
}
func (o *Community) Members() map[string]*protobuf.CommunityMember {
if o != nil &&
o.config != nil &&
o.config.CommunityDescription != nil {
return o.config.CommunityDescription.Members
}
return nil
}
func (o *Community) MembersCount() int {
if o != nil &&
o.config != nil &&

View File

@ -532,6 +532,47 @@ func (m *Manager) EditCommunityTokenPermission(request *requests.EditCommunityTo
m.publish(&Subscription{Community: community})
// check if members still fulfill the token criteria of all
// BECOME_MEMBER permissions and kick them if necessary
//
// We do this in a separate routine to not block
// this function
go func() {
if tokenPermission.Type == protobuf.CommunityTokenPermission_BECOME_MEMBER {
becomeMemberPermissions := community.TokenPermissionsByType(protobuf.CommunityTokenPermission_BECOME_MEMBER)
for memberKey, member := range community.Members() {
if memberKey == common.PubkeyToHex(&m.identity.PublicKey) {
continue
}
walletAddresses := make([]gethcommon.Address, 0)
for _, walletAddress := range member.WalletAccounts {
walletAddresses = append(walletAddresses, gethcommon.HexToAddress(walletAddress))
}
hasPermission, err := m.checkPermissionToJoin(becomeMemberPermissions, walletAddresses)
if err != nil {
m.logger.Debug("failed to check permission to join", zap.Error(err))
continue
}
if !hasPermission {
pk, err := common.HexToPubkey(memberKey)
if err != nil {
m.logger.Debug("failed to convert hex key to pubkey", zap.Error(err))
continue
}
_, err = community.RemoveUserFromOrg(pk)
if err != nil {
m.logger.Debug("failed to remove member from community", zap.Error(err))
}
}
}
m.publish(&Subscription{Community: community})
}
}()
return community, changes, nil
}