Display peers of a user that it has access to (#571)

If a user has a non-admin role, display all peers
that user's peers have access to when calling
/peers endpoint of the HTTP API.
This commit is contained in:
Misha Bragin 2022-11-21 17:45:14 +01:00 committed by GitHub
parent e965d6c022
commit 8b0a1bbae0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -106,13 +106,29 @@ func (am *DefaultAccountManager) GetPeers(accountID, userID string) ([]*Peer, er
return nil, err
}
peers := make([]*Peer, 0, len(account.Peers))
peers := make([]*Peer, 0)
peersMap := make(map[string]*Peer)
for _, peer := range account.Peers {
if !user.IsAdmin() && user.Id != peer.UserID {
// only display peers that belong to the current user if the current user is not an admin
continue
}
peers = append(peers, peer.Copy())
p := peer.Copy()
peers = append(peers, p)
peersMap[peer.Key] = p
}
// fetch all the peers that have access to the user's peers
for _, peer := range peers {
aclPeers := am.getPeersByACL(account, peer.Key)
for _, p := range aclPeers {
peersMap[p.Key] = p
}
}
peers = make([]*Peer, 0, len(peersMap))
for _, peer := range peersMap {
peers = append(peers, peer)
}
return peers, nil