Merge pull request #6612 from owncloud/user-no-change-displayname-stable

Backport: Re-add the config options to remove the ability for users to change their displayname
This commit is contained in:
icewind1991 2014-01-14 07:13:21 -08:00
commit c6ca9be406
7 changed files with 156 additions and 91 deletions

View file

@ -4,7 +4,7 @@
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*
*
*/
namespace OC;
@ -15,6 +15,7 @@ namespace OC;
class AllConfig implements \OCP\IConfig {
/**
* Sets a new system wide value
*
* @param string $key the key of the value, under which will be saved
* @param string $value the value that should be stored
* @todo need a use case for this
@ -25,16 +26,19 @@ class AllConfig implements \OCP\IConfig {
/**
* Looks up a system wide defined value
*
* @param string $key the key of the value, under which it was saved
* @param string $default the default value to be returned if the value isn't set
* @return string the saved value
*/
public function getSystemValue($key) {
return \OCP\Config::getSystemValue($key, '');
public function getSystemValue($key, $default = '') {
return \OCP\Config::getSystemValue($key, $default);
}
/**
* Writes a new app wide value
*
* @param string $appName the appName that we want to store the value under
* @param string $key the key of the value, under which will be saved
* @param string $value the value that should be stored
@ -45,17 +49,20 @@ class AllConfig implements \OCP\IConfig {
/**
* Looks up an app wide defined value
*
* @param string $appName the appName that we stored the value under
* @param string $key the key of the value, under which it was saved
* @param string $default the default value to be returned if the value isn't set
* @return string the saved value
*/
public function getAppValue($appName, $key) {
return \OCP\Config::getAppValue($appName, $key, '');
public function getAppValue($appName, $key, $default = '') {
return \OCP\Config::getAppValue($appName, $key, $default);
}
/**
* Set a user defined value
*
* @param string $userId the userId of the user that we want to store the value under
* @param string $appName the appName that we want to store the value under
* @param string $key the key under which the value is being stored
@ -67,11 +74,14 @@ class AllConfig implements \OCP\IConfig {
/**
* Shortcut for getting a user defined value
*
* @param string $userId the userId of the user that we want to store the value under
* @param string $appName the appName that we stored the value under
* @param string $key the key under which the value is being stored
* @param string $default the default value to be returned if the value isn't set
* @return string
*/
public function getUserValue($userId, $appName, $key){
return \OCP\Config::getUserValue($userId, $appName, $key);
public function getUserValue($userId, $appName, $key, $default = '') {
return \OCP\Config::getUserValue($userId, $appName, $key, $default);
}
}

View file

@ -69,10 +69,18 @@ class Server extends SimpleContainer implements IServerContainer {
return new Root($manager, $view, $user);
});
$this->registerService('UserManager', function($c) {
return new \OC\User\Manager();
/**
* @var SimpleContainer $c
* @var \OC\AllConfig $config
*/
$config = $c->query('AllConfig');
return new \OC\User\Manager($config);
});
$this->registerService('UserSession', function($c) {
/** @var $c SimpleContainer */
/**
* @var SimpleContainer $c
* @var \OC\User\Manager $manager
*/
$manager = $c->query('UserManager');
$userSession = new \OC\User\Session($manager, \OC::$session);
$userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) {

View file

@ -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;
}
}

View file

@ -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) == '') {

View file

@ -37,12 +37,23 @@ 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);
}
}
}

View file

@ -36,6 +36,7 @@ namespace OCP;
interface IConfig {
/**
* Sets a new system wide value
*
* @param string $key the key of the value, under which will be saved
* @param string $value the value that should be stored
* @todo need a use case for this
@ -44,14 +45,17 @@ interface IConfig {
/**
* Looks up a system wide defined value
*
* @param string $key the key of the value, under which it was saved
* @param string $default the default value to be returned if the value isn't set
* @return string the saved value
*/
public function getSystemValue($key);
public function getSystemValue($key, $default = '');
/**
* Writes a new app wide value
*
* @param string $appName the appName that we want to store the value under
* @param string $key the key of the value, under which will be saved
* @param string $value the value that should be stored
@ -60,15 +64,18 @@ interface IConfig {
/**
* Looks up an app wide defined value
*
* @param string $appName the appName that we stored the value under
* @param string $key the key of the value, under which it was saved
* @param string $default the default value to be returned if the value isn't set
* @return string the saved value
*/
public function getAppValue($appName, $key);
public function getAppValue($appName, $key, $default = '');
/**
* Set a user defined value
*
* @param string $userId the userId of the user that we want to store the value under
* @param string $appName the appName that we want to store the value under
* @param string $key the key under which the value is being stored
@ -78,9 +85,11 @@ interface IConfig {
/**
* Shortcut for getting a user defined value
*
* @param string $userId the userId of the user that we want to store the value under
* @param string $appName the appName that we stored the value under
* @param string $key the key under which the value is being stored
* @param string $default the default value to be returned if the value isn't set
*/
public function getUserValue($userId, $appName, $key);
public function getUserValue($userId, $appName, $key, $default = '');
}

View file

@ -9,6 +9,7 @@
namespace Test\User;
use OC\AllConfig;
use OC\Hooks\PublicEmitter;
class User extends \PHPUnit_Framework_TestCase {
@ -205,7 +206,9 @@ class User extends \PHPUnit_Framework_TestCase {
->method('implementsActions')
->will($this->returnValue(false));
$user = new \OC\User\User('foo', $backend);
$allConfig = new AllConfig();
$user = new \OC\User\User('foo', $backend, null, $allConfig);
$this->assertEquals(\OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data") . '/foo', $user->getHome());
}