From cd48b89f3166e7fa732b5cb06d33fba018af9127 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 15 Dec 2010 14:57:00 -0800 Subject: Consolidate all the random code into a random helper that offers: random::hash() random::string() random::percent() random::int() So that we don't have lots of different ways to get random values all over the code. Follow-on to #1527. --- modules/user/controllers/password.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules/user') diff --git a/modules/user/controllers/password.php b/modules/user/controllers/password.php index 2e5eac5f..567e56dc 100644 --- a/modules/user/controllers/password.php +++ b/modules/user/controllers/password.php @@ -51,7 +51,7 @@ class Password_Controller extends Controller { $user_name = $form->reset->inputs["name"]->value; $user = user::lookup_by_name($user_name); if ($user && !empty($user->email)) { - $user->hash = md5(uniqid(mt_rand(), true)); + $user->hash = random::hash(); $user->save(); $message = new View("reset_password.html"); $message->confirm_url = url::abs_site("password/do_reset?key=$user->hash"); -- cgit v1.2.3 From 1057436b7c483c60b3c128fab993a3b78fac7093 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 15 Dec 2010 16:28:18 -0800 Subject: Cache the result of User_Model::groups() and Group_Model::users() and invalidate it on save/delete for efficiency. Fixes #1529. --- modules/user/models/group.php | 8 +++++++- modules/user/models/user.php | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'modules/user') diff --git a/modules/user/models/group.php b/modules/user/models/group.php index 17d9320b..66c9aafc 100644 --- a/modules/user/models/group.php +++ b/modules/user/models/group.php @@ -19,6 +19,7 @@ */ class Group_Model_Core extends ORM implements Group_Definition { protected $has_and_belongs_to_many = array("users"); + protected $users_cache = null; /** * @see ORM::delete() @@ -28,10 +29,14 @@ class Group_Model_Core extends ORM implements Group_Definition { module::event("group_before_delete", $this); parent::delete($id); module::event("group_deleted", $old); + unset($this->users_cache); } public function users() { - return $this->users->find_all(); + if (!$this->users_cache) { + $this->users_cache = $this->users->find_all(); + } + return $this->users_cache; } /** @@ -60,6 +65,7 @@ class Group_Model_Core extends ORM implements Group_Definition { module::event("group_updated", $original, $this); } + unset($this->users_cache); return $this; } diff --git a/modules/user/models/user.php b/modules/user/models/user.php index 55bb3d6a..47fa7107 100644 --- a/modules/user/models/user.php +++ b/modules/user/models/user.php @@ -20,6 +20,7 @@ class User_Model_Core extends ORM implements User_Definition { protected $has_and_belongs_to_many = array("groups"); protected $password_length = null; + protected $groups_cache = null; public function __set($column, $value) { switch ($column) { @@ -43,6 +44,7 @@ class User_Model_Core extends ORM implements User_Definition { module::event("user_before_delete", $this); parent::delete($id); module::event("user_deleted", $old); + unset($this->groups_cache); } /** @@ -56,7 +58,10 @@ class User_Model_Core extends ORM implements User_Definition { } public function groups() { - return $this->groups->find_all(); + if (!$this->groups_cache) { + $this->groups_cache = $this->groups->find_all(); + } + return $this->groups_cache; } /** @@ -108,6 +113,7 @@ class User_Model_Core extends ORM implements User_Definition { module::event("user_updated", $original, $this); } + unset($this->groups_cache); return $this; } -- cgit v1.2.3 From 8a5bbc896b2b281039a5e34a5fc330b826825dd2 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 15 Dec 2010 19:57:09 -0800 Subject: Follow on to 1057436b7c483c60b3c128fab993a3b78fac7093 -- cache the users and groups as an array so that multiple calls will not call ORM_Iterator->current() repeatedly. --- modules/user/models/group.php | 6 +++--- modules/user/models/user.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'modules/user') diff --git a/modules/user/models/group.php b/modules/user/models/group.php index 66c9aafc..8f8e218d 100644 --- a/modules/user/models/group.php +++ b/modules/user/models/group.php @@ -29,12 +29,12 @@ class Group_Model_Core extends ORM implements Group_Definition { module::event("group_before_delete", $this); parent::delete($id); module::event("group_deleted", $old); - unset($this->users_cache); + $this->users_cache = null; } public function users() { if (!$this->users_cache) { - $this->users_cache = $this->users->find_all(); + $this->users_cache = $this->users->find_all()->as_array(); } return $this->users_cache; } @@ -65,7 +65,7 @@ class Group_Model_Core extends ORM implements Group_Definition { module::event("group_updated", $original, $this); } - unset($this->users_cache); + $this->users_cache = null; return $this; } diff --git a/modules/user/models/user.php b/modules/user/models/user.php index 47fa7107..585f4b96 100644 --- a/modules/user/models/user.php +++ b/modules/user/models/user.php @@ -44,7 +44,7 @@ class User_Model_Core extends ORM implements User_Definition { module::event("user_before_delete", $this); parent::delete($id); module::event("user_deleted", $old); - unset($this->groups_cache); + $this->groups_cache = null; } /** @@ -59,7 +59,7 @@ class User_Model_Core extends ORM implements User_Definition { public function groups() { if (!$this->groups_cache) { - $this->groups_cache = $this->groups->find_all(); + $this->groups_cache = $this->groups->find_all()->as_array(); } return $this->groups_cache; } @@ -113,7 +113,7 @@ class User_Model_Core extends ORM implements User_Definition { module::event("user_updated", $original, $this); } - unset($this->groups_cache); + $this->groups_cache = null; return $this; } -- cgit v1.2.3