Blame view
sources/lib/private/user/session.php
6.78 KB
|
31b7f2792
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php /** * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> * This file is licensed under the Affero General Public License version 3 or * later. * See the COPYING-README file. */ namespace OC\User; use OC\Hooks\Emitter; |
|
6d9380f96
|
13 |
use OCP\IUserSession; |
|
31b7f2792
|
14 15 16 17 18 19 20 21 22 23 24 25 |
/** * Class Session * * Hooks available in scope \OC\User: * - preSetPassword(\OC\User\User $user, string $password, string $recoverPassword) * - postSetPassword(\OC\User\User $user, string $password, string $recoverPassword) * - preDelete(\OC\User\User $user) * - postDelete(\OC\User\User $user) * - preCreateUser(string $uid, string $password) * - postCreateUser(\OC\User\User $user) * - preLogin(string $user, string $password) |
|
6d9380f96
|
26 27 28 |
* - postLogin(\OC\User\User $user, string $password) * - preRememberedLogin(string $uid) * - postRememberedLogin(\OC\User\User $user) |
|
31b7f2792
|
29 30 31 32 |
* - logout() * * @package OC\User */ |
|
6d9380f96
|
33 |
class Session implements IUserSession, Emitter {
|
|
31b7f2792
|
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
/**
* @var \OC\User\Manager $manager
*/
private $manager;
/**
* @var \OC\Session\Session $session
*/
private $session;
/**
* @var \OC\User\User $activeUser
*/
protected $activeUser;
/**
* @param \OC\User\Manager $manager
* @param \OC\Session\Session $session
*/
public function __construct($manager, $session) {
$this->manager = $manager;
$this->session = $session;
}
/**
* @param string $scope
* @param string $method
* @param callable $callback
*/
public function listen($scope, $method, $callback) {
$this->manager->listen($scope, $method, $callback);
}
/**
* @param string $scope optional
* @param string $method optional
* @param callable $callback optional
*/
public function removeListener($scope = null, $method = null, $callback = null) {
$this->manager->removeListener($scope, $method, $callback);
}
/**
* get the manager object
*
* @return \OC\User\Manager
*/
public function getManager() {
return $this->manager;
}
/**
* set the currently active user
*
|
|
6d9380f96
|
88 |
* @param \OC\User\User|null $user |
|
31b7f2792
|
89 90 91 |
*/
public function setUser($user) {
if (is_null($user)) {
|
|
6d9380f96
|
92 |
$this->getSession()->remove('user_id');
|
|
31b7f2792
|
93 |
} else {
|
|
6d9380f96
|
94 |
$this->getSession()->set('user_id', $user->getUID());
|
|
31b7f2792
|
95 96 97 98 99 100 101 102 103 104 105 106 107 |
}
$this->activeUser = $user;
}
/**
* get the current active user
*
* @return \OC\User\User
*/
public function getUser() {
if ($this->activeUser) {
return $this->activeUser;
} else {
|
|
6d9380f96
|
108 |
$uid = $this->getSession()->get('user_id');
|
|
f7d878ff1
|
109 |
if ($uid !== null) {
|
|
31b7f2792
|
110 111 112 113 114 115 116 117 118 |
$this->activeUser = $this->manager->get($uid);
return $this->activeUser;
} else {
return null;
}
}
}
/**
|
|
a293d369c
|
119 120 |
* set the login name * |
|
6d9380f96
|
121 |
* @param string|null $loginName for the logged in user |
|
a293d369c
|
122 123 124 |
*/
public function setLoginName($loginName) {
if (is_null($loginName)) {
|
|
6d9380f96
|
125 |
$this->getSession()->remove('loginname');
|
|
a293d369c
|
126 |
} else {
|
|
6d9380f96
|
127 |
$this->getSession()->set('loginname', $loginName);
|
|
a293d369c
|
128 129 130 131 132 133 134 135 136 137 |
}
}
/**
* get the login name of the current user
*
* @return string
*/
public function getLoginName() {
if ($this->activeUser) {
|
|
6d9380f96
|
138 |
return $this->getSession()->get('loginname');
|
|
a293d369c
|
139 |
} else {
|
|
6d9380f96
|
140 |
$uid = $this->getSession()->get('user_id');
|
|
a293d369c
|
141 142 |
if ($uid) {
$this->activeUser = $this->manager->get($uid);
|
|
6d9380f96
|
143 |
return $this->getSession()->get('loginname');
|
|
a293d369c
|
144 145 146 147 148 149 150 |
} else {
return null;
}
}
}
/**
|
|
31b7f2792
|
151 152 153 154 |
* try to login with the provided credentials * * @param string $uid * @param string $password |
|
6d9380f96
|
155 |
* @return boolean|null |
|
31b7f2792
|
156 157 158 159 160 161 162 163 |
*/
public function login($uid, $password) {
$this->manager->emit('\OC\User', 'preLogin', array($uid, $password));
$user = $this->manager->checkPassword($uid, $password);
if($user !== false) {
if (!is_null($user)) {
if ($user->isEnabled()) {
$this->setUser($user);
|
|
a293d369c
|
164 |
$this->setLoginName($uid); |
|
31b7f2792
|
165 166 167 168 169 170 171 172 173 174 175 176 |
$this->manager->emit('\OC\User', 'postLogin', array($user, $password));
return true;
} else {
return false;
}
}
} else {
return false;
}
}
/**
|
|
6d9380f96
|
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
* perform login using the magic cookie (remember login)
*
* @param string $uid the username
* @param string $currentToken
* @return bool
*/
public function loginWithCookie($uid, $currentToken) {
$this->manager->emit('\OC\User', 'preRememberedLogin', array($uid));
$user = $this->manager->get($uid);
if(is_null($user)) {
// user does not exist
return false;
}
// get stored tokens
$tokens = \OC_Preferences::getKeys($uid, 'login_token');
// test cookies token against stored tokens
if(!in_array($currentToken, $tokens, true)) {
return false;
}
// replace successfully used token with a new one
\OC_Preferences::deleteKey($uid, 'login_token', $currentToken);
$newToken = \OC_Util::generateRandomBytes(32);
\OC_Preferences::setValue($uid, 'login_token', $newToken, time());
$this->setMagicInCookie($user->getUID(), $newToken);
//login
$this->setUser($user);
$this->manager->emit('\OC\User', 'postRememberedLogin', array($user));
return true;
}
/**
|
|
31b7f2792
|
210 211 212 213 214 |
* logout the user from the session
*/
public function logout() {
$this->manager->emit('\OC\User', 'logout');
$this->setUser(null);
|
|
a293d369c
|
215 |
$this->setLoginName(null); |
|
31b7f2792
|
216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
$this->unsetMagicInCookie();
}
/**
* Set cookie value to use in next page load
*
* @param string $username username to be set
* @param string $token
*/
public function setMagicInCookie($username, $token) {
$secure_cookie = \OC_Config::getValue("forcessl", false); //TODO: DI for cookies and OC_Config
$expires = time() + \OC_Config::getValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15);
setcookie("oc_username", $username, $expires, \OC::$WEBROOT, '', $secure_cookie);
setcookie("oc_token", $token, $expires, \OC::$WEBROOT, '', $secure_cookie, true);
|
|
6d9380f96
|
230 |
setcookie("oc_remember_login", "1", $expires, \OC::$WEBROOT, '', $secure_cookie);
|
|
31b7f2792
|
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
}
/**
* Remove cookie for "remember username"
*/
public function unsetMagicInCookie() {
unset($_COOKIE["oc_username"]); //TODO: DI
unset($_COOKIE["oc_token"]);
unset($_COOKIE["oc_remember_login"]);
setcookie('oc_username', '', time()-3600, \OC::$WEBROOT);
setcookie('oc_token', '', time()-3600, \OC::$WEBROOT);
setcookie('oc_remember_login', '', time()-3600, \OC::$WEBROOT);
// old cookies might be stored under /webroot/ instead of /webroot
// and Firefox doesn't like it!
setcookie('oc_username', '', time()-3600, \OC::$WEBROOT . '/');
setcookie('oc_token', '', time()-3600, \OC::$WEBROOT . '/');
setcookie('oc_remember_login', '', time()-3600, \OC::$WEBROOT . '/');
}
|
|
6d9380f96
|
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
/**
* will keep the session instance in sync with \OC::$session
* @return \OC\Session\Session
*/
private function getSession() {
//keep $this->session in sync with \OC::$session
if ($this->session !== \OC::$session) {
\OC::$server->getLogger()->debug(
'\OC::$session has been replaced with a new instance. '.
'Closing and replacing session in UserSession instance.'
);
$this->session->close();
$this->session = \OC::$session;
}
return $this->session;
}
|
|
31b7f2792
|
266 |
} |