Remove unneeded use of AccountCache from Set*Preferences REST endpoints
As result of the account update we get the updated AccountState back. There is no need to retrieve it from the account cache. If the account update returns an empty Optional it means that the account couldn't be updated because it was missing. Throw an exception in this case. Change-Id: Icdc23cc4abdb827d696f7c5e019421cdb9fab988 Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
@@ -15,13 +15,15 @@
|
|||||||
package com.google.gerrit.server.restapi.account;
|
package com.google.gerrit.server.restapi.account;
|
||||||
|
|
||||||
import com.google.gerrit.extensions.client.DiffPreferencesInfo;
|
import com.google.gerrit.extensions.client.DiffPreferencesInfo;
|
||||||
import com.google.gerrit.extensions.restapi.AuthException;
|
|
||||||
import com.google.gerrit.extensions.restapi.BadRequestException;
|
import com.google.gerrit.extensions.restapi.BadRequestException;
|
||||||
|
import com.google.gerrit.extensions.restapi.IdString;
|
||||||
|
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||||
|
import com.google.gerrit.extensions.restapi.RestApiException;
|
||||||
import com.google.gerrit.extensions.restapi.RestModifyView;
|
import com.google.gerrit.extensions.restapi.RestModifyView;
|
||||||
import com.google.gerrit.reviewdb.client.Account;
|
import com.google.gerrit.reviewdb.client.Account;
|
||||||
import com.google.gerrit.server.CurrentUser;
|
import com.google.gerrit.server.CurrentUser;
|
||||||
import com.google.gerrit.server.account.AccountCache;
|
|
||||||
import com.google.gerrit.server.account.AccountResource;
|
import com.google.gerrit.server.account.AccountResource;
|
||||||
|
import com.google.gerrit.server.account.AccountState;
|
||||||
import com.google.gerrit.server.account.AccountsUpdate;
|
import com.google.gerrit.server.account.AccountsUpdate;
|
||||||
import com.google.gerrit.server.permissions.GlobalPermission;
|
import com.google.gerrit.server.permissions.GlobalPermission;
|
||||||
import com.google.gerrit.server.permissions.PermissionBackend;
|
import com.google.gerrit.server.permissions.PermissionBackend;
|
||||||
@@ -38,25 +40,22 @@ import org.eclipse.jgit.errors.RepositoryNotFoundException;
|
|||||||
public class SetDiffPreferences implements RestModifyView<AccountResource, DiffPreferencesInfo> {
|
public class SetDiffPreferences implements RestModifyView<AccountResource, DiffPreferencesInfo> {
|
||||||
private final Provider<CurrentUser> self;
|
private final Provider<CurrentUser> self;
|
||||||
private final PermissionBackend permissionBackend;
|
private final PermissionBackend permissionBackend;
|
||||||
private final AccountCache accountCache;
|
|
||||||
private final AccountsUpdate.User accountsUpdate;
|
private final AccountsUpdate.User accountsUpdate;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
SetDiffPreferences(
|
SetDiffPreferences(
|
||||||
Provider<CurrentUser> self,
|
Provider<CurrentUser> self,
|
||||||
PermissionBackend permissionBackend,
|
PermissionBackend permissionBackend,
|
||||||
AccountCache accountCache,
|
|
||||||
AccountsUpdate.User accountsUpdate) {
|
AccountsUpdate.User accountsUpdate) {
|
||||||
this.self = self;
|
this.self = self;
|
||||||
this.permissionBackend = permissionBackend;
|
this.permissionBackend = permissionBackend;
|
||||||
this.accountCache = accountCache;
|
|
||||||
this.accountsUpdate = accountsUpdate;
|
this.accountsUpdate = accountsUpdate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DiffPreferencesInfo apply(AccountResource rsrc, DiffPreferencesInfo input)
|
public DiffPreferencesInfo apply(AccountResource rsrc, DiffPreferencesInfo input)
|
||||||
throws AuthException, BadRequestException, ConfigInvalidException,
|
throws RestApiException, ConfigInvalidException, RepositoryNotFoundException, IOException,
|
||||||
RepositoryNotFoundException, IOException, PermissionBackendException, OrmException {
|
PermissionBackendException, OrmException {
|
||||||
if (self.get() != rsrc.getUser()) {
|
if (self.get() != rsrc.getUser()) {
|
||||||
permissionBackend.user(self).check(GlobalPermission.MODIFY_ACCOUNT);
|
permissionBackend.user(self).check(GlobalPermission.MODIFY_ACCOUNT);
|
||||||
}
|
}
|
||||||
@@ -66,9 +65,10 @@ public class SetDiffPreferences implements RestModifyView<AccountResource, DiffP
|
|||||||
}
|
}
|
||||||
|
|
||||||
Account.Id id = rsrc.getUser().getAccountId();
|
Account.Id id = rsrc.getUser().getAccountId();
|
||||||
accountsUpdate
|
return accountsUpdate
|
||||||
.create()
|
.create()
|
||||||
.update("Set Diff Preferences via API", id, u -> u.setDiffPreferences(input));
|
.update("Set Diff Preferences via API", id, u -> u.setDiffPreferences(input))
|
||||||
return accountCache.get(id).getDiffPreferences();
|
.map(AccountState::getDiffPreferences)
|
||||||
|
.orElseThrow(() -> new ResourceNotFoundException(IdString.fromDecoded(id.toString())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,13 +15,15 @@
|
|||||||
package com.google.gerrit.server.restapi.account;
|
package com.google.gerrit.server.restapi.account;
|
||||||
|
|
||||||
import com.google.gerrit.extensions.client.EditPreferencesInfo;
|
import com.google.gerrit.extensions.client.EditPreferencesInfo;
|
||||||
import com.google.gerrit.extensions.restapi.AuthException;
|
|
||||||
import com.google.gerrit.extensions.restapi.BadRequestException;
|
import com.google.gerrit.extensions.restapi.BadRequestException;
|
||||||
|
import com.google.gerrit.extensions.restapi.IdString;
|
||||||
|
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||||
|
import com.google.gerrit.extensions.restapi.RestApiException;
|
||||||
import com.google.gerrit.extensions.restapi.RestModifyView;
|
import com.google.gerrit.extensions.restapi.RestModifyView;
|
||||||
import com.google.gerrit.reviewdb.client.Account;
|
import com.google.gerrit.reviewdb.client.Account;
|
||||||
import com.google.gerrit.server.CurrentUser;
|
import com.google.gerrit.server.CurrentUser;
|
||||||
import com.google.gerrit.server.account.AccountCache;
|
|
||||||
import com.google.gerrit.server.account.AccountResource;
|
import com.google.gerrit.server.account.AccountResource;
|
||||||
|
import com.google.gerrit.server.account.AccountState;
|
||||||
import com.google.gerrit.server.account.AccountsUpdate;
|
import com.google.gerrit.server.account.AccountsUpdate;
|
||||||
import com.google.gerrit.server.permissions.GlobalPermission;
|
import com.google.gerrit.server.permissions.GlobalPermission;
|
||||||
import com.google.gerrit.server.permissions.PermissionBackend;
|
import com.google.gerrit.server.permissions.PermissionBackend;
|
||||||
@@ -39,25 +41,22 @@ public class SetEditPreferences implements RestModifyView<AccountResource, EditP
|
|||||||
|
|
||||||
private final Provider<CurrentUser> self;
|
private final Provider<CurrentUser> self;
|
||||||
private final PermissionBackend permissionBackend;
|
private final PermissionBackend permissionBackend;
|
||||||
private final AccountCache accountCache;
|
|
||||||
private final AccountsUpdate.User accountsUpdate;
|
private final AccountsUpdate.User accountsUpdate;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
SetEditPreferences(
|
SetEditPreferences(
|
||||||
Provider<CurrentUser> self,
|
Provider<CurrentUser> self,
|
||||||
PermissionBackend permissionBackend,
|
PermissionBackend permissionBackend,
|
||||||
AccountCache accountCache,
|
|
||||||
AccountsUpdate.User accountsUpdate) {
|
AccountsUpdate.User accountsUpdate) {
|
||||||
this.self = self;
|
this.self = self;
|
||||||
this.permissionBackend = permissionBackend;
|
this.permissionBackend = permissionBackend;
|
||||||
this.accountCache = accountCache;
|
|
||||||
this.accountsUpdate = accountsUpdate;
|
this.accountsUpdate = accountsUpdate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EditPreferencesInfo apply(AccountResource rsrc, EditPreferencesInfo input)
|
public EditPreferencesInfo apply(AccountResource rsrc, EditPreferencesInfo input)
|
||||||
throws AuthException, BadRequestException, RepositoryNotFoundException, IOException,
|
throws RestApiException, RepositoryNotFoundException, IOException, ConfigInvalidException,
|
||||||
ConfigInvalidException, PermissionBackendException, OrmException {
|
PermissionBackendException, OrmException {
|
||||||
if (self.get() != rsrc.getUser()) {
|
if (self.get() != rsrc.getUser()) {
|
||||||
permissionBackend.user(self).check(GlobalPermission.MODIFY_ACCOUNT);
|
permissionBackend.user(self).check(GlobalPermission.MODIFY_ACCOUNT);
|
||||||
}
|
}
|
||||||
@@ -67,9 +66,10 @@ public class SetEditPreferences implements RestModifyView<AccountResource, EditP
|
|||||||
}
|
}
|
||||||
|
|
||||||
Account.Id id = rsrc.getUser().getAccountId();
|
Account.Id id = rsrc.getUser().getAccountId();
|
||||||
accountsUpdate
|
return accountsUpdate
|
||||||
.create()
|
.create()
|
||||||
.update("Set Edit Preferences via API", id, u -> u.setEditPreferences(input));
|
.update("Set Edit Preferences via API", id, u -> u.setEditPreferences(input))
|
||||||
return accountCache.get(id).getEditPreferences();
|
.map(AccountState::getEditPreferences)
|
||||||
|
.orElseThrow(() -> new ResourceNotFoundException(IdString.fromDecoded(id.toString())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,13 +18,15 @@ import com.google.common.base.Strings;
|
|||||||
import com.google.gerrit.extensions.client.GeneralPreferencesInfo;
|
import com.google.gerrit.extensions.client.GeneralPreferencesInfo;
|
||||||
import com.google.gerrit.extensions.config.DownloadScheme;
|
import com.google.gerrit.extensions.config.DownloadScheme;
|
||||||
import com.google.gerrit.extensions.registration.DynamicMap;
|
import com.google.gerrit.extensions.registration.DynamicMap;
|
||||||
import com.google.gerrit.extensions.restapi.AuthException;
|
|
||||||
import com.google.gerrit.extensions.restapi.BadRequestException;
|
import com.google.gerrit.extensions.restapi.BadRequestException;
|
||||||
|
import com.google.gerrit.extensions.restapi.IdString;
|
||||||
|
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||||
|
import com.google.gerrit.extensions.restapi.RestApiException;
|
||||||
import com.google.gerrit.extensions.restapi.RestModifyView;
|
import com.google.gerrit.extensions.restapi.RestModifyView;
|
||||||
import com.google.gerrit.reviewdb.client.Account;
|
import com.google.gerrit.reviewdb.client.Account;
|
||||||
import com.google.gerrit.server.CurrentUser;
|
import com.google.gerrit.server.CurrentUser;
|
||||||
import com.google.gerrit.server.account.AccountCache;
|
|
||||||
import com.google.gerrit.server.account.AccountResource;
|
import com.google.gerrit.server.account.AccountResource;
|
||||||
|
import com.google.gerrit.server.account.AccountState;
|
||||||
import com.google.gerrit.server.account.AccountsUpdate;
|
import com.google.gerrit.server.account.AccountsUpdate;
|
||||||
import com.google.gerrit.server.account.Preferences;
|
import com.google.gerrit.server.account.Preferences;
|
||||||
import com.google.gerrit.server.permissions.GlobalPermission;
|
import com.google.gerrit.server.permissions.GlobalPermission;
|
||||||
@@ -40,7 +42,6 @@ import org.eclipse.jgit.errors.ConfigInvalidException;
|
|||||||
@Singleton
|
@Singleton
|
||||||
public class SetPreferences implements RestModifyView<AccountResource, GeneralPreferencesInfo> {
|
public class SetPreferences implements RestModifyView<AccountResource, GeneralPreferencesInfo> {
|
||||||
private final Provider<CurrentUser> self;
|
private final Provider<CurrentUser> self;
|
||||||
private final AccountCache cache;
|
|
||||||
private final PermissionBackend permissionBackend;
|
private final PermissionBackend permissionBackend;
|
||||||
private final AccountsUpdate.User accountsUpdate;
|
private final AccountsUpdate.User accountsUpdate;
|
||||||
private final DynamicMap<DownloadScheme> downloadSchemes;
|
private final DynamicMap<DownloadScheme> downloadSchemes;
|
||||||
@@ -48,12 +49,10 @@ public class SetPreferences implements RestModifyView<AccountResource, GeneralPr
|
|||||||
@Inject
|
@Inject
|
||||||
SetPreferences(
|
SetPreferences(
|
||||||
Provider<CurrentUser> self,
|
Provider<CurrentUser> self,
|
||||||
AccountCache cache,
|
|
||||||
PermissionBackend permissionBackend,
|
PermissionBackend permissionBackend,
|
||||||
AccountsUpdate.User accountsUpdate,
|
AccountsUpdate.User accountsUpdate,
|
||||||
DynamicMap<DownloadScheme> downloadSchemes) {
|
DynamicMap<DownloadScheme> downloadSchemes) {
|
||||||
this.self = self;
|
this.self = self;
|
||||||
this.cache = cache;
|
|
||||||
this.permissionBackend = permissionBackend;
|
this.permissionBackend = permissionBackend;
|
||||||
this.accountsUpdate = accountsUpdate;
|
this.accountsUpdate = accountsUpdate;
|
||||||
this.downloadSchemes = downloadSchemes;
|
this.downloadSchemes = downloadSchemes;
|
||||||
@@ -61,8 +60,8 @@ public class SetPreferences implements RestModifyView<AccountResource, GeneralPr
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public GeneralPreferencesInfo apply(AccountResource rsrc, GeneralPreferencesInfo input)
|
public GeneralPreferencesInfo apply(AccountResource rsrc, GeneralPreferencesInfo input)
|
||||||
throws AuthException, BadRequestException, IOException, ConfigInvalidException,
|
throws RestApiException, IOException, ConfigInvalidException, PermissionBackendException,
|
||||||
PermissionBackendException, OrmException {
|
OrmException {
|
||||||
if (self.get() != rsrc.getUser()) {
|
if (self.get() != rsrc.getUser()) {
|
||||||
permissionBackend.user(self).check(GlobalPermission.MODIFY_ACCOUNT);
|
permissionBackend.user(self).check(GlobalPermission.MODIFY_ACCOUNT);
|
||||||
}
|
}
|
||||||
@@ -71,10 +70,11 @@ public class SetPreferences implements RestModifyView<AccountResource, GeneralPr
|
|||||||
Preferences.validateMy(input.my);
|
Preferences.validateMy(input.my);
|
||||||
Account.Id id = rsrc.getUser().getAccountId();
|
Account.Id id = rsrc.getUser().getAccountId();
|
||||||
|
|
||||||
accountsUpdate
|
return accountsUpdate
|
||||||
.create()
|
.create()
|
||||||
.update("Set General Preferences via API", id, u -> u.setGeneralPreferences(input));
|
.update("Set General Preferences via API", id, u -> u.setGeneralPreferences(input))
|
||||||
return cache.get(id).getGeneralPreferences();
|
.map(AccountState::getGeneralPreferences)
|
||||||
|
.orElseThrow(() -> new ResourceNotFoundException(IdString.fromDecoded(id.toString())));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkDownloadScheme(String downloadScheme) throws BadRequestException {
|
private void checkDownloadScheme(String downloadScheme) throws BadRequestException {
|
||||||
|
|||||||
Reference in New Issue
Block a user