Accounts API: Add methods to create an account
Change-Id: I798f98384fc513987c00aa1a6102c9967813b542 Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -52,6 +52,12 @@ public interface Accounts {
|
||||
*/
|
||||
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.
|
||||
* <p>
|
||||
@@ -232,6 +238,16 @@ public interface Accounts {
|
||||
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
|
||||
public SuggestAccountsRequest suggestAccounts() throws RestApiException {
|
||||
throw new NotImplementedException();
|
||||
|
||||
@@ -20,10 +20,10 @@ import com.google.gerrit.common.data.GlobalCapability;
|
||||
import com.google.gerrit.common.data.GroupDescriptions;
|
||||
import com.google.gerrit.common.errors.InvalidSshKeyException;
|
||||
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.registration.DynamicSet;
|
||||
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.Response;
|
||||
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.server.ReviewDb;
|
||||
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.group.GroupsCollection;
|
||||
import com.google.gerrit.server.index.account.AccountIndexer;
|
||||
@@ -57,17 +56,8 @@ import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@RequiresCapability(GlobalCapability.CREATE_ACCOUNT)
|
||||
public class CreateAccount implements RestModifyView<TopLevelResource, Input> {
|
||||
public static class Input {
|
||||
@DefaultInput
|
||||
public String username;
|
||||
public String name;
|
||||
public String email;
|
||||
public String sshKey;
|
||||
public String httpPassword;
|
||||
public List<String> groups;
|
||||
}
|
||||
|
||||
public class CreateAccount
|
||||
implements RestModifyView<TopLevelResource, AccountInput> {
|
||||
public interface Factory {
|
||||
CreateAccount create(String username);
|
||||
}
|
||||
@@ -113,12 +103,12 @@ public class CreateAccount implements RestModifyView<TopLevelResource, Input> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response<AccountInfo> apply(TopLevelResource rsrc, Input input)
|
||||
public Response<AccountInfo> apply(TopLevelResource rsrc, AccountInput input)
|
||||
throws BadRequestException, ResourceConflictException,
|
||||
UnprocessableEntityException, OrmException, IOException,
|
||||
ConfigInvalidException {
|
||||
if (input == null) {
|
||||
input = new Input();
|
||||
input = new AccountInput();
|
||||
}
|
||||
if (input.username != null && !username.equals(input.username)) {
|
||||
throw new BadRequestException("username must match URL");
|
||||
|
||||
@@ -14,15 +14,16 @@
|
||||
|
||||
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.RestModifyView;
|
||||
import com.google.gerrit.server.account.CreateAccount.Input;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class PutAccount implements RestModifyView<AccountResource, Input> {
|
||||
public class PutAccount
|
||||
implements RestModifyView<AccountResource, AccountInput> {
|
||||
@Override
|
||||
public Object apply(AccountResource resource, Input input)
|
||||
public Object apply(AccountResource resource, AccountInput input)
|
||||
throws ResourceConflictException {
|
||||
throw new ResourceConflictException("account exists");
|
||||
}
|
||||
|
||||
@@ -14,23 +14,32 @@
|
||||
|
||||
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.AccountInput;
|
||||
import com.google.gerrit.extensions.api.accounts.Accounts;
|
||||
import com.google.gerrit.extensions.client.ListAccountsOption;
|
||||
import com.google.gerrit.extensions.common.AccountInfo;
|
||||
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.RestApiException;
|
||||
import com.google.gerrit.extensions.restapi.TopLevelResource;
|
||||
import com.google.gerrit.server.CurrentUser;
|
||||
import com.google.gerrit.server.account.AccountResource;
|
||||
import com.google.gerrit.server.account.AccountsCollection;
|
||||
import com.google.gerrit.server.account.CreateAccount;
|
||||
import com.google.gerrit.server.account.QueryAccounts;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import org.eclipse.jgit.errors.ConfigInvalidException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@Singleton
|
||||
@@ -38,16 +47,19 @@ public class AccountsImpl implements Accounts {
|
||||
private final AccountsCollection accounts;
|
||||
private final AccountApiImpl.Factory api;
|
||||
private final Provider<CurrentUser> self;
|
||||
private final CreateAccount.Factory createAccount;
|
||||
private final Provider<QueryAccounts> queryAccountsProvider;
|
||||
|
||||
@Inject
|
||||
AccountsImpl(AccountsCollection accounts,
|
||||
AccountApiImpl.Factory api,
|
||||
Provider<CurrentUser> self,
|
||||
CreateAccount.Factory createAccount,
|
||||
Provider<QueryAccounts> queryAccountsProvider) {
|
||||
this.accounts = accounts;
|
||||
this.api = api;
|
||||
this.self = self;
|
||||
this.createAccount = createAccount;
|
||||
this.queryAccountsProvider = queryAccountsProvider;
|
||||
}
|
||||
|
||||
@@ -74,6 +86,28 @@ public class AccountsImpl implements Accounts {
|
||||
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
|
||||
public SuggestAccountsRequest suggestAccounts() throws RestApiException {
|
||||
return new SuggestAccountsRequest() {
|
||||
|
||||
@@ -20,6 +20,7 @@ import com.google.common.base.Function;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.gerrit.common.data.GlobalCapability;
|
||||
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.TopLevelResource;
|
||||
import com.google.gerrit.reviewdb.client.AccountGroup;
|
||||
@@ -67,7 +68,7 @@ final class CreateAccountCommand extends SshCommand {
|
||||
@Override
|
||||
protected void run() throws OrmException, IOException, ConfigInvalidException,
|
||||
UnloggedFailure {
|
||||
CreateAccount.Input input = new CreateAccount.Input();
|
||||
AccountInput input = new AccountInput();
|
||||
input.username = username;
|
||||
input.email = email;
|
||||
input.name = fullName;
|
||||
|
||||
Reference in New Issue
Block a user