aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/user
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/user')
-rw-r--r--lib/private/user/backend.php17
-rw-r--r--lib/private/user/database.php139
-rw-r--r--lib/private/user/dummy.php9
-rw-r--r--lib/private/user/manager.php39
-rw-r--r--lib/private/user/session.php34
-rw-r--r--lib/private/user/user.php50
6 files changed, 203 insertions, 85 deletions
diff --git a/lib/private/user/backend.php b/lib/private/user/backend.php
index 02c93d13bdf..f4e5618e04a 100644
--- a/lib/private/user/backend.php
+++ b/lib/private/user/backend.php
@@ -31,13 +31,15 @@ define('OC_USER_BACKEND_NOT_IMPLEMENTED', -501);
/**
* actions that user backends can define
*/
-define('OC_USER_BACKEND_CREATE_USER', 0x0000001);
-define('OC_USER_BACKEND_SET_PASSWORD', 0x0000010);
-define('OC_USER_BACKEND_CHECK_PASSWORD', 0x0000100);
-define('OC_USER_BACKEND_GET_HOME', 0x0001000);
-define('OC_USER_BACKEND_GET_DISPLAYNAME', 0x0010000);
-define('OC_USER_BACKEND_SET_DISPLAYNAME', 0x0100000);
-define('OC_USER_BACKEND_PROVIDE_AVATAR', 0x1000000);
+define('OC_USER_BACKEND_CREATE_USER', 0x00000001);
+define('OC_USER_BACKEND_SET_PASSWORD', 0x00000010);
+define('OC_USER_BACKEND_CHECK_PASSWORD', 0x00000100);
+define('OC_USER_BACKEND_GET_HOME', 0x00001000);
+define('OC_USER_BACKEND_GET_DISPLAYNAME', 0x00010000);
+define('OC_USER_BACKEND_SET_DISPLAYNAME', 0x00100000);
+define('OC_USER_BACKEND_PROVIDE_AVATAR', 0x01000000);
+define('OC_USER_BACKEND_COUNT_USERS', 0x10000000);
+//more actions cannot be defined without breaking 32bit platforms!
/**
* Abstract base class for user management. Provides methods for querying backend
@@ -55,6 +57,7 @@ abstract class OC_User_Backend implements OC_User_Interface {
OC_USER_BACKEND_GET_DISPLAYNAME => 'getDisplayName',
OC_USER_BACKEND_SET_DISPLAYNAME => 'setDisplayName',
OC_USER_BACKEND_PROVIDE_AVATAR => 'canChangeAvatar',
+ OC_USER_BACKEND_COUNT_USERS => 'countUsers',
);
/**
diff --git a/lib/private/user/database.php b/lib/private/user/database.php
index 3db770f9898..1a63755b980 100644
--- a/lib/private/user/database.php
+++ b/lib/private/user/database.php
@@ -42,13 +42,13 @@ class OC_User_Database extends OC_User_Backend {
/**
* @var PasswordHash
*/
- static private $hasher=null;
+ static private $hasher = null;
private function getHasher() {
- if(!self::$hasher) {
+ if (!self::$hasher) {
//we don't want to use DES based crypt(), since it doesn't return a hash with a recognisable prefix
- $forcePortable=(CRYPT_BLOWFISH!=1);
- self::$hasher=new PasswordHash(8, $forcePortable);
+ $forcePortable = (CRYPT_BLOWFISH != 1);
+ self::$hasher = new PasswordHash(8, $forcePortable);
}
return self::$hasher;
@@ -63,14 +63,14 @@ class OC_User_Database extends OC_User_Backend {
* Creates a new user. Basic checking of username is done in OC_User
* itself, not in its subclasses.
*/
- public function createUser( $uid, $password ) {
- if( $this->userExists($uid) ) {
+ public function createUser($uid, $password) {
+ if ($this->userExists($uid)) {
return false;
- }else{
- $hasher=$this->getHasher();
- $hash = $hasher->HashPassword($password.OC_Config::getValue('passwordsalt', ''));
- $query = OC_DB::prepare( 'INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )' );
- $result = $query->execute( array( $uid, $hash));
+ } else {
+ $hasher = $this->getHasher();
+ $hash = $hasher->HashPassword($password . OC_Config::getValue('passwordsalt', ''));
+ $query = OC_DB::prepare('INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )');
+ $result = $query->execute(array($uid, $hash));
return $result ? true : false;
}
@@ -83,10 +83,10 @@ class OC_User_Database extends OC_User_Backend {
*
* Deletes a user
*/
- public function deleteUser( $uid ) {
+ public function deleteUser($uid) {
// Delete user-group-relation
- $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*users` WHERE `uid` = ?' );
- $query->execute( array( $uid ));
+ $query = OC_DB::prepare('DELETE FROM `*PREFIX*users` WHERE `uid` = ?');
+ $query->execute(array($uid));
return true;
}
@@ -98,15 +98,15 @@ class OC_User_Database extends OC_User_Backend {
*
* Change the password of a user
*/
- public function setPassword( $uid, $password ) {
- if( $this->userExists($uid) ) {
- $hasher=$this->getHasher();
- $hash = $hasher->HashPassword($password.OC_Config::getValue('passwordsalt', ''));
- $query = OC_DB::prepare( 'UPDATE `*PREFIX*users` SET `password` = ? WHERE `uid` = ?' );
- $query->execute( array( $hash, $uid ));
+ public function setPassword($uid, $password) {
+ if ($this->userExists($uid)) {
+ $hasher = $this->getHasher();
+ $hash = $hasher->HashPassword($password . OC_Config::getValue('passwordsalt', ''));
+ $query = OC_DB::prepare('UPDATE `*PREFIX*users` SET `password` = ? WHERE `uid` = ?');
+ $query->execute(array($hash, $uid));
return true;
- }else{
+ } else {
return false;
}
}
@@ -119,12 +119,12 @@ class OC_User_Database extends OC_User_Backend {
*
* Change the display name of a user
*/
- public function setDisplayName( $uid, $displayName ) {
- if( $this->userExists($uid) ) {
- $query = OC_DB::prepare( 'UPDATE `*PREFIX*users` SET `displayname` = ? WHERE `uid` = ?' );
- $query->execute( array( $displayName, $uid ));
+ public function setDisplayName($uid, $displayName) {
+ if ($this->userExists($uid)) {
+ $query = OC_DB::prepare('UPDATE `*PREFIX*users` SET `displayname` = ? WHERE LOWER(`uid`) = ?');
+ $query->execute(array($displayName, $uid));
return true;
- }else{
+ } else {
return false;
}
}
@@ -132,18 +132,16 @@ class OC_User_Database extends OC_User_Backend {
/**
* @brief get display name of the user
* @param $uid user ID of the user
- * @return display name
+ * @return string display name
*/
public function getDisplayName($uid) {
- if( $this->userExists($uid) ) {
- $query = OC_DB::prepare( 'SELECT `displayname` FROM `*PREFIX*users` WHERE `uid` = ?' );
- $result = $query->execute( array( $uid ))->fetchAll();
- $displayName = trim($result[0]['displayname'], ' ');
- if ( !empty($displayName) ) {
- return $displayName;
- } else {
- return $uid;
- }
+ $query = OC_DB::prepare('SELECT `displayname` FROM `*PREFIX*users` WHERE `uid` = ?');
+ $result = $query->execute(array($uid))->fetchAll();
+ $displayName = trim($result[0]['displayname'], ' ');
+ if (!empty($displayName)) {
+ return $displayName;
+ } else {
+ return $uid;
}
}
@@ -156,9 +154,9 @@ class OC_User_Database extends OC_User_Backend {
public function getDisplayNames($search = '', $limit = null, $offset = null) {
$displayNames = array();
$query = OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users`'
- .' WHERE LOWER(`displayname`) LIKE LOWER(?) OR '
- .'LOWER(`uid`) LIKE LOWER(?)', $limit, $offset);
- $result = $query->execute(array($search.'%', $search.'%'));
+ . ' WHERE LOWER(`displayname`) LIKE LOWER(?) OR '
+ . 'LOWER(`uid`) LIKE LOWER(?)', $limit, $offset);
+ $result = $query->execute(array($search . '%', $search . '%'));
$users = array();
while ($row = $result->fetchRow()) {
$displayNames[$row['uid']] = $row['displayname'];
@@ -176,30 +174,30 @@ class OC_User_Database extends OC_User_Backend {
* Check if the password is correct without logging in the user
* returns the user id or false
*/
- public function checkPassword( $uid, $password ) {
- $query = OC_DB::prepare( 'SELECT `uid`, `password` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)' );
- $result = $query->execute( array( $uid));
+ public function checkPassword($uid, $password) {
+ $query = OC_DB::prepare('SELECT `uid`, `password` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)');
+ $result = $query->execute(array($uid));
- $row=$result->fetchRow();
- if($row) {
- $storedHash=$row['password'];
- if ($storedHash[0]=='$') {//the new phpass based hashing
- $hasher=$this->getHasher();
- if($hasher->CheckPassword($password.OC_Config::getValue('passwordsalt', ''), $storedHash)) {
+ $row = $result->fetchRow();
+ if ($row) {
+ $storedHash = $row['password'];
+ if ($storedHash[0] == '$') { //the new phpass based hashing
+ $hasher = $this->getHasher();
+ if ($hasher->CheckPassword($password . OC_Config::getValue('passwordsalt', ''), $storedHash)) {
return $row['uid'];
- }else{
+ } else {
return false;
}
- }else{//old sha1 based hashing
- if(sha1($password)==$storedHash) {
+ } else { //old sha1 based hashing
+ if (sha1($password) == $storedHash) {
//upgrade to new hashing
$this->setPassword($row['uid'], $password);
return $row['uid'];
- }else{
+ } else {
return false;
}
}
- }else{
+ } else {
return false;
}
}
@@ -212,7 +210,7 @@ class OC_User_Database extends OC_User_Backend {
*/
public function getUsers($search = '', $limit = null, $offset = null) {
$query = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*users` WHERE LOWER(`uid`) LIKE LOWER(?)', $limit, $offset);
- $result = $query->execute(array($search.'%'));
+ $result = $query->execute(array($search . '%'));
$users = array();
while ($row = $result->fetchRow()) {
$users[] = $row['uid'];
@@ -226,8 +224,8 @@ class OC_User_Database extends OC_User_Backend {
* @return boolean
*/
public function userExists($uid) {
- $query = OC_DB::prepare( 'SELECT COUNT(*) FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)' );
- $result = $query->execute( array( $uid ));
+ $query = OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)');
+ $result = $query->execute(array($uid));
if (OC_DB::isError($result)) {
OC_Log::write('core', OC_DB::getErrorMessage($result), OC_Log::ERROR);
return false;
@@ -236,14 +234,14 @@ class OC_User_Database extends OC_User_Backend {
}
/**
- * @brief get the user's home directory
- * @param string $uid the username
- * @return boolean
- */
+ * @brief get the user's home directory
+ * @param string $uid the username
+ * @return boolean
+ */
public function getHome($uid) {
- if($this->userExists($uid)) {
- return OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" ) . '/' . $uid;
- }else{
+ if ($this->userExists($uid)) {
+ return OC_Config::getValue("datadirectory", OC::$SERVERROOT . "/data") . '/' . $uid;
+ } else {
return false;
}
}
@@ -255,4 +253,19 @@ class OC_User_Database extends OC_User_Backend {
return true;
}
+ /**
+ * counts the users in the database
+ *
+ * @return int | bool
+ */
+ public function countUsers() {
+ $query = OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users`');
+ $result = $query->execute();
+ if (OC_DB::isError($result)) {
+ OC_Log::write('core', OC_DB::getErrorMessage($result), OC_Log::ERROR);
+ return false;
+ }
+ return $result->fetchOne();
+ }
+
}
diff --git a/lib/private/user/dummy.php b/lib/private/user/dummy.php
index 52be7edfa75..fc15a630cf3 100644
--- a/lib/private/user/dummy.php
+++ b/lib/private/user/dummy.php
@@ -123,4 +123,13 @@ class OC_User_Dummy extends OC_User_Backend {
public function hasUserListings() {
return true;
}
+
+ /**
+ * counts the users in the database
+ *
+ * @return int | bool
+ */
+ public function countUsers() {
+ return 0;
+ }
}
diff --git a/lib/private/user/manager.php b/lib/private/user/manager.php
index 703c8cd7413..90970ef9963 100644
--- a/lib/private/user/manager.php
+++ b/lib/private/user/manager.php
@@ -35,7 +35,16 @@ class Manager extends PublicEmitter {
*/
private $cachedUsers = array();
- public function __construct() {
+ /**
+ * @var \OC\AllConfig $config
+ */
+ private $config;
+
+ /**
+ * @param \OC\AllConfig $config
+ */
+ public function __construct($config = null) {
+ $this->config = $config;
$cachedUsers = $this->cachedUsers;
$this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) {
$i = array_search($user, $cachedUsers);
@@ -103,7 +112,7 @@ class Manager extends PublicEmitter {
if (isset($this->cachedUsers[$uid])) {
return $this->cachedUsers[$uid];
}
- $this->cachedUsers[$uid] = new User($uid, $backend, $this);
+ $this->cachedUsers[$uid] = new User($uid, $backend, $this, $this->config);
return $this->cachedUsers[$uid];
}
@@ -141,7 +150,7 @@ class Manager extends PublicEmitter {
*/
public function checkPassword($loginname, $password) {
foreach ($this->backends as $backend) {
- if($backend->implementsActions(\OC_USER_BACKEND_CHECK_PASSWORD)) {
+ if ($backend->implementsActions(\OC_USER_BACKEND_CHECK_PASSWORD)) {
$uid = $backend->checkPassword($loginname, $password);
if ($uid !== false) {
return $this->getUserObject($uid, $backend);
@@ -234,7 +243,7 @@ class Manager extends PublicEmitter {
// Allowed are: "a-z", "A-Z", "0-9" and "_.@-"
if (preg_match('/[^a-zA-Z0-9 _\.@\-]/', $uid)) {
throw new \Exception('Only the following characters are allowed in a username:'
- . ' "a-z", "A-Z", "0-9", and "_.@-"');
+ . ' "a-z", "A-Z", "0-9", and "_.@-"');
}
// No empty username
if (trim($uid) == '') {
@@ -261,4 +270,26 @@ class Manager extends PublicEmitter {
}
return false;
}
+
+ /**
+ * returns how many users per backend exist (if supported by backend)
+ *
+ * @return array with backend class as key and count number as value
+ */
+ public function countUsers() {
+ $userCountStatistics = array();
+ foreach ($this->backends as $backend) {
+ if ($backend->implementsActions(\OC_USER_BACKEND_COUNT_USERS)) {
+ $backendusers = $backend->countUsers();
+ if($backendusers !== false) {
+ if(isset($userCountStatistics[get_class($backend)])) {
+ $userCountStatistics[get_class($backend)] += $backendusers;
+ } else {
+ $userCountStatistics[get_class($backend)] = $backendusers;
+ }
+ }
+ }
+ }
+ return $userCountStatistics;
+ }
}
diff --git a/lib/private/user/session.php b/lib/private/user/session.php
index 9c9bee3da25..1e299416fb3 100644
--- a/lib/private/user/session.php
+++ b/lib/private/user/session.php
@@ -113,6 +113,38 @@ class Session implements Emitter, \OCP\IUserSession {
}
/**
+ * set the login name
+ *
+ * @param string $loginName for the logged in user
+ */
+ public function setLoginName($loginName) {
+ if (is_null($loginName)) {
+ $this->session->remove('loginname');
+ } else {
+ $this->session->set('loginname', $loginName);
+ }
+ }
+
+ /**
+ * get the login name of the current user
+ *
+ * @return string
+ */
+ public function getLoginName() {
+ if ($this->activeUser) {
+ return $this->session->get('loginname');
+ } else {
+ $uid = $this->session->get('user_id');
+ if ($uid) {
+ $this->activeUser = $this->manager->get($uid);
+ return $this->session->get('loginname');
+ } else {
+ return null;
+ }
+ }
+ }
+
+ /**
* try to login with the provided credentials
*
* @param string $uid
@@ -126,6 +158,7 @@ class Session implements Emitter, \OCP\IUserSession {
if (!is_null($user)) {
if ($user->isEnabled()) {
$this->setUser($user);
+ $this->setLoginName($uid);
$this->manager->emit('\OC\User', 'postLogin', array($user, $password));
return true;
} else {
@@ -143,6 +176,7 @@ class Session implements Emitter, \OCP\IUserSession {
public function logout() {
$this->manager->emit('\OC\User', 'logout');
$this->setUser(null);
+ $this->setLoginName(null);
$this->unsetMagicInCookie();
}
diff --git a/lib/private/user/user.php b/lib/private/user/user.php
index e773473ec41..ef5364cbf7b 100644
--- a/lib/private/user/user.php
+++ b/lib/private/user/user.php
@@ -38,11 +38,22 @@ class User {
private $emitter;
/**
+ * @var string $home
+ */
+ private $home;
+
+ /**
+ * @var \OC\AllConfig $config
+ */
+ private $config;
+
+ /**
* @param string $uid
* @param \OC_User_Backend $backend
- * @param Emitter $emitter
+ * @param \OC\Hooks\Emitter $emitter
+ * @param \OC\AllConfig $config
*/
- public function __construct($uid, $backend, $emitter = null) {
+ public function __construct($uid, $backend, $emitter = null, $config = null) {
$this->uid = $uid;
if ($backend and $backend->implementsActions(OC_USER_BACKEND_GET_DISPLAYNAME)) {
$this->displayName = $backend->getDisplayName($uid);
@@ -51,8 +62,13 @@ class User {
}
$this->backend = $backend;
$this->emitter = $emitter;
- $enabled = \OC_Preferences::getValue($uid, 'core', 'enabled', 'true'); //TODO: DI for OC_Preferences
- $this->enabled = ($enabled === 'true');
+ $this->config = $config;
+ if ($this->config) {
+ $enabled = $this->config->getUserValue($uid, 'core', 'enabled', 'true');
+ $this->enabled = ($enabled === 'true');
+ } else {
+ $this->enabled = true;
+ }
}
/**
@@ -133,10 +149,16 @@ class User {
* @return string
*/
public function getHome() {
- if ($this->backend->implementsActions(\OC_USER_BACKEND_GET_HOME) and $home = $this->backend->getHome($this->uid)) {
- return $home;
+ if (!$this->home) {
+ if ($this->backend->implementsActions(\OC_USER_BACKEND_GET_HOME) and $home = $this->backend->getHome($this->uid)) {
+ $this->home = $home;
+ } elseif ($this->config) {
+ $this->home = $this->config->getSystemValue('datadirectory') . '/' . $this->uid;
+ } else {
+ $this->home = \OC::$SERVERROOT . '/data/' . $this->uid;
+ }
}
- return \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data") . '/' . $this->uid; //TODO switch to Config object once implemented
+ return $this->home;
}
/**
@@ -145,7 +167,7 @@ class User {
* @return bool
*/
public function canChangeAvatar() {
- if($this->backend->implementsActions(\OC_USER_BACKEND_PROVIDE_AVATAR)) {
+ if ($this->backend->implementsActions(\OC_USER_BACKEND_PROVIDE_AVATAR)) {
return $this->backend->canChangeAvatar($this->uid);
}
return true;
@@ -166,7 +188,11 @@ class User {
* @return bool
*/
public function canChangeDisplayName() {
- return $this->backend->implementsActions(\OC_USER_BACKEND_SET_DISPLAYNAME);
+ if ($this->config and $this->config->getSystemValue('allow_user_to_change_display_name') === false) {
+ return false;
+ } else {
+ return $this->backend->implementsActions(\OC_USER_BACKEND_SET_DISPLAYNAME);
+ }
}
/**
@@ -185,7 +211,9 @@ class User {
*/
public function setEnabled($enabled) {
$this->enabled = $enabled;
- $enabled = ($enabled) ? 'true' : 'false';
- \OC_Preferences::setValue($this->uid, 'core', 'enabled', $enabled);
+ if ($this->config) {
+ $enabled = ($enabled) ? 'true' : 'false';
+ $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled);
+ }
}
}