Support setting an email address as preferred email address via REST
It is now possible to set an email address as preferred email address for an account by PUT on /accounts/<account-id>/emails/<email-id>/preferred. Change-Id: I4dcc393174bfa04cca4e4a240d98c89e3b3cc6bb Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
parent
fd02b28c49
commit
e1f8288fc7
@ -224,6 +224,27 @@ describes the email address.
|
|||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
[[set-preferred-email]]
|
||||||
|
Set Preferred Email
|
||||||
|
~~~~~~~~~~~~~~~~~~~
|
||||||
|
[verse]
|
||||||
|
'PUT /accounts/link:#account-id[\{account-id\}]/emails/link:#email-id[\{email-id\}]/preferred'
|
||||||
|
|
||||||
|
Sets an email address as preferred email address for an account.
|
||||||
|
|
||||||
|
.Request
|
||||||
|
----
|
||||||
|
PUT /accounts/self/emails/john.doe@example.com/preferred HTTP/1.0
|
||||||
|
----
|
||||||
|
|
||||||
|
.Response
|
||||||
|
----
|
||||||
|
HTTP/1.1 201 Created
|
||||||
|
----
|
||||||
|
|
||||||
|
If the email address was already the preferred email address of the
|
||||||
|
account the response is "`200 OK`".
|
||||||
|
|
||||||
[[list-account-capabilities]]
|
[[list-account-capabilities]]
|
||||||
List Account Capabilities
|
List Account Capabilities
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
@ -39,6 +39,7 @@ public class Module extends RestApiModule {
|
|||||||
delete(ACCOUNT_KIND, "name").to(PutName.class);
|
delete(ACCOUNT_KIND, "name").to(PutName.class);
|
||||||
child(ACCOUNT_KIND, "emails").to(Emails.class);
|
child(ACCOUNT_KIND, "emails").to(Emails.class);
|
||||||
get(EMAIL_KIND).to(GetEmail.class);
|
get(EMAIL_KIND).to(GetEmail.class);
|
||||||
|
put(EMAIL_KIND, "preferred").to(PutPreferred.class);
|
||||||
get(ACCOUNT_KIND, "avatar").to(GetAvatar.class);
|
get(ACCOUNT_KIND, "avatar").to(GetAvatar.class);
|
||||||
get(ACCOUNT_KIND, "avatar.change.url").to(GetAvatarChangeUrl.class);
|
get(ACCOUNT_KIND, "avatar.change.url").to(GetAvatarChangeUrl.class);
|
||||||
child(ACCOUNT_KIND, "capabilities").to(Capabilities.class);
|
child(ACCOUNT_KIND, "capabilities").to(Capabilities.class);
|
||||||
|
@ -0,0 +1,70 @@
|
|||||||
|
// Copyright (C) 2013 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.account;
|
||||||
|
|
||||||
|
import com.google.gerrit.extensions.restapi.AuthException;
|
||||||
|
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;
|
||||||
|
import com.google.gerrit.reviewdb.server.ReviewDb;
|
||||||
|
import com.google.gerrit.server.CurrentUser;
|
||||||
|
import com.google.gerrit.server.IdentifiedUser;
|
||||||
|
import com.google.gerrit.server.account.PutPreferred.Input;
|
||||||
|
import com.google.gwtorm.server.OrmException;
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import com.google.inject.Provider;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
public class PutPreferred implements
|
||||||
|
RestModifyView<AccountResource.Email, Input> {
|
||||||
|
static class Input {
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Provider<CurrentUser> self;
|
||||||
|
private final Provider<ReviewDb> dbProvider;
|
||||||
|
private final AccountCache byIdCache;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
PutPreferred(Provider<CurrentUser> self, Provider<ReviewDb> dbProvider,
|
||||||
|
AccountCache byIdCache) {
|
||||||
|
this.self = self;
|
||||||
|
this.dbProvider = dbProvider;
|
||||||
|
this.byIdCache = byIdCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object apply(AccountResource.Email rsrc, Input input)
|
||||||
|
throws AuthException, ResourceNotFoundException, OrmException {
|
||||||
|
IdentifiedUser s = (IdentifiedUser) self.get();
|
||||||
|
if (s.getAccountId().get() != rsrc.getUser().getAccountId().get()
|
||||||
|
&& !self.get().getCapabilities().canAdministrateServer()) {
|
||||||
|
throw new AuthException("not allowed to set preferred email address");
|
||||||
|
}
|
||||||
|
Account a = dbProvider.get().accounts().get(rsrc.getUser().getAccountId());
|
||||||
|
if (a == null) {
|
||||||
|
throw new ResourceNotFoundException("No such account: "
|
||||||
|
+ rsrc.getUser().getAccountId());
|
||||||
|
}
|
||||||
|
if (rsrc.getEmail().equals(a.getPreferredEmail())) {
|
||||||
|
return Response.ok("");
|
||||||
|
}
|
||||||
|
a.setPreferredEmail(rsrc.getEmail());
|
||||||
|
dbProvider.get().accounts().update(Collections.singleton(a));
|
||||||
|
byIdCache.evict(a.getId());
|
||||||
|
return Response.created("");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user