Merge pull request #23215 from eileenmcnaughton/test_amount
[civicrm-core.git] / Civi / Crypto / RotateKeys.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 namespace Civi\Crypto;
13
14 use Civi\Core\Event\GenericHookEvent;
15
16 /**
17 * Class RotateKeys
18 *
19 * @package Civi\Crypto
20 */
21 class RotateKeys {
22
23 /**
24 * The SMTP password is stored inside of the 'mailing_backend' setting.
25 *
26 * @see CRM_Utils_Hook::cryptoRotateKey()
27 */
28 public static function rotateSmtp(GenericHookEvent $e) {
29 if ($e->tag !== 'CRED') {
30 return;
31 }
32
33 $mand = \Civi::settings()->getMandatory('mailing_backend');
34 if ($mand !== NULL && !empty($mand['smtpPassword'])) {
35 $e->log->warning('The settings override for smtpPassword cannot be changed automatically.');
36 }
37
38 $exp = \Civi::settings()->getExplicit('mailing_backend');
39 if ($exp !== NULL && !empty($exp['smtpPassword'])) {
40 $cryptoToken = \Civi::service('crypto.token');
41 $newValue = $cryptoToken->rekey($exp['smtpPassword'], 'CRED');
42 if ($newValue !== NULL) {
43 $exp['smtpPassword'] = $newValue;
44 \Civi::settings()->set('mailing_backend', $exp);
45 $e->log->info('Updated mailing_backend.smtpPassword');
46 }
47 }
48 }
49
50 }