use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.
the class AbstractQueryGroupsTest method createAccount.
private Account.Id createAccount(String username, String fullName, String email, boolean active) throws Exception {
try (ManualRequestContext ctx = oneOffRequestContext.open()) {
Account.Id id = accountManager.authenticate(AuthRequest.forUser(username)).getAccountId();
if (email != null) {
accountManager.link(id, AuthRequest.forEmail(email));
}
Account a = db.accounts().get(id);
a.setFullName(fullName);
a.setPreferredEmail(email);
a.setActive(active);
db.accounts().update(ImmutableList.of(a));
accountCache.evict(id);
return id;
}
}
use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.
the class PutActive method apply.
@Override
public Response<String> apply(AccountResource rsrc, Input input) throws ResourceNotFoundException, OrmException, IOException {
AtomicBoolean alreadyActive = new AtomicBoolean(false);
Account a = dbProvider.get().accounts().atomicUpdate(rsrc.getUser().getAccountId(), new AtomicUpdate<Account>() {
@Override
public Account update(Account a) {
if (a.isActive()) {
alreadyActive.set(true);
} else {
a.setActive(true);
}
return a;
}
});
if (a == null) {
throw new ResourceNotFoundException("account not found");
}
byIdCache.evict(a.getId());
return alreadyActive.get() ? Response.ok("") : Response.created("");
}
use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.
the class PutAgreement method apply.
@Override
public Response<String> apply(AccountResource resource, AgreementInput input) throws IOException, OrmException, RestApiException {
if (!agreementsEnabled) {
throw new MethodNotAllowedException("contributor agreements disabled");
}
if (self.get() != resource.getUser()) {
throw new AuthException("not allowed to enter contributor agreement");
}
String agreementName = Strings.nullToEmpty(input.name);
ContributorAgreement ca = projectCache.getAllProjects().getConfig().getContributorAgreement(agreementName);
if (ca == null) {
throw new UnprocessableEntityException("contributor agreement not found");
}
if (ca.getAutoVerify() == null) {
throw new BadRequestException("cannot enter a non-autoVerify agreement");
}
AccountGroup.UUID uuid = ca.getAutoVerify().getUUID();
if (uuid == null) {
throw new ResourceConflictException("autoverify group uuid not found");
}
AccountGroup group = groupCache.get(uuid);
if (group == null) {
throw new ResourceConflictException("autoverify group not found");
}
Account account = self.get().getAccount();
addMembers.addMembers(group.getId(), ImmutableList.of(account.getId()));
agreementSignup.fire(account, agreementName);
return Response.ok(agreementName);
}
use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.
the class AccountManager method create.
private AuthResult create(ReviewDb db, AuthRequest who) throws OrmException, AccountException, IOException, ConfigInvalidException {
Account.Id newId = new Account.Id(db.nextAccountId());
Account account = new Account(newId, TimeUtil.nowTs());
ExternalId extId = ExternalId.createWithEmail(who.getExternalIdKey(), newId, who.getEmailAddress());
account.setFullName(who.getDisplayName());
account.setPreferredEmail(extId.email());
boolean isFirstAccount = awaitsFirstAccountCheck.getAndSet(false) && db.accounts().anyAccounts().toList().isEmpty();
try {
AccountsUpdate accountsUpdate = accountsUpdateFactory.create();
accountsUpdate.upsert(db, account);
ExternalId existingExtId = externalIds.get(extId.key());
if (existingExtId != null && !existingExtId.accountId().equals(extId.accountId())) {
// external ID is assigned to another account, do not overwrite
accountsUpdate.delete(db, account);
throw new AccountException("Cannot assign external ID \"" + extId.key().get() + "\" to account " + newId + "; external ID already in use.");
}
externalIdsUpdateFactory.create().upsert(extId);
} finally {
// If adding the account failed, it may be that it actually was the
// first account. So we reset the 'check for first account'-guard, as
// otherwise the first account would not get administration permissions.
awaitsFirstAccountCheck.set(isFirstAccount);
}
if (isFirstAccount) {
// This is the first user account on our site. Assume this user
// is going to be the site's administrator and just make them that
// to bootstrap the authentication database.
//
Permission admin = projectCache.getAllProjects().getConfig().getAccessSection(AccessSection.GLOBAL_CAPABILITIES).getPermission(GlobalCapability.ADMINISTRATE_SERVER);
AccountGroup.UUID uuid = admin.getRules().get(0).getGroup().getUUID();
AccountGroup g = db.accountGroups().byUUID(uuid).iterator().next();
AccountGroup.Id adminId = g.getId();
AccountGroupMember m = new AccountGroupMember(new AccountGroupMember.Key(newId, adminId));
auditService.dispatchAddAccountsToGroup(newId, Collections.singleton(m));
db.accountGroupMembers().insert(Collections.singleton(m));
}
if (who.getUserName() != null) {
// Only set if the name hasn't been used yet, but was given to us.
//
IdentifiedUser user = userFactory.create(newId);
try {
changeUserNameFactory.create(user, who.getUserName()).call();
} catch (NameAlreadyUsedException e) {
String message = "Cannot assign user name \"" + who.getUserName() + "\" to account " + newId + "; name already in use.";
handleSettingUserNameFailure(db, account, extId, message, e, false);
} catch (InvalidUserNameException e) {
String message = "Cannot assign user name \"" + who.getUserName() + "\" to account " + newId + "; name does not conform.";
handleSettingUserNameFailure(db, account, extId, message, e, false);
} catch (OrmException e) {
String message = "Cannot assign user name";
handleSettingUserNameFailure(db, account, extId, message, e, true);
}
}
byEmailCache.evict(account.getPreferredEmail());
byIdCache.evict(account.getId());
realm.onCreateAccount(who, account);
return new AuthResult(newId, extId.key(), true);
}
use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.
the class AccountResolver method find.
/**
* Locate exactly one account matching the name or name/email string.
*
* @param nameOrEmail a string of the format "Full Name <email@example>", just the email
* address ("email@example"), a full name ("Full Name"), an account id ("18419") or an user
* name ("username").
* @return the single account that matches; null if no account matches or there are multiple
* candidates.
*/
public Account find(ReviewDb db, String nameOrEmail) throws OrmException {
Set<Account.Id> r = findAll(db, nameOrEmail);
if (r.size() == 1) {
return byId.get(r.iterator().next()).getAccount();
}
Account match = null;
for (Account.Id id : r) {
Account account = byId.get(id).getAccount();
if (!account.isActive()) {
continue;
}
if (match != null) {
return null;
}
match = account;
}
return match;
}
Aggregations