Merge "Support 'self' for REST endpoints to get/add/delete a group member"

This commit is contained in:
Shawn Pearce 2013-03-01 21:35:24 +00:00 committed by Gerrit Code Review
commit 1c830c11e7
5 changed files with 53 additions and 41 deletions

View File

@ -961,17 +961,10 @@ IDs
---
[[account-id]]
\{account-id\}
~~~~~~~~~~~~~~
Identifier that uniquely identifies one account.
This can be:
* a string of the format "Full Name <email@example.com>"
* just the email address ("email@example")
* a full name if it is unique ("Full Name")
* an account ID ("18419")
* a user name ("username")
link:rest-api-accounts.html#account-id[\{account-id\}]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
--
[[group-id]]
\{group-id\}

View File

@ -56,11 +56,16 @@ public class AccountsCollection implements
@Override
public AccountResource parse(TopLevelResource root, IdString id)
throws ResourceNotFoundException, AuthException, OrmException {
return new AccountResource(parse(id.get()));
}
public IdentifiedUser parse(String id) throws AuthException,
ResourceNotFoundException, OrmException {
CurrentUser user = self.get();
if (id.equals("self")) {
if (user instanceof IdentifiedUser) {
return new AccountResource((IdentifiedUser) user);
return (IdentifiedUser) user;
} else if (user instanceof AnonymousUser) {
throw new AuthException("Authentication required");
} else {
@ -68,7 +73,7 @@ public class AccountsCollection implements
}
}
Set<Account.Id> matches = resolver.findAll(id.get());
Set<Account.Id> matches = resolver.findAll(id);
if (matches.size() != 1) {
throw new ResourceNotFoundException(id);
}
@ -76,7 +81,7 @@ public class AccountsCollection implements
Account.Id a = Iterables.getOnlyElement(matches);
if (accountControlFactory.get().canSee(a)
|| user.getCapabilities().canAdministrateServer()) {
return new AccountResource(userFactory.create(a));
return userFactory.create(a);
} else {
throw new ResourceNotFoundException(id);
}

View File

@ -23,6 +23,7 @@ import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.DefaultInput;
import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountGroup;
@ -36,6 +37,7 @@ import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.AccountException;
import com.google.gerrit.server.account.AccountManager;
import com.google.gerrit.server.account.AccountResolver;
import com.google.gerrit.server.account.AccountsCollection;
import com.google.gerrit.server.account.AuthRequest;
import com.google.gerrit.server.account.GroupControl;
import com.google.gerrit.server.config.AuthConfig;
@ -71,6 +73,7 @@ class AddMembers implements RestModifyView<GroupResource, Input> {
private final AccountManager accountManager;
private final AuthType authType;
private final Provider<AccountsCollection> accounts;
private final AccountResolver accountResolver;
private final AccountCache accountCache;
private final ReviewDb db;
@ -78,11 +81,13 @@ class AddMembers implements RestModifyView<GroupResource, Input> {
@Inject
AddMembers(AccountManager accountManager,
AuthConfig authConfig,
Provider<AccountsCollection> accounts,
AccountResolver accountResolver,
AccountCache accountCache,
ReviewDb db) {
this.accountManager = accountManager;
this.authType = authConfig.getAuthType();
this.accounts = accounts;
this.accountResolver = accountResolver;
this.accountCache = accountCache;
this.db = db;
@ -146,18 +151,25 @@ class AddMembers implements RestModifyView<GroupResource, Input> {
}
private Account findAccount(String nameOrEmail) throws OrmException {
Account r = accountResolver.find(nameOrEmail);
if (r == null) {
try {
return accounts.get().parse(nameOrEmail).getAccount();
} catch (AuthException e) {
return null;
} catch (ResourceNotFoundException e) {
// might be because the account does not exist or because the account is not visible
switch (authType) {
case HTTP_LDAP:
case CLIENT_SSL_CERT_LDAP:
case LDAP:
r = createAccountByLdap(nameOrEmail);
if (accountResolver.find(nameOrEmail) == null) {
// account does not exist, try to create it
return createAccountByLdap(nameOrEmail);
}
break;
default:
}
return null;
}
return r;
}
private Account createAccountByLdap(String user) {

View File

@ -21,6 +21,7 @@ import com.google.gerrit.common.errors.NoSuchGroupException;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.reviewdb.client.Account;
@ -32,7 +33,7 @@ import com.google.gerrit.server.BadRequestHandler;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.AccountResolver;
import com.google.gerrit.server.account.AccountsCollection;
import com.google.gerrit.server.account.GroupControl;
import com.google.gerrit.server.group.AddMembers.Input;
import com.google.gwtorm.server.OrmException;
@ -43,15 +44,15 @@ import java.util.List;
import java.util.Map;
public class DeleteMembers implements RestModifyView<GroupResource, Input> {
private final AccountResolver accountResolver;
private final Provider<AccountsCollection> accounts;
private final AccountCache accountCache;
private final ReviewDb db;
private final Provider<CurrentUser> self;
@Inject
DeleteMembers(AccountResolver accountResolver, AccountCache accountCache,
ReviewDb db, Provider<CurrentUser> self) {
this.accountResolver = accountResolver;
DeleteMembers(Provider<AccountsCollection> accounts,
AccountCache accountCache, ReviewDb db, Provider<CurrentUser> self) {
this.accounts = accounts;
this.accountCache = accountCache;
this.db = db;
this.self = self;
@ -73,7 +74,7 @@ public class DeleteMembers implements RestModifyView<GroupResource, Input> {
final BadRequestHandler badRequest = new BadRequestHandler("removing group members");
for (final String nameOrEmail : input.members) {
Account a = accountResolver.find(nameOrEmail);
Account a = parse(nameOrEmail);
if (a == null) {
badRequest.addError(new NoSuchAccountException(nameOrEmail));
continue;
@ -100,6 +101,16 @@ public class DeleteMembers implements RestModifyView<GroupResource, Input> {
return Response.none();
}
private Account parse(String nameOrEmail) throws OrmException {
try {
return accounts.get().parse(nameOrEmail).getAccount();
} catch (AuthException e) {
return null;
} catch (ResourceNotFoundException e) {
return null;
}
}
private void writeAudits(final List<AccountGroupMember> toBeRemoved)
throws OrmException {
final Account.Id me = ((IdentifiedUser) self.get()).getAccountId();

View File

@ -26,7 +26,7 @@ import com.google.gerrit.reviewdb.client.Account;
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.AccountResolver;
import com.google.gerrit.server.account.AccountsCollection;
import com.google.gerrit.server.group.AddMembers.PutMember;
import com.google.inject.Inject;
import com.google.inject.Provider;
@ -36,22 +36,19 @@ public class MembersCollection implements
AcceptsCreate<GroupResource> {
private final DynamicMap<RestView<MemberResource>> views;
private final Provider<ListMembers> list;
private final IdentifiedUser.GenericFactory userGenericFactory;
private final AccountResolver accountResolver;
private final Provider<AccountsCollection> accounts;
private final Provider<ReviewDb> db;
private final Provider<AddMembers> put;
@Inject
MembersCollection(DynamicMap<RestView<MemberResource>> views,
Provider<ListMembers> list,
IdentifiedUser.GenericFactory userGenericFactory,
AccountResolver accountResolver,
Provider<AccountsCollection> accounts,
Provider<ReviewDb> db,
Provider<AddMembers> put) {
this.views = views;
this.list = list;
this.userGenericFactory = userGenericFactory;
this.accountResolver = accountResolver;
this.accounts = accounts;
this.db = db;
this.put = put;
}
@ -69,17 +66,11 @@ public class MembersCollection implements
throw new ResourceNotFoundException(id);
}
Account a = accountResolver.find(id.get());
if (a == null) {
throw new ResourceNotFoundException(id);
}
AccountGroupMember.Key key = new AccountGroupMember.Key(
a.getId(),
parent.toAccountGroup().getId());
IdentifiedUser user = accounts.get().parse(id.get());
AccountGroupMember.Key key =
new AccountGroupMember.Key(user.getAccountId(), parent.toAccountGroup().getId());
if (db.get().accountGroupMembers().get(key) != null
&& parent.getControl().canSeeMember(a.getId())) {
IdentifiedUser user = userGenericFactory.create(a.getId());
&& parent.getControl().canSeeMember(user.getAccountId())) {
return new MemberResource(parent, user);
}
throw new ResourceNotFoundException(id);