Line data Source code
1 : // Copyright (C) 2014 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.api.accounts; 16 : 17 : import static com.google.gerrit.server.api.ApiUtil.asRestApiException; 18 : import static java.util.Objects.requireNonNull; 19 : 20 : import com.google.gerrit.extensions.api.accounts.AccountApi; 21 : import com.google.gerrit.extensions.api.accounts.AccountInput; 22 : import com.google.gerrit.extensions.api.accounts.Accounts; 23 : import com.google.gerrit.extensions.client.ListAccountsOption; 24 : import com.google.gerrit.extensions.common.AccountInfo; 25 : import com.google.gerrit.extensions.restapi.AuthException; 26 : import com.google.gerrit.extensions.restapi.BadRequestException; 27 : import com.google.gerrit.extensions.restapi.IdString; 28 : import com.google.gerrit.extensions.restapi.RestApiException; 29 : import com.google.gerrit.extensions.restapi.TopLevelResource; 30 : import com.google.gerrit.server.CurrentUser; 31 : import com.google.gerrit.server.account.AccountResource; 32 : import com.google.gerrit.server.permissions.GlobalPermission; 33 : import com.google.gerrit.server.permissions.PermissionBackend; 34 : import com.google.gerrit.server.restapi.account.AccountsCollection; 35 : import com.google.gerrit.server.restapi.account.CreateAccount; 36 : import com.google.gerrit.server.restapi.account.QueryAccounts; 37 : import com.google.inject.Inject; 38 : import com.google.inject.Provider; 39 : import com.google.inject.Singleton; 40 : import java.util.List; 41 : 42 : @Singleton 43 : public class AccountsImpl implements Accounts { 44 : private final AccountsCollection accounts; 45 : private final AccountApiImpl.Factory api; 46 : private final PermissionBackend permissionBackend; 47 : private final Provider<CurrentUser> self; 48 : private final CreateAccount createAccount; 49 : private final Provider<QueryAccounts> queryAccountsProvider; 50 : 51 : @Inject 52 : AccountsImpl( 53 : AccountsCollection accounts, 54 : AccountApiImpl.Factory api, 55 : PermissionBackend permissionBackend, 56 : Provider<CurrentUser> self, 57 : CreateAccount createAccount, 58 149 : Provider<QueryAccounts> queryAccountsProvider) { 59 149 : this.accounts = accounts; 60 149 : this.api = api; 61 149 : this.permissionBackend = permissionBackend; 62 149 : this.self = self; 63 149 : this.createAccount = createAccount; 64 149 : this.queryAccountsProvider = queryAccountsProvider; 65 149 : } 66 : 67 : @Override 68 : public AccountApi id(String id) throws RestApiException { 69 : try { 70 27 : return api.create(accounts.parse(TopLevelResource.INSTANCE, IdString.fromDecoded(id))); 71 9 : } catch (Exception e) { 72 9 : throw asRestApiException("Cannot parse account", e); 73 : } 74 : } 75 : 76 : @Override 77 : public AccountApi id(int id) throws RestApiException { 78 24 : return id(String.valueOf(id)); 79 : } 80 : 81 : @Override 82 : public AccountApi self() throws RestApiException { 83 21 : if (!self.get().isIdentifiedUser()) { 84 1 : throw new AuthException("Authentication required"); 85 : } 86 21 : return api.create(new AccountResource(self.get().asIdentifiedUser())); 87 : } 88 : 89 : @Override 90 : public AccountApi create(String username) throws RestApiException { 91 2 : AccountInput in = new AccountInput(); 92 2 : in.username = username; 93 2 : return create(in); 94 : } 95 : 96 : @Override 97 : public AccountApi create(AccountInput in) throws RestApiException { 98 4 : if (requireNonNull(in, "AccountInput").username == null) { 99 0 : throw new BadRequestException("AccountInput must specify username"); 100 : } 101 : try { 102 4 : permissionBackend 103 4 : .currentUser() 104 4 : .checkAny(GlobalPermission.fromAnnotation(createAccount.getClass())); 105 4 : AccountInfo info = 106 : createAccount 107 4 : .apply(TopLevelResource.INSTANCE, IdString.fromDecoded(in.username), in) 108 4 : .value(); 109 4 : return id(info._accountId); 110 1 : } catch (Exception e) { 111 1 : throw asRestApiException("Cannot create account " + in.username, e); 112 : } 113 : } 114 : 115 : @Override 116 : public SuggestAccountsRequest suggestAccounts() throws RestApiException { 117 1 : return new SuggestAccountsRequest() { 118 : @Override 119 : public List<AccountInfo> get() throws RestApiException { 120 1 : return AccountsImpl.this.suggestAccounts(this); 121 : } 122 : }; 123 : } 124 : 125 : @Override 126 : public SuggestAccountsRequest suggestAccounts(String query) throws RestApiException { 127 1 : return suggestAccounts().withQuery(query); 128 : } 129 : 130 : private List<AccountInfo> suggestAccounts(SuggestAccountsRequest r) throws RestApiException { 131 : try { 132 1 : QueryAccounts myQueryAccounts = queryAccountsProvider.get(); 133 1 : myQueryAccounts.setSuggest(true); 134 1 : myQueryAccounts.setQuery(r.getQuery()); 135 1 : myQueryAccounts.setLimit(r.getLimit()); 136 1 : return myQueryAccounts.apply(TopLevelResource.INSTANCE).value(); 137 0 : } catch (Exception e) { 138 0 : throw asRestApiException("Cannot retrieve suggested accounts", e); 139 : } 140 : } 141 : 142 : @Override 143 : public QueryRequest query() throws RestApiException { 144 5 : return new QueryRequest() { 145 : @Override 146 : public List<AccountInfo> get() throws RestApiException { 147 5 : return AccountsImpl.this.query(this); 148 : } 149 : }; 150 : } 151 : 152 : @Override 153 : public QueryRequest query(String query) throws RestApiException { 154 5 : return query().withQuery(query); 155 : } 156 : 157 : private List<AccountInfo> query(QueryRequest r) throws RestApiException { 158 : try { 159 5 : QueryAccounts myQueryAccounts = queryAccountsProvider.get(); 160 5 : myQueryAccounts.setQuery(r.getQuery()); 161 5 : myQueryAccounts.setLimit(r.getLimit()); 162 5 : myQueryAccounts.setStart(r.getStart()); 163 5 : myQueryAccounts.setSuggest(r.getSuggest()); 164 5 : for (ListAccountsOption option : r.getOptions()) { 165 2 : myQueryAccounts.addOption(option); 166 2 : } 167 5 : return myQueryAccounts.apply(TopLevelResource.INSTANCE).value(); 168 2 : } catch (Exception e) { 169 2 : throw asRestApiException("Cannot retrieve suggested accounts", e); 170 : } 171 : } 172 : }