LCOV - code coverage report
Current view: top level - server/account - AccountProperties.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 42 42 100.0 %
Date: 2022-11-19 15:00:39 Functions: 15 15 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2018 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.account;
      16             : 
      17             : import com.google.common.base.Strings;
      18             : import com.google.gerrit.common.Nullable;
      19             : import com.google.gerrit.entities.Account;
      20             : import java.time.Instant;
      21             : import org.eclipse.jgit.lib.Config;
      22             : import org.eclipse.jgit.lib.ObjectId;
      23             : 
      24             : /**
      25             :  * Parses/writes account properties from/to a {@link Config} file.
      26             :  *
      27             :  * <p>This is a low-level API. Read/write of account properties in a user branch should be done
      28             :  * through {@link AccountsUpdate} or {@link AccountConfig}.
      29             :  *
      30             :  * <p>The config file has one 'account' section with the properties of the account:
      31             :  *
      32             :  * <pre>
      33             :  *   [account]
      34             :  *     active = false
      35             :  *     fullName = John Doe
      36             :  *     displayName = John
      37             :  *     preferredEmail = john.doe@foo.com
      38             :  *     status = Overloaded with reviews
      39             :  * </pre>
      40             :  *
      41             :  * <p>All keys are optional.
      42             :  *
      43             :  * <p>Not setting a key and setting a key to an empty string are treated the same way and result in
      44             :  * a {@code null} value.
      45             :  *
      46             :  * <p>If no value for 'active' is specified, by default the account is considered as active.
      47             :  *
      48             :  * <p>The account is lazily parsed.
      49             :  */
      50             : public class AccountProperties {
      51             :   public static final String ACCOUNT_CONFIG = "account.config";
      52             :   public static final String ACCOUNT = "account";
      53             :   public static final String KEY_ACTIVE = "active";
      54             :   public static final String KEY_FULL_NAME = "fullName";
      55             :   public static final String KEY_DISPLAY_NAME = "displayName";
      56             :   public static final String KEY_PREFERRED_EMAIL = "preferredEmail";
      57             :   public static final String KEY_STATUS = "status";
      58             : 
      59             :   private final Account.Id accountId;
      60             :   private final Instant registeredOn;
      61             :   private final Config accountConfig;
      62             :   private @Nullable ObjectId metaId;
      63             :   private Account account;
      64             : 
      65             :   AccountProperties(
      66         151 :       Account.Id accountId, Instant registeredOn, Config accountConfig, @Nullable ObjectId metaId) {
      67         151 :     this.accountId = accountId;
      68         151 :     this.registeredOn = registeredOn;
      69         151 :     this.accountConfig = accountConfig;
      70         151 :     this.metaId = metaId;
      71         151 :   }
      72             : 
      73             :   Account getAccount() {
      74         151 :     if (account == null) {
      75         151 :       parse();
      76             :     }
      77         151 :     return account;
      78             :   }
      79             : 
      80             :   public Instant getRegisteredOn() {
      81         151 :     return registeredOn;
      82             :   }
      83             : 
      84             :   void setMetaId(@Nullable ObjectId metaId) {
      85         151 :     this.metaId = metaId;
      86         151 :     this.account = null;
      87         151 :   }
      88             : 
      89             :   private void parse() {
      90         151 :     Account.Builder accountBuilder = Account.builder(accountId, registeredOn);
      91         151 :     accountBuilder.setActive(accountConfig.getBoolean(ACCOUNT, null, KEY_ACTIVE, true));
      92         151 :     accountBuilder.setFullName(get(accountConfig, KEY_FULL_NAME));
      93         151 :     accountBuilder.setDisplayName(get(accountConfig, KEY_DISPLAY_NAME));
      94             : 
      95         151 :     String preferredEmail = get(accountConfig, KEY_PREFERRED_EMAIL);
      96         151 :     accountBuilder.setPreferredEmail(preferredEmail);
      97             : 
      98         151 :     accountBuilder.setStatus(get(accountConfig, KEY_STATUS));
      99         151 :     accountBuilder.setMetaId(metaId != null ? metaId.name() : null);
     100         151 :     account = accountBuilder.build();
     101         151 :   }
     102             : 
     103             :   Config save(AccountDelta accountDelta) {
     104         151 :     writeToAccountConfig(accountDelta, accountConfig);
     105         151 :     return accountConfig;
     106             :   }
     107             : 
     108             :   public static void writeToAccountConfig(AccountDelta accountDelta, Config cfg) {
     109         151 :     accountDelta.getActive().ifPresent(active -> setActive(cfg, active));
     110         151 :     accountDelta.getFullName().ifPresent(fullName -> set(cfg, KEY_FULL_NAME, fullName));
     111         151 :     accountDelta.getDisplayName().ifPresent(displayName -> set(cfg, KEY_DISPLAY_NAME, displayName));
     112         151 :     accountDelta
     113         151 :         .getPreferredEmail()
     114         151 :         .ifPresent(preferredEmail -> set(cfg, KEY_PREFERRED_EMAIL, preferredEmail));
     115         151 :     accountDelta.getStatus().ifPresent(status -> set(cfg, KEY_STATUS, status));
     116         151 :   }
     117             : 
     118             :   /**
     119             :    * Gets the given key from the given config.
     120             :    *
     121             :    * <p>Empty values are returned as {@code null}
     122             :    *
     123             :    * @param cfg the config
     124             :    * @param key the key
     125             :    * @return the value, {@code null} if key was not set or key was set to empty string
     126             :    */
     127             :   private static String get(Config cfg, String key) {
     128         151 :     return Strings.emptyToNull(cfg.getString(ACCOUNT, null, key));
     129             :   }
     130             : 
     131             :   /**
     132             :    * Sets/Unsets {@code account.active} in the given config.
     133             :    *
     134             :    * <p>{@code account.active} is set to {@code false} if the account is inactive.
     135             :    *
     136             :    * <p>If the account is active {@code account.active} is unset since {@code true} is the default
     137             :    * if this field is missing.
     138             :    *
     139             :    * @param cfg the config
     140             :    * @param value whether the account is active
     141             :    */
     142             :   private static void setActive(Config cfg, boolean value) {
     143          16 :     if (!value) {
     144          12 :       cfg.setBoolean(ACCOUNT, null, KEY_ACTIVE, false);
     145             :     } else {
     146          11 :       cfg.unset(ACCOUNT, null, KEY_ACTIVE);
     147             :     }
     148          16 :   }
     149             : 
     150             :   /**
     151             :    * Sets/Unsets the given key in the given config.
     152             :    *
     153             :    * <p>The key unset if the value is {@code null}.
     154             :    *
     155             :    * @param cfg the config
     156             :    * @param key the key
     157             :    * @param value the value
     158             :    */
     159             :   private static void set(Config cfg, String key, String value) {
     160         151 :     if (!Strings.isNullOrEmpty(value)) {
     161         149 :       cfg.setString(ACCOUNT, null, key, value);
     162             :     } else {
     163         147 :       cfg.unset(ACCOUNT, null, key);
     164             :     }
     165         151 :   }
     166             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750