Search in sources :

Example 16 with GeneralPreferencesInfo

use of com.google.gerrit.extensions.client.GeneralPreferencesInfo in project gerrit by GerritCodeReview.

the class SetPreferences method apply.

@Override
public GeneralPreferencesInfo apply(AccountResource rsrc, GeneralPreferencesInfo i) throws AuthException, BadRequestException, IOException, ConfigInvalidException, PermissionBackendException {
    if (self.get() != rsrc.getUser()) {
        permissionBackend.user(self).check(GlobalPermission.MODIFY_ACCOUNT);
    }
    checkDownloadScheme(i.downloadScheme);
    Account.Id id = rsrc.getUser().getAccountId();
    GeneralPreferencesInfo n = loader.merge(id, i);
    n.changeTable = i.changeTable;
    n.my = i.my;
    n.urlAliases = i.urlAliases;
    writeToGit(id, n);
    return cache.get(id).getAccount().getGeneralPreferencesInfo();
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) GeneralPreferencesInfo(com.google.gerrit.extensions.client.GeneralPreferencesInfo)

Example 17 with GeneralPreferencesInfo

use of com.google.gerrit.extensions.client.GeneralPreferencesInfo in project gerrit by GerritCodeReview.

the class GeneralPreferencesLoader method read.

private GeneralPreferencesInfo read(Account.Id id, GeneralPreferencesInfo in) throws IOException, ConfigInvalidException, RepositoryNotFoundException {
    try (Repository allUsers = gitMgr.openRepository(allUsersName)) {
        // Load all users default prefs
        VersionedAccountPreferences dp = VersionedAccountPreferences.forDefault();
        dp.load(allUsers);
        // Load user prefs
        VersionedAccountPreferences p = VersionedAccountPreferences.forUser(id);
        p.load(allUsers);
        GeneralPreferencesInfo r = loadSection(p.getConfig(), UserConfigSections.GENERAL, null, new GeneralPreferencesInfo(), readDefaultsFromGit(dp.getConfig(), in), in);
        loadChangeTableColumns(r, p, dp);
        return loadMyMenusAndUrlAliases(r, p, dp);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) GeneralPreferencesInfo(com.google.gerrit.extensions.client.GeneralPreferencesInfo)

Example 18 with GeneralPreferencesInfo

use of com.google.gerrit.extensions.client.GeneralPreferencesInfo in project gerrit by GerritCodeReview.

the class OutgoingEmail method send.

/**
   * Format and enqueue the message for delivery.
   *
   * @throws EmailException
   */
public void send() throws EmailException {
    if (NotifyHandling.NONE.equals(notify) && accountsToNotify.isEmpty()) {
        return;
    }
    if (!args.emailSender.isEnabled()) {
        //
        return;
    }
    init();
    if (useHtml()) {
        appendHtml(soyHtmlTemplate("HeaderHtml"));
    }
    format();
    appendText(textTemplate("Footer"));
    if (useHtml()) {
        appendHtml(soyHtmlTemplate("FooterHtml"));
    }
    Set<Address> smtpRcptToPlaintextOnly = new HashSet<>();
    if (shouldSendMessage()) {
        if (fromId != null) {
            final Account fromUser = args.accountCache.get(fromId).getAccount();
            GeneralPreferencesInfo senderPrefs = fromUser.getGeneralPreferencesInfo();
            if (senderPrefs != null && senderPrefs.getEmailStrategy() == CC_ON_OWN_COMMENTS) {
                // If we are impersonating a user, make sure they receive a CC of
                // this message so they can always review and audit what we sent
                // on their behalf to others.
                //
                add(RecipientType.CC, fromId);
            } else if (!accountsToNotify.containsValue(fromId) && rcptTo.remove(fromId)) {
                // If they don't want a copy, but we queued one up anyway,
                // drop them from the recipient lists.
                //
                removeUser(fromUser);
            }
        }
        // In addition, check if users only want to receive plaintext email.
        for (Account.Id id : rcptTo) {
            Account thisUser = args.accountCache.get(id).getAccount();
            GeneralPreferencesInfo prefs = thisUser.getGeneralPreferencesInfo();
            if (prefs == null || prefs.getEmailStrategy() == DISABLED) {
                removeUser(thisUser);
            } else if (useHtml() && prefs.getEmailFormat() == EmailFormat.PLAINTEXT) {
                removeUser(thisUser);
                smtpRcptToPlaintextOnly.add(new Address(thisUser.getFullName(), thisUser.getPreferredEmail()));
            }
            if (smtpRcptTo.isEmpty() && smtpRcptToPlaintextOnly.isEmpty()) {
                return;
            }
        }
        // inbound email replies.
        if (!headers.containsKey("Reply-To")) {
            StringJoiner j = new StringJoiner(", ");
            if (fromId != null) {
                Address address = toAddress(fromId);
                if (address != null) {
                    j.add(address.getEmail());
                }
            }
            smtpRcptTo.stream().forEach(a -> j.add(a.getEmail()));
            smtpRcptToPlaintextOnly.stream().forEach(a -> j.add(a.getEmail()));
            setHeader("Reply-To", j.toString());
        }
        String textPart = textBody.toString();
        OutgoingEmailValidationListener.Args va = new OutgoingEmailValidationListener.Args();
        va.messageClass = messageClass;
        va.smtpFromAddress = smtpFromAddress;
        va.smtpRcptTo = smtpRcptTo;
        va.headers = headers;
        va.body = textPart;
        if (useHtml()) {
            va.htmlBody = htmlBody.toString();
        } else {
            va.htmlBody = null;
        }
        for (OutgoingEmailValidationListener validator : args.outgoingEmailValidationListeners) {
            try {
                validator.validateOutgoingEmail(va);
            } catch (ValidationException e) {
                return;
            }
        }
        if (!smtpRcptTo.isEmpty()) {
            // Send multipart message
            args.emailSender.send(va.smtpFromAddress, va.smtpRcptTo, va.headers, va.body, va.htmlBody);
        }
        if (!smtpRcptToPlaintextOnly.isEmpty()) {
            // Send plaintext message
            Map<String, EmailHeader> shallowCopy = new HashMap<>();
            shallowCopy.putAll(headers);
            // Remove To and Cc
            shallowCopy.remove(HDR_TO);
            shallowCopy.remove(HDR_CC);
            for (Address a : smtpRcptToPlaintextOnly) {
                // Add new To
                EmailHeader.AddressList to = new EmailHeader.AddressList();
                to.add(a);
                shallowCopy.put(HDR_TO, to);
            }
            args.emailSender.send(va.smtpFromAddress, smtpRcptToPlaintextOnly, shallowCopy, va.body);
        }
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ValidationException(com.google.gerrit.server.validators.ValidationException) Address(com.google.gerrit.server.mail.Address) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) OutgoingEmailValidationListener(com.google.gerrit.server.validators.OutgoingEmailValidationListener) AddressList(com.google.gerrit.server.mail.send.EmailHeader.AddressList) GeneralPreferencesInfo(com.google.gerrit.extensions.client.GeneralPreferencesInfo) AddressList(com.google.gerrit.server.mail.send.EmailHeader.AddressList) StringJoiner(java.util.StringJoiner) HashSet(java.util.HashSet)

Example 19 with GeneralPreferencesInfo

use of com.google.gerrit.extensions.client.GeneralPreferencesInfo in project gerrit by GerritCodeReview.

the class SetPreferences method writeToGit.

private GeneralPreferencesInfo writeToGit(GeneralPreferencesInfo i) throws RepositoryNotFoundException, IOException, ConfigInvalidException {
    try (MetaDataUpdate md = metaDataUpdateFactory.get().create(allUsersName)) {
        VersionedAccountPreferences p = VersionedAccountPreferences.forDefault();
        p.load(md);
        storeSection(p.getConfig(), UserConfigSections.GENERAL, null, i, GeneralPreferencesInfo.defaults());
        com.google.gerrit.server.account.SetPreferences.storeMyMenus(p, i.my);
        com.google.gerrit.server.account.SetPreferences.storeUrlAliases(p, i.urlAliases);
        p.commit(md);
        accountCache.evictAll();
        GeneralPreferencesInfo r = loadSection(p.getConfig(), UserConfigSections.GENERAL, null, new GeneralPreferencesInfo(), GeneralPreferencesInfo.defaults(), null);
        return loader.loadMyMenusAndUrlAliases(r, p, null);
    }
}
Also used : GeneralPreferencesInfo(com.google.gerrit.extensions.client.GeneralPreferencesInfo) VersionedAccountPreferences(com.google.gerrit.server.account.VersionedAccountPreferences) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Aggregations

GeneralPreferencesInfo (com.google.gerrit.extensions.client.GeneralPreferencesInfo)19 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)8 Test (org.junit.Test)8 Repository (org.eclipse.jgit.lib.Repository)4 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)3 Account (com.google.gerrit.reviewdb.client.Account)3 VersionedAccountPreferences (com.google.gerrit.server.account.VersionedAccountPreferences)3 MetaDataUpdate (com.google.gerrit.server.git.MetaDataUpdate)2 HashMap (java.util.HashMap)2 RevWalk (org.eclipse.jgit.revwalk.RevWalk)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 RestResponse (com.google.gerrit.acceptance.RestResponse)1 Capable (com.google.gerrit.common.data.Capable)1 MenuItem (com.google.gerrit.extensions.client.MenuItem)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)1 Change (com.google.gerrit.reviewdb.client.Change)1