in src/main/java/com/googlesource/gerrit/plugins/serviceuser/CreateServiceUser.java [119:196]
public Response<ServiceUserInfo> apply(
ConfigResource parentResource, IdString id, CreateServiceUser.Input input)
throws RestApiException, IOException, ConfigInvalidException, PermissionBackendException {
CurrentUser user = userProvider.get();
if (user == null || !user.isIdentifiedUser()) {
throw new AuthException("authentication required");
}
if (input == null) {
input = new Input();
}
String username = id.get();
if (input.username != null && !username.equals(input.username)) {
throw new BadRequestException("username must match URL");
}
if (Strings.isNullOrEmpty(input.sshKey)) {
throw new BadRequestException("sshKey not set");
}
if (!SshKeyValidator.validateFormat(input.sshKey)) {
throw new BadRequestException("sshKey invalid.");
}
if (blockedNameFilter.isBlocked(username)) {
throw new BadRequestException(
"The username '" + username + "' is not allowed as name for service users.");
}
input.email = Strings.emptyToNull(input.email);
if (input.email != null) {
Boolean emailAllowed;
try {
emailAllowed = getConfig.get().apply(new ConfigResource()).value().allowEmail;
} catch (Exception e) {
throw asRestApiException("Cannot get configuration", e);
}
if (emailAllowed == null || !emailAllowed) {
throw new ResourceConflictException("email not allowed");
}
}
AccountInput in = new ServiceUserInput(username, input.email, input.sshKey);
in.groups = Arrays.asList(cfg.getStringList("group"));
AccountInfo response;
try {
response = createAccount.apply(IdString.fromDecoded(username), in).value();
} catch (Exception e) {
throw asRestApiException("Cannot create account", e);
}
String creator = user.getUserName().get();
Account.Id creatorId = ((IdentifiedUser) user).getAccountId();
String creationDate = rfc2822DateFormatter.format(new Date());
try (MetaDataUpdate md = metaDataUpdateFactory.create(allProjects)) {
ProjectLevelConfig.Bare update = configProvider.get();
update.load(md);
Config db = update.getConfig();
db.setInt(USER, response.username, KEY_CREATOR_ID, creatorId.get());
if (creator != null) {
db.setString(USER, response.username, KEY_CREATED_BY, creator);
}
db.setString(USER, response.username, KEY_CREATED_AT, creationDate);
md.setMessage("Create service user '" + username + "'\n");
update.commit(md);
storageCache.invalidate();
}
ServiceUserInfo info = new ServiceUserInfo(response);
AccountLoader al = accountLoader.create(true);
info.createdBy = al.get(creatorId);
al.fill();
info.createdAt = creationDate;
return Response.created(info);
}