@@ -128,9 +128,46 @@ public OpenCGAResult<User> create(User user, String password, String token) thro
128
128
129
129
Organization organization = getOrganizationDBAdaptor (organizationId ).get (OrganizationManager .INCLUDE_ORGANIZATION_CONFIGURATION )
130
130
.first ();
131
-
131
+ validateNewUser ( user , password , organization . getConfiguration (). getDefaultUserExpirationDate (), organizationId );
132
132
ObjectMap auditParams = new ObjectMap ("user" , user );
133
133
134
+ if (!ParamConstants .ADMIN_ORGANIZATION .equals (organizationId ) || !OPENCGA .equals (user .getId ())) {
135
+ JwtPayload jwtPayload = validateToken (token );
136
+ // If it's not one of the SUPERADMIN users or the owner or one of the admins of the organisation, we should not allow it
137
+ if (!authorizationManager .isAtLeastOrganizationOwnerOrAdmin (organizationId , jwtPayload .getUserId (organizationId ))) {
138
+ String errorMsg = "Please ask your administrator to create your account." ;
139
+ auditManager .auditCreate (organizationId , user .getId (), Enums .Resource .USER , user .getId (), "" , "" , "" , auditParams ,
140
+ new AuditRecord .Status (AuditRecord .Status .Result .ERROR , new Error (0 , "" , errorMsg )));
141
+ throw new CatalogException (errorMsg );
142
+ }
143
+ }
144
+
145
+ checkUserExists (organizationId , user .getId ());
146
+
147
+ try {
148
+ if (StringUtils .isNotEmpty (password ) && !PasswordUtils .isStrongPassword (password )) {
149
+ throw new CatalogException ("Invalid password. " + PasswordUtils .PASSWORD_REQUIREMENT );
150
+ }
151
+ if (user .getProjects () != null && !user .getProjects ().isEmpty ()) {
152
+ throw new CatalogException ("Creating user and projects in a single transaction is forbidden" );
153
+ }
154
+
155
+ getUserDBAdaptor (organizationId ).insert (user , password , QueryOptions .empty ());
156
+
157
+ auditManager .auditCreate (organizationId , user .getId (), Enums .Resource .USER , user .getId (), "" , "" , "" , auditParams ,
158
+ new AuditRecord .Status (AuditRecord .Status .Result .SUCCESS ));
159
+
160
+ return getUserDBAdaptor (organizationId ).get (user .getId (), QueryOptions .empty ());
161
+ } catch (CatalogIOException | CatalogDBException e ) {
162
+ auditManager .auditCreate (organizationId , user .getId (), Enums .Resource .USER , user .getId (), "" , "" , "" , auditParams ,
163
+ new AuditRecord .Status (AuditRecord .Status .Result .ERROR , e .getError ()));
164
+
165
+ throw e ;
166
+ }
167
+ }
168
+
169
+ public void validateNewUser (User user , String password , String defaultUserExpirationDate , String organizationId )
170
+ throws CatalogException {
134
171
// Initialise fields
135
172
ParamUtils .checkObj (user , "User" );
136
173
ParamUtils .checkValidUserId (user .getId ());
@@ -157,7 +194,7 @@ public OpenCGAResult<User> create(User user, String password, String token) thro
157
194
Account account = user .getInternal ().getAccount ();
158
195
account .setPassword (ParamUtils .defaultObject (account .getPassword (), Password ::new ));
159
196
if (StringUtils .isEmpty (account .getExpirationDate ())) {
160
- account .setExpirationDate (organization . getConfiguration (). getDefaultUserExpirationDate () );
197
+ account .setExpirationDate (defaultUserExpirationDate );
161
198
} else {
162
199
// Validate expiration date is not over
163
200
ParamUtils .checkDateIsNotExpired (account .getExpirationDate (), UserDBAdaptor .QueryParams .INTERNAL_ACCOUNT_EXPIRATION_DATE .key ());
@@ -188,40 +225,6 @@ public OpenCGAResult<User> create(User user, String password, String token) thro
188
225
Date date = TimeUtils .addDaysToCurrentDate (configuration .getAccount ().getPasswordExpirationDays ());
189
226
account .getPassword ().setExpirationDate (TimeUtils .getTime (date ));
190
227
}
191
-
192
- if (!ParamConstants .ADMIN_ORGANIZATION .equals (organizationId ) || !OPENCGA .equals (user .getId ())) {
193
- JwtPayload jwtPayload = validateToken (token );
194
- // If it's not one of the SUPERADMIN users or the owner or one of the admins of the organisation, we should not allow it
195
- if (!authorizationManager .isAtLeastOrganizationOwnerOrAdmin (organizationId , jwtPayload .getUserId (organizationId ))) {
196
- String errorMsg = "Please ask your administrator to create your account." ;
197
- auditManager .auditCreate (organizationId , user .getId (), Enums .Resource .USER , user .getId (), "" , "" , "" , auditParams ,
198
- new AuditRecord .Status (AuditRecord .Status .Result .ERROR , new Error (0 , "" , errorMsg )));
199
- throw new CatalogException (errorMsg );
200
- }
201
- }
202
-
203
- checkUserExists (organizationId , user .getId ());
204
-
205
- try {
206
- if (StringUtils .isNotEmpty (password ) && !PasswordUtils .isStrongPassword (password )) {
207
- throw new CatalogException ("Invalid password. " + PasswordUtils .PASSWORD_REQUIREMENT );
208
- }
209
- if (user .getProjects () != null && !user .getProjects ().isEmpty ()) {
210
- throw new CatalogException ("Creating user and projects in a single transaction is forbidden" );
211
- }
212
-
213
- getUserDBAdaptor (organizationId ).insert (user , password , QueryOptions .empty ());
214
-
215
- auditManager .auditCreate (organizationId , user .getId (), Enums .Resource .USER , user .getId (), "" , "" , "" , auditParams ,
216
- new AuditRecord .Status (AuditRecord .Status .Result .SUCCESS ));
217
-
218
- return getUserDBAdaptor (organizationId ).get (user .getId (), QueryOptions .empty ());
219
- } catch (CatalogIOException | CatalogDBException e ) {
220
- auditManager .auditCreate (organizationId , user .getId (), Enums .Resource .USER , user .getId (), "" , "" , "" , auditParams ,
221
- new AuditRecord .Status (AuditRecord .Status .Result .ERROR , e .getError ()));
222
-
223
- throw e ;
224
- }
225
228
}
226
229
227
230
/**
0 commit comments