use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.
the class ReviewersUtil method suggestGroupAsReviewer.
private GroupAsReviewer suggestGroupAsReviewer(SuggestReviewers suggestReviewers, Project project, GroupReference group, VisibilityControl visibilityControl) throws IOException {
GroupAsReviewer result = new GroupAsReviewer();
int maxAllowed = suggestReviewers.getMaxAllowed();
int maxAllowedWithoutConfirmation = suggestReviewers.getMaxAllowedWithoutConfirmation();
logger.atFine().log("maxAllowedWithoutConfirmation: %s", maxAllowedWithoutConfirmation);
if (!ReviewerModifier.isLegalReviewerGroup(group.getUUID())) {
logger.atFine().log("Ignore group %s that is not legal as reviewer", group.getUUID());
return result;
}
try {
Set<Account> members = groupMembers.listAccounts(group.getUUID(), project.getNameKey());
if (members.isEmpty()) {
logger.atFine().log("Ignore group %s since it has no members", group.getUUID());
return result;
}
result.size = members.size();
if (maxAllowed > 0 && result.size > maxAllowed) {
return result;
}
boolean needsConfirmation = maxAllowedWithoutConfirmation > 0 && result.size > maxAllowedWithoutConfirmation;
if (needsConfirmation) {
logger.atFine().log("group %s needs confirmation to be added as reviewer, it has %d members", group.getUUID(), result.size);
}
// require that at least one member in the group can see the change
for (Account account : members) {
if (visibilityControl.isVisibleTo(account.id())) {
if (needsConfirmation) {
result.allowedWithConfirmation = true;
} else {
result.allowed = true;
}
logger.atFine().log("Suggest group %s", group.getUUID());
return result;
}
}
logger.atFine().log("Ignore group %s since none of its members can see the change", group.getUUID());
} catch (NoSuchProjectException e) {
return result;
}
return result;
}
use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.
the class AddMembers method apply.
@Override
public Response<List<AccountInfo>> apply(GroupResource resource, Input input) throws AuthException, NotInternalGroupException, UnprocessableEntityException, IOException, ConfigInvalidException, ResourceNotFoundException, PermissionBackendException {
GroupDescription.Internal internalGroup = resource.asInternalGroup().orElseThrow(NotInternalGroupException::new);
input = Input.init(input);
GroupControl control = resource.getControl();
if (!control.canAddMember()) {
throw new AuthException("Cannot add members to group " + internalGroup.getName());
}
Set<Account.Id> newMemberIds = new LinkedHashSet<>();
for (String nameOrEmailOrId : input.members) {
Account a = findAccount(nameOrEmailOrId);
if (!a.isActive()) {
throw new UnprocessableEntityException(String.format("Account Inactive: %s", nameOrEmailOrId));
}
newMemberIds.add(a.id());
}
AccountGroup.UUID groupUuid = internalGroup.getGroupUUID();
try {
addMembers(groupUuid, newMemberIds);
} catch (NoSuchGroupException e) {
throw new ResourceNotFoundException(String.format("Group %s not found", groupUuid), e);
}
return Response.ok(toAccountInfoList(newMemberIds));
}
use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.
the class LsUserRefs method run.
@Override
protected void run() throws Failure {
enableGracefulStop();
Account.Id userAccountId;
try {
userAccountId = accountResolver.resolve(userName).asUnique().account().id();
} catch (UnprocessableEntityException e) {
stdout.println(e.getMessage());
stdout.flush();
return;
} catch (StorageException | IOException | ConfigInvalidException e) {
throw die(e);
}
Project.NameKey projectName = projectState.getNameKey();
try (Repository repo = repoManager.openRepository(projectName);
ManualRequestContext ctx = requestContext.openAs(userAccountId)) {
try {
Collection<Ref> refsMap = permissionBackend.user(ctx.getUser()).project(projectName).filter(repo.getRefDatabase().getRefs(), repo, RefFilterOptions.defaults());
for (Ref ref : refsMap) {
if (!onlyRefsHeads || ref.getName().startsWith(RefNames.REFS_HEADS)) {
stdout.println(ref);
}
}
} catch (IOException | PermissionBackendException e) {
throw new Failure(1, "fatal: Error reading refs: '" + projectName, e);
}
} catch (RepositoryNotFoundException e) {
throw die("'" + projectName + "': not a git archive", e);
} catch (IOException e) {
throw die("Error opening: '" + projectName, e);
}
}
use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.
the class QueryChangesIT method queryByFullNameEmailFormatWithEmptyFullNameWhenEmailMatchesSeveralAccounts.
@Test
public void queryByFullNameEmailFormatWithEmptyFullNameWhenEmailMatchesSeveralAccounts() throws Exception {
// Create 2 accounts with the same preferred email (both account must have no external ID for
// the email because otherwise the account with the external ID takes precedence).
String email = "foo.bar@example.com";
Account.Id account1 = accountOperations.newAccount().create();
accountOperations.account(account1).forInvalidation().preferredEmailWithoutExternalId(email).invalidate();
Account.Id account2 = accountOperations.newAccount().create();
accountOperations.account(account2).forInvalidation().preferredEmailWithoutExternalId(email).invalidate();
// Search with "Full Name <email>" format, but without full name. Both created accounts match
// the email. In this case Gerrit falls back to match on the full name. Check that this logic
// doesn't fail if the full name in the input string is not present.
QueryChanges queryChanges = queryChangesProvider.get();
queryChanges.addQuery("<" + email + ">");
assertThat(queryChanges.apply(TopLevelResource.INSTANCE).statusCode()).isEqualTo(SC_OK);
}
use of com.google.gerrit.entities.Account in project gerrit by GerritCodeReview.
the class PostGpgKeys method apply.
@Override
public Response<Map<String, GpgKeyInfo>> apply(AccountResource rsrc, GpgKeysInput input) throws RestApiException, PGPException, IOException, ConfigInvalidException {
GpgKeys.checkVisible(self, rsrc);
Collection<ExternalId> existingExtIds = externalIds.byAccount(rsrc.getUser().getAccountId(), SCHEME_GPGKEY);
try (PublicKeyStore store = storeProvider.get()) {
Map<ExternalId, Fingerprint> toRemove = readKeysToRemove(input, existingExtIds);
Collection<Fingerprint> fingerprintsToRemove = toRemove.values();
List<PGPPublicKeyRing> newKeys = readKeysToAdd(input, fingerprintsToRemove);
List<ExternalId> newExtIds = new ArrayList<>(existingExtIds.size());
for (PGPPublicKeyRing keyRing : newKeys) {
PGPPublicKey key = keyRing.getPublicKey();
ExternalId.Key extIdKey = toExtIdKey(key.getFingerprint());
Account account = getAccountByExternalId(extIdKey);
if (account != null) {
if (!account.id().equals(rsrc.getUser().getAccountId())) {
throw new ResourceConflictException("GPG key already associated with another account");
}
} else {
newExtIds.add(externalIdFactory.create(extIdKey, rsrc.getUser().getAccountId()));
}
}
storeKeys(rsrc, newKeys, fingerprintsToRemove);
accountsUpdateProvider.get().update("Update GPG Keys via API", rsrc.getUser().getAccountId(), u -> u.replaceExternalIds(toRemove.keySet(), newExtIds));
return Response.ok(toJson(newKeys, fingerprintsToRemove, store, rsrc.getUser()));
}
}
Aggregations