22
33import static com .appirio .tech .core .service .identity .util .Constants .*;
44import static javax .servlet .http .HttpServletResponse .*;
5+
6+ import com .appirio .tech .core .service .identity .util .m2mscope .UserProfilesFactory ;
57import io .dropwizard .auth .Auth ;
68import io .dropwizard .jersey .PATCH ;
79
@@ -91,26 +93,6 @@ public class UserResource implements GetResource<User>, DDLResource<User> {
9193 // TODO: switch to slf4j directly (this delegates to it) - it's more efficient
9294 private static final Logger logger = Logger .getLogger (UserResource .class );
9395
94- /**
95- * Represents the create scopes for machine token validation.
96- */
97- public static final String [] ReadScopes = {"read:user_profiles" , "all:user_profiles" };
98-
99- /**
100- * Represents the create scopes for machine token validation.
101- */
102- public static final String [] CreateScopes = {"create:user_profiles" , "all:user_profiles" };
103-
104- /**
105- * Represents the delete scopes for machine token validation.
106- */
107- public static final String [] DeleteScopes = {"delete:user_profiles" , "all:user_profiles" };
108-
109- /**
110- * Represents the update scopes for machine token validation.
111- */
112- public static final String [] UpdateScopes = {"update:user_profiles" , "all:user_profiles" };
113-
11496 private int resetTokenExpirySeconds = 30 * 60 ; //30min
11597
11698 private int resendActivationCodeExpirySeconds = 30 * 60 ; //30min
@@ -139,6 +121,8 @@ public class UserResource implements GetResource<User>, DDLResource<User> {
139121 * The event bus service client field used to send the event
140122 */
141123 private final EventBusServiceClient eventBusServiceClient ;
124+
125+ private final UserProfilesFactory userProfilesFactory ;
142126
143127 /**
144128 * Create UserResource
@@ -148,18 +132,43 @@ public class UserResource implements GetResource<User>, DDLResource<User> {
148132 * @param cacheService the cacheService to use
149133 * @param eventProducer the eventProducer to use
150134 * @param eventBusServiceClient the eventBusServiceClient to use
135+ * @param userProfilesFactory the user profiles scopes configuration.
151136 */
152137 public UserResource (
153138 UserDAO userDao ,
154139 RoleDAO roleDao ,
155140 CacheService cacheService ,
156141 EventProducer eventProducer ,
157- EventBusServiceClient eventBusServiceClient ) {
142+ EventBusServiceClient eventBusServiceClient , UserProfilesFactory userProfilesFactory ) {
158143 this .userDao = userDao ;
159144 this .roleDao = roleDao ;
160145 this .cacheService = cacheService ;
161146 this .eventProducer = eventProducer ;
162147 this .eventBusServiceClient = eventBusServiceClient ;
148+ if (userProfilesFactory == null ) {
149+ // create a default one
150+ this .userProfilesFactory = new UserProfilesFactory ();
151+ } else {
152+ this .userProfilesFactory = userProfilesFactory ;
153+ }
154+ }
155+
156+ /**
157+ * Create UserResource
158+ *
159+ * @param userDao the userDao to use
160+ * @param roleDao the roleDao to use
161+ * @param cacheService the cacheService to use
162+ * @param eventProducer the eventProducer to use
163+ * @param eventBusServiceClient the eventBusServiceClient to use
164+ */
165+ public UserResource (
166+ UserDAO userDao ,
167+ RoleDAO roleDao ,
168+ CacheService cacheService ,
169+ EventProducer eventProducer ,
170+ EventBusServiceClient eventBusServiceClient ) {
171+ this (userDao , roleDao , cacheService , eventProducer , eventBusServiceClient , null );
163172 }
164173
165174 protected void setObjectMapper (ObjectMapper objectMapper ) {
@@ -205,7 +214,7 @@ public ApiResponse createSSOUserLogin(@Auth AuthUser authUser,
205214 @ Valid PostPutRequest <UserProfile > postRequest ) {
206215 UserProfile profile = postRequest .getParam ();
207216
208- checkAccessAndUserProfile (authUser , userId , profile , CreateScopes );
217+ checkAccessAndUserProfile (authUser , userId , profile , userProfilesFactory . getCreateScopes () );
209218
210219 try {
211220 SSOUserDAO ssoUserDao = this .userDao .createSSOUserDAO ();
@@ -246,7 +255,7 @@ public ApiResponse updateSSOUserLogin(@Auth AuthUser authUser,
246255 @ PathParam ("userId" ) long userId ,
247256 @ Valid PostPutRequest <UserProfile > postRequest ) {
248257 UserProfile profile = postRequest .getParam ();
249- checkAccessAndUserProfile (authUser , userId , profile , UpdateScopes );
258+ checkAccessAndUserProfile (authUser , userId , profile , userProfilesFactory . getUpdateScopes () );
250259
251260 try {
252261 SSOUserDAO ssoUserDao = this .userDao .createSSOUserDAO ();
@@ -283,7 +292,7 @@ public ApiResponse updateSSOUserLogin(@Auth AuthUser authUser,
283292 @ Path ("/{userId}/SSOUserLogin" )
284293 public ApiResponse deleteSSOUserLogin (@ Auth AuthUser authUser ,
285294 @ PathParam ("userId" ) long userId , @ QueryParam ("provider" ) String provider , @ QueryParam ("providerId" ) Long providerId ) {
286- Utils .checkAccess (authUser , DeleteScopes , Utils .AdminRoles );
295+ Utils .checkAccess (authUser , userProfilesFactory . getDeleteScopes () , Utils .AdminRoles );
287296 if (userId <= 0 ) {
288297 throw new APIRuntimeException (SC_BAD_REQUEST , "userId should be positive:" + userId );
289298 }
@@ -339,7 +348,7 @@ public ApiResponse deleteSSOUserLogin(@Auth AuthUser authUser,
339348 @ Path ("/{userId}/SSOUserLogins" )
340349 public ApiResponse getSSOUserLoginsByUserId (@ Auth AuthUser authUser ,
341350 @ PathParam ("userId" ) long userId ) {
342- Utils .checkAccess (authUser , ReadScopes , Utils .AdminRoles );
351+ Utils .checkAccess (authUser , userProfilesFactory . getReadScopes () , Utils .AdminRoles );
343352 if (userId <= 0 ) {
344353 throw new APIRuntimeException (SC_BAD_REQUEST , "userId should be positive:" + userId );
345354 }
@@ -365,7 +374,7 @@ public ApiResponse getObjects(
365374 @ APIQueryParam (repClass = User .class ) QueryParameter query ,
366375 @ Context HttpServletRequest request ) {
367376 logger .info ("getObjects" );
368- Utils .checkAccess (authUser , ReadScopes , Utils .AdminRoles );
377+ Utils .checkAccess (authUser , userProfilesFactory . getReadScopes () , Utils .AdminRoles );
369378
370379 try {
371380 List <User > users = userDao .findUsers (
@@ -394,7 +403,7 @@ public ApiResponse getObject(
394403 @ PathParam ("resourceId" ) TCID resourceId ,
395404 @ APIFieldParam (repClass = User .class ) FieldSelector selector ,
396405 @ Context HttpServletRequest request ) throws Exception {
397- validateResourceIdAndCheckPermission (authUser , resourceId , ReadScopes );
406+ validateResourceIdAndCheckPermission (authUser , resourceId , userProfilesFactory . getReadScopes () );
398407
399408 User user = this .userDao .populateById (selector , resourceId );
400409 if (user == null ) {
@@ -508,7 +517,7 @@ public ApiResponse updateObject(
508517
509518 TCID id = new TCID (resourceId );
510519
511- validateResourceIdAndCheckPermission (authUser , id , UpdateScopes );
520+ validateResourceIdAndCheckPermission (authUser , id , userProfilesFactory . getUpdateScopes () );
512521 // checking param
513522 checkParam (patchRequest );
514523
@@ -603,7 +612,7 @@ public ApiResponse createUserProfile(
603612 logger .info (String .format ("createUserProfile(%s)" , resourceId ));
604613
605614 TCID id = new TCID (resourceId );
606- validateResourceIdAndCheckPermission (authUser , id , CreateScopes );
615+ validateResourceIdAndCheckPermission (authUser , id , userProfilesFactory . getCreateScopes () );
607616 // checking param
608617 checkParam (postRequest );
609618
@@ -678,7 +687,7 @@ public ApiResponse deleteUserProfile(
678687 throw new APIRuntimeException (SC_BAD_REQUEST , String .format (Constants .MSG_TEMPLATE_MANDATORY , "provider" ));
679688
680689 TCID id = new TCID (resourceId );
681- validateResourceIdAndCheckPermission (authUser , id , DeleteScopes );
690+ validateResourceIdAndCheckPermission (authUser , id , userProfilesFactory . getDeleteScopes () );
682691
683692 ProviderType providerType = ProviderType .getByName (provider );
684693 if (providerType ==null )
@@ -842,7 +851,7 @@ public ApiResponse updateHandle(
842851 logger .info (String .format ("updateHandle(%s)" , resourceId ));
843852
844853 TCID id = new TCID (resourceId );
845- validateResourceIdAndCheckPermission (authUser , id , UpdateScopes );
854+ validateResourceIdAndCheckPermission (authUser , id , userProfilesFactory . getUpdateScopes () );
846855 // checking param
847856 checkParam (patchRequest );
848857
@@ -888,7 +897,7 @@ public ApiResponse updatePrimaryEmail(
888897 logger .info (String .format ("updatePrimaryEmail(%s)" , resourceId ));
889898
890899 TCID id = new TCID (resourceId );
891- validateResourceIdAndCheckPermission (authUser , id , UpdateScopes );
900+ validateResourceIdAndCheckPermission (authUser , id , userProfilesFactory . getUpdateScopes () );
892901 // checking param
893902 checkParam (patchRequest );
894903
@@ -993,7 +1002,7 @@ public ApiResponse updateStatus(
9931002 logger .info (String .format ("updateStatus(%s, %s)" , resourceId , comment ));
9941003
9951004 TCID id = new TCID (resourceId );
996- validateResourceIdAndCheckPermission (authUser , id , UpdateScopes );
1005+ validateResourceIdAndCheckPermission (authUser , id , userProfilesFactory . getUpdateScopes () );
9971006 // checking param
9981007 checkParam (patchRequest );
9991008
@@ -1165,7 +1174,7 @@ public ApiResponse getAchievements(
11651174
11661175 logger .info (String .format ("getAchievements(%s)" , resourceId ));
11671176
1168- validateResourceIdAndCheckPermission (authUser , resourceId , ReadScopes );
1177+ validateResourceIdAndCheckPermission (authUser , resourceId , userProfilesFactory . getReadScopes () );
11691178
11701179 Long userId = Utils .toLongValue (resourceId );
11711180 logger .debug (String .format ("findUserById(%s)" , userId ));
0 commit comments