use of com.google.gerrit.server.account.Realm in project gerrit by GerritCodeReview.
the class DeleteEmail method apply.
public Response<?> apply(IdentifiedUser user, String email) throws ResourceNotFoundException, ResourceConflictException, MethodNotAllowedException, IOException, ConfigInvalidException {
Account.Id accountId = user.getAccountId();
if (realm.accountBelongsToRealm(externalIds.byAccount(accountId)) && !realm.allowsEdit(AccountFieldName.REGISTER_NEW_EMAIL)) {
throw new MethodNotAllowedException("realm does not allow deleting emails");
}
Set<ExternalId> extIds = externalIds.byAccount(accountId).stream().filter(e -> email.equals(e.email())).collect(toSet());
if (extIds.isEmpty()) {
throw new ResourceNotFoundException(email);
}
if (realm.accountBelongsToRealm(extIds)) {
String errorMsg = String.format("Cannot remove e-mail '%s' which is directly associated with %s authentication", email, authType);
throw new ResourceConflictException(errorMsg);
}
try {
accountManager.unlink(user.getAccountId(), extIds.stream().map(ExternalId::key).collect(toSet()));
} catch (AccountException e) {
throw new ResourceConflictException(e.getMessage());
}
return Response.none();
}
use of com.google.gerrit.server.account.Realm in project gerrit by GerritCodeReview.
the class PutName method apply.
public Response<String> apply(IdentifiedUser user, NameInput input) throws MethodNotAllowedException, ResourceNotFoundException, IOException, ConfigInvalidException {
if (input == null) {
input = new NameInput();
}
Account.Id accountId = user.getAccountId();
if (realm.accountBelongsToRealm(externalIds.byAccount(accountId)) && !realm.allowsEdit(AccountFieldName.FULL_NAME)) {
throw new MethodNotAllowedException("realm does not allow editing name");
}
String newName = input.name;
AccountState accountState = accountsUpdateProvider.get().update("Set Full Name via API", accountId, u -> u.setFullName(newName)).orElseThrow(() -> new ResourceNotFoundException("account not found"));
return Strings.isNullOrEmpty(accountState.account().fullName()) ? Response.none() : Response.ok(accountState.account().fullName());
}
use of com.google.gerrit.server.account.Realm in project gerrit by GerritCodeReview.
the class IdentifiedUserTest method setUp.
@Before
public void setUp() throws Exception {
final FakeAccountCache accountCache = new FakeAccountCache();
final Realm mockRealm = new FakeRealm() {
HashSet<String> emails = new HashSet<>(Arrays.asList(TEST_CASES));
@Override
public boolean hasEmailAddress(IdentifiedUser who, String email) {
return emails.contains(email);
}
@Override
public Set<String> getEmailAddresses(IdentifiedUser who) {
return emails;
}
};
AbstractModule mod = new AbstractModule() {
@Override
protected void configure() {
bind(Boolean.class).annotatedWith(EnablePeerIPInReflogRecord.class).toInstance(Boolean.FALSE);
bind(Config.class).annotatedWith(GerritServerConfig.class).toInstance(config);
bind(String.class).annotatedWith(AnonymousCowardName.class).toProvider(AnonymousCowardNameProvider.class);
bind(String.class).annotatedWith(CanonicalWebUrl.class).toInstance("http://localhost:8080/");
bind(AccountCache.class).toInstance(accountCache);
bind(GroupBackend.class).to(SystemGroupBackend.class).in(SINGLETON);
bind(Realm.class).toInstance(mockRealm);
}
};
Injector injector = Guice.createInjector(mod);
injector.injectMembers(this);
Account account = Account.builder(Account.id(1), TimeUtil.now()).setMetaId("1234567812345678123456781234567812345678").build();
Account.Id ownerId = account.id();
identifiedUser = identifiedUserFactory.create(ownerId);
/* Trigger identifiedUser to load the email addresses from mockRealm */
identifiedUser.getEmailAddresses();
}
Aggregations