Add REST endpoints to get/set default user preferences

GET on /config/server/preferences returns the default user preferences
while PUT on /config/server/preferences allows administrators to set
them.

These REST endpoints only support the options that are stored in the
All-Users repository. This means at the moment only for the 'My' menu
entries a default can be set.

Change-Id: I064dd8baaa798b979938be6cdc2a36ab249bb694
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2014-04-09 08:28:41 +02:00
parent ecdc64f98b
commit a44a3193af
5 changed files with 151 additions and 41 deletions

View File

@@ -76,10 +76,10 @@ public class GetPreferences implements RestReadView<AccountResource> {
rsrc.getUser().getAccountId(), allUsers);
}
static class PreferenceInfo {
public static class PreferenceInfo {
final String kind = "gerritcodereview#preferences";
short changesPerPage;
Short changesPerPage;
Boolean showSiteHeader;
Boolean useFlashClipboard;
DownloadScheme downloadScheme;
@@ -96,8 +96,14 @@ public class GetPreferences implements RestReadView<AccountResource> {
ChangeScreen changeScreen;
List<TopMenu.MenuItem> my;
PreferenceInfo(AccountGeneralPreferences p, Account.Id accountId,
public PreferenceInfo(AccountGeneralPreferences p, Account.Id accountId,
ProjectState allUsers) {
this(p, RefNames.refsUsers(accountId), allUsers);
}
public PreferenceInfo(AccountGeneralPreferences p, String ref,
ProjectState allUsers) {
if (p != null) {
changesPerPage = p.getMaximumPageSize();
showSiteHeader = p.isShowSiteHeader() ? true : null;
useFlashClipboard = p.isUseFlashClipboard() ? true : null;
@@ -113,12 +119,13 @@ public class GetPreferences implements RestReadView<AccountResource> {
commentVisibilityStrategy = p.getCommentVisibilityStrategy();
diffView = p.getDiffView();
changeScreen = p.getChangeScreen();
my = my(accountId, allUsers);
}
my = my(ref, allUsers);
}
private List<TopMenu.MenuItem> my(Account.Id accountId, ProjectState allUsers) {
List<TopMenu.MenuItem> my = my(allUsers, RefNames.refsUsers(accountId));
if (my.isEmpty()) {
private List<TopMenu.MenuItem> my(String ref, ProjectState allUsers) {
List<TopMenu.MenuItem> my = my(allUsers, ref);
if (my.isEmpty() && !ref.equals(RefNames.REFS_USER + "default")) {
my = my(allUsers, RefNames.REFS_USER + "default");
}
if (my.isEmpty()) {

View File

@@ -53,23 +53,23 @@ import java.util.Collections;
import java.util.List;
public class SetPreferences implements RestModifyView<AccountResource, Input> {
static class Input {
Short changesPerPage;
Boolean showSiteHeader;
Boolean useFlashClipboard;
DownloadScheme downloadScheme;
DownloadCommand downloadCommand;
Boolean copySelfOnEmail;
DateFormat dateFormat;
TimeFormat timeFormat;
Boolean reversePatchSetOrder;
Boolean showUsernameInReviewCategory;
Boolean relativeDateInChangeTable;
Boolean sizeBarInChangeTable;
CommentVisibilityStrategy commentVisibilityStrategy;
DiffView diffView;
ChangeScreen changeScreen;
List<TopMenu.MenuItem> my;
public static class Input {
public Short changesPerPage;
public Boolean showSiteHeader;
public Boolean useFlashClipboard;
public DownloadScheme downloadScheme;
public DownloadCommand downloadCommand;
public Boolean copySelfOnEmail;
public DateFormat dateFormat;
public TimeFormat timeFormat;
public Boolean reversePatchSetOrder;
public Boolean showUsernameInReviewCategory;
public Boolean relativeDateInChangeTable;
public Boolean sizeBarInChangeTable;
public CommentVisibilityStrategy commentVisibilityStrategy;
public DiffView diffView;
public ChangeScreen changeScreen;
public List<TopMenu.MenuItem> my;
}
private final Provider<CurrentUser> self;
@@ -173,9 +173,13 @@ public class SetPreferences implements RestModifyView<AccountResource, Input> {
private void storeMyMenus(Account.Id accountId, List<TopMenu.MenuItem> my)
throws IOException {
storeMyMenus(RefNames.refsUsers(accountId), my);
}
public void storeMyMenus(String ref, List<TopMenu.MenuItem> my)
throws IOException {
ProjectLevelConfig prefsCfg =
allUsers.getConfig(PREFERENCES,
RefNames.refsUsers(accountId));
allUsers.getConfig(PREFERENCES, ref);
Config cfg = prefsCfg.get();
if (my != null) {
unsetSection(cfg, MY);

View File

@@ -0,0 +1,36 @@
// Copyright (C) 2014 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.server.config;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.server.account.GetPreferences.PreferenceInfo;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.ProjectState;
import com.google.inject.Inject;
public class GetPreferences implements RestReadView<ConfigResource> {
private final ProjectState allUsers;
@Inject
public GetPreferences(ProjectCache projectCache) {
this.allUsers = projectCache.getAllUsers();
}
@Override
public PreferenceInfo apply(ConfigResource rsrc) {
return new PreferenceInfo(null, RefNames.REFS_USER + "default", allUsers);
}
}

View File

@@ -30,5 +30,7 @@ public class Module extends RestApiModule {
child(CONFIG_KIND, "capabilities").to(CapabilitiesCollection.class);
child(CONFIG_KIND, "top-menus").to(TopMenuCollection.class);
get(CONFIG_KIND, "version").to(GetVersion.class);
get(CONFIG_KIND, "preferences").to(GetPreferences.class);
put(CONFIG_KIND, "preferences").to(SetPreferences.class);
}
}

View File

@@ -0,0 +1,61 @@
// Copyright (C) 2014 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.server.config;
import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.extensions.annotations.RequiresCapability;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.server.account.SetPreferences.Input;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.io.IOException;
@RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
public class SetPreferences implements RestModifyView<ConfigResource, Input> {
private final Provider<com.google.gerrit.server.account.SetPreferences> setPreferences;
private final Provider<GetPreferences> getDefaultPreferences;
@Inject
SetPreferences(
Provider<com.google.gerrit.server.account.SetPreferences> setPreferences,
Provider<GetPreferences> getDefaultPreferences) {
this.setPreferences = setPreferences;
this.getDefaultPreferences = getDefaultPreferences;
}
@Override
public Object apply(ConfigResource rsrc, Input i) throws BadRequestException,
IOException {
if (i.changesPerPage != null || i.showSiteHeader != null
|| i.useFlashClipboard != null || i.downloadScheme != null
|| i.downloadCommand != null || i.copySelfOnEmail != null
|| i.dateFormat != null || i.timeFormat != null
|| i.reversePatchSetOrder != null
|| i.showUsernameInReviewCategory != null
|| i.relativeDateInChangeTable != null
|| i.sizeBarInChangeTable != null
|| i.commentVisibilityStrategy != null || i.diffView != null
|| i.changeScreen != null) {
throw new BadRequestException("unsupported option");
}
if (i.my != null) {
setPreferences.get().storeMyMenus(RefNames.REFS_USER + "default", i.my);
}
return getDefaultPreferences.get().apply(rsrc);
}
}