fix caching of user objects

This commit is contained in:
Robin Appelman 2013-05-31 17:42:51 +02:00
parent a8fb803319
commit f00844a003
1 changed files with 22 additions and 5 deletions

View File

@ -32,6 +32,16 @@ class Manager extends PublicEmitter {
private $cachedUsers = array(); private $cachedUsers = array();
public function __construct() {
$cachedUsers = $this->cachedUsers;
$this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) {
$i = array_search($user, $cachedUsers);
if ($i !== false) {
unset($cachedUsers[$i]);
}
});
}
/** /**
* @param \OC_User_Backend $backend * @param \OC_User_Backend $backend
*/ */
@ -62,13 +72,20 @@ class Manager extends PublicEmitter {
} }
foreach ($this->backends as $backend) { foreach ($this->backends as $backend) {
if ($backend->userExists($uid)) { if ($backend->userExists($uid)) {
$this->cachedUsers[$uid] = new User($uid, $backend, $this); return $this->getUserObject($uid, $backend);
return $this->cachedUsers[$uid];
} }
} }
return null; return null;
} }
protected function getUserObject($uid, $backend) {
if (isset($this->cachedUsers[$uid])) {
return $this->cachedUsers[$uid];
}
$this->cachedUsers[$uid] = new User($uid, $backend, $this);
return $this->cachedUsers[$uid];
}
/** /**
* @param string $uid * @param string $uid
* @return bool * @return bool
@ -96,7 +113,7 @@ class Manager extends PublicEmitter {
$backendUsers = $backend->getUsers($pattern, $limit, $offset); $backendUsers = $backend->getUsers($pattern, $limit, $offset);
if (is_array($backendUsers)) { if (is_array($backendUsers)) {
foreach ($backendUsers as $uid) { foreach ($backendUsers as $uid) {
$users[] = $this->get($uid); $users[] = $this->getUserObject($uid, $backend);
if (!is_null($limit)) { if (!is_null($limit)) {
$limit--; $limit--;
} }
@ -132,7 +149,7 @@ class Manager extends PublicEmitter {
$backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset);
if (is_array($backendUsers)) { if (is_array($backendUsers)) {
foreach ($backendUsers as $uid => $displayName) { foreach ($backendUsers as $uid => $displayName) {
$users[] = $this->get($uid); $users[] = $this->getUserObject($uid, $backend);
if (!is_null($limit)) { if (!is_null($limit)) {
$limit--; $limit--;
} }
@ -185,7 +202,7 @@ class Manager extends PublicEmitter {
foreach ($this->backends as $backend) { foreach ($this->backends as $backend) {
if ($backend->implementsActions(\OC_USER_BACKEND_CREATE_USER)) { if ($backend->implementsActions(\OC_USER_BACKEND_CREATE_USER)) {
$backend->createUser($uid, $password); $backend->createUser($uid, $password);
$user = $this->get($uid); $user = $this->getUserObject($uid, $backend);
$this->emit('\OC\User', 'postCreateUser', array($user, $password)); $this->emit('\OC\User', 'postCreateUser', array($user, $password));
return $user; return $user;
} }