|  | 
|  | 1 | +<?php | 
|  | 2 | +/** | 
|  | 3 | + * @copyright Copyright (c) 2016, ownCloud, Inc. | 
|  | 4 | + * | 
|  | 5 | + * @author Guillaume Colson <guillaume.colson@univ-lorraine.fr> | 
|  | 6 | + * | 
|  | 7 | + * @license AGPL-3.0 | 
|  | 8 | + * | 
|  | 9 | + * This code is free software: you can redistribute it and/or modify | 
|  | 10 | + * it under the terms of the GNU Affero General Public License, version 3, | 
|  | 11 | + * as published by the Free Software Foundation. | 
|  | 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, version 3, | 
|  | 19 | + * along with this program. If not, see <http://www.gnu.org/licenses/> | 
|  | 20 | + * | 
|  | 21 | + */ | 
|  | 22 | +namespace OCA\User_LDAP\Command; | 
|  | 23 | + | 
|  | 24 | +use OC\ServerNotAvailableException; | 
|  | 25 | +use OCA\User_LDAP\AccessFactory; | 
|  | 26 | +use OCA\User_LDAP\Configuration; | 
|  | 27 | +use OCA\User_LDAP\ConnectionFactory; | 
|  | 28 | +use OCA\User_LDAP\Helper; | 
|  | 29 | +use OCA\User_LDAP\LDAP; | 
|  | 30 | +use OCA\User_LDAP\Mapping\UserMapping; | 
|  | 31 | +use OCA\User_LDAP\User\Manager; | 
|  | 32 | + | 
|  | 33 | +use OCA\User_LDAP\User_Proxy; | 
|  | 34 | +use OCP\IAvatarManager; | 
|  | 35 | +use OCP\IConfig; | 
|  | 36 | +use OCP\IDBConnection; | 
|  | 37 | +use OCP\IUserManager; | 
|  | 38 | +use OCP\Notification\IManager; | 
|  | 39 | + | 
|  | 40 | +use Symfony\Component\Console\Command\Command; | 
|  | 41 | +use Symfony\Component\Console\Input\InputArgument; | 
|  | 42 | +use Symfony\Component\Console\Input\InputInterface; | 
|  | 43 | +use Symfony\Component\Console\Input\InputOption; | 
|  | 44 | +use Symfony\Component\Console\Output\OutputInterface; | 
|  | 45 | + | 
|  | 46 | +class SyncUser extends Command { | 
|  | 47 | +	/** @var \OCP\IConfig */ | 
|  | 48 | +	protected $ocConfig; | 
|  | 49 | +	/** @var  Manager */ | 
|  | 50 | +	protected $userManager; | 
|  | 51 | +	/** @var  IDBConnection */ | 
|  | 52 | +	protected $dbc; | 
|  | 53 | + | 
|  | 54 | +	public function __construct(IConfig $ocConfig) { | 
|  | 55 | +		$this->ocConfig = $ocConfig; | 
|  | 56 | +		$this->dbc = \OC::$server->getDatabaseConnection(); | 
|  | 57 | +		$this->userManager = new \OCA\User_LDAP\User\Manager( | 
|  | 58 | +			\OC::$server->getConfig(), | 
|  | 59 | +			new \OCA\User_LDAP\FilesystemHelper(), | 
|  | 60 | +			new \OCA\User_LDAP\LogWrapper(), | 
|  | 61 | +			\OC::$server->getAvatarManager(), | 
|  | 62 | +			new \OCP\Image(), | 
|  | 63 | +			\OC::$server->getUserManager(), | 
|  | 64 | +			\OC::$server->getNotificationManager(), | 
|  | 65 | +			\OC::$server->getShareManager() | 
|  | 66 | +		); | 
|  | 67 | + | 
|  | 68 | +		parent::__construct(); | 
|  | 69 | +	} | 
|  | 70 | + | 
|  | 71 | +	protected function configure() { | 
|  | 72 | +		$this | 
|  | 73 | +			->setName('ldap:syncuser') | 
|  | 74 | +			->setDescription('Synchronize user from LDAP immediately') | 
|  | 75 | +			->addArgument( | 
|  | 76 | +					'uid', | 
|  | 77 | +					InputArgument::REQUIRED, | 
|  | 78 | +					'the uid of the account to sync' | 
|  | 79 | +					 ) | 
|  | 80 | +		; | 
|  | 81 | +	} | 
|  | 82 | + | 
|  | 83 | +	protected function execute(InputInterface $input, OutputInterface $output): int { | 
|  | 84 | +		$helper = new Helper($this->ocConfig, \OC::$server->getDatabaseConnection()); | 
|  | 85 | +		$configPrefixes = $helper->getServerConfigurationPrefixes(true); | 
|  | 86 | +		$prefix = $this->ocConfig->getAppValue('user_ldap', 'background_sync_prefix', null); | 
|  | 87 | +		$ldapWrapper = new LDAP(); | 
|  | 88 | + | 
|  | 89 | +		$connectionFactory = new ConnectionFactory($ldapWrapper); | 
|  | 90 | +		$connection = $connectionFactory->get($prefix); | 
|  | 91 | + | 
|  | 92 | +		$accessFactory = new AccessFactory( | 
|  | 93 | +			$ldapWrapper, | 
|  | 94 | +			$this->userManager, | 
|  | 95 | +			$helper, | 
|  | 96 | +			$this->ocConfig, | 
|  | 97 | +			\OC::$server->getUserManager() | 
|  | 98 | +		); | 
|  | 99 | + | 
|  | 100 | +		$access = $accessFactory->get($connection); | 
|  | 101 | +		$access->setUserMapper(new UserMapping($this->dbc)); | 
|  | 102 | + | 
|  | 103 | +		$loginName = $access->escapeFilterPart($input->getArgument('uid')); | 
|  | 104 | +		$filter = str_replace('%uid', $loginName, $connection->ldapLoginFilter); | 
|  | 105 | + | 
|  | 106 | +		$results = $access->fetchListOfUsers( | 
|  | 107 | +			$filter, | 
|  | 108 | +			$access->userManager->getAttributes(), | 
|  | 109 | +			1, | 
|  | 110 | +			0, | 
|  | 111 | +			true | 
|  | 112 | +		); | 
|  | 113 | + | 
|  | 114 | +		if (count($results) > 0) { | 
|  | 115 | +			$line = 'Sync of '. $results[0]['cn'][0] .' ('. $results[0]['uid'][0] .') done'; | 
|  | 116 | +			$output->writeln($line); | 
|  | 117 | +		} else { | 
|  | 118 | +			$output->writeln('No user found with uid : '.$input->getArgument('uid')); | 
|  | 119 | +		} | 
|  | 120 | +		return 0; | 
|  | 121 | +	} | 
|  | 122 | +} | 
0 commit comments