diff options
-rw-r--r-- | modules/gallery/helpers/access.php | 4 | ||||
-rw-r--r-- | modules/gallery/libraries/Identity.php | 34 | ||||
-rw-r--r-- | modules/gallery/libraries/drivers/Identity.php | 196 | ||||
-rw-r--r-- | modules/gallery/tests/Albums_Controller_Test.php | 1 | ||||
-rw-r--r-- | modules/gallery/tests/Photos_Controller_Test.php | 2 | ||||
-rw-r--r-- | modules/gallery/views/admin_identity.html.php | 6 | ||||
-rw-r--r-- | modules/user/controllers/password.php (renamed from modules/gallery/controllers/password.php) | 4 | ||||
-rw-r--r-- | modules/user/helpers/group.php | 4 | ||||
-rw-r--r-- | modules/user/helpers/user.php | 15 | ||||
-rw-r--r-- | modules/user/libraries/drivers/Identity/Gallery.php | 99 | ||||
-rw-r--r-- | modules/user/models/group.php | 2 | ||||
-rw-r--r-- | modules/user/models/user.php | 2 | ||||
-rw-r--r-- | modules/user/views/admin_users.html.php | 2 | ||||
-rw-r--r-- | modules/user/views/reset_password.html.php (renamed from modules/gallery/views/reset_password.html.php) | 0 |
14 files changed, 58 insertions, 313 deletions
diff --git a/modules/gallery/helpers/access.php b/modules/gallery/helpers/access.php index 21f4de81..fba161e3 100644 --- a/modules/gallery/helpers/access.php +++ b/modules/gallery/helpers/access.php @@ -197,8 +197,8 @@ class access_Core { * @param Item_Model $item * @param boolean $value */ - private static function _set(Group_Model $group, $perm_name, $album, $value) { - if (get_class($group) != "Group_Model") { + private static function _set(Group_Definition $group, $perm_name, $album, $value) { + if (!($group instanceof Group_Definition)) { throw new Exception("@todo PERMISSIONS_ONLY_WORK_ON_GROUPS"); } if (!$album->loaded) { diff --git a/modules/gallery/libraries/Identity.php b/modules/gallery/libraries/Identity.php index 9e5f0bb5..e77fd2d2 100644 --- a/modules/gallery/libraries/Identity.php +++ b/modules/gallery/libraries/Identity.php @@ -133,37 +133,17 @@ class Identity_Core { } /** - * @see Identity_Driver::hash_password. - */ - static function hash_password($password) { - return self::instance()->driver->hash_password($password); - } - - /** - * Look up a user by id. - * @param integer $id the user id - * @return User_Definition the user object, or null if the id was invalid. + * @see Identity_Driver::lookup_user. */ static function lookup_user($id) { - return self::instance()->driver->lookup_user_by_field("id", $id); + return self::instance()->driver->lookup_user($id); } /** - * Look up a user by name. - * @param integer $name the user name - * @return User_Definition the user object, or null if the name was invalid. + * @see Identity_Driver::lookup_user_by_name. */ static function lookup_user_by_name($name) { - return self::instance()->driver->lookup_user_by_field("name", $name); - } - - /** - * Look up a user by hash. - * @param string $name the user name - * @return User_Definition the user object, or null if the name was invalid. - */ - static function lookup_user_by_hash($hash) { - return self::instance()->driver->lookup_user_by_field("hash", $hash); + return self::instance()->driver->lookup_user_by_name($name); } /** @@ -188,12 +168,10 @@ class Identity_Core { } /** - * Look up a group by name. - * @param integer $id the group name - * @return Group_Definition the group object, or null if the name was invalid. + * @see Identity_Driver::lookup_group_by_name. */ static function lookup_group_by_name($name) { - return self::instance()->driver->lookup_group_by_field("name", $name); + return self::instance()->driver->lookup_group_by_name($name); } /** diff --git a/modules/gallery/libraries/drivers/Identity.php b/modules/gallery/libraries/drivers/Identity.php index a9e1a75b..6ab001cb 100644 --- a/modules/gallery/libraries/drivers/Identity.php +++ b/modules/gallery/libraries/drivers/Identity.php @@ -45,19 +45,18 @@ interface Identity_Driver { public function is_correct_password($user, $password); /** - * Create the hashed passwords. - * @param string $password a plaintext password - * @return string hashed password + * Look up a user by id. + * @param integer id + * @return User_Definition the user object, or null if the name was invalid. */ - public function hash_password($password); + public function lookup_user($id); /** - * Look up a user by by search the specified field. - * @param string search field - * @param string search value - * @return User_Definition the user object, or null if the name was invalid. + * Look up a user by name. + * @param string name + * @return User_Definition the user object, or null if the name was invalid. */ - public function lookup_user_by_field($field, $value); + public function lookup_user_by_name($name); /** * Create a new group. @@ -90,181 +89,6 @@ interface Identity_Driver { } // End Identity Driver Definition -/** - * User Data wrapper - */ -abstract class User_Definition { - protected $user; - public function __get($column) { - switch ($column) { - case "id": - case "name": - case "full_name": - case "password": - case "login_count": - case "last_login": - case "email": - case "admin": - case "guest": - case "hash": - case "url": - case "locale": - case "groups": - case "hashed_password": - return $this->user->$column; - default: - throw new Exception("@todo UNSUPPORTED FIELD: $column"); - break; - } - } - - public function __set($column, $value) { - switch ($column) { - case "id": - case "groups": - throw new Exception("@todo READ ONLY FIELD: $column"); - break; - case "name": - case "full_name": - case "hashed_password": - case "password": - case "login_count": - case "last_login": - case "email": - case "admin": - case "guest": - case "hash": - case "url": - case "locale": - $this->user->$column = $value; - break; - default: - throw new Exception("@todo UNSUPPORTED FIELD: $column"); - break; - } - } - - public function __isset($column) { - return isset($this->user->$column); - } - - public function __unset($column) { - switch ($column) { - case "id": - case "groups": - throw new Exception("@todo READ ONLY FIELD: $column"); - break; - case "name": - case "full_name": - case "password": - case "login_count": - case "last_login": - case "email": - case "admin": - case "guest": - case "hash": - case "url": - case "locale": - case "hashed_password": - unset($this->user->$column); - break; - default: - throw new Exception("@todo UNSUPPORTED FIELD: $column"); - break; - } - } - - /** - * Return a url to the user's avatar image. - * @param integer $size the target size of the image (default 80px) - * @return string a url - */ - abstract public function avatar_url($size=80, $default=null); - - /** - * Return the best version of the user's name. Either their specified full name, or fall back - * to the user name. - * @return string - */ - abstract public function display_name(); - - /** - * Return the internal user object without the wrapper. - * This method is used by implementing classes to access the internal user object. - * Consider it pseudo private and only declared public as PHP as not internal or friend modifier - */ - public function _uncloaked() { - return $this->user; - } - - abstract public function save(); - abstract public function delete(); -} - -/** - * Group Data wrapper - */ -abstract class Group_Definition { - protected $group; - - public function __get($column) { - switch ($column) { - case "id": - case "name": - case "special": - case "users": - return $this->group->$column; - default: - throw new Exception("@todo UNSUPPORTED FIELD: $column"); - break; - } - } - - public function __set($column, $value) { - switch ($column) { - case "id": - case "users": - throw new Exception("@todo READ ONLY FIELD: $column"); - break; - case "name": - case "special": - $this->group->$column = $value; - default: - throw new Exception("@todo UNSUPPORTED FIELD: $column"); - break; - } - } - - public function __isset($column) { - return isset($this->group->$column); - } - - public function __unset($column) { - switch ($column) { - case "id": - case "users": - throw new Exception("@todo READ ONLY FIELD: $column"); - break; - case "name": - case "special": - unset($this->group->$column); - default: - throw new Exception("@todo UNSUPPORTED FIELD: $column"); - break; - } - } - - /** - * Return the internal group object without the wrapper. - * This method is used by implementing classes to access the internal group object. - * Consider it pseudo private and only declared public as PHP as not internal or friend modifier - */ - public function _uncloaked() { - return $this->group; - } +interface Group_Definition {} - abstract public function save(); - abstract public function delete(); - abstract public function add($user); - abstract public function remove($user); -} +interface User_Definition {} diff --git a/modules/gallery/tests/Albums_Controller_Test.php b/modules/gallery/tests/Albums_Controller_Test.php index 046cb5ad..fa46d924 100644 --- a/modules/gallery/tests/Albums_Controller_Test.php +++ b/modules/gallery/tests/Albums_Controller_Test.php @@ -43,6 +43,7 @@ class Albums_Controller_Test extends Unit_Test_Case { $_POST["column"] = "weight"; $_POST["direction"] = "ASC"; $_POST["csrf"] = access::csrf_token(); + $_POST["slug"] = "new_name"; $_POST["_method"] = "put"; access::allow(Identity::everybody(), "edit", $root); diff --git a/modules/gallery/tests/Photos_Controller_Test.php b/modules/gallery/tests/Photos_Controller_Test.php index cdb4ae4f..59c3f78a 100644 --- a/modules/gallery/tests/Photos_Controller_Test.php +++ b/modules/gallery/tests/Photos_Controller_Test.php @@ -31,7 +31,7 @@ class Photos_Controller_Test extends Unit_Test_Case { $root = ORM::factory("item", 1); $photo = photo::create( $root, MODPATH . "gallery/tests/test.jpg", "test.jpeg", - "test", "test", Session::active_user(), "slug"); + "test", "test", Session::active_user()->id, "slug"); $orig_name = $photo->name; $_POST["filename"] = "test.jpeg"; diff --git a/modules/gallery/views/admin_identity.html.php b/modules/gallery/views/admin_identity.html.php index dcf1dbc1..1405cacb 100644 --- a/modules/gallery/views/admin_identity.html.php +++ b/modules/gallery/views/admin_identity.html.php @@ -15,11 +15,11 @@ height:165, modal: true, overlay: { - backgroundColor: '#000', - opacity: 0.5 + backgroundColor: '#000', + opacity: 0.5 }, buttons: { - "Continue": function() { + "Continue": function() { $("##g-dialog form").submit(); }, Cancel: function() { diff --git a/modules/gallery/controllers/password.php b/modules/user/controllers/password.php index ce6d67b1..a8f1c5ca 100644 --- a/modules/gallery/controllers/password.php +++ b/modules/user/controllers/password.php @@ -32,7 +32,7 @@ class Password_Controller extends Controller { if (request::method() == "post") { $this->_change_password(); } else { - $user = Identity::lookup_user_by_hash(Input::instance()->get("key")); + $user = user::lookup_user_by_field("hash", Input::instance()->get("key")); if (!empty($user)) { print $this->_new_password_form($user->hash); } else { @@ -116,7 +116,7 @@ class Password_Controller extends Controller { private function _change_password() { $view = $this->_new_password_form(); if ($view->content->validate()) { - $user = Identity::lookup_user_by_hash(Input::instance()->get("key")); + $user = user::lookup_user_by_field("hash", Input::instance()->get("key")); if (empty($user)) { throw new Exception("@todo FORBIDDEN", 503); } diff --git a/modules/user/helpers/group.php b/modules/user/helpers/group.php index cf5c050f..8ad52564 100644 --- a/modules/user/helpers/group.php +++ b/modules/user/helpers/group.php @@ -42,14 +42,14 @@ class group_Core { * @see Identity_Driver::everbody. */ static function everybody() { - return Identity::instance()->everybody(); + return model_cache::get("group", 1); } /** * @see Identity_Driver::registered_users. */ static function registered_users() { - return Identity::instance()->everybody(); + return model_cache::get("group", 2); } /** diff --git a/modules/user/helpers/user.php b/modules/user/helpers/user.php index fa7b320f..5ef2b726 100644 --- a/modules/user/helpers/user.php +++ b/modules/user/helpers/user.php @@ -25,14 +25,21 @@ */ class user_Core { /** - * @see Identity_Driver::guest. + * Return the guest user. + * + * @return User_Model the user object */ static function guest() { return model_cache::get("user", 1); } /** - * @see Identity_Driver::create_user. + * Create a new user. + * + * @param string $name + * @param string $full_name + * @param string $password + * @return User_Definition the user object */ static function create($name, $full_name, $password) { $user = ORM::factory("user")->where("name", $name)->find(); @@ -53,7 +60,9 @@ class user_Core { } /** - * @see Identity_Driver::hash_password. + * Hash the password to the internal value + * @param string $password the user password + * @param string The hashed equivalent */ static function hash_password($password) { require_once(MODPATH . "user/lib/PasswordHash.php"); diff --git a/modules/user/libraries/drivers/Identity/Gallery.php b/modules/user/libraries/drivers/Identity/Gallery.php index 77db11a3..f405b710 100644 --- a/modules/user/libraries/drivers/Identity/Gallery.php +++ b/modules/user/libraries/drivers/Identity/Gallery.php @@ -25,14 +25,14 @@ class Identity_Gallery_Driver implements Identity_Driver { * @see Identity_Driver::guest. */ public function guest() { - return new Gallery_User(user::guest()); + return user::guest(); } /** * @see Identity_Driver::create_user. */ public function create_user($name, $full_name, $password) { - return new Gallery_User(user::create($name, $full_name, $password)); + return user::create($name, $full_name, $password); } /** @@ -67,122 +67,55 @@ class Identity_Gallery_Driver implements Identity_Driver { } /** - * @see Identity_Driver::hash_password. + * @see Identity_Driver::lookup_user. */ - public function hash_password($password) { - return user::hash_password($password); + public function lookup_user($id) { + return user::lookup_by_field("id", $id); } /** - * @see Identity_Driver::lookup_user_by_field. + * @see Identity_Driver::lookup_user_by_name. */ - public function lookup_user_by_field($field_name, $value) { - return new Gallery_User(user::lookup_by_field($field_name, $value)); + public function lookup_user_by_name($name) { + return user::lookup_by_field("name", $name); } /** * @see Identity_Driver::create_group. */ public function create_group($name) { - return new Gallery_Group(group::create($name)); + return group::create($name); } /** * @see Identity_Driver::everybody. */ public function everybody() { - return new Gallery_Group(group::everybody()); + return group::everybody(); } /** * @see Identity_Driver::registered_users. */ public function registered_users() { - return new Gallery_Group(group::registered_users()); + return group::registered_users(); } /** - * @see Identity_Driver::lookup_group_by_field. + * @see Identity_Driver::lookup_group_by_name. */ - public function lookup_group_by_field($field_name, $value) { - return new Gallery_Group(group::lookup_by_field($field_name, $value)); + static function lookup_group_by_name($name) { + return group::lookup_by_field("name", $name); } /** * @see Identity_Driver::get_user_list. */ public function get_user_list($ids) { - $results = ORM::factory("user") + return ORM::factory("user") ->in("id", ids) ->find_all() - ->as_array();; - $users = array(); - foreach ($results as $user) { - $users[] = new Gallery_User($user); - } - return $users; + ->as_array(); } } // End Identity Gallery Driver -/** - * User Data wrapper - */ -class Gallery_User extends User_Definition { - /* - * Not for general user, allows the back-end to easily create the interface object - */ - function __construct($user) { - $this->user = $user; - } - - /** - * @see User_Definition::avatar_url - */ - public function avatar_url($size=80, $default=null) { - return $this->user->avatar_url($size, $default); - } - - /** - * @see User_Definition::display_name - */ - public function display_name() { - return $this->user->display_name(); - } - - public function save() { - $this->user->save(); - } - - public function delete() { - $this->user->delete(); - } - -} - -/** - * Group Data wrapper - */ -class Gallery_Group extends Group_Definition { - /* - * Not for general user, allows the back-end to easily create the interface object - */ - function __construct($group) { - $this->group = $group; - } - - public function save() { - $this->group->save(); - } - - public function delete() { - $this->group->delete(); - } - - public function add($user) { - $this->group->add($user->_uncloaked()); - } - - public function remove($user) { - $this->group->remove($user->_uncloaked()); - } -} diff --git a/modules/user/models/group.php b/modules/user/models/group.php index 8af78012..4432fc69 100644 --- a/modules/user/models/group.php +++ b/modules/user/models/group.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Group_Model extends ORM { +class Group_Model extends ORM implements Group_Definition { protected $has_and_belongs_to_many = array("users"); var $rules = array( diff --git a/modules/user/models/user.php b/modules/user/models/user.php index d99603b2..c51fc720 100644 --- a/modules/user/models/user.php +++ b/modules/user/models/user.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class User_Model extends ORM { +class User_Model extends ORM implements User_Definition { protected $has_and_belongs_to_many = array("groups"); var $rules = array( diff --git a/modules/user/views/admin_users.html.php b/modules/user/views/admin_users.html.php index 7c54d93d..ee8d413c 100644 --- a/modules/user/views/admin_users.html.php +++ b/modules/user/views/admin_users.html.php @@ -91,7 +91,7 @@ open_text="<?= t("close") ?>" class="g-panel-link g-button ui-state-default ui-corner-all ui-icon-left"> <span class="ui-icon ui-icon-pencil"></span><span class="g-button-text"><?= t("edit") ?></span></a> - <? if (user::active()->id != $user->id && !$user->guest): ?> + <? if (Session::active_user()->id != $user->id && !$user->guest): ?> <a href="<?= url::site("admin/users/delete_user_form/$user->id") ?>" class="g-dialog-link g-button ui-state-default ui-corner-all ui-icon-left"> <span class="ui-icon ui-icon-trash"></span><?= t("delete") ?></a> diff --git a/modules/gallery/views/reset_password.html.php b/modules/user/views/reset_password.html.php index 92ca4917..92ca4917 100644 --- a/modules/gallery/views/reset_password.html.php +++ b/modules/user/views/reset_password.html.php |