( ctx context.Context, entityID *ttnpb.EntityIdentifiers, )
| 39 | } |
| 40 | |
| 41 | func (is *IdentityServer) getRights( |
| 42 | ctx context.Context, entityID *ttnpb.EntityIdentifiers, |
| 43 | ) (entityRights, universalRights *ttnpb.Rights, err error) { |
| 44 | authInfo, err := is.authInfo(ctx) |
| 45 | if err != nil { |
| 46 | return nil, nil, err |
| 47 | } |
| 48 | |
| 49 | authInfoRights := ttnpb.RightsFrom(authInfo.GetRights()...) |
| 50 | universalRights = allPotentialRights(entityID, authInfo.GetUniversalRights()) |
| 51 | if len(universalRights.GetRights()) == 0 { |
| 52 | universalRights = nil |
| 53 | } |
| 54 | allPotentialRights := allPotentialRights(entityID, authInfoRights) |
| 55 | |
| 56 | // If the rights of the auth do not contain any rights for the entity type, |
| 57 | // there's nothing more to do. |
| 58 | if len(allPotentialRights.GetRights()) == 0 { |
| 59 | return nil, universalRights, nil |
| 60 | } |
| 61 | |
| 62 | // If the caller is the requested entity, |
| 63 | // we can directly return the rights of the auth. |
| 64 | authenticatedAs := authInfo.GetEntityIdentifiers() |
| 65 | if entityID.EntityType() == authenticatedAs.EntityType() && |
| 66 | entityID.IDString() == authenticatedAs.IDString() { |
| 67 | return authInfoRights, universalRights, nil |
| 68 | } |
| 69 | |
| 70 | // If the caller is not an organization or user, there's nothing more to do. |
| 71 | ouID := authInfo.GetOrganizationOrUserIdentifiers() |
| 72 | if ouID == nil { |
| 73 | return nil, universalRights, nil |
| 74 | } |
| 75 | |
| 76 | // If the caller is requesting a user, and they're not that user (see above), |
| 77 | // they don't have rights on it, so nothing more to do. |
| 78 | if entityID.GetUserIds() != nil { |
| 79 | return nil, universalRights, nil |
| 80 | } |
| 81 | |
| 82 | err = is.store.Transact(ctx, func(ctx context.Context, st store.Store) error { |
| 83 | membershipChains, err := st.FindAccountMembershipChains(ctx, ouID, entityID.EntityType(), entityID.IDString()) |
| 84 | if err != nil { |
| 85 | return err |
| 86 | } |
| 87 | for _, chain := range membershipChains { |
| 88 | entityRights = entityRights.Union(chain.GetRights()) |
| 89 | } |
| 90 | return nil |
| 91 | }) |
| 92 | if err != nil { |
| 93 | return nil, nil, err |
| 94 | } |
| 95 | |
| 96 | entityRights = entityRights.Intersect(authInfoRights) |
| 97 | |
| 98 | return entityRights, universalRights, err |
no test coverage detected