@@ -3,26 +3,20 @@ package v1beta1
33import (
44 "context"
55
6- "github.com/raystack/frontier/core/serviceuser"
7-
8- "github.com/raystack/frontier/core/authenticate"
9-
10- "go.uber.org/zap"
11-
6+ grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
7+ "github.com/pkg/errors"
128 "github.com/raystack/frontier/core/audit"
13- "github.com/raystack/frontier/core/role"
14- "github.com/raystack/frontier/pkg/pagination"
15- "github.com/raystack/frontier/pkg/utils"
16-
17- "github.com/raystack/frontier/internal/bootstrap/schema"
18-
9+ "github.com/raystack/frontier/core/authenticate"
10+ "github.com/raystack/frontier/core/policy"
1911 "github.com/raystack/frontier/core/project"
20-
21- "github.com/pkg/errors "
12+ "github.com/raystack/frontier/core/role"
13+ "github.com/raystack/frontier/core/serviceuser "
2214 "github.com/raystack/frontier/core/user"
2315 "github.com/raystack/frontier/pkg/metadata"
24-
25- grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
16+ "github.com/raystack/frontier/pkg/pagination"
17+ "github.com/raystack/frontier/pkg/utils"
18+ "github.com/raystck/frontier/internal/bootstrap/schema"
19+ "go.uber.org/zap"
2620
2721 "github.com/raystack/frontier/core/organization"
2822
@@ -248,6 +242,10 @@ func (h Handler) ListOrganizationAdmins(ctx context.Context, request *frontierv1
248242}
249243
250244func (h Handler ) ListOrganizationUsers (ctx context.Context , request * frontierv1beta1.ListOrganizationUsersRequest ) (* frontierv1beta1.ListOrganizationUsersResponse , error ) {
245+ if len (request .GetRoleFilters ()) > 0 && request .GetWithRoles () {
246+ return nil , status .Errorf (codes .InvalidArgument , "cannot use role filters and with_roles together" )
247+ }
248+
251249 logger := grpczap .Extract (ctx )
252250 orgResp , err := h .orgService .Get (ctx , request .GetId ())
253251 if err != nil {
@@ -261,9 +259,67 @@ func (h Handler) ListOrganizationUsers(ctx context.Context, request *frontierv1b
261259 }
262260 }
263261
264- users , err := h .userService .ListByOrg (ctx , orgResp .ID , request .GetPermissionFilter ())
265- if err != nil {
266- return nil , err
262+ var users []user.User
263+ var rolePairPBs []* frontierv1beta1.ListOrganizationUsersResponse_RolePair
264+
265+ if len (request .GetRoleFilters ()) > 0 {
266+ // convert role names to ids if needed
267+ roleIDs := request .GetRoleFilters ()
268+ for i , roleFilter := range request .GetRoleFilters () {
269+ if ! utils .IsValidUUID (roleFilter ) {
270+ role , err := h .roleService .Get (ctx , roleFilter )
271+ if err != nil {
272+ return nil , err
273+ }
274+ roleIDs [i ] = role .ID
275+ }
276+ }
277+
278+ // need to fetch users with roles assigned to them
279+ policies , err := h .policyService .List (ctx , policy.Filter {
280+ OrgID : request .GetId (),
281+ PrincipalType : schema .UserPrincipal ,
282+ ResourceType : schema .OrganizationNamespace ,
283+ RoleIDs : roleIDs ,
284+ })
285+ if err != nil {
286+ return nil , err
287+ }
288+ users = utils .Filter (utils .Map (policies , func (pol policy.Policy ) user.User {
289+ u , _ := h .userService .GetByID (ctx , pol .PrincipalID )
290+ return u
291+ }), func (u user.User ) bool {
292+ return u .ID != ""
293+ })
294+ } else {
295+ // list all users
296+ users , err = h .userService .ListByOrg (ctx , orgResp .ID , request .GetPermissionFilter ())
297+ if err != nil {
298+ return nil , err
299+ }
300+ if request .GetWithRoles () {
301+ for _ , user := range users {
302+ roles , err := h .policyService .ListRoles (ctx , schema .UserPrincipal , user .ID , schema .OrganizationNamespace , request .GetId ())
303+ if err != nil {
304+ return nil , err
305+ }
306+
307+ rolesPb := utils .Filter (utils .Map (roles , func (role role.Role ) * frontierv1beta1.Role {
308+ pb , err := transformRoleToPB (role )
309+ if err != nil {
310+ logger .Error ("failed to transform role for group" , zap .Error (err ))
311+ return nil
312+ }
313+ return & pb
314+ }), func (role * frontierv1beta1.Role ) bool {
315+ return role != nil
316+ })
317+ rolePairPBs = append (rolePairPBs , & frontierv1beta1.ListOrganizationUsersResponse_RolePair {
318+ UserId : user .ID ,
319+ Roles : rolesPb ,
320+ })
321+ }
322+ }
267323 }
268324
269325 var usersPB []* frontierv1beta1.User
@@ -272,35 +328,8 @@ func (h Handler) ListOrganizationUsers(ctx context.Context, request *frontierv1b
272328 if err != nil {
273329 return nil , err
274330 }
275-
276331 usersPB = append (usersPB , u )
277332 }
278-
279- var rolePairPBs []* frontierv1beta1.ListOrganizationUsersResponse_RolePair
280- if request .GetWithRoles () {
281- for _ , user := range users {
282- roles , err := h .policyService .ListRoles (ctx , schema .UserPrincipal , user .ID , schema .OrganizationNamespace , request .GetId ())
283- if err != nil {
284- return nil , err
285- }
286-
287- rolesPb := utils .Filter (utils .Map (roles , func (role role.Role ) * frontierv1beta1.Role {
288- pb , err := transformRoleToPB (role )
289- if err != nil {
290- logger .Error ("failed to transform role for group" , zap .Error (err ))
291- return nil
292- }
293- return & pb
294- }), func (role * frontierv1beta1.Role ) bool {
295- return role != nil
296- })
297- rolePairPBs = append (rolePairPBs , & frontierv1beta1.ListOrganizationUsersResponse_RolePair {
298- UserId : user .ID ,
299- Roles : rolesPb ,
300- })
301- }
302- }
303-
304333 return & frontierv1beta1.ListOrganizationUsersResponse {
305334 Users : usersPB ,
306335 RolePairs : rolePairPBs ,
0 commit comments