Search in sources :

Example 1 with AccountGroupName

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

the class InitAdminUser method postRun.

@Override
public void postRun() throws Exception {
    AuthType authType = flags.cfg.getEnum(AuthType.values(), "auth", null, "type", null);
    if (authType != AuthType.DEVELOPMENT_BECOME_ANY_ACCOUNT) {
        return;
    }
    try (ReviewDb db = dbFactory.open()) {
        if (db.accounts().anyAccounts().toList().isEmpty()) {
            ui.header("Gerrit Administrator");
            if (ui.yesno(true, "Create administrator user")) {
                Account.Id id = new Account.Id(db.nextAccountId());
                String username = ui.readString("admin", "username");
                String name = ui.readString("Administrator", "name");
                String httpPassword = ui.readString("secret", "HTTP password");
                AccountSshKey sshKey = readSshKey(id);
                String email = readEmail(sshKey);
                List<ExternalId> extIds = new ArrayList<>(2);
                extIds.add(ExternalId.createUsername(username, id, httpPassword));
                if (email != null) {
                    extIds.add(ExternalId.createEmail(id, email));
                }
                externalIds.insert("Add external IDs for initial admin user", extIds);
                Account a = new Account(id, TimeUtil.nowTs());
                a.setFullName(name);
                a.setPreferredEmail(email);
                accounts.insert(db, a);
                AccountGroupName adminGroupName = db.accountGroupNames().get(new AccountGroup.NameKey("Administrators"));
                AccountGroupMember m = new AccountGroupMember(new AccountGroupMember.Key(id, adminGroupName.getId()));
                db.accountGroupMembers().insert(Collections.singleton(m));
                if (sshKey != null) {
                    VersionedAuthorizedKeysOnInit authorizedKeys = authorizedKeysFactory.create(id).load();
                    authorizedKeys.addKey(sshKey.getSshPublicKey());
                    authorizedKeys.save("Add SSH key for initial admin user\n");
                }
                AccountGroup adminGroup = db.accountGroups().get(adminGroupName.getId());
                AccountState as = new AccountState(a, Collections.singleton(adminGroup.getGroupUUID()), extIds, new HashMap<>());
                for (AccountIndex accountIndex : indexCollection.getWriteIndexes()) {
                    accountIndex.replace(as);
                }
            }
        }
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) AccountGroupName(com.google.gerrit.reviewdb.client.AccountGroupName) AccountGroupMember(com.google.gerrit.reviewdb.client.AccountGroupMember) AccountIndex(com.google.gerrit.server.index.account.AccountIndex) AccountSshKey(com.google.gerrit.reviewdb.client.AccountSshKey) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ArrayList(java.util.ArrayList) AccountState(com.google.gerrit.server.account.AccountState) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) AuthType(com.google.gerrit.extensions.client.AuthType) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 2 with AccountGroupName

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

the class CreateGroup method createGroup.

private AccountGroup createGroup(CreateGroupArgs createGroupArgs) throws OrmException, ResourceConflictException, IOException {
    // Do not allow creating groups with the same name as system groups
    for (String name : systemGroupBackend.getNames()) {
        if (name.toLowerCase(Locale.US).equals(createGroupArgs.getGroupName().toLowerCase(Locale.US))) {
            throw new ResourceConflictException("group '" + name + "' already exists");
        }
    }
    for (String name : systemGroupBackend.getReservedNames()) {
        if (name.toLowerCase(Locale.US).equals(createGroupArgs.getGroupName().toLowerCase(Locale.US))) {
            throw new ResourceConflictException("group name '" + name + "' is reserved");
        }
    }
    AccountGroup.Id groupId = new AccountGroup.Id(db.nextAccountGroupId());
    AccountGroup.UUID uuid = GroupUUID.make(createGroupArgs.getGroupName(), self.get().newCommitterIdent(serverIdent.getWhen(), serverIdent.getTimeZone()));
    AccountGroup group = new AccountGroup(createGroupArgs.getGroup(), groupId, uuid, TimeUtil.nowTs());
    group.setVisibleToAll(createGroupArgs.visibleToAll);
    if (createGroupArgs.ownerGroupId != null) {
        AccountGroup ownerGroup = groupCache.get(createGroupArgs.ownerGroupId);
        if (ownerGroup != null) {
            group.setOwnerGroupUUID(ownerGroup.getGroupUUID());
        }
    }
    if (createGroupArgs.groupDescription != null) {
        group.setDescription(createGroupArgs.groupDescription);
    }
    AccountGroupName gn = new AccountGroupName(group);
    // already been used to create another group
    try {
        db.accountGroupNames().insert(Collections.singleton(gn));
    } catch (OrmDuplicateKeyException e) {
        throw new ResourceConflictException("group '" + createGroupArgs.getGroupName() + "' already exists");
    }
    db.accountGroups().insert(Collections.singleton(group));
    addMembers.addMembers(groupId, createGroupArgs.initialMembers);
    groupCache.onCreateGroup(createGroupArgs.getGroup());
    return group;
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) AccountGroupName(com.google.gerrit.reviewdb.client.AccountGroupName) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) OrmDuplicateKeyException(com.google.gwtorm.server.OrmDuplicateKeyException)

Example 3 with AccountGroupName

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

the class PutName method renameGroup.

private GroupDetail renameGroup(AccountGroup group, String newName) throws ResourceConflictException, OrmException, NoSuchGroupException, IOException {
    AccountGroup.Id groupId = group.getId();
    AccountGroup.NameKey old = group.getNameKey();
    AccountGroup.NameKey key = new AccountGroup.NameKey(newName);
    try {
        AccountGroupName id = new AccountGroupName(key, groupId);
        db.get().accountGroupNames().insert(Collections.singleton(id));
    } catch (OrmException e) {
        AccountGroupName other = db.get().accountGroupNames().get(key);
        if (other != null) {
            //
            if (other.getId().equals(groupId)) {
                return groupDetailFactory.create(groupId).call();
            }
            //
            throw new ResourceConflictException("group with name " + newName + "already exists");
        }
        throw e;
    }
    group.setNameKey(key);
    db.get().accountGroups().update(Collections.singleton(group));
    AccountGroupName priorName = db.get().accountGroupNames().get(old);
    if (priorName != null) {
        db.get().accountGroupNames().delete(Collections.singleton(priorName));
    }
    groupCache.evict(group);
    groupCache.evictAfterRename(old, key);
    @SuppressWarnings("unused") Future<?> possiblyIgnoredError = renameGroupOpFactory.create(currentUser.get().newCommitterIdent(new Date(), TimeZone.getDefault()), group.getGroupUUID(), old.get(), newName).start(0, TimeUnit.MILLISECONDS);
    return groupDetailFactory.create(groupId).call();
}
Also used : AccountGroupName(com.google.gerrit.reviewdb.client.AccountGroupName) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) OrmException(com.google.gwtorm.server.OrmException) Date(java.util.Date)

Aggregations

AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)3 AccountGroupName (com.google.gerrit.reviewdb.client.AccountGroupName)3 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 AuthType (com.google.gerrit.extensions.client.AuthType)1 Account (com.google.gerrit.reviewdb.client.Account)1 AccountGroupMember (com.google.gerrit.reviewdb.client.AccountGroupMember)1 AccountSshKey (com.google.gerrit.reviewdb.client.AccountSshKey)1 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)1 AccountState (com.google.gerrit.server.account.AccountState)1 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)1 AccountIndex (com.google.gerrit.server.index.account.AccountIndex)1 OrmDuplicateKeyException (com.google.gwtorm.server.OrmDuplicateKeyException)1 OrmException (com.google.gwtorm.server.OrmException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1