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;
}
use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.
the class AccountResolver method findAllByNameOrEmail.
/**
* Locate exactly one account matching the name or name/email string.
*
* @param db open database handle.
* @param nameOrEmail a string of the format "Full Name <email@example>", just the email
* address ("email@example"), a full name ("Full Name").
* @return the accounts that match, empty collection if none. Never null.
*/
public Set<Account.Id> findAllByNameOrEmail(ReviewDb db, String nameOrEmail) throws OrmException {
int lt = nameOrEmail.indexOf('<');
int gt = nameOrEmail.indexOf('>');
if (lt >= 0 && gt > lt && nameOrEmail.contains("@")) {
Set<Account.Id> ids = byEmail.get(nameOrEmail.substring(lt + 1, gt));
if (ids.isEmpty() || ids.size() == 1) {
return ids;
}
// more than one match, try to return the best one
String name = nameOrEmail.substring(0, lt - 1);
Set<Account.Id> nameMatches = new HashSet<>();
for (Account.Id id : ids) {
Account a = byId.get(id).getAccount();
if (name.equals(a.getFullName())) {
nameMatches.add(id);
}
}
return nameMatches.isEmpty() ? ids : nameMatches;
}
if (nameOrEmail.contains("@")) {
return byEmail.get(nameOrEmail);
}
Account.Id id = realm.lookup(nameOrEmail);
if (id != null) {
return Collections.singleton(id);
}
List<AccountState> m = accountQueryProvider.get().byFullName(nameOrEmail);
if (m.size() == 1) {
return Collections.singleton(m.get(0).getAccount().getId());
}
// and pray we come up with a reasonable result list.
return accountQueryProvider.get().byDefault(nameOrEmail).stream().map(a -> a.getAccount().getId()).collect(toSet());
}
use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.
the class AccountsCollection method parseIdOnBehalfOf.
private IdentifiedUser parseIdOnBehalfOf(@Nullable CurrentUser caller, String id) throws AuthException, OrmException {
if (id.equals("self")) {
CurrentUser user = self.get();
if (user.isIdentifiedUser()) {
return user.asIdentifiedUser();
} else if (user instanceof AnonymousUser) {
throw new AuthException("Authentication required");
} else {
return null;
}
}
Account match = resolver.find(db.get(), id);
if (match == null) {
return null;
}
CurrentUser realUser = caller != null ? caller.getRealUser() : null;
return userFactory.runAs(null, match.getId(), realUser);
}
use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.
the class PutName method apply.
public Response<String> apply(IdentifiedUser user, Input input) throws MethodNotAllowedException, ResourceNotFoundException, OrmException, IOException {
if (input == null) {
input = new Input();
}
if (!realm.allowsEdit(AccountFieldName.FULL_NAME)) {
throw new MethodNotAllowedException("realm does not allow editing name");
}
String newName = input.name;
Account a = dbProvider.get().accounts().atomicUpdate(user.getAccountId(), new AtomicUpdate<Account>() {
@Override
public Account update(Account a) {
a.setFullName(newName);
return a;
}
});
if (a == null) {
throw new ResourceNotFoundException("account not found");
}
byIdCache.evict(a.getId());
return Strings.isNullOrEmpty(a.getFullName()) ? Response.<String>none() : Response.ok(a.getFullName());
}
use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.
the class PutStatus method apply.
public Response<String> apply(IdentifiedUser user, Input input) throws ResourceNotFoundException, OrmException, IOException {
if (input == null) {
input = new Input();
}
String newStatus = input.status;
Account a = dbProvider.get().accounts().atomicUpdate(user.getAccountId(), new AtomicUpdate<Account>() {
@Override
public Account update(Account a) {
a.setStatus(Strings.nullToEmpty(newStatus));
return a;
}
});
if (a == null) {
throw new ResourceNotFoundException("account not found");
}
byIdCache.evict(a.getId());
return Strings.isNullOrEmpty(a.getStatus()) ? Response.none() : Response.ok(a.getStatus());
}
Aggregations