2014-03-27 17:01:14 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 14:49:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2016-07-21 14:49:16 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Juan Pablo Villafáñez <jvillafanez@solidgear.es>
|
2016-09-29 08:16:13 +00:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-03-31 07:16:22 +00:00
|
|
|
* @author Roger Szabo <roger.szabo@web.de>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2014-03-27 17:01:14 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @license AGPL-3.0
|
2014-03-27 17:01:14 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2014-03-27 17:01:14 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2014-03-27 17:01:14 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 10:44:34 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2014-03-27 17:01:14 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-03-27 17:01:14 +00:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2016-05-12 09:25:50 +00:00
|
|
|
namespace OCA\User_LDAP\User;
|
2014-03-27 17:01:14 +00:00
|
|
|
|
2018-10-30 21:42:24 +00:00
|
|
|
use OCA\User_LDAP\Access;
|
2016-05-12 14:35:33 +00:00
|
|
|
use OCA\User_LDAP\Connection;
|
2016-05-12 14:31:35 +00:00
|
|
|
use OCA\User_LDAP\FilesystemHelper;
|
2016-05-12 14:16:06 +00:00
|
|
|
use OCA\User_LDAP\LogWrapper;
|
2016-01-19 14:48:38 +00:00
|
|
|
use OCP\IAvatarManager;
|
|
|
|
use OCP\IConfig;
|
2018-04-25 13:22:28 +00:00
|
|
|
use OCP\ILogger;
|
2016-04-04 14:56:59 +00:00
|
|
|
use OCP\Image;
|
2018-06-27 23:11:36 +00:00
|
|
|
use OCP\IUser;
|
2016-01-19 14:48:38 +00:00
|
|
|
use OCP\IUserManager;
|
2017-03-31 07:16:22 +00:00
|
|
|
use OCP\Notification\IManager as INotificationManager;
|
2014-03-27 17:01:14 +00:00
|
|
|
|
2014-04-24 11:44:25 +00:00
|
|
|
/**
|
|
|
|
* User
|
|
|
|
*
|
|
|
|
* represents an LDAP user, gets and holds user-specific information from LDAP
|
|
|
|
*/
|
2014-03-27 17:01:14 +00:00
|
|
|
class User {
|
|
|
|
/**
|
2018-10-30 21:42:24 +00:00
|
|
|
* @var Access
|
2014-03-27 17:01:14 +00:00
|
|
|
*/
|
|
|
|
protected $access;
|
|
|
|
/**
|
|
|
|
* @var Connection
|
|
|
|
*/
|
|
|
|
protected $connection;
|
|
|
|
/**
|
2016-01-19 14:48:38 +00:00
|
|
|
* @var IConfig
|
2014-03-27 17:01:14 +00:00
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
/**
|
|
|
|
* @var FilesystemHelper
|
|
|
|
*/
|
|
|
|
protected $fs;
|
|
|
|
/**
|
2016-04-04 14:56:59 +00:00
|
|
|
* @var Image
|
2014-03-27 17:01:14 +00:00
|
|
|
*/
|
|
|
|
protected $image;
|
|
|
|
/**
|
|
|
|
* @var LogWrapper
|
|
|
|
*/
|
|
|
|
protected $log;
|
|
|
|
/**
|
2016-01-19 14:48:38 +00:00
|
|
|
* @var IAvatarManager
|
2014-03-27 17:01:14 +00:00
|
|
|
*/
|
|
|
|
protected $avatarManager;
|
2016-01-19 14:48:38 +00:00
|
|
|
/**
|
|
|
|
* @var IUserManager
|
|
|
|
*/
|
|
|
|
protected $userManager;
|
2017-03-31 07:16:22 +00:00
|
|
|
/**
|
|
|
|
* @var INotificationManager
|
|
|
|
*/
|
|
|
|
protected $notificationManager;
|
2014-03-27 17:01:14 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $dn;
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $uid;
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $refreshedFeatures = array();
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $avatarImage;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DB config keys for user preferences
|
|
|
|
*/
|
|
|
|
const USER_PREFKEY_FIRSTLOGIN = 'firstLoginAccomplished';
|
|
|
|
const USER_PREFKEY_LASTREFRESH = 'lastFeatureRefresh';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief constructor, make sure the subclasses call this one!
|
2015-10-15 06:27:24 +00:00
|
|
|
* @param string $username the internal username
|
|
|
|
* @param string $dn the LDAP DN
|
2018-10-30 21:42:24 +00:00
|
|
|
* @param Access $access
|
2016-01-19 14:48:38 +00:00
|
|
|
* @param IConfig $config
|
2015-10-15 06:27:24 +00:00
|
|
|
* @param FilesystemHelper $fs
|
2016-04-04 14:56:59 +00:00
|
|
|
* @param Image $image any empty instance
|
2015-10-15 06:27:24 +00:00
|
|
|
* @param LogWrapper $log
|
2016-01-19 14:48:38 +00:00
|
|
|
* @param IAvatarManager $avatarManager
|
|
|
|
* @param IUserManager $userManager
|
2017-03-31 07:16:22 +00:00
|
|
|
* @param INotificationManager $notificationManager
|
2014-03-27 17:01:14 +00:00
|
|
|
*/
|
2018-10-30 21:42:24 +00:00
|
|
|
public function __construct($username, $dn, Access $access,
|
2016-04-04 14:56:59 +00:00
|
|
|
IConfig $config, FilesystemHelper $fs, Image $image,
|
2017-03-31 07:16:22 +00:00
|
|
|
LogWrapper $log, IAvatarManager $avatarManager, IUserManager $userManager,
|
|
|
|
INotificationManager $notificationManager) {
|
|
|
|
|
2016-09-29 08:16:13 +00:00
|
|
|
if ($username === null) {
|
2018-04-25 13:22:28 +00:00
|
|
|
$log->log("uid for '$dn' must not be null!", ILogger::ERROR);
|
2016-10-06 09:21:43 +00:00
|
|
|
throw new \InvalidArgumentException('uid must not be null!');
|
2016-09-29 08:16:13 +00:00
|
|
|
} else if ($username === '') {
|
2018-04-25 13:22:28 +00:00
|
|
|
$log->log("uid for '$dn' must not be an empty string", ILogger::ERROR);
|
2016-10-06 09:21:43 +00:00
|
|
|
throw new \InvalidArgumentException('uid must not be an empty string!');
|
2016-09-29 08:16:13 +00:00
|
|
|
}
|
|
|
|
|
2017-03-31 07:16:22 +00:00
|
|
|
$this->access = $access;
|
|
|
|
$this->connection = $access->getConnection();
|
|
|
|
$this->config = $config;
|
|
|
|
$this->fs = $fs;
|
|
|
|
$this->dn = $dn;
|
|
|
|
$this->uid = $username;
|
|
|
|
$this->image = $image;
|
|
|
|
$this->log = $log;
|
|
|
|
$this->avatarManager = $avatarManager;
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->notificationManager = $notificationManager;
|
|
|
|
|
|
|
|
\OCP\Util::connectHook('OC_User', 'post_login', $this, 'handlePasswordExpiry');
|
2014-03-27 17:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief updates properties like email, quota or avatar provided by LDAP
|
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function update() {
|
|
|
|
if(is_null($this->dn)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$hasLoggedIn = $this->config->getUserValue($this->uid, 'user_ldap',
|
|
|
|
self::USER_PREFKEY_FIRSTLOGIN, 0);
|
|
|
|
|
|
|
|
if($this->needsRefresh()) {
|
|
|
|
$this->updateEmail();
|
|
|
|
$this->updateQuota();
|
|
|
|
if($hasLoggedIn !== 0) {
|
|
|
|
//we do not need to try it, when the user has not been logged in
|
|
|
|
//before, because the file system will not be ready.
|
|
|
|
$this->updateAvatar();
|
|
|
|
//in order to get an avatar as soon as possible, mark the user
|
|
|
|
//as refreshed only when updating the avatar did happen
|
|
|
|
$this->markRefreshTime();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 22:55:42 +00:00
|
|
|
/**
|
|
|
|
* processes results from LDAP for attributes as returned by getAttributesToRead()
|
|
|
|
* @param array $ldapEntry the user entry as retrieved from LDAP
|
|
|
|
*/
|
|
|
|
public function processAttributes($ldapEntry) {
|
|
|
|
$this->markRefreshTime();
|
|
|
|
//Quota
|
|
|
|
$attr = strtolower($this->connection->ldapQuotaAttribute);
|
|
|
|
if(isset($ldapEntry[$attr])) {
|
2015-10-08 18:32:15 +00:00
|
|
|
$this->updateQuota($ldapEntry[$attr][0]);
|
2017-03-14 13:07:33 +00:00
|
|
|
} else {
|
|
|
|
if ($this->connection->ldapQuotaDefault !== '') {
|
|
|
|
$this->updateQuota();
|
|
|
|
}
|
2015-08-20 22:55:42 +00:00
|
|
|
}
|
|
|
|
unset($attr);
|
|
|
|
|
|
|
|
//displayName
|
2016-02-03 12:16:55 +00:00
|
|
|
$displayName = $displayName2 = '';
|
2015-08-20 22:55:42 +00:00
|
|
|
$attr = strtolower($this->connection->ldapUserDisplayName);
|
|
|
|
if(isset($ldapEntry[$attr])) {
|
2018-01-26 14:01:27 +00:00
|
|
|
$displayName = (string)$ldapEntry[$attr][0];
|
2016-02-03 12:16:55 +00:00
|
|
|
}
|
|
|
|
$attr = strtolower($this->connection->ldapUserDisplayName2);
|
|
|
|
if(isset($ldapEntry[$attr])) {
|
2018-01-26 14:01:27 +00:00
|
|
|
$displayName2 = (string)$ldapEntry[$attr][0];
|
2016-02-03 12:16:55 +00:00
|
|
|
}
|
2016-09-22 22:30:57 +00:00
|
|
|
if ($displayName !== '') {
|
2016-02-03 12:16:55 +00:00
|
|
|
$this->composeAndStoreDisplayName($displayName);
|
|
|
|
$this->access->cacheUserDisplayName(
|
|
|
|
$this->getUsername(),
|
|
|
|
$displayName,
|
|
|
|
$displayName2
|
|
|
|
);
|
2015-08-20 22:55:42 +00:00
|
|
|
}
|
|
|
|
unset($attr);
|
|
|
|
|
2017-10-31 18:42:17 +00:00
|
|
|
//Email
|
|
|
|
//email must be stored after displayname, because it would cause a user
|
|
|
|
//change event that will trigger fetching the display name again
|
|
|
|
$attr = strtolower($this->connection->ldapEmailAttribute);
|
|
|
|
if(isset($ldapEntry[$attr])) {
|
|
|
|
$this->updateEmail($ldapEntry[$attr][0]);
|
|
|
|
}
|
|
|
|
unset($attr);
|
|
|
|
|
2015-08-20 22:55:42 +00:00
|
|
|
// LDAP Username, needed for s2s sharing
|
|
|
|
if(isset($ldapEntry['uid'])) {
|
2015-10-08 18:32:15 +00:00
|
|
|
$this->storeLDAPUserName($ldapEntry['uid'][0]);
|
2015-08-20 22:55:42 +00:00
|
|
|
} else if(isset($ldapEntry['samaccountname'])) {
|
2015-10-08 18:32:15 +00:00
|
|
|
$this->storeLDAPUserName($ldapEntry['samaccountname'][0]);
|
2015-08-20 22:55:42 +00:00
|
|
|
}
|
2015-10-08 18:32:15 +00:00
|
|
|
|
2015-08-20 22:55:42 +00:00
|
|
|
//homePath
|
|
|
|
if(strpos($this->connection->homeFolderNamingRule, 'attr:') === 0) {
|
|
|
|
$attr = strtolower(substr($this->connection->homeFolderNamingRule, strlen('attr:')));
|
|
|
|
if(isset($ldapEntry[$attr])) {
|
|
|
|
$this->access->cacheUserHome(
|
2015-10-08 18:32:15 +00:00
|
|
|
$this->getUsername(), $this->getHomePath($ldapEntry[$attr][0]));
|
2015-08-20 22:55:42 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-08 18:32:15 +00:00
|
|
|
|
2015-08-20 22:55:42 +00:00
|
|
|
//memberOf groups
|
|
|
|
$cacheKey = 'getMemberOf'.$this->getUsername();
|
|
|
|
$groups = false;
|
|
|
|
if(isset($ldapEntry['memberof'])) {
|
|
|
|
$groups = $ldapEntry['memberof'];
|
|
|
|
}
|
|
|
|
$this->connection->writeToCache($cacheKey, $groups);
|
2015-10-08 18:32:15 +00:00
|
|
|
|
2015-08-20 22:55:42 +00:00
|
|
|
//Avatar
|
2018-07-03 22:10:43 +00:00
|
|
|
/** @var Connection $connection */
|
|
|
|
$connection = $this->access->getConnection();
|
|
|
|
$attributes = $connection->resolveRule('avatar');
|
|
|
|
foreach ($attributes as $attribute) {
|
|
|
|
if(isset($ldapEntry[$attribute])) {
|
|
|
|
$this->avatarImage = $ldapEntry[$attribute][0];
|
2016-03-16 19:51:03 +00:00
|
|
|
// the call to the method that saves the avatar in the file
|
|
|
|
// system must be postponed after the login. It is to ensure
|
|
|
|
// external mounts are mounted properly (e.g. with login
|
|
|
|
// credentials from the session).
|
|
|
|
\OCP\Util::connectHook('OC_User', 'post_login', $this, 'updateAvatarPostLogin');
|
2015-08-20 22:55:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-27 17:01:14 +00:00
|
|
|
/**
|
|
|
|
* @brief returns the LDAP DN of the user
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDN() {
|
|
|
|
return $this->dn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-12 04:16:27 +00:00
|
|
|
* @brief returns the Nextcloud internal username of the user
|
2014-03-27 17:01:14 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUsername() {
|
|
|
|
return $this->uid;
|
|
|
|
}
|
|
|
|
|
2015-08-20 22:55:42 +00:00
|
|
|
/**
|
|
|
|
* returns the home directory of the user if specified by LDAP settings
|
|
|
|
* @param string $valueFromLDAP
|
|
|
|
* @return bool|string
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function getHomePath($valueFromLDAP = null) {
|
2018-01-26 14:01:27 +00:00
|
|
|
$path = (string)$valueFromLDAP;
|
2015-09-23 14:52:48 +00:00
|
|
|
$attr = null;
|
2015-08-20 22:55:42 +00:00
|
|
|
|
2016-09-22 22:30:57 +00:00
|
|
|
if (is_null($valueFromLDAP)
|
2015-08-20 22:55:42 +00:00
|
|
|
&& strpos($this->access->connection->homeFolderNamingRule, 'attr:') === 0
|
|
|
|
&& $this->access->connection->homeFolderNamingRule !== 'attr:')
|
|
|
|
{
|
|
|
|
$attr = substr($this->access->connection->homeFolderNamingRule, strlen('attr:'));
|
|
|
|
$homedir = $this->access->readAttribute(
|
|
|
|
$this->access->username2dn($this->getUsername()), $attr);
|
|
|
|
if ($homedir && isset($homedir[0])) {
|
|
|
|
$path = $homedir[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 22:30:57 +00:00
|
|
|
if ($path !== '') {
|
2015-08-20 22:55:42 +00:00
|
|
|
//if attribute's value is an absolute path take this, otherwise append it to data dir
|
|
|
|
//check for / at the beginning or pattern c:\ resp. c:/
|
|
|
|
if( '/' !== $path[0]
|
|
|
|
&& !(3 < strlen($path) && ctype_alpha($path[0])
|
|
|
|
&& $path[1] === ':' && ('\\' === $path[2] || '/' === $path[2]))
|
|
|
|
) {
|
|
|
|
$path = $this->config->getSystemValue('datadirectory',
|
|
|
|
\OC::$SERVERROOT.'/data' ) . '/' . $path;
|
|
|
|
}
|
|
|
|
//we need it to store it in the DB as well in case a user gets
|
|
|
|
//deleted so we can clean up afterwards
|
|
|
|
$this->config->setUserValue(
|
|
|
|
$this->getUsername(), 'user_ldap', 'homePath', $path
|
|
|
|
);
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
2015-09-23 14:52:48 +00:00
|
|
|
if( !is_null($attr)
|
|
|
|
&& $this->config->getAppValue('user_ldap', 'enforce_home_folder_naming_rule', true)
|
|
|
|
) {
|
2015-08-20 22:55:42 +00:00
|
|
|
// a naming rule attribute is defined, but it doesn't exist for that LDAP user
|
|
|
|
throw new \Exception('Home dir attribute can\'t be read from LDAP for uid: ' . $this->getUsername());
|
|
|
|
}
|
|
|
|
|
|
|
|
//false will apply default behaviour as defined and done by OC_User
|
|
|
|
$this->config->setUserValue($this->getUsername(), 'user_ldap', 'homePath', '');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMemberOfGroups() {
|
|
|
|
$cacheKey = 'getMemberOf'.$this->getUsername();
|
2016-04-18 08:32:15 +00:00
|
|
|
$memberOfGroups = $this->connection->getFromCache($cacheKey);
|
|
|
|
if(!is_null($memberOfGroups)) {
|
|
|
|
return $memberOfGroups;
|
2015-08-20 22:55:42 +00:00
|
|
|
}
|
|
|
|
$groupDNs = $this->access->readAttribute($this->getDN(), 'memberOf');
|
|
|
|
$this->connection->writeToCache($cacheKey, $groupDNs);
|
|
|
|
return $groupDNs;
|
|
|
|
}
|
|
|
|
|
2014-03-27 17:01:14 +00:00
|
|
|
/**
|
|
|
|
* @brief reads the image from LDAP that shall be used as Avatar
|
|
|
|
* @return string data (provided by LDAP) | false
|
|
|
|
*/
|
|
|
|
public function getAvatarImage() {
|
|
|
|
if(!is_null($this->avatarImage)) {
|
|
|
|
return $this->avatarImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->avatarImage = false;
|
2018-07-03 22:10:43 +00:00
|
|
|
/** @var Connection $connection */
|
|
|
|
$connection = $this->access->getConnection();
|
|
|
|
$attributes = $connection->resolveRule('avatar');
|
2014-03-27 17:01:14 +00:00
|
|
|
foreach($attributes as $attribute) {
|
|
|
|
$result = $this->access->readAttribute($this->dn, $attribute);
|
|
|
|
if($result !== false && is_array($result) && isset($result[0])) {
|
|
|
|
$this->avatarImage = $result[0];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->avatarImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief marks the user as having logged in at least once
|
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function markLogin() {
|
|
|
|
$this->config->setUserValue(
|
|
|
|
$this->uid, 'user_ldap', self::USER_PREFKEY_FIRSTLOGIN, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief marks the time when user features like email have been updated
|
|
|
|
* @return null
|
|
|
|
*/
|
2015-08-20 22:55:42 +00:00
|
|
|
public function markRefreshTime() {
|
2014-03-27 17:01:14 +00:00
|
|
|
$this->config->setUserValue(
|
|
|
|
$this->uid, 'user_ldap', self::USER_PREFKEY_LASTREFRESH, time());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief checks whether user features needs to be updated again by
|
|
|
|
* comparing the difference of time of the last refresh to now with the
|
|
|
|
* desired interval
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function needsRefresh() {
|
|
|
|
$lastChecked = $this->config->getUserValue($this->uid, 'user_ldap',
|
|
|
|
self::USER_PREFKEY_LASTREFRESH, 0);
|
|
|
|
|
2018-01-26 14:01:27 +00:00
|
|
|
if((time() - (int)$lastChecked) < (int)$this->config->getAppValue('user_ldap', 'updateAttributesInterval', 86400)) {
|
2014-03-27 17:01:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-21 15:59:13 +00:00
|
|
|
/**
|
|
|
|
* Stores a key-value pair in relation to this user
|
2016-02-03 12:16:55 +00:00
|
|
|
*
|
2014-08-21 15:59:13 +00:00
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
*/
|
|
|
|
private function store($key, $value) {
|
|
|
|
$this->config->setUserValue($this->uid, 'user_ldap', $key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-03 12:16:55 +00:00
|
|
|
* Composes the display name and stores it in the database. The final
|
|
|
|
* display name is returned.
|
|
|
|
*
|
2014-08-21 15:59:13 +00:00
|
|
|
* @param string $displayName
|
2016-02-03 12:16:55 +00:00
|
|
|
* @param string $displayName2
|
2018-10-25 21:41:12 +00:00
|
|
|
* @return string the effective display name
|
2014-08-21 15:59:13 +00:00
|
|
|
*/
|
2016-02-03 12:16:55 +00:00
|
|
|
public function composeAndStoreDisplayName($displayName, $displayName2 = '') {
|
2018-01-26 14:01:27 +00:00
|
|
|
$displayName2 = (string)$displayName2;
|
2016-09-22 22:30:57 +00:00
|
|
|
if($displayName2 !== '') {
|
2016-02-03 12:16:55 +00:00
|
|
|
$displayName .= ' (' . $displayName2 . ')';
|
|
|
|
}
|
2018-10-26 15:38:42 +00:00
|
|
|
$oldName = $this->config->getUserValue($this->uid, 'user_ldap', 'displayName', null);
|
|
|
|
if ($oldName !== $displayName) {
|
2018-10-25 21:41:12 +00:00
|
|
|
$this->store('displayName', $displayName);
|
|
|
|
$user = $this->userManager->get($this->getUsername());
|
2018-10-26 15:38:42 +00:00
|
|
|
if (!empty($oldName) && $user instanceof \OC\User\User) {
|
|
|
|
// if it was empty, it would be a new record, not a change emitting the trigger could
|
|
|
|
// potentially cause a UniqueConstraintViolationException, depending on some factors.
|
2018-10-25 21:41:12 +00:00
|
|
|
$user->triggerChange('displayName', $displayName);
|
|
|
|
}
|
|
|
|
}
|
2016-02-03 12:16:55 +00:00
|
|
|
return $displayName;
|
2014-08-21 15:59:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores the LDAP Username in the Database
|
|
|
|
* @param string $userName
|
|
|
|
*/
|
|
|
|
public function storeLDAPUserName($userName) {
|
|
|
|
$this->store('uid', $userName);
|
|
|
|
}
|
|
|
|
|
2014-03-27 17:01:14 +00:00
|
|
|
/**
|
|
|
|
* @brief checks whether an update method specified by feature was run
|
|
|
|
* already. If not, it will marked like this, because it is expected that
|
|
|
|
* the method will be run, when false is returned.
|
2015-10-15 06:27:24 +00:00
|
|
|
* @param string $feature email | quota | avatar (can be extended)
|
2014-03-27 17:01:14 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function wasRefreshed($feature) {
|
|
|
|
if(isset($this->refreshedFeatures[$feature])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$this->refreshedFeatures[$feature] = 1;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-12 04:16:27 +00:00
|
|
|
* fetches the email from LDAP and stores it as Nextcloud user value
|
2015-08-20 22:55:42 +00:00
|
|
|
* @param string $valueFromLDAP if known, to save an LDAP read request
|
2014-03-27 17:01:14 +00:00
|
|
|
* @return null
|
|
|
|
*/
|
2015-08-20 22:55:42 +00:00
|
|
|
public function updateEmail($valueFromLDAP = null) {
|
2014-03-27 17:01:14 +00:00
|
|
|
if($this->wasRefreshed('email')) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-26 14:01:27 +00:00
|
|
|
$email = (string)$valueFromLDAP;
|
2015-08-20 22:55:42 +00:00
|
|
|
if(is_null($valueFromLDAP)) {
|
|
|
|
$emailAttribute = $this->connection->ldapEmailAttribute;
|
2016-09-22 22:30:57 +00:00
|
|
|
if ($emailAttribute !== '') {
|
2015-08-20 22:55:42 +00:00
|
|
|
$aEmail = $this->access->readAttribute($this->dn, $emailAttribute);
|
|
|
|
if(is_array($aEmail) && (count($aEmail) > 0)) {
|
2018-01-26 14:01:27 +00:00
|
|
|
$email = (string)$aEmail[0];
|
2015-08-20 22:55:42 +00:00
|
|
|
}
|
2014-03-27 17:01:14 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-22 22:30:57 +00:00
|
|
|
if ($email !== '') {
|
2016-01-19 14:48:38 +00:00
|
|
|
$user = $this->userManager->get($this->uid);
|
2016-04-04 14:56:59 +00:00
|
|
|
if (!is_null($user)) {
|
2018-01-26 14:01:27 +00:00
|
|
|
$currentEmail = (string)$user->getEMailAddress();
|
2016-07-26 10:31:45 +00:00
|
|
|
if ($currentEmail !== $email) {
|
|
|
|
$user->setEMailAddress($email);
|
|
|
|
}
|
2016-04-04 14:56:59 +00:00
|
|
|
}
|
2015-08-20 22:55:42 +00:00
|
|
|
}
|
2014-03-27 17:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-17 11:56:27 +00:00
|
|
|
* Overall process goes as follow:
|
|
|
|
* 1. fetch the quota from LDAP and check if it's parseable with the "verifyQuotaValue" function
|
|
|
|
* 2. if the value can't be fetched, is empty or not parseable, use the default LDAP quota
|
2017-04-12 04:16:27 +00:00
|
|
|
* 3. if the default LDAP quota can't be parsed, use the Nextcloud's default quota (use 'default')
|
2017-03-17 11:56:27 +00:00
|
|
|
* 4. check if the target user exists and set the quota for the user.
|
|
|
|
*
|
|
|
|
* In order to improve performance and prevent an unwanted extra LDAP call, the $valueFromLDAP
|
|
|
|
* parameter can be passed with the value of the attribute. This value will be considered as the
|
|
|
|
* quota for the user coming from the LDAP server (step 1 of the process) It can be useful to
|
|
|
|
* fetch all the user's attributes in one call and use the fetched values in this function.
|
|
|
|
* The expected value for that parameter is a string describing the quota for the user. Valid
|
2017-04-12 04:16:27 +00:00
|
|
|
* values are 'none' (unlimited), 'default' (the Nextcloud's default quota), '1234' (quota in
|
2017-03-17 11:56:27 +00:00
|
|
|
* bytes), '1234 MB' (quota in MB - check the \OC_Helper::computerFileSize method for more info)
|
|
|
|
*
|
2017-04-12 04:16:27 +00:00
|
|
|
* fetches the quota from LDAP and stores it as Nextcloud user value
|
2015-08-20 22:55:42 +00:00
|
|
|
* @param string $valueFromLDAP the quota attribute's value can be passed,
|
|
|
|
* to save the readAttribute request
|
2014-03-27 17:01:14 +00:00
|
|
|
* @return null
|
|
|
|
*/
|
2015-08-20 22:55:42 +00:00
|
|
|
public function updateQuota($valueFromLDAP = null) {
|
2014-03-27 17:01:14 +00:00
|
|
|
if($this->wasRefreshed('quota')) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-29 14:19:45 +00:00
|
|
|
|
2018-06-27 23:11:36 +00:00
|
|
|
$quotaAttribute = $this->connection->ldapQuotaAttribute;
|
|
|
|
$defaultQuota = $this->connection->ldapQuotaDefault;
|
|
|
|
if($quotaAttribute === '' && $defaultQuota === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-14 13:07:33 +00:00
|
|
|
$quota = false;
|
2018-06-27 23:11:36 +00:00
|
|
|
if(is_null($valueFromLDAP) && $quotaAttribute !== '') {
|
|
|
|
$aQuota = $this->access->readAttribute($this->dn, $quotaAttribute);
|
|
|
|
if($aQuota && (count($aQuota) > 0) && $this->verifyQuotaValue($aQuota[0])) {
|
|
|
|
$quota = $aQuota[0];
|
|
|
|
} else if(is_array($aQuota) && isset($aQuota[0])) {
|
|
|
|
$this->log->log('no suitable LDAP quota found for user ' . $this->uid . ': [' . $aQuota[0] . ']', ILogger::DEBUG);
|
2014-03-27 17:01:14 +00:00
|
|
|
}
|
2018-06-27 23:11:36 +00:00
|
|
|
} else if ($this->verifyQuotaValue($valueFromLDAP)) {
|
|
|
|
$quota = $valueFromLDAP;
|
2017-03-14 13:07:33 +00:00
|
|
|
} else {
|
2018-06-27 23:11:36 +00:00
|
|
|
$this->log->log('no suitable LDAP quota found for user ' . $this->uid . ': [' . $valueFromLDAP . ']', ILogger::DEBUG);
|
2017-03-14 13:07:33 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 23:11:36 +00:00
|
|
|
if ($quota === false && $this->verifyQuotaValue($defaultQuota)) {
|
2017-03-14 13:07:33 +00:00
|
|
|
// quota not found using the LDAP attribute (or not parseable). Try the default quota
|
2018-06-27 23:11:36 +00:00
|
|
|
$quota = $defaultQuota;
|
|
|
|
} else if($quota === false) {
|
|
|
|
$this->log->log('no suitable default quota found for user ' . $this->uid . ': [' . $defaultQuota . ']', ILogger::DEBUG);
|
|
|
|
return;
|
2014-03-27 17:01:14 +00:00
|
|
|
}
|
2017-03-14 13:07:33 +00:00
|
|
|
|
2017-03-14 16:08:05 +00:00
|
|
|
$targetUser = $this->userManager->get($this->uid);
|
2018-06-27 23:11:36 +00:00
|
|
|
if ($targetUser instanceof IUser) {
|
|
|
|
$targetUser->setQuota($quota);
|
2017-03-14 13:07:33 +00:00
|
|
|
} else {
|
2018-06-27 23:11:36 +00:00
|
|
|
$this->log->log('trying to set a quota for user ' . $this->uid . ' but the user is missing', ILogger::INFO);
|
2014-03-27 17:01:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 13:07:33 +00:00
|
|
|
private function verifyQuotaValue($quotaValue) {
|
|
|
|
return $quotaValue === 'none' || $quotaValue === 'default' || \OC_Helper::computerFileSize($quotaValue) !== false;
|
|
|
|
}
|
|
|
|
|
2016-03-16 19:51:03 +00:00
|
|
|
/**
|
|
|
|
* called by a post_login hook to save the avatar picture
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public function updateAvatarPostLogin($params) {
|
|
|
|
if(isset($params['uid']) && $params['uid'] === $this->getUsername()) {
|
|
|
|
$this->updateAvatar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-27 17:01:14 +00:00
|
|
|
/**
|
2017-04-12 04:16:27 +00:00
|
|
|
* @brief attempts to get an image from LDAP and sets it as Nextcloud avatar
|
2018-07-02 22:38:25 +00:00
|
|
|
* @return bool
|
2014-03-27 17:01:14 +00:00
|
|
|
*/
|
2018-07-02 22:38:25 +00:00
|
|
|
public function updateAvatar($force = false) {
|
|
|
|
if(!$force && $this->wasRefreshed('avatar')) {
|
|
|
|
return false;
|
2014-03-27 17:01:14 +00:00
|
|
|
}
|
|
|
|
$avatarImage = $this->getAvatarImage();
|
|
|
|
if($avatarImage === false) {
|
|
|
|
//not set, nothing left to do;
|
2018-07-02 22:38:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!$this->image->loadFromBase64(base64_encode($avatarImage))) {
|
|
|
|
return false;
|
2014-03-27 17:01:14 +00:00
|
|
|
}
|
2018-07-02 22:38:25 +00:00
|
|
|
return $this->setOwnCloudAvatar();
|
2014-03-27 17:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-12 04:16:27 +00:00
|
|
|
* @brief sets an image as Nextcloud avatar
|
2018-07-02 22:38:25 +00:00
|
|
|
* @return bool
|
2014-03-27 17:01:14 +00:00
|
|
|
*/
|
|
|
|
private function setOwnCloudAvatar() {
|
|
|
|
if(!$this->image->valid()) {
|
2018-07-03 22:10:43 +00:00
|
|
|
$this->log->log('avatar image data from LDAP invalid for '.$this->dn, ILogger::ERROR);
|
2018-07-02 22:38:25 +00:00
|
|
|
return false;
|
2014-03-27 17:01:14 +00:00
|
|
|
}
|
|
|
|
//make sure it is a square and not bigger than 128x128
|
|
|
|
$size = min(array($this->image->width(), $this->image->height(), 128));
|
|
|
|
if(!$this->image->centerCrop($size)) {
|
2018-04-25 13:22:28 +00:00
|
|
|
$this->log->log('croping image for avatar failed for '.$this->dn, ILogger::ERROR);
|
2018-07-02 22:38:25 +00:00
|
|
|
return false;
|
2014-03-27 17:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!$this->fs->isLoaded()) {
|
|
|
|
$this->fs->setup($this->uid);
|
|
|
|
}
|
|
|
|
|
2015-07-08 00:17:44 +00:00
|
|
|
try {
|
2016-01-05 10:41:06 +00:00
|
|
|
$avatar = $this->avatarManager->getAvatar($this->uid);
|
2015-07-08 00:17:44 +00:00
|
|
|
$avatar->set($this->image);
|
2018-07-02 22:38:25 +00:00
|
|
|
return true;
|
2015-07-08 00:17:44 +00:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 14:21:56 +00:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'message' => 'Could not set avatar for ' . $this->dn,
|
2018-04-25 13:22:28 +00:00
|
|
|
'level' => ILogger::INFO,
|
2018-01-17 14:21:56 +00:00
|
|
|
'app' => 'user_ldap',
|
|
|
|
]);
|
2015-07-08 00:17:44 +00:00
|
|
|
}
|
2018-07-02 22:38:25 +00:00
|
|
|
return false;
|
2014-03-27 17:01:14 +00:00
|
|
|
}
|
|
|
|
|
2017-03-31 07:16:22 +00:00
|
|
|
/**
|
|
|
|
* called by a post_login hook to handle password expiry
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public function handlePasswordExpiry($params) {
|
|
|
|
$ppolicyDN = $this->connection->ldapDefaultPPolicyDN;
|
2018-01-26 14:01:27 +00:00
|
|
|
if (empty($ppolicyDN) || ((int)$this->connection->turnOnPasswordChange !== 1)) {
|
2017-03-31 07:16:22 +00:00
|
|
|
return;//password expiry handling disabled
|
|
|
|
}
|
|
|
|
$uid = $params['uid'];
|
|
|
|
if(isset($uid) && $uid === $this->getUsername()) {
|
2017-04-06 08:26:41 +00:00
|
|
|
//retrieve relevant user attributes
|
2018-05-02 10:17:21 +00:00
|
|
|
$result = $this->access->search('objectclass=*', array($this->dn), ['pwdpolicysubentry', 'pwdgraceusetime', 'pwdreset', 'pwdchangedtime']);
|
2017-04-06 08:26:41 +00:00
|
|
|
|
|
|
|
if(array_key_exists('pwdpolicysubentry', $result[0])) {
|
|
|
|
$pwdPolicySubentry = $result[0]['pwdpolicysubentry'];
|
|
|
|
if($pwdPolicySubentry && (count($pwdPolicySubentry) > 0)){
|
|
|
|
$ppolicyDN = $pwdPolicySubentry[0];//custom ppolicy DN
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$pwdGraceUseTime = array_key_exists('pwdgraceusetime', $result[0]) ? $result[0]['pwdgraceusetime'] : null;
|
|
|
|
$pwdReset = array_key_exists('pwdreset', $result[0]) ? $result[0]['pwdreset'] : null;
|
|
|
|
$pwdChangedTime = array_key_exists('pwdchangedtime', $result[0]) ? $result[0]['pwdchangedtime'] : null;
|
|
|
|
|
|
|
|
//retrieve relevant password policy attributes
|
2017-04-06 08:57:07 +00:00
|
|
|
$cacheKey = 'ppolicyAttributes' . $ppolicyDN;
|
|
|
|
$result = $this->connection->getFromCache($cacheKey);
|
|
|
|
if(is_null($result)) {
|
2018-05-02 10:17:21 +00:00
|
|
|
$result = $this->access->search('objectclass=*', array($ppolicyDN), ['pwdgraceauthnlimit', 'pwdmaxage', 'pwdexpirewarning']);
|
2017-04-06 08:57:07 +00:00
|
|
|
$this->connection->writeToCache($cacheKey, $result);
|
|
|
|
}
|
|
|
|
|
2017-04-06 08:26:41 +00:00
|
|
|
$pwdGraceAuthNLimit = array_key_exists('pwdgraceauthnlimit', $result[0]) ? $result[0]['pwdgraceauthnlimit'] : null;
|
|
|
|
$pwdMaxAge = array_key_exists('pwdmaxage', $result[0]) ? $result[0]['pwdmaxage'] : null;
|
|
|
|
$pwdExpireWarning = array_key_exists('pwdexpirewarning', $result[0]) ? $result[0]['pwdexpirewarning'] : null;
|
|
|
|
|
2017-03-31 07:16:22 +00:00
|
|
|
//handle grace login
|
|
|
|
$pwdGraceUseTimeCount = count($pwdGraceUseTime);
|
|
|
|
if($pwdGraceUseTime && $pwdGraceUseTimeCount > 0) { //was this a grace login?
|
|
|
|
if($pwdGraceAuthNLimit
|
|
|
|
&& (count($pwdGraceAuthNLimit) > 0)
|
2018-01-26 14:01:27 +00:00
|
|
|
&&($pwdGraceUseTimeCount < (int)$pwdGraceAuthNLimit[0])) { //at least one more grace login available?
|
2017-03-31 07:16:22 +00:00
|
|
|
$this->config->setUserValue($uid, 'user_ldap', 'needsPasswordReset', 'true');
|
|
|
|
header('Location: '.\OC::$server->getURLGenerator()->linkToRouteAbsolute(
|
|
|
|
'user_ldap.renewPassword.showRenewPasswordForm', array('user' => $uid)));
|
|
|
|
} else { //no more grace login available
|
|
|
|
header('Location: '.\OC::$server->getURLGenerator()->linkToRouteAbsolute(
|
|
|
|
'user_ldap.renewPassword.showLoginFormInvalidPassword', array('user' => $uid)));
|
|
|
|
}
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
//handle pwdReset attribute
|
|
|
|
if($pwdReset && (count($pwdReset) > 0) && $pwdReset[0] === 'TRUE') { //user must change his password
|
2017-03-31 07:27:50 +00:00
|
|
|
$this->config->setUserValue($uid, 'user_ldap', 'needsPasswordReset', 'true');
|
|
|
|
header('Location: '.\OC::$server->getURLGenerator()->linkToRouteAbsolute(
|
|
|
|
'user_ldap.renewPassword.showRenewPasswordForm', array('user' => $uid)));
|
|
|
|
exit();
|
2017-03-31 07:16:22 +00:00
|
|
|
}
|
|
|
|
//handle password expiry warning
|
|
|
|
if($pwdChangedTime && (count($pwdChangedTime) > 0)) {
|
|
|
|
if($pwdMaxAge && (count($pwdMaxAge) > 0)
|
|
|
|
&& $pwdExpireWarning && (count($pwdExpireWarning) > 0)) {
|
2018-01-26 14:01:27 +00:00
|
|
|
$pwdMaxAgeInt = (int)$pwdMaxAge[0];
|
|
|
|
$pwdExpireWarningInt = (int)$pwdExpireWarning[0];
|
2017-03-31 07:16:22 +00:00
|
|
|
if($pwdMaxAgeInt > 0 && $pwdExpireWarningInt > 0){
|
|
|
|
$pwdChangedTimeDt = \DateTime::createFromFormat('YmdHisZ', $pwdChangedTime[0]);
|
|
|
|
$pwdChangedTimeDt->add(new \DateInterval('PT'.$pwdMaxAgeInt.'S'));
|
|
|
|
$currentDateTime = new \DateTime();
|
|
|
|
$secondsToExpiry = $pwdChangedTimeDt->getTimestamp() - $currentDateTime->getTimestamp();
|
|
|
|
if($secondsToExpiry <= $pwdExpireWarningInt) {
|
|
|
|
//remove last password expiry warning if any
|
|
|
|
$notification = $this->notificationManager->createNotification();
|
|
|
|
$notification->setApp('user_ldap')
|
|
|
|
->setUser($uid)
|
|
|
|
->setObject('pwd_exp_warn', $uid)
|
|
|
|
;
|
|
|
|
$this->notificationManager->markProcessed($notification);
|
|
|
|
//create new password expiry warning
|
|
|
|
$notification = $this->notificationManager->createNotification();
|
|
|
|
$notification->setApp('user_ldap')
|
|
|
|
->setUser($uid)
|
|
|
|
->setDateTime($currentDateTime)
|
|
|
|
->setObject('pwd_exp_warn', $uid)
|
2017-04-26 07:27:55 +00:00
|
|
|
->setSubject('pwd_exp_warn_days', [(int) ceil($secondsToExpiry / 60 / 60 / 24)])
|
2017-03-31 07:16:22 +00:00
|
|
|
;
|
|
|
|
$this->notificationManager->notify($notification);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-27 17:01:14 +00:00
|
|
|
}
|