|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Nextcloud - user_sql |
| 4 | + * |
| 5 | + * @copyright 2019 Björn Kinscher <dev@bjoern-kinscher.de> |
| 6 | + * @author Björn Kinscher <dev@bjoern-kinscher.de> |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Affero General Public License as |
| 10 | + * published by the Free Software Foundation, either version 3 of the |
| 11 | + * License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU Affero General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Affero General Public License |
| 19 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | + */ |
| 21 | + |
| 22 | +namespace OCA\UserSQL\Action; |
| 23 | + |
| 24 | +use OCA\UserSQL\Constant\App; |
| 25 | +use OCA\UserSQL\Constant\Opt; |
| 26 | +use OCA\UserSQL\Model\User; |
| 27 | +use OCA\UserSQL\Properties; |
| 28 | +use OCA\UserSQL\Repository\UserRepository; |
| 29 | +use OCP\IConfig; |
| 30 | +use OCP\ILogger; |
| 31 | + |
| 32 | +/** |
| 33 | + * Synchronizes the user name. |
| 34 | + * |
| 35 | + * @author Björn Kinscher <dev@bjoern-kinscher.de> |
| 36 | + */ |
| 37 | +class NameSync implements IUserAction |
| 38 | +{ |
| 39 | + /** |
| 40 | + * @var string The application name. |
| 41 | + */ |
| 42 | + private $appName; |
| 43 | + /** |
| 44 | + * @var ILogger The logger instance. |
| 45 | + */ |
| 46 | + private $logger; |
| 47 | + /** |
| 48 | + * @var Properties The properties array. |
| 49 | + */ |
| 50 | + private $properties; |
| 51 | + /** |
| 52 | + * @var IConfig The config instance. |
| 53 | + */ |
| 54 | + private $config; |
| 55 | + /** |
| 56 | + * @var UserRepository The user repository. |
| 57 | + */ |
| 58 | + private $userRepository; |
| 59 | + |
| 60 | + /** |
| 61 | + * The default constructor. |
| 62 | + * |
| 63 | + * @param string $appName The application name. |
| 64 | + * @param ILogger $logger The logger instance. |
| 65 | + * @param Properties $properties The properties array. |
| 66 | + * @param IConfig $config The config instance. |
| 67 | + * @param UserRepository $userRepository The user repository. |
| 68 | + */ |
| 69 | + public function __construct( |
| 70 | + $appName, ILogger $logger, Properties $properties, IConfig $config, |
| 71 | + UserRepository $userRepository |
| 72 | + ) { |
| 73 | + $this->appName = $appName; |
| 74 | + $this->logger = $logger; |
| 75 | + $this->properties = $properties; |
| 76 | + $this->config = $config; |
| 77 | + $this->userRepository = $userRepository; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @inheritdoc |
| 82 | + * @throws \OCP\PreConditionNotMetException |
| 83 | + */ |
| 84 | + public function doAction(User $user) |
| 85 | + { |
| 86 | + $this->logger->debug( |
| 87 | + "Entering NameSync#doAction($user->uid)", ["app" => $this->appName] |
| 88 | + ); |
| 89 | + |
| 90 | + $ncName = $this->config->getUserValue( |
| 91 | + $user->uid, "settings", "displayName", "" |
| 92 | + ); |
| 93 | + |
| 94 | + $result = false; |
| 95 | + |
| 96 | + if (!empty($user->name) && $user->name !== $ncName) { |
| 97 | + $this->config->setUserValue( |
| 98 | + $user->uid, "settings", "displayName", $user->name |
| 99 | + ); |
| 100 | + \OC::$server->getUserManager()->get($user->uid)->triggerChange('displayName', $user->name, null); |
| 101 | + } |
| 102 | + |
| 103 | + $result = true; |
| 104 | + |
| 105 | + |
| 106 | + $this->logger->debug( |
| 107 | + "Returning NameSync#doAction($user->uid): " . ($result ? "true" |
| 108 | + : "false"), |
| 109 | + ["app" => $this->appName] |
| 110 | + ); |
| 111 | + |
| 112 | + return $result; |
| 113 | + } |
| 114 | +} |
0 commit comments