use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.
the class BecomeAnyAccountLoginServlet method prepareHtmlOutput.
private byte[] prepareHtmlOutput() throws IOException, OrmException {
final String pageName = "BecomeAnyAccount.html";
Document doc = headers.parse(getClass(), pageName);
if (doc == null) {
throw new FileNotFoundException("No " + pageName + " in webapp");
}
Element userlistElement = HtmlDomUtil.find(doc, "userlist");
try (ReviewDb db = schema.open()) {
ResultSet<Account> accounts = db.accounts().firstNById(100);
for (Account a : accounts) {
String displayName;
if (a.getUserName() != null) {
displayName = a.getUserName();
} else if (a.getFullName() != null && !a.getFullName().isEmpty()) {
displayName = a.getFullName();
} else if (a.getPreferredEmail() != null) {
displayName = a.getPreferredEmail();
} else {
displayName = a.getId().toString();
}
Element linkElement = doc.createElement("a");
linkElement.setAttribute("href", "?account_id=" + a.getId().toString());
linkElement.setTextContent(displayName);
userlistElement.appendChild(linkElement);
userlistElement.appendChild(doc.createElement("br"));
}
}
return HtmlDomUtil.toUTF8(doc);
}
use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.
the class ReviewersUtil method suggestGroupAsReviewer.
private GroupAsReviewer suggestGroupAsReviewer(SuggestReviewers suggestReviewers, Project project, GroupReference group, VisibilityControl visibilityControl) throws OrmException, IOException {
GroupAsReviewer result = new GroupAsReviewer();
int maxAllowed = suggestReviewers.getMaxAllowed();
int maxAllowedWithoutConfirmation = suggestReviewers.getMaxAllowedWithoutConfirmation();
if (!PostReviewers.isLegalReviewerGroup(group.getUUID())) {
return result;
}
try {
Set<Account> members = groupMembersFactory.create(currentUser.get()).listAccounts(group.getUUID(), project.getNameKey());
if (members.isEmpty()) {
return result;
}
result.size = members.size();
if (maxAllowed > 0 && result.size > maxAllowed) {
return result;
}
boolean needsConfirmation = result.size > maxAllowedWithoutConfirmation;
// require that at least one member in the group can see the change
for (Account account : members) {
if (visibilityControl.isVisibleTo(account.getId())) {
if (needsConfirmation) {
result.allowedWithConfirmation = true;
} else {
result.allowed = true;
}
return result;
}
}
} catch (NoSuchGroupException e) {
return result;
} catch (NoSuchProjectException e) {
return result;
}
return result;
}
use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.
the class AccountManager method link.
/**
* Link another authentication identity to an existing account.
*
* @param to account to link the identity onto.
* @param who the additional identity.
* @return the result of linking the identity to the user.
* @throws AccountException the identity belongs to a different account, or it cannot be linked at
* this time.
*/
public AuthResult link(Account.Id to, AuthRequest who) throws AccountException, OrmException, IOException, ConfigInvalidException {
try (ReviewDb db = schema.open()) {
ExternalId extId = findExternalId(who.getExternalIdKey());
if (extId != null) {
if (!extId.accountId().equals(to)) {
throw new AccountException("Identity in use by another account");
}
update(db, who, extId);
} else {
externalIdsUpdateFactory.create().insert(ExternalId.createWithEmail(who.getExternalIdKey(), to, who.getEmailAddress()));
if (who.getEmailAddress() != null) {
Account a = db.accounts().get(to);
if (a.getPreferredEmail() == null) {
a.setPreferredEmail(who.getEmailAddress());
db.accounts().update(Collections.singleton(a));
}
}
if (who.getEmailAddress() != null) {
byEmailCache.evict(who.getEmailAddress());
}
byIdCache.evict(to);
}
return new AuthResult(to, who.getExternalIdKey(), false);
}
}
use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.
the class AccountManager method update.
private void update(ReviewDb db, AuthRequest who, ExternalId extId) throws OrmException, IOException, ConfigInvalidException {
IdentifiedUser user = userFactory.create(extId.accountId());
Account toUpdate = null;
// If the email address was modified by the authentication provider,
// update our records to match the changed email.
//
String newEmail = who.getEmailAddress();
String oldEmail = extId.email();
if (newEmail != null && !newEmail.equals(oldEmail)) {
if (oldEmail != null && oldEmail.equals(user.getAccount().getPreferredEmail())) {
toUpdate = load(toUpdate, user.getAccountId(), db);
toUpdate.setPreferredEmail(newEmail);
}
externalIdsUpdateFactory.create().replace(extId, ExternalId.create(extId.key(), extId.accountId(), newEmail, extId.password()));
}
if (!realm.allowsEdit(AccountFieldName.FULL_NAME) && !Strings.isNullOrEmpty(who.getDisplayName()) && !eq(user.getAccount().getFullName(), who.getDisplayName())) {
toUpdate = load(toUpdate, user.getAccountId(), db);
toUpdate.setFullName(who.getDisplayName());
}
if (!realm.allowsEdit(AccountFieldName.USER_NAME) && who.getUserName() != null && !eq(user.getUserName(), who.getUserName())) {
log.warn(String.format("Not changing already set username %s to %s", user.getUserName(), who.getUserName()));
}
if (toUpdate != null) {
db.accounts().update(Collections.singleton(toUpdate));
}
if (newEmail != null && !newEmail.equals(oldEmail)) {
byEmailCache.evict(oldEmail);
byEmailCache.evict(newEmail);
}
if (toUpdate != null) {
byIdCache.evict(toUpdate.getId());
}
}
use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.
the class IdentifiedUser method newCommitterIdent.
public PersonIdent newCommitterIdent(final Date when, final TimeZone tz) {
final Account ua = getAccount();
String name = ua.getFullName();
String email = ua.getPreferredEmail();
if (email == null || email.isEmpty()) {
// No preferred email is configured. Use a generic identity so we
// don't leak an address the user may have given us, but doesn't
// necessarily want to publish through Git records.
//
String user = getUserName();
if (user == null || user.isEmpty()) {
user = "account-" + ua.getId().toString();
}
String host;
if (canonicalUrl.get() != null) {
try {
host = new URL(canonicalUrl.get()).getHost();
} catch (MalformedURLException e) {
host = SystemReader.getInstance().getHostname();
}
} else {
host = SystemReader.getInstance().getHostname();
}
email = user + "@" + host;
}
if (name == null || name.isEmpty()) {
final int at = email.indexOf('@');
if (0 < at) {
name = email.substring(0, at);
} else {
name = anonymousCowardName;
}
}
return new PersonIdent(name, email, when, tz);
}
Aggregations