Skip to content

Commit 0aa8043

Browse files
committed
catalog: extract new user validator, #TASK-7192
1 parent a7b3ebf commit 0aa8043

File tree

1 file changed

+39
-36
lines changed
  • opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers

1 file changed

+39
-36
lines changed

opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/UserManager.java

+39-36
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,46 @@ public OpenCGAResult<User> create(User user, String password, String token) thro
128128

129129
Organization organization = getOrganizationDBAdaptor(organizationId).get(OrganizationManager.INCLUDE_ORGANIZATION_CONFIGURATION)
130130
.first();
131-
131+
validateNewUser(user, password, organization.getConfiguration().getDefaultUserExpirationDate(), organizationId);
132132
ObjectMap auditParams = new ObjectMap("user", user);
133133

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 {
134171
// Initialise fields
135172
ParamUtils.checkObj(user, "User");
136173
ParamUtils.checkValidUserId(user.getId());
@@ -157,7 +194,7 @@ public OpenCGAResult<User> create(User user, String password, String token) thro
157194
Account account = user.getInternal().getAccount();
158195
account.setPassword(ParamUtils.defaultObject(account.getPassword(), Password::new));
159196
if (StringUtils.isEmpty(account.getExpirationDate())) {
160-
account.setExpirationDate(organization.getConfiguration().getDefaultUserExpirationDate());
197+
account.setExpirationDate(defaultUserExpirationDate);
161198
} else {
162199
// Validate expiration date is not over
163200
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
188225
Date date = TimeUtils.addDaysToCurrentDate(configuration.getAccount().getPasswordExpirationDays());
189226
account.getPassword().setExpirationDate(TimeUtils.getTime(date));
190227
}
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-
}
225228
}
226229

227230
/**

0 commit comments

Comments
 (0)