Merge pull request #12984 from owncloud/ldap-user-cleanup-master
Ldap user cleanup masterremotes/origin/fix-10825
commit
a5099b01f9
@ -1 +1 @@ |
||||
0.4.4 |
||||
0.4.5 |
||||
|
||||
@ -0,0 +1,121 @@ |
||||
<?php |
||||
/** |
||||
* Copyright (c) 2014 Arthur Schiwon <blizzz@owncloud.com> |
||||
* This file is licensed under the Affero General Public License version 3 or |
||||
* later. |
||||
* See the COPYING-README file. |
||||
*/ |
||||
|
||||
namespace OCA\user_ldap\Command; |
||||
|
||||
use Symfony\Component\Console\Command\Command; |
||||
use Symfony\Component\Console\Input\InputArgument; |
||||
use Symfony\Component\Console\Input\InputInterface; |
||||
use Symfony\Component\Console\Input\InputOption; |
||||
use Symfony\Component\Console\Output\OutputInterface; |
||||
|
||||
use OCA\user_ldap\lib\user\User; |
||||
use OCA\User_LDAP\lib\User\DeletedUsersIndex; |
||||
use OCA\User_LDAP\Mapping\UserMapping; |
||||
use OCA\user_ldap\lib\Helper as LDAPHelper; |
||||
use OCA\user_ldap\User_Proxy; |
||||
|
||||
class CheckUser extends Command { |
||||
/** @var \OCA\user_ldap\User_Proxy */ |
||||
protected $backend; |
||||
|
||||
/** @var \OCA\User_LDAP\lib\Helper */ |
||||
protected $helper; |
||||
|
||||
/** @var \OCA\User_LDAP\lib\User\DeletedUsersIndex */ |
||||
protected $dui; |
||||
|
||||
/** @var \OCA\User_LDAP\Mapping\UserMapping */ |
||||
protected $mapping; |
||||
|
||||
/** |
||||
* @param OCA\user_ldap\User_Proxy $uBackend |
||||
* @param OCA\user_ldap\lib\Helper $helper |
||||
* @param OCA\User_LDAP\lib\User\DeletedUsersIndex $dui |
||||
* @param OCA\User_LDAP\Mapping\UserMapping $mapping |
||||
*/ |
||||
public function __construct(User_Proxy $uBackend, LDAPHelper $helper, DeletedUsersIndex $dui, UserMapping $mapping) { |
||||
$this->backend = $uBackend; |
||||
$this->helper = $helper; |
||||
$this->dui = $dui; |
||||
$this->mapping = $mapping; |
||||
parent::__construct(); |
||||
} |
||||
|
||||
protected function configure() { |
||||
$this |
||||
->setName('ldap:check-user') |
||||
->setDescription('checks whether a user exists on LDAP.') |
||||
->addArgument( |
||||
'ocName', |
||||
InputArgument::REQUIRED, |
||||
'the user name as used in ownCloud' |
||||
) |
||||
->addOption( |
||||
'force', |
||||
null, |
||||
InputOption::VALUE_NONE, |
||||
'ignores disabled LDAP configuration' |
||||
) |
||||
; |
||||
} |
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output) { |
||||
try { |
||||
$uid = $input->getArgument('ocName'); |
||||
$this->isAllowed($input->getOption('force')); |
||||
$this->confirmUserIsMapped($uid); |
||||
$exists = $this->backend->userExistsOnLDAP($uid); |
||||
if($exists === true) { |
||||
$output->writeln('The user is still available on LDAP.'); |
||||
return; |
||||
} |
||||
|
||||
$this->dui->markUser($uid); |
||||
$output->writeln('The user does not exists on LDAP anymore.'); |
||||
$output->writeln('Clean up the user\'s remnants by: ./occ user:delete "' |
||||
. $uid . '"'); |
||||
} catch (\Exception $e) { |
||||
$output->writeln('<error>' . $e->getMessage(). '</error>'); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* checks whether a user is actually mapped |
||||
* @param string $ocName the username as used in ownCloud |
||||
* @throws \Exception |
||||
* @return true |
||||
*/ |
||||
protected function confirmUserIsMapped($ocName) { |
||||
$dn = $this->mapping->getDNByName($ocName); |
||||
if ($dn === false) { |
||||
throw new \Exception('The given user is not a recognized LDAP user.'); |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
/** |
||||
* checks whether the setup allows reliable checking of LDAP user existence |
||||
* @throws \Exception |
||||
* @return true |
||||
*/ |
||||
protected function isAllowed($force) { |
||||
if($this->helper->haveDisabledConfigurations() && !$force) { |
||||
throw new \Exception('Cannot check user existence, because ' |
||||
. 'disabled LDAP configurations are present.'); |
||||
} |
||||
|
||||
// we don't check ldapUserCleanupInterval from config.php because this |
||||
// action is triggered manually, while the setting only controls the |
||||
// background job. |
||||
|
||||
return true; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,73 @@ |
||||
<?php |
||||
/** |
||||
* Copyright (c) 2014 Arthur Schiwon <blizzz@owncloud.com> |
||||
* This file is licensed under the Affero General Public License version 3 or |
||||
* later. |
||||
* See the COPYING-README file. |
||||
*/ |
||||
|
||||
namespace OCA\user_ldap\Command; |
||||
|
||||
use Symfony\Component\Console\Command\Command; |
||||
use Symfony\Component\Console\Input\InputInterface; |
||||
use Symfony\Component\Console\Output\OutputInterface; |
||||
|
||||
use OCA\user_ldap\lib\user\DeletedUsersIndex; |
||||
use OCP\IDateTimeFormatter; |
||||
|
||||
class ShowRemnants extends Command { |
||||
/** @var \OCA\User_LDAP\lib\User\DeletedUsersIndex */ |
||||
protected $dui; |
||||
|
||||
/** @var \OCP\IDateTimeFormatter */ |
||||
protected $dateFormatter; |
||||
|
||||
/** |
||||
* @param OCA\user_ldap\lib\user\DeletedUsersIndex $dui |
||||
* @param OCP\IDateTimeFormatter $dateFormatter |
||||
*/ |
||||
public function __construct(DeletedUsersIndex $dui, IDateTimeFormatter $dateFormatter) { |
||||
$this->dui = $dui; |
||||
$this->dateFormatter = $dateFormatter; |
||||
parent::__construct(); |
||||
} |
||||
|
||||
protected function configure() { |
||||
$this |
||||
->setName('ldap:show-remnants') |
||||
->setDescription('shows which users are not available on LDAP anymore, but have remnants in ownCloud.') |
||||
; |
||||
} |
||||
|
||||
/** |
||||
* executes the command, i.e. creeates and outputs a table of LDAP users marked as deleted |
||||
* |
||||
* {@inheritdoc} |
||||
*/ |
||||
protected function execute(InputInterface $input, OutputInterface $output) { |
||||
/** @var \Symfony\Component\Console\Helper\Table $table */ |
||||
$table = $this->getHelperSet()->get('table'); |
||||
$table->setHeaders(array( |
||||
'ownCloud name', 'Display Name', 'LDAP UID', 'LDAP DN', 'Last Login', |
||||
'Dir', 'Sharer')); |
||||
$rows = array(); |
||||
$resultSet = $this->dui->getUsers(); |
||||
foreach($resultSet as $user) { |
||||
$hAS = $user->getHasActiveShares() ? 'Y' : 'N'; |
||||
$lastLogin = ($user->getLastLogin() > 0) ? |
||||
$this->dateFormatter->formatDate($user->getLastLogin()) : '-'; |
||||
$rows[] = array( |
||||
$user->getOCName(), |
||||
$user->getDisplayName(), |
||||
$user->getUid(), |
||||
$user->getDN(), |
||||
$lastLogin, |
||||
$user->getHomePath(), |
||||
$hAS |
||||
); |
||||
} |
||||
|
||||
$table->setRows($rows); |
||||
$table->render($output); |
||||
} |
||||
} |
||||
@ -0,0 +1,217 @@ |
||||
<?php |
||||
/** |
||||
* Copyright (c) 2014 Arthur Schiwon <blizzz@owncloud.com> |
||||
* This file is licensed under the Affero General Public License version 3 or |
||||
* later. |
||||
* See the COPYING-README file. |
||||
*/ |
||||
|
||||
namespace OCA\User_LDAP\Jobs; |
||||
|
||||
use \OC\BackgroundJob\TimedJob; |
||||
use \OCA\user_ldap\User_LDAP; |
||||
use \OCA\user_ldap\User_Proxy; |
||||
use \OCA\user_ldap\lib\Helper; |
||||
use \OCA\user_ldap\lib\LDAP; |
||||
use \OCA\user_ldap\lib\user\DeletedUsersIndex; |
||||
use \OCA\User_LDAP\Mapping\UserMapping; |
||||
|
||||
/** |
||||
* Class CleanUp |
||||
* |
||||
* a Background job to clean up deleted users |
||||
* |
||||
* @package OCA\user_ldap\lib; |
||||
*/ |
||||
class CleanUp extends TimedJob { |
||||
/** @var int $limit amount of users that should be checked per run */ |
||||
protected $limit = 50; |
||||
|
||||
/** @var int $defaultIntervalMin default interval in minutes */ |
||||
protected $defaultIntervalMin = 51; |
||||
|
||||
/** @var User_LDAP|User_Proxy $userBackend */ |
||||
protected $userBackend; |
||||
|
||||
/** @var \OCP\IConfig $ocConfig */ |
||||
protected $ocConfig; |
||||
|
||||
/** @var \OCP\IDBConnection $db */ |
||||
protected $db; |
||||
|
||||
/** @var Helper $ldapHelper */ |
||||
protected $ldapHelper; |
||||
|
||||
/** @var \OCA\User_LDAP\Mapping\UserMapping */ |
||||
protected $mapping; |
||||
|
||||
/** @var \OCA\User_LDAP\lib\User\DeletedUsersIndex */ |
||||
protected $dui; |
||||
|
||||
public function __construct() { |
||||
$minutes = \OC::$server->getConfig()->getSystemValue( |
||||
'ldapUserCleanupInterval', strval($this->defaultIntervalMin)); |
||||
$this->setInterval(intval($minutes) * 60); |
||||
} |
||||
|
||||
/** |
||||
* assigns the instances passed to run() to the class properties |
||||
* @param array $arguments |
||||
*/ |
||||
public function setArguments($arguments) { |
||||
//Dependency Injection is not possible, because the constructor will |
||||
//only get values that are serialized to JSON. I.e. whatever we would |
||||
//pass in app.php we do add here, except something else is passed e.g. |
||||
//in tests. |
||||
|
||||
if(isset($arguments['helper'])) { |
||||
$this->ldapHelper = $arguments['helper']; |
||||
} else { |
||||
$this->ldapHelper = new Helper(); |
||||
} |
||||
|
||||
if(isset($arguments['ocConfig'])) { |
||||
$this->ocConfig = $arguments['ocConfig']; |
||||
} else { |
||||
$this->ocConfig = \OC::$server->getConfig(); |
||||
} |
||||
|
||||
if(isset($arguments['userBackend'])) { |
||||
$this->userBackend = $arguments['userBackend']; |
||||
} else { |
||||
$this->userBackend = new User_Proxy( |
||||
$this->ldapHelper->getServerConfigurationPrefixes(true), |
||||
new LDAP(), |
||||
$this->ocConfig |
||||
); |
||||
} |
||||
|
||||
if(isset($arguments['db'])) { |
||||
$this->db = $arguments['db']; |
||||
} else { |
||||
$this->db = \OC::$server->getDatabaseConnection(); |
||||
} |
||||
|
||||
if(isset($arguments['mapping'])) { |
||||
$this->mapping = $arguments['mapping']; |
||||
} else { |
||||
$this->mapping = new UserMapping($this->db); |
||||
} |
||||
|
||||
if(isset($arguments['deletedUsersIndex'])) { |
||||
$this->dui = $arguments['deletedUsersIndex']; |
||||
} else { |
||||
$this->dui = new DeletedUsersIndex( |
||||
$this->ocConfig, $this->db, $this->mapping); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* makes the background job do its work |
||||
* @param array $argument |
||||
*/ |
||||
public function run($argument) { |
||||
$this->setArguments($argument); |
||||
|
||||
if(!$this->isCleanUpAllowed()) { |
||||
return; |
||||
} |
||||
$users = $this->mapping->getList($this->getOffset(), $this->limit); |
||||
if(!is_array($users)) { |
||||
//something wrong? Let's start from the beginning next time and |
||||
//abort |
||||
$this->setOffset(true); |
||||
return; |
||||
} |
||||
$resetOffset = $this->isOffsetResetNecessary(count($users)); |
||||
$this->checkUsers($users); |
||||
$this->setOffset($resetOffset); |
||||
} |
||||
|
||||
/** |
||||
* checks whether next run should start at 0 again |
||||
* @param int $resultCount |
||||
* @return bool |
||||
*/ |
||||
public function isOffsetResetNecessary($resultCount) { |
||||
return ($resultCount < $this->limit) ? true : false; |
||||
} |
||||
|
||||
/** |
||||
* checks whether cleaning up LDAP users is allowed |
||||
* @return bool |
||||
*/ |
||||
public function isCleanUpAllowed() { |
||||
try { |
||||
if($this->ldapHelper->haveDisabledConfigurations()) { |
||||
return false; |
||||
} |
||||
} catch (\Exception $e) { |
||||
return false; |
||||
} |
||||
|
||||
$enabled = $this->isCleanUpEnabled(); |
||||
|
||||
return $enabled; |
||||
} |
||||
|
||||
/** |
||||
* checks whether clean up is enabled by configuration |
||||
* @return bool |
||||
*/ |
||||
private function isCleanUpEnabled() { |
||||
return (bool)$this->ocConfig->getSystemValue( |
||||
'ldapUserCleanupInterval', strval($this->defaultIntervalMin)); |
||||
} |
||||
|
||||
/** |
||||
* checks users whether they are still existing |
||||
* @param array $users result from getMappedUsers() |
||||
*/ |
||||
private function checkUsers(array $users) { |
||||
foreach($users as $user) { |
||||
$this->checkUser($user); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* checks whether a user is still existing in LDAP |
||||
* @param string[] $user |
||||
*/ |
||||
private function checkUser(array $user) { |
||||
if($this->userBackend->userExistsOnLDAP($user['name'])) { |
||||
//still available, all good |
||||
|
||||
return; |
||||
} |
||||
|
||||
$this->dui->markUser($user['name']); |
||||
} |
||||
|
||||
/** |
||||
* gets the offset to fetch users from the mappings table |
||||
* @return int |
||||
*/ |
||||
private function getOffset() { |
||||
return intval($this->ocConfig->getAppValue('user_ldap', 'cleanUpJobOffset', 0)); |
||||
} |
||||
|
||||
/** |
||||
* sets the new offset for the next run |
||||
* @param bool $reset whether the offset should be set to 0 |
||||
*/ |
||||
public function setOffset($reset = false) { |
||||
$newOffset = $reset ? 0 : |
||||
$this->getOffset() + $this->limit; |
||||
$this->ocConfig->setAppValue('user_ldap', 'cleanUpJobOffset', $newOffset); |
||||
} |
||||
|
||||
/** |
||||
* returns the chunk size (limit in DB speak) |
||||
* @return int |
||||
*/ |
||||
public function getChunkSize() { |
||||
return $this->limit; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,114 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* ownCloud – LDAP Helper |
||||
* |
||||
* @author Arthur Schiwon |
||||
* @copyright 2014 Arthur Schiwon <blizzz@owncloud.com> |
||||
* |
||||
* This library is free software; you can redistribute it and/or |
||||
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
||||
* License as published by the Free Software Foundation; either |
||||
* version 3 of the License, or any later version. |
||||
* |
||||
* This library is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
||||
* |
||||
* You should have received a copy of the GNU Affero General Public |
||||
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
||||
* |
||||
*/ |
||||
|
||||
namespace OCA\user_ldap\lib\user; |
||||
|
||||
use OCA\user_ldap\lib\user\OfflineUser; |
||||
use OCA\User_LDAP\Mapping\UserMapping; |
||||
|
||||
/** |
||||
* Class DeletedUsersIndex |
||||
* @package OCA\User_LDAP |
||||
*/ |
||||
class DeletedUsersIndex { |
||||
/** |
||||
* @var \OCP\IConfig $config |
||||
*/ |
||||
protected $config; |
||||
|
||||
/** |
||||
* @var \OCP\IDBConnection $db |
||||
*/ |
||||
protected $db; |
||||
|
||||
/** |
||||
* @var \OCA\User_LDAP\Mapping\UserMapping $mapping |
||||
*/ |
||||
protected $mapping; |
||||
|
||||
/** |
||||
* @var array $deletedUsers |
||||
*/ |
||||
protected $deletedUsers; |
||||
|
||||
/** |
||||
* @param OCP\IConfig $config |
||||
* @param OCP\IDBConnection $db |
||||
* @param OCA\User_LDAP\Mapping\UserMapping $mapping |
||||
*/ |
||||
public function __construct(\OCP\IConfig $config, \OCP\IDBConnection $db, UserMapping $mapping) { |
||||
$this->config = $config; |
||||
$this->db = $db; |
||||
$this->mapping = $mapping; |
||||
} |
||||
|
||||
/** |
||||
* reads LDAP users marked as deleted from the database |
||||
* @return OCA\user_ldap\lib\user\OfflineUser[] |
||||
*/ |
||||
private function fetchDeletedUsers() { |
||||
$deletedUsers = $this->config->getUsersForUserValue( |
||||
'user_ldap', 'isDeleted', '1'); |
||||
|
||||
$userObjects = array(); |
||||
foreach($deletedUsers as $user) { |
||||
$userObjects[] = new OfflineUser($user, $this->config, $this->db, $this->mapping); |
||||
} |
||||
$this->deletedUsers = $userObjects; |
||||
|
||||
return $this->deletedUsers; |
||||
} |
||||
|
||||
/** |
||||
* returns all LDAP users that are marked as deleted |
||||
* @return OCA\user_ldap\lib\user\OfflineUser[] |
||||
*/ |
||||
public function getUsers() { |
||||
if(is_array($this->deletedUsers)) { |
||||
return $this->deletedUsers; |
||||
} |
||||
return $this->fetchDeletedUsers(); |
||||
} |
||||
|
||||
/** |
||||
* whether at least one user was detected as deleted |
||||
* @return bool |
||||
*/ |
||||
public function hasUsers() { |
||||
if($this->deletedUsers === false) { |
||||
$this->fetchDeletedUsers(); |
||||
} |
||||
if(is_array($this->deletedUsers) && count($this->deletedUsers) > 0) { |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* marks a user as deleted |
||||
* @param string ocName |
||||
*/ |
||||
public function markUser($ocName) { |
||||
$this->config->setUserValue($ocName, 'user_ldap', 'isDeleted', '1'); |
||||
} |
||||
} |
||||
@ -0,0 +1,223 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* ownCloud – LDAP User |
||||
* |
||||
* @author Arthur Schiwon |
||||
* @copyright 2014 Arthur Schiwon blizzz@owncloud.com |
||||
* |
||||
* This library is free software; you can redistribute it and/or |
||||
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
||||
* License as published by the Free Software Foundation; either |
||||
* version 3 of the License, or any later version. |
||||
* |
||||
* This library is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
||||
* |
||||
* You should have received a copy of the GNU Affero General Public |
||||
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
||||
* |
||||
*/ |
||||
|
||||
namespace OCA\user_ldap\lib\user; |
||||
|
||||
use OCA\User_LDAP\Mapping\UserMapping; |
||||
|
||||
class OfflineUser { |
||||
/** |
||||
* @var string $ocName |
||||
*/ |
||||
protected $ocName; |
||||
/** |
||||
* @var string $dn |
||||
*/ |
||||
protected $dn; |
||||
/** |
||||
* @var string $uid the UID as provided by LDAP |
||||
*/ |
||||
protected $uid; |
||||
/** |
||||
* @var string $displayName |
||||
*/ |
||||
protected $displayName; |
||||
/** |
||||
* @var string $homePath |
||||
*/ |
||||
protected $homePath; |
||||
/** |
||||
* @var string $lastLogin the timestamp of the last login |
||||
*/ |
||||
protected $lastLogin; |
||||
/** |
||||
* @var string $email |
||||
*/ |
||||
protected $email; |
||||
/** |
||||
* @var bool $hasActiveShares |
||||
*/ |
||||
protected $hasActiveShares; |
||||
/** |
||||
* @var \OCP\IConfig $config |
||||
*/ |
||||
protected $config; |
||||
/** |
||||
* @var \OCP\IDBConnection $db |
||||
*/ |
||||
protected $db; |
||||
/** |
||||
* @var \OCA\User_LDAP\Mapping\UserMapping |
||||
*/ |
||||
protected $mapping; |
||||
|
||||
/** |
||||
* @param string $ocName |
||||
* @param OCP\IConfig $config |
||||
* @param OCP\IDBConnection $db |
||||
* @param OCA\User_LDAP\Mapping\UserMapping $mapping |
||||
*/ |
||||
public function __construct($ocName, \OCP\IConfig $config, \OCP\IDBConnection $db, UserMapping $mapping) { |
||||
$this->ocName = $ocName; |
||||
$this->config = $config; |
||||
$this->db = $db; |
||||
$this->mapping = $mapping; |
||||
$this->fetchDetails(); |
||||
} |
||||
|
||||
/** |
||||
* exports the user details in an assoc array |
||||
* @return array |
||||
*/ |
||||
public function export() { |
||||
$data = array(); |
||||
$data['ocName'] = $this->getOCName(); |
||||
$data['dn'] = $this->getDN(); |
||||
$data['uid'] = $this->getUID(); |
||||
$data['displayName'] = $this->getDisplayName(); |
||||
$data['homePath'] = $this->getHomePath(); |
||||
$data['lastLogin'] = $this->getLastLogin(); |
||||
$data['email'] = $this->getEmail(); |
||||
$data['hasActiveShares'] = $this->getHasActiveShares(); |
||||
|
||||
return $data; |
||||
} |
||||
|
||||
/** |
||||
* getter for ownCloud internal name |
||||
* @return string |
||||
*/ |
||||
public function getOCName() { |
||||
return $this->ocName; |
||||
} |
||||
|
||||
/** |
||||
* getter for LDAP uid |
||||
* @return string |
||||
*/ |
||||
public function getUID() { |
||||
return $this->uid; |
||||
} |
||||
|
||||
/** |
||||
* getter for LDAP DN |
||||
* @return string |
||||
*/ |
||||
public function getDN() { |
||||
return $this->dn; |
||||
} |
||||
|
||||
/** |
||||
* getter for display name |
||||
* @return string |
||||
*/ |
||||
public function getDisplayName() { |
||||
return $this->displayName; |
||||
} |
||||
|
||||
/** |
||||
* getter for email |
||||
* @return string |
||||
*/ |
||||
public function getEmail() { |
||||
return $this->email; |
||||
} |
||||
|
||||
/** |
||||
* getter for home directory path |
||||
* @return string |
||||
*/ |
||||
public function getHomePath() { |
||||
return $this->homePath; |
||||
} |
||||
|
||||
/** |
||||
* getter for the last login timestamp |
||||
* @return int |
||||
*/ |
||||
public function getLastLogin() { |
||||
return intval($this->lastLogin); |
||||
} |
||||
|
||||
/** |
||||
* getter for having active shares |
||||
* @return bool |
||||
*/ |
||||
public function getHasActiveShares() { |
||||
return $this->hasActiveShares; |
||||
} |
||||
|
||||
/** |
||||
* reads the user details |
||||
*/ |
||||
protected function fetchDetails() { |
||||
$properties = array ( |
||||
'displayName' => 'user_ldap', |
||||
'uid' => 'user_ldap', |
||||
'homePath' => 'user_ldap', |
||||
'email' => 'settings', |
||||
'lastLogin' => 'login' |
||||
); |
||||
foreach($properties as $property => $app) { |
||||
$this->$property = $this->config->getUserValue($this->ocName, $app, $property, ''); |
||||
} |
||||
|
||||
$dn = $this->mapping->getDNByName($this->ocName); |
||||
$this->dn = ($dn !== false) ? $dn : ''; |
||||
|
||||
$this->determineShares(); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* finds out whether the user has active shares. The result is stored in |
||||
* $this->hasActiveShares |
||||
*/ |
||||
protected function determineShares() { |
||||
$query = $this->db->prepare(' |
||||
SELECT COUNT(`uid_owner`) |
||||
FROM `*PREFIX*share` |
||||
WHERE `uid_owner` = ? |
||||
', 1); |
||||
$query->execute(array($this->ocName)); |
||||
$sResult = $query->fetchColumn(0); |
||||
if(intval($sResult) === 1) { |
||||
$this->hasActiveShares = true; |
||||
return; |
||||
} |
||||
|
||||
$query = $this->db->prepare(' |
||||
SELECT COUNT(`owner`) |
||||
FROM `*PREFIX*share_external` |
||||
WHERE `owner` = ? |
||||
', 1); |
||||
$query->execute(array($this->ocName)); |
||||
$sResult = $query->fetchColumn(0); |
||||
if(intval($sResult) === 1) { |
||||
$this->hasActiveShares = true; |
||||
return; |
||||
} |
||||
|
||||
$this->hasActiveShares = false; |
||||
} |
||||
} |
||||
@ -0,0 +1,135 @@ |
||||
<?php |
||||
/** |
||||
* Copyright (c) 2014 Arthur Schiwon <blizzz@owncloud.com> |
||||
* This file is licensed under the Affero General Public License version 3 or |
||||
* later. |
||||
* See the COPYING-README file. |
||||
*/ |
||||
|
||||
namespace OCA\user_ldap\tests; |
||||
|
||||
class Test_CleanUp extends \PHPUnit_Framework_TestCase { |
||||
public function getMocks() { |
||||
$mocks = array(); |
||||
$mocks['userBackend'] = |
||||
$this->getMockBuilder('\OCA\user_ldap\User_Proxy') |
||||
->disableOriginalConstructor() |
||||
->getMock(); |
||||
$mocks['deletedUsersIndex'] = |
||||
$this->getMockBuilder('\OCA\user_ldap\lib\user\deletedUsersIndex') |
||||
->disableOriginalConstructor() |
||||
->getMock(); |
||||
$mocks['ocConfig'] = $this->getMock('\OCP\IConfig'); |
||||
$mocks['db'] = $this->getMock('\OCP\IDBConnection'); |
||||
$mocks['helper'] = $this->getMock('\OCA\user_ldap\lib\Helper'); |
||||
|
||||
return $mocks; |
||||
} |
||||
|
||||
/** |
||||
* clean up job must not run when there are disabled configurations |
||||
*/ |
||||
public function test_runNotAllowedByDisabledConfigurations() { |
||||
$args = $this->getMocks(); |
||||
$args['helper']->expects($this->once()) |
||||
->method('haveDisabledConfigurations') |
||||
->will($this->returnValue(true) ); |
||||
|
||||
$args['ocConfig']->expects($this->never()) |
||||
->method('getSystemValue'); |
||||
|
||||
$bgJob = new \OCA\User_LDAP\Jobs\CleanUp(); |
||||
$bgJob->setArguments($args); |
||||
|
||||
$result = $bgJob->isCleanUpAllowed(); |
||||
$this->assertSame(false, $result); |
||||
} |
||||
|
||||
/** |
||||
* clean up job must not run when LDAP Helper is broken i.e. |
||||
* returning unexpected results |
||||
*/ |
||||
public function test_runNotAllowedByBrokenHelper() { |
||||
$args = $this->getMocks(); |
||||
$args['helper']->expects($this->once()) |
||||
->method('haveDisabledConfigurations') |
||||
->will($this->throwException(new \Exception())); |
||||
|
||||
$args['ocConfig']->expects($this->never()) |
||||
->method('getSystemValue'); |
||||
|
||||
$bgJob = new \OCA\User_LDAP\Jobs\CleanUp(); |
||||
$bgJob->setArguments($args); |
||||
|
||||
$result = $bgJob->isCleanUpAllowed(); |
||||
$this->assertSame(false, $result); |
||||
} |
||||
|
||||
/** |
||||
* clean up job must not run when it is not enabled |
||||
*/ |
||||
public function test_runNotAllowedBySysConfig() { |
||||
$args = $this->getMocks(); |
||||
$args['helper']->expects($this->once()) |
||||
->method('haveDisabledConfigurations') |
||||
->will($this->returnValue(false)); |
||||
|
||||
$args['ocConfig']->expects($this->once()) |
||||
->method('getSystemValue') |
||||
->will($this->returnValue(false)); |
||||
|
||||
$bgJob = new \OCA\User_LDAP\Jobs\CleanUp(); |
||||
$bgJob->setArguments($args); |
||||
|
||||
$result = $bgJob->isCleanUpAllowed(); |
||||
$this->assertSame(false, $result); |
||||
} |
||||
|
||||
/** |
||||
* clean up job is allowed to run |
||||
*/ |
||||
public function test_runIsAllowed() { |
||||
$args = $this->getMocks(); |
||||
$args['helper']->expects($this->once()) |
||||
->method('haveDisabledConfigurations') |
||||
->will($this->returnValue(false)); |
||||
|
||||
$args['ocConfig']->expects($this->once()) |
||||
->method('getSystemValue') |
||||
->will($this->returnValue(true)); |
||||
|
||||
$bgJob = new \OCA\User_LDAP\Jobs\CleanUp(); |
||||
$bgJob->setArguments($args); |
||||
|
||||
$result = $bgJob->isCleanUpAllowed(); |
||||
$this->assertSame(true, $result); |
||||
} |
||||
|
||||
/** |
||||
* check whether offset will be reset when it needs to |
||||
*/ |
||||
public function test_OffsetResetIsNecessary() { |
||||
$args = $this->getMocks(); |
||||
|
||||
$bgJob = new \OCA\User_LDAP\Jobs\CleanUp(); |
||||
$bgJob->setArguments($args); |
||||
|
||||
$result = $bgJob->isOffsetResetNecessary($bgJob->getChunkSize() - 1); |
||||
$this->assertSame(true, $result); |
||||
} |
||||
|
||||
/** |
||||
* make sure offset is not reset when it is not due |
||||
*/ |
||||
public function test_OffsetResetIsNotNecessary() { |
||||
$args = $this->getMocks(); |
||||
|
||||
$bgJob = new \OCA\User_LDAP\Jobs\CleanUp(); |
||||
$bgJob->setArguments($args); |
||||
|
||||
$result = $bgJob->isOffsetResetNecessary($bgJob->getChunkSize()); |
||||
$this->assertSame(false, $result); |
||||
} |
||||
|
||||
} |
||||
|
||||
@ -0,0 +1,47 @@ |
||||
<?php |
||||
/** |
||||
* Copyright (c) 2014 Arthur Schiwon <blizzz@owncloud.com> |
||||
* This file is licensed under the Affero General Public License version 3 or |
||||
* later. |
||||
* See the COPYING-README file. |
||||
*/ |
||||
|
||||
namespace OC\Core\Command\User; |
||||
|
||||
use Symfony\Component\Console\Command\Command; |
||||
use Symfony\Component\Console\Input\InputInterface; |
||||
use Symfony\Component\Console\Output\OutputInterface; |
||||
use Symfony\Component\Console\Input\InputArgument; |
||||
|
||||
class Delete extends Command { |
||||
/** @var \OC\User\Manager */ |
||||
protected $userManager; |
||||
|
||||
/** |
||||
* @param \OC\User\Manager $userManager |
||||
*/ |
||||
public function __construct(\OC\User\Manager $userManager) { |
||||
$this->userManager = $userManager; |
||||
parent::__construct(); |
||||
} |
||||
|
||||
protected function configure() { |
||||
$this |
||||
->setName('user:delete') |
||||
->setDescription('deletes the specified user') |
||||
->addArgument( |
||||
'uid', |
||||
InputArgument::REQUIRED, |
||||
'the username' |
||||
); |
||||
} |
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output) { |
||||
$wasSuccessful = $this->userManager->get($input->getArgument('uid'))->delete(); |
||||
if($wasSuccessful === true) { |
||||
$output->writeln('The specified user was deleted'); |
||||
return; |
||||
} |
||||
$output->writeln('<error>The specified could not be deleted. Please check the logs.</error>'); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue