Add UserSession to server container

This commit is contained in:
Bart Visscher 2013-09-20 12:45:56 +02:00
parent 69c28400b4
commit ac73ce1b2a
4 changed files with 94 additions and 41 deletions

View File

@ -62,6 +62,13 @@ interface IServerContainer {
*/ */
function getRootFolder(); function getRootFolder();
/**
* Returns the user session
*
* @return \OCP\IUserSession
*/
function getUserSession();
/** /**
* Returns an ICache instance * Returns an ICache instance
* *

View File

@ -0,0 +1,30 @@
<?php
/**
* Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*
*/
namespace OCP;
/**
* User session
*/
interface IUserSession {
/**
* Do a user login
* @param string $user the username
* @param string $password the password
* @return bool true if successful
*/
public function login($user, $password);
/**
* @brief Logs the user out including all the session data
* Logout, destroys session
*/
public function logout();
}

View File

@ -56,6 +56,47 @@ class Server extends SimpleContainer implements IServerContainer {
$view = new View(); $view = new View();
return new Root($manager, $view, $user); return new Root($manager, $view, $user);
}); });
$this->registerService('UserManager', function($c) {
return new \OC\User\Manager();
});
$this->registerService('UserSession', function($c) {
$manager = $c->query('UserManager');
$userSession = new \OC\User\Session($manager, \OC::$session);
$userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) {
\OC_Hook::emit('OC_User', 'pre_createUser', array('run' => true, 'uid' => $uid, 'password' => $password));
});
$userSession->listen('\OC\User', 'postCreateUser', function ($user, $password) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'post_createUser', array('uid' => $user->getUID(), 'password' => $password));
});
$userSession->listen('\OC\User', 'preDelete', function ($user) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'pre_deleteUser', array('run' => true, 'uid' => $user->getUID()));
});
$userSession->listen('\OC\User', 'postDelete', function ($user) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'post_deleteUser', array('uid' => $user->getUID()));
});
$userSession->listen('\OC\User', 'preSetPassword', function ($user, $password, $recoveryPassword) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'pre_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
});
$userSession->listen('\OC\User', 'postSetPassword', function ($user, $password, $recoveryPassword) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'post_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
});
$userSession->listen('\OC\User', 'preLogin', function ($uid, $password) {
\OC_Hook::emit('OC_User', 'pre_login', array('run' => true, 'uid' => $uid, 'password' => $password));
});
$userSession->listen('\OC\User', 'postLogin', function ($user, $password) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'post_login', array('run' => true, 'uid' => $user->getUID(), 'password' => $password));
});
$userSession->listen('\OC\User', 'logout', function () {
\OC_Hook::emit('OC_User', 'logout', array());
});
return $userSession;
});
$this->registerService('UserCache', function($c) { $this->registerService('UserCache', function($c) {
return new UserCache(); return new UserCache();
}); });
@ -97,6 +138,20 @@ class Server extends SimpleContainer implements IServerContainer {
return $this->query('RootFolder'); return $this->query('RootFolder');
} }
/**
* @return \OC\User\Manager
*/
function getUserManager() {
return $this->query('UserManager');
}
/**
* @return \OC\User\Session
*/
function getUserSession() {
return $this->query('UserSession');
}
/** /**
* Returns an ICache instance * Returns an ICache instance
* *

View File

@ -37,54 +37,15 @@
* logout() * logout()
*/ */
class OC_User { class OC_User {
public static $userSession = null;
public static function getUserSession() { public static function getUserSession() {
if (!self::$userSession) { return OC::$server->getUserSession();
$manager = new \OC\User\Manager();
self::$userSession = new \OC\User\Session($manager, \OC::$session);
self::$userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) {
\OC_Hook::emit('OC_User', 'pre_createUser', array('run' => true, 'uid' => $uid, 'password' => $password));
});
self::$userSession->listen('\OC\User', 'postCreateUser', function ($user, $password) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'post_createUser', array('uid' => $user->getUID(), 'password' => $password));
});
self::$userSession->listen('\OC\User', 'preDelete', function ($user) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'pre_deleteUser', array('run' => true, 'uid' => $user->getUID()));
});
self::$userSession->listen('\OC\User', 'postDelete', function ($user) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'post_deleteUser', array('uid' => $user->getUID()));
});
self::$userSession->listen('\OC\User', 'preSetPassword', function ($user, $password, $recoveryPassword) {
/** @var $user \OC\User\User */
OC_Hook::emit('OC_User', 'pre_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
});
self::$userSession->listen('\OC\User', 'postSetPassword', function ($user, $password, $recoveryPassword) {
/** @var $user \OC\User\User */
OC_Hook::emit('OC_User', 'post_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
});
self::$userSession->listen('\OC\User', 'preLogin', function ($uid, $password) {
\OC_Hook::emit('OC_User', 'pre_login', array('run' => true, 'uid' => $uid, 'password' => $password));
});
self::$userSession->listen('\OC\User', 'postLogin', function ($user, $password) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'post_login', array('run' => true, 'uid' => $user->getUID(), 'password' => $password));
});
self::$userSession->listen('\OC\User', 'logout', function () {
\OC_Hook::emit('OC_User', 'logout', array());
});
}
return self::$userSession;
} }
/** /**
* @return \OC\User\Manager * @return \OC\User\Manager
*/ */
public static function getManager() { public static function getManager() {
return self::getUserSession()->getManager(); return OC::$server->getUserManager();
} }
private static $_backends = array(); private static $_backends = array();