Search in sources :

Example 21 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class OutgoingEmail method toAddress.

private Address toAddress(final Account.Id id) {
    final Account a = args.accountCache.get(id).getAccount();
    final String e = a.getPreferredEmail();
    if (!a.isActive() || e == null) {
        return null;
    }
    return new Address(a.getFullName(), e);
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) Address(com.google.gerrit.server.mail.Address)

Example 22 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class OutgoingEmail method getNameFor.

/** Lookup a human readable name for an account, usually the "full name". */
protected String getNameFor(final Account.Id accountId) {
    if (accountId == null) {
        return args.gerritPersonIdent.getName();
    }
    final Account userAccount = args.accountCache.get(accountId).getAccount();
    String name = userAccount.getFullName();
    if (name == null) {
        name = userAccount.getPreferredEmail();
    }
    if (name == null) {
        name = args.anonymousCowardName + " #" + accountId;
    }
    return name;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account)

Example 23 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class CheckAccess method apply.

@Override
public AccessCheckInfo apply(ConfigResource unused, AccessCheckInput input) throws OrmException, PermissionBackendException, RestApiException, IOException {
    permissionBackend.user(currentUser.get()).check(GlobalPermission.ADMINISTRATE_SERVER);
    if (input == null) {
        throw new BadRequestException("input is required");
    }
    if (Strings.isNullOrEmpty(input.account)) {
        throw new BadRequestException("input requires 'account'");
    }
    if (Strings.isNullOrEmpty(input.project)) {
        throw new BadRequestException("input requires 'project'");
    }
    Account match = accountResolver.find(db.get(), input.account);
    if (match == null) {
        throw new BadRequestException(String.format("cannot find account %s", input.account));
    }
    AccessCheckInfo info = new AccessCheckInfo();
    Project.NameKey key = new Project.NameKey(input.project);
    if (projectCache.get(key) == null) {
        info.message = String.format("project %s does not exist", key);
        info.status = HttpServletResponse.SC_NOT_FOUND;
        return info;
    }
    IdentifiedUser user = userFactory.create(match.getId());
    try {
        permissionBackend.user(user).project(key).check(ProjectPermission.ACCESS);
    } catch (AuthException | PermissionBackendException e) {
        info.message = String.format("user %s (%s) cannot see project %s", user.getNameEmail(), user.getAccount().getId(), key);
        info.status = HttpServletResponse.SC_FORBIDDEN;
        return info;
    }
    if (!Strings.isNullOrEmpty(input.ref)) {
        try {
            permissionBackend.user(user).ref(new Branch.NameKey(key, input.ref)).check(RefPermission.READ);
        } catch (AuthException | PermissionBackendException e) {
            info.status = HttpServletResponse.SC_FORBIDDEN;
            info.message = String.format("user %s (%s) cannot see ref %s in project %s", user.getNameEmail(), user.getAccount().getId(), input.ref, key);
            return info;
        }
    }
    info.status = HttpServletResponse.SC_OK;
    return info;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) Project(com.google.gerrit.reviewdb.client.Project) AccessCheckInfo(com.google.gerrit.extensions.api.config.AccessCheckInfo) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AuthException(com.google.gerrit.extensions.restapi.AuthException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 24 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class AccountCreator method create.

public synchronized TestAccount create(@Nullable String username, @Nullable String email, @Nullable String fullName, String... groups) throws Exception {
    TestAccount account = accounts.get(username);
    if (account != null) {
        return account;
    }
    try (ReviewDb db = reviewDbProvider.open()) {
        Account.Id id = new Account.Id(db.nextAccountId());
        List<ExternalId> extIds = new ArrayList<>(2);
        String httpPass = null;
        if (username != null) {
            httpPass = "http-pass";
            extIds.add(ExternalId.createUsername(username, id, httpPass));
        }
        if (email != null) {
            extIds.add(ExternalId.createEmail(id, email));
        }
        externalIdsUpdate.create().insert(extIds);
        Account a = new Account(id, TimeUtil.nowTs());
        a.setFullName(fullName);
        a.setPreferredEmail(email);
        accountsUpdate.create().insert(db, a);
        if (groups != null) {
            for (String n : groups) {
                AccountGroup.NameKey k = new AccountGroup.NameKey(n);
                AccountGroup g = groupCache.get(k);
                checkArgument(g != null, "group not found: %s", n);
                AccountGroupMember m = new AccountGroupMember(new AccountGroupMember.Key(id, g.getId()));
                db.accountGroupMembers().insert(Collections.singleton(m));
            }
        }
        KeyPair sshKey = null;
        if (SshMode.useSsh() && username != null) {
            sshKey = genSshKey();
            authorizedKeys.addKey(id, publicKey(sshKey, email));
            sshKeyCache.evict(username);
        }
        if (username != null) {
            accountCache.evictByUsername(username);
        }
        byEmailCache.evict(email);
        indexer.index(id);
        account = new TestAccount(id, username, email, fullName, sshKey, httpPass);
        if (username != null) {
            accounts.put(username, account);
        }
        return account;
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) KeyPair(com.jcraft.jsch.KeyPair) AccountGroupMember(com.google.gerrit.reviewdb.client.AccountGroupMember) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ArrayList(java.util.ArrayList) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 25 with Account

use of com.google.gerrit.reviewdb.client.Account in project gerrit by GerritCodeReview.

the class GetAccountDetailIT method getDetail.

@Test
public void getDetail() throws Exception {
    RestResponse r = adminRestSession.get("/accounts/" + admin.username + "/detail/");
    AccountDetailInfo info = newGson().fromJson(r.getReader(), AccountDetailInfo.class);
    assertAccountInfo(admin, info);
    Account account = accountCache.get(admin.getId()).getAccount();
    assertThat(info.registeredOn).isEqualTo(account.getRegisteredOn());
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) AccountDetailInfo(com.google.gerrit.server.account.GetDetail.AccountDetailInfo) RestResponse(com.google.gerrit.acceptance.RestResponse) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

Account (com.google.gerrit.reviewdb.client.Account)75 ArrayList (java.util.ArrayList)13 OrmException (com.google.gwtorm.server.OrmException)11 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)10 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)10 AuthException (com.google.gerrit.extensions.restapi.AuthException)8 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)8 HashSet (java.util.HashSet)8 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)7 AccountGroupMember (com.google.gerrit.reviewdb.client.AccountGroupMember)7 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)7 PersonIdent (org.eclipse.jgit.lib.PersonIdent)7 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)6 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)6 CurrentUser (com.google.gerrit.server.CurrentUser)6 Ref (org.eclipse.jgit.lib.Ref)6 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)5 IOException (java.io.IOException)5 Test (org.junit.Test)5 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)4