| // Copyright 2008 Google Inc. |
| // |
| // 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; |
| |
| import com.google.gerrit.client.account.AccountSecurity; |
| import com.google.gerrit.client.reviewdb.Account; |
| import com.google.gerrit.client.reviewdb.AccountAgreement; |
| import com.google.gerrit.client.reviewdb.AccountExternalId; |
| import com.google.gerrit.client.reviewdb.AccountSshKey; |
| import com.google.gerrit.client.reviewdb.ContactInformation; |
| import com.google.gerrit.client.reviewdb.ContributorAgreement; |
| import com.google.gerrit.client.reviewdb.ReviewDb; |
| import com.google.gerrit.client.rpc.BaseServiceImplementation; |
| import com.google.gerrit.client.rpc.Common; |
| import com.google.gerrit.client.rpc.NoSuchEntityException; |
| import com.google.gerrit.server.ssh.SshUtil; |
| import com.google.gwt.user.client.rpc.AsyncCallback; |
| import com.google.gwtjsonrpc.client.VoidResult; |
| import com.google.gwtorm.client.OrmException; |
| import com.google.gwtorm.client.Transaction; |
| |
| import java.security.NoSuchAlgorithmException; |
| import java.security.NoSuchProviderException; |
| import java.security.spec.InvalidKeySpecException; |
| import java.util.Collections; |
| import java.util.List; |
| import java.util.Set; |
| |
| public class AccountSecurityImpl extends BaseServiceImplementation implements |
| AccountSecurity { |
| public void mySshKeys(final AsyncCallback<List<AccountSshKey>> callback) { |
| run(callback, new Action<List<AccountSshKey>>() { |
| public List<AccountSshKey> run(ReviewDb db) throws OrmException { |
| return db.accountSshKeys().byAccount(Common.getAccountId()).toList(); |
| } |
| }); |
| } |
| |
| public void addSshKey(final String keyText, |
| final AsyncCallback<AccountSshKey> callback) { |
| run(callback, new Action<AccountSshKey>() { |
| public AccountSshKey run(final ReviewDb db) throws OrmException { |
| int max = 0; |
| final Account.Id me = Common.getAccountId(); |
| for (final AccountSshKey k : db.accountSshKeys().byAccount(me)) { |
| max = Math.max(max, k.getKey().get()); |
| } |
| |
| String keyStr = keyText; |
| if (keyStr.startsWith("---- BEGIN SSH2 PUBLIC KEY ----")) { |
| keyStr = SshUtil.toOpenSshPublicKey(keyStr); |
| } |
| |
| final AccountSshKey newKey = |
| new AccountSshKey(new AccountSshKey.Id(me, max + 1), keyStr); |
| try { |
| SshUtil.parse(newKey); |
| } catch (NoSuchAlgorithmException e) { |
| newKey.setInvalid(); |
| } catch (InvalidKeySpecException e) { |
| newKey.setInvalid(); |
| } catch (NoSuchProviderException e) { |
| newKey.setInvalid(); |
| } |
| db.accountSshKeys().insert(Collections.singleton(newKey)); |
| SshUtil.invalidate(Common.getAccountCache().get(me, db)); |
| return newKey; |
| } |
| }); |
| } |
| |
| public void deleteSshKeys(final Set<AccountSshKey.Id> ids, |
| final AsyncCallback<VoidResult> callback) { |
| run(callback, new Action<VoidResult>() { |
| public VoidResult run(final ReviewDb db) throws OrmException, Failure { |
| final Account.Id me = Common.getAccountId(); |
| for (final AccountSshKey.Id keyId : ids) { |
| if (!me.equals(keyId.getParentKey())) |
| throw new Failure(new NoSuchEntityException()); |
| } |
| |
| final List<AccountSshKey> k = db.accountSshKeys().get(ids).toList(); |
| if (!k.isEmpty()) { |
| final Transaction txn = db.beginTransaction(); |
| db.accountSshKeys().delete(k, txn); |
| txn.commit(); |
| SshUtil.invalidate(Common.getAccountCache().get(me, db)); |
| } |
| |
| return VoidResult.INSTANCE; |
| } |
| }); |
| } |
| |
| public void myExternalIds(AsyncCallback<List<AccountExternalId>> callback) { |
| run(callback, new Action<List<AccountExternalId>>() { |
| public List<AccountExternalId> run(ReviewDb db) throws OrmException { |
| final Account.Id me = Common.getAccountId(); |
| return db.accountExternalIds().byAccount(me).toList(); |
| } |
| }); |
| } |
| |
| public void updateContact(final String fullName, final String emailAddr, |
| final ContactInformation info, final AsyncCallback<VoidResult> callback) { |
| run(callback, new Action<VoidResult>() { |
| public VoidResult run(ReviewDb db) throws OrmException { |
| final Account me = db.accounts().get(Common.getAccountId()); |
| me.setFullName(fullName); |
| me.setPreferredEmail(emailAddr); |
| me.setContactInformation(info); |
| db.accounts().update(Collections.singleton(me)); |
| Common.getAccountCache().invalidate(me.getId()); |
| return VoidResult.INSTANCE; |
| } |
| }); |
| } |
| |
| public void enterAgreement(final ContributorAgreement.Id id, |
| final AsyncCallback<VoidResult> callback) { |
| run(callback, new Action<VoidResult>() { |
| public VoidResult run(final ReviewDb db) throws OrmException, Failure { |
| final ContributorAgreement cla = db.contributorAgreements().get(id); |
| if (cla == null || !cla.isActive()) { |
| throw new Failure(new NoSuchEntityException()); |
| } |
| |
| final AccountAgreement a = |
| new AccountAgreement(new AccountAgreement.Key( |
| Common.getAccountId(), id)); |
| if (cla.isAutoVerify()) { |
| a.review(AccountAgreement.Status.VERIFIED, null); |
| } |
| db.accountAgreements().insert(Collections.singleton(a)); |
| return VoidResult.INSTANCE; |
| } |
| }); |
| } |
| } |