Accounts API: Add methods to create an account

Change-Id: I798f98384fc513987c00aa1a6102c9967813b542
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2016-07-06 16:57:04 +02:00
parent 0d4ef2ddba
commit a62673ca77
6 changed files with 90 additions and 19 deletions

View File

@@ -0,0 +1,29 @@
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.extensions.api.accounts;
import com.google.gerrit.extensions.restapi.DefaultInput;
import java.util.List;
public class AccountInput {
@DefaultInput
public String username;
public String name;
public String email;
public String sshKey;
public String httpPassword;
public List<String> groups;
}

View File

@@ -52,6 +52,12 @@ public interface Accounts {
*/ */
AccountApi self() throws RestApiException; AccountApi self() throws RestApiException;
/** Create a new account with the given username and default options. */
AccountApi create(String username) throws RestApiException;
/** Create a new account. */
AccountApi create(AccountInput input) throws RestApiException;
/** /**
* Suggest users for a given query. * Suggest users for a given query.
* <p> * <p>
@@ -232,6 +238,16 @@ public interface Accounts {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override
public AccountApi create(String username) throws RestApiException {
throw new NotImplementedException();
}
@Override
public AccountApi create(AccountInput input) throws RestApiException {
throw new NotImplementedException();
}
@Override @Override
public SuggestAccountsRequest suggestAccounts() throws RestApiException { public SuggestAccountsRequest suggestAccounts() throws RestApiException {
throw new NotImplementedException(); throw new NotImplementedException();

View File

@@ -20,10 +20,10 @@ import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.common.data.GroupDescriptions; import com.google.gerrit.common.data.GroupDescriptions;
import com.google.gerrit.common.errors.InvalidSshKeyException; import com.google.gerrit.common.errors.InvalidSshKeyException;
import com.google.gerrit.extensions.annotations.RequiresCapability; import com.google.gerrit.extensions.annotations.RequiresCapability;
import com.google.gerrit.extensions.api.accounts.AccountInput;
import com.google.gerrit.extensions.common.AccountInfo; import com.google.gerrit.extensions.common.AccountInfo;
import com.google.gerrit.extensions.registration.DynamicSet; import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.extensions.restapi.BadRequestException; import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.DefaultInput;
import com.google.gerrit.extensions.restapi.ResourceConflictException; import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.Response; import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestModifyView; import com.google.gerrit.extensions.restapi.RestModifyView;
@@ -35,7 +35,6 @@ import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.reviewdb.client.AccountGroupMember; import com.google.gerrit.reviewdb.client.AccountGroupMember;
import com.google.gerrit.reviewdb.server.ReviewDb; import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.IdentifiedUser; import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.CreateAccount.Input;
import com.google.gerrit.server.api.accounts.AccountExternalIdCreator; import com.google.gerrit.server.api.accounts.AccountExternalIdCreator;
import com.google.gerrit.server.group.GroupsCollection; import com.google.gerrit.server.group.GroupsCollection;
import com.google.gerrit.server.index.account.AccountIndexer; import com.google.gerrit.server.index.account.AccountIndexer;
@@ -57,17 +56,8 @@ import java.util.List;
import java.util.Set; import java.util.Set;
@RequiresCapability(GlobalCapability.CREATE_ACCOUNT) @RequiresCapability(GlobalCapability.CREATE_ACCOUNT)
public class CreateAccount implements RestModifyView<TopLevelResource, Input> { public class CreateAccount
public static class Input { implements RestModifyView<TopLevelResource, AccountInput> {
@DefaultInput
public String username;
public String name;
public String email;
public String sshKey;
public String httpPassword;
public List<String> groups;
}
public interface Factory { public interface Factory {
CreateAccount create(String username); CreateAccount create(String username);
} }
@@ -113,12 +103,12 @@ public class CreateAccount implements RestModifyView<TopLevelResource, Input> {
} }
@Override @Override
public Response<AccountInfo> apply(TopLevelResource rsrc, Input input) public Response<AccountInfo> apply(TopLevelResource rsrc, AccountInput input)
throws BadRequestException, ResourceConflictException, throws BadRequestException, ResourceConflictException,
UnprocessableEntityException, OrmException, IOException, UnprocessableEntityException, OrmException, IOException,
ConfigInvalidException { ConfigInvalidException {
if (input == null) { if (input == null) {
input = new Input(); input = new AccountInput();
} }
if (input.username != null && !username.equals(input.username)) { if (input.username != null && !username.equals(input.username)) {
throw new BadRequestException("username must match URL"); throw new BadRequestException("username must match URL");

View File

@@ -14,15 +14,16 @@
package com.google.gerrit.server.account; package com.google.gerrit.server.account;
import com.google.gerrit.extensions.api.accounts.AccountInput;
import com.google.gerrit.extensions.restapi.ResourceConflictException; import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.RestModifyView; import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.server.account.CreateAccount.Input;
import com.google.inject.Singleton; import com.google.inject.Singleton;
@Singleton @Singleton
public class PutAccount implements RestModifyView<AccountResource, Input> { public class PutAccount
implements RestModifyView<AccountResource, AccountInput> {
@Override @Override
public Object apply(AccountResource resource, Input input) public Object apply(AccountResource resource, AccountInput input)
throws ResourceConflictException { throws ResourceConflictException {
throw new ResourceConflictException("account exists"); throw new ResourceConflictException("account exists");
} }

View File

@@ -14,23 +14,32 @@
package com.google.gerrit.server.api.accounts; package com.google.gerrit.server.api.accounts;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.gerrit.server.account.CapabilityUtils.checkRequiresCapability;
import com.google.gerrit.extensions.api.accounts.AccountApi; import com.google.gerrit.extensions.api.accounts.AccountApi;
import com.google.gerrit.extensions.api.accounts.AccountInput;
import com.google.gerrit.extensions.api.accounts.Accounts; import com.google.gerrit.extensions.api.accounts.Accounts;
import com.google.gerrit.extensions.client.ListAccountsOption; import com.google.gerrit.extensions.client.ListAccountsOption;
import com.google.gerrit.extensions.common.AccountInfo; import com.google.gerrit.extensions.common.AccountInfo;
import com.google.gerrit.extensions.restapi.AuthException; import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.IdString; import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.RestApiException; import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.TopLevelResource; import com.google.gerrit.extensions.restapi.TopLevelResource;
import com.google.gerrit.server.CurrentUser; import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.account.AccountResource; import com.google.gerrit.server.account.AccountResource;
import com.google.gerrit.server.account.AccountsCollection; import com.google.gerrit.server.account.AccountsCollection;
import com.google.gerrit.server.account.CreateAccount;
import com.google.gerrit.server.account.QueryAccounts; import com.google.gerrit.server.account.QueryAccounts;
import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Provider; import com.google.inject.Provider;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import org.eclipse.jgit.errors.ConfigInvalidException;
import java.io.IOException;
import java.util.List; import java.util.List;
@Singleton @Singleton
@@ -38,16 +47,19 @@ public class AccountsImpl implements Accounts {
private final AccountsCollection accounts; private final AccountsCollection accounts;
private final AccountApiImpl.Factory api; private final AccountApiImpl.Factory api;
private final Provider<CurrentUser> self; private final Provider<CurrentUser> self;
private final CreateAccount.Factory createAccount;
private final Provider<QueryAccounts> queryAccountsProvider; private final Provider<QueryAccounts> queryAccountsProvider;
@Inject @Inject
AccountsImpl(AccountsCollection accounts, AccountsImpl(AccountsCollection accounts,
AccountApiImpl.Factory api, AccountApiImpl.Factory api,
Provider<CurrentUser> self, Provider<CurrentUser> self,
CreateAccount.Factory createAccount,
Provider<QueryAccounts> queryAccountsProvider) { Provider<QueryAccounts> queryAccountsProvider) {
this.accounts = accounts; this.accounts = accounts;
this.api = api; this.api = api;
this.self = self; this.self = self;
this.createAccount = createAccount;
this.queryAccountsProvider = queryAccountsProvider; this.queryAccountsProvider = queryAccountsProvider;
} }
@@ -74,6 +86,28 @@ public class AccountsImpl implements Accounts {
return api.create(new AccountResource(self.get().asIdentifiedUser())); return api.create(new AccountResource(self.get().asIdentifiedUser()));
} }
@Override
public AccountApi create(String username) throws RestApiException {
AccountInput in = new AccountInput();
in.username = username;
return create(in);
}
@Override
public AccountApi create(AccountInput in) throws RestApiException {
if (checkNotNull(in, "AccountInput").username == null) {
throw new BadRequestException("AccountInput must specify username");
}
checkRequiresCapability(self, null, CreateAccount.class);
try {
AccountInfo info = createAccount.create(in.username)
.apply(TopLevelResource.INSTANCE, in).value();
return id(info._accountId);
} catch (OrmException | IOException | ConfigInvalidException e) {
throw new RestApiException("Cannot create account " + in.username, e);
}
}
@Override @Override
public SuggestAccountsRequest suggestAccounts() throws RestApiException { public SuggestAccountsRequest suggestAccounts() throws RestApiException {
return new SuggestAccountsRequest() { return new SuggestAccountsRequest() {

View File

@@ -20,6 +20,7 @@ import com.google.common.base.Function;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.gerrit.common.data.GlobalCapability; import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.extensions.annotations.RequiresCapability; import com.google.gerrit.extensions.annotations.RequiresCapability;
import com.google.gerrit.extensions.api.accounts.AccountInput;
import com.google.gerrit.extensions.restapi.RestApiException; import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.TopLevelResource; import com.google.gerrit.extensions.restapi.TopLevelResource;
import com.google.gerrit.reviewdb.client.AccountGroup; import com.google.gerrit.reviewdb.client.AccountGroup;
@@ -67,7 +68,7 @@ final class CreateAccountCommand extends SshCommand {
@Override @Override
protected void run() throws OrmException, IOException, ConfigInvalidException, protected void run() throws OrmException, IOException, ConfigInvalidException,
UnloggedFailure { UnloggedFailure {
CreateAccount.Input input = new CreateAccount.Input(); AccountInput input = new AccountInput();
input.username = username; input.username = username;
input.email = email; input.email = email;
input.name = fullName; input.name = fullName;