[Cs-webapplibs-commits] SF.net SVN: cs-webapplibs:[201] trunk/0.4
Status: Beta
Brought to you by:
crazedsanity
From: <cra...@us...> - 2011-01-27 06:18:16
|
Revision: 201 http://cs-webapplibs.svn.sourceforge.net/cs-webapplibs/?rev=201&view=rev Author: crazedsanity Date: 2011-01-27 06:18:09 +0000 (Thu, 27 Jan 2011) Log Message: ----------- permissions to allow id-paths; use dbTableHandler to make SQL easier. /cs_genericPermission.class.php: * MAIN::: -- changed objTable to permTable, from const to protected -- changed objSeq to permSeq, from const to protected -- objectDelimiter [NEW]: character to separate objects in a path. -- pathCleaner [NEW]: how to clean the path -- dbTableHandler [NEW]: object to create & retrieve data from the db. * __construct(): -- ARG CHANGE: NEW ARG: #2 ($objectDelimiter=NULL) -- ARG CHANGE: NEW ARG: #3 ($useUrlCleaner=true) -- set internal vars for objectDelimiter and useUrlCleaner -- setup internal var for dbTableHandler * build_permission_string(): -- added comment to explain what might otherwise seem illogical -- more helpful information in the event of an exception * create_permission(): -- minor column name changes (object_name to object_path) -- use dbTableHandler::create_record() instead of arbitrary SQL. * get_object() [DELETED]: -- unnecessary alias method * get_permission(): -- use dbTableHandler::get_single_record() instead of arbitrary SQL. * get_object_by_id() [DELETED]: -- unnecessary alias method * get_permission_by_id(): -- use dbTableHandler::get_record_by_id() instead of arbitrary SQL. * get_permission_list(): -- remove some commented-out code. /abstract/cs_genericGroup.abstract.class.php: * MAIN::: -- changed groupTable from constant to protected -- changed groupSeq from constant to protected -- dbTableHandler [NEW]: object to create & retrieve data from db. * __construct(): -- setup the dbTableHandler object. * create_group(): -- use dbTableHandler::create_record() instead of arbitrary SQL. * get_group(): -- use dbTableHandler::get_single_record() instead of arbitrary SQL. * get_group_by_id(): -- use dbTableHandler::get_record_by_id() instead of arbitrary SQL. /abstract/cs_genericUserGroup.abstract.class.php: * MAIN::: -- changed ugTable from constant to protected -- changed ugSeq from constant to protected -- dbTableHandler [NEW]: for table interaction * __construct(): -- set dbTableHandler object * create_user_group(): -- use dbTableHandler::create_record() * get_user_groups(): -- use dbTableHandler::get_records() * is_group_member(): -- fixed to work properly /setup/genericPermissions.pgsql.sql: * cswal_object_table: -- updated comment -- remove "is_hidden" column, unnecessary (and confusing) * INSERT STATEMENTS::: -- updated to match new schema. /tests/testOfCSGenericPermissions.php: * test_userGroups(): -- updated tests * test_permissions(): -- removed calls to deleted methods Modified Paths: -------------- trunk/0.4/abstract/cs_genericGroup.abstract.class.php trunk/0.4/abstract/cs_genericUserGroup.abstract.class.php trunk/0.4/cs_genericPermission.class.php trunk/0.4/setup/genericPermissions.pgsql.sql trunk/0.4/tests/testOfCSGenericPermissions.php Modified: trunk/0.4/abstract/cs_genericGroup.abstract.class.php =================================================================== --- trunk/0.4/abstract/cs_genericGroup.abstract.class.php 2011-01-27 05:04:56 UTC (rev 200) +++ trunk/0.4/abstract/cs_genericGroup.abstract.class.php 2011-01-27 06:18:09 UTC (rev 201) @@ -20,15 +20,25 @@ public $gfObj; /** Table name used to store groups. */ - const groupTable = "cswal_group_table"; + protected $groupTable = "cswal_group_table"; /** Sequence for groups table. */ - const groupSeq = "cswal_group_table_group_id_seq"; + protected $groupSeq = "cswal_group_table_group_id_seq"; + /** Table handler object for simple SQL handling */ + private $dbTableHandler; + //============================================================================ public function __construct(cs_phpDB $db) { $this->db = $db; $this->gfObj = new cs_globalFunctions; + + //setup table handler. + $cleanString = array( + 'group_name' => 'text', + 'group_admin' => 'integer' + ); + $this->dbTableHandler = new cs_dbTableHandler($this->db, $this->groupTable, $this->groupSeq, 'group_id', $cleanString); }//end __construct() //============================================================================ @@ -51,9 +61,7 @@ //============================================================================ public function create_group($name) { try{ - $name = $this->clean_group_name($name); - $sql = "INSERT INTO ". self::groupTable ." (group_name) VALUES ('". $name ."')"; - $newId = $this->db->run_insert($sql, self::groupSeq); + $newId = $this->dbTableHandler->create_record(array('group_name'=>$this->clean_group_name($name))); } catch(Exception $e) { throw new exception(__METHOD__ .":: failed to create new record, DETAILS::: ". $e->getMessage()); @@ -68,9 +76,7 @@ //============================================================================ public function get_group($name) { try { - $name = $this->clean_group_name($name); - $sql = "SELECT * FROM ". self::groupTable ." WHERE group_name='". $name ."'"; - $retval = $this->db->run_query($sql); + $retval = $this->dbTableHandler->get_single_record(array('group_name' => $this->clean_group_name($name))); } catch(Exception $e) { throw new exception(__METHOD__ .":: error while locating group '". $name ."', DETAILS::: ". $e->getMessage()); @@ -85,8 +91,7 @@ //============================================================================ public function get_all_groups() { try { - $sql = "SELECT * FROM ". self::groupTable ." ORDER BY group_name"; - $retval = $this->db->run_query($sql); + $retval = $this->dbTableHandler->get_records(); } catch(Exception $e) { throw new exception(__METHOD__ .":: failed to retrieve groups, DETAILS::: ". $e->getMessage()); @@ -102,8 +107,7 @@ public function get_group_by_id($groupId) { try { if(!is_null($groupId) && is_numeric($groupId)) { - $sql = "SELECT * FROM ". self::groupTable ." WHERE group_id='". $groupId ."'"; - $retval = $this->db->run_query($sql); + $retval = $this->dbTableHandler->get_record_by_id($groupId); } else { throw new exception(__METHOD__ .":: invalid group ID (". $groupId .")"); Modified: trunk/0.4/abstract/cs_genericUserGroup.abstract.class.php =================================================================== --- trunk/0.4/abstract/cs_genericUserGroup.abstract.class.php 2011-01-27 05:04:56 UTC (rev 200) +++ trunk/0.4/abstract/cs_genericUserGroup.abstract.class.php 2011-01-27 06:18:09 UTC (rev 201) @@ -1,8 +1,6 @@ <?php /* - * Created on June 18, 2010 - * * FILE INFORMATION: * * $HeadURL$ @@ -15,14 +13,22 @@ abstract class cs_genericUserGroupAbstract extends cs_genericGroupAbstract { /** Table name used to store user_group records. */ - const ugTable = "cswal_user_group_table"; + protected $ugTable = "cswal_user_group_table"; /** Sequence for user_group table. */ - const ugSeq = "cswal_user_group_table_user_group_id_seq"; + protected $ugSeq = "cswal_user_group_table_user_group_id_seq"; + /** dbTableHandler{} object for simplifying SQL. */ + private $dbTableHandler; + //============================================================================ public function __construct(cs_phpDB $db) { parent::__construct($db); + $cleanString = array( + 'user_id' => 'integer', + 'group_id' => 'integer' + ); + $this->dbTableHandler = new cs_dbTableHandler($this->db, $this->ugTable, $this->ugSeq, 'user_group_id', $cleanString); }//end __construct() //============================================================================ @@ -32,8 +38,7 @@ public function create_user_group($userId, $groupId) { if(is_numeric($userId) && is_numeric($groupId) && $userId >= 0 && $groupId >= 0) { try { - $sql = "INSERT INTO ". self::ugTable ." (user_id, group_id) VALUES (". $userId .", ". $groupId .")"; - $newId = $this->db->run_insert($sql, self::ugSeq); + $newId = $this->dbTableHandler->create_record(array('user_id'=>$userId,'group_id'=>$groupId)); } catch(Exception $e) { throw new exception(__METHOD__ .":: failed to create user group, DETAILS::: ". $e->getMessage()); @@ -52,9 +57,7 @@ public function get_user_groups($userId) { if(is_numeric($userId) && $userId >= 0) { try { - $sql = "SELECT ug.*, g.group_name, g.group_admin FROM ". self::ugTable ." AS ug INNER " - ."JOIN ". parent::groupTable ." as g USING (group_id) WHERE user_id=". $userId; - $retval = $this->db->run_query($sql, 'group_id'); + $retval = $this->dbTableHandler->get_records(array('user_id'=>$userId)); } catch(Exception $e) { throw new exception(__METHOD__ .":: failed to locate groups for user_id=(". $userId ."), DETAILS::: ". $e->getMessage()); @@ -73,8 +76,11 @@ public function is_group_member($userId, $groupId) { $groupList = $this->get_user_groups($userId); $retval = false; - if(isset($groupList[$groupId])) { - $retval = true; + if(is_array($groupList)) { + $keys = array_keys($groupList); + if($groupList[$keys[0]]['group_id'] == $groupId) { + $retval = true; + } } return($retval); }//end is_group_member() Modified: trunk/0.4/cs_genericPermission.class.php =================================================================== --- trunk/0.4/cs_genericPermission.class.php 2011-01-27 05:04:56 UTC (rev 200) +++ trunk/0.4/cs_genericPermission.class.php 2011-01-27 06:18:09 UTC (rev 201) @@ -20,19 +20,29 @@ public $gfObj; /** Table name used to store permissions. */ - const objTable = "cswal_object_table"; + protected $permTable = "cswal_permission_table"; /** Sequence for permissions table. */ - const objSeq = "cswal_object_table_object_id_seq"; + protected $permSeq = "cswal_permission_table_permission_id_seq"; /** List of valid keys... */ protected $keys = array(); + /** Determine object path pieces based on this... */ + protected $objectDelimiter="/"; + + /** How to clean the path (if at all); boolean true = use cs_globalFunctions::clean_url(); boolean false will + cause it to not be cleaned at all; a string will use cs_globalFunctions::cleanString({string})*/ + protected $pathCleaner=true; + + /** dbTableHandler{} object for easier SQL. */ + private $dbTableHandler; + //============================================================================ /** * Generic permission system based on *nix filesystem permissions. */ - public function __construct(cs_phpDB $db) { + public function __construct(cs_phpDB $db, $objectDelimiter=NULL, $useUrlCleaner=true) { $this->db = $db; parent::__construct($db); $this->gfObj = new cs_globalFunctions; @@ -47,6 +57,29 @@ 7 => 'o_w', 8 => 'o_x' ); + if(!is_null($objectDelimiter) && is_string($objectDelimiter) && strlen($objectDelimiter)) { + $this->objectDelimiter=$objectDelimiter; + } + if(is_bool($useUrlCleaner) || (is_string($useUrlCleaner) && strlen($useUrlCleaner))) { + $this->pathCleaner = $useUrlCleaner; + } + $cleanString = array( + 'system_name' => 'integer', + 'object_path' => 'text', + 'user_id' => 'integer', + 'group_id' => 'integer', + 'inherit' => 'bool', + 'u_r' => 'bool', + 'u_w' => 'bool', + 'u_x' => 'bool', + 'g_r' => 'bool', + 'g_w' => 'bool', + 'g_x' => 'bool', + 'o_r' => 'bool', + 'o_w' => 'bool', + 'o_x' => 'bool', + ); + $this->dbTableHandler = new cs_dbTableHandler($this->db, $this->permTable, $this->permSeq, 'permission_id', $cleanString); }//end __construct() //============================================================================ @@ -98,6 +131,9 @@ */ protected function build_permission_string(array $perms) { $this->_sanityCheck(); + + //NOTE:: the incoming $perms must have more (or equal) items vs. $this->keys so that it can accept arrays with extra + // items, but can disregard those that obviously do not have enough. if(is_array($perms) && count($perms) >= count($this->keys)) { $retval = ""; foreach($this->keys as $dbColName) { @@ -118,7 +154,11 @@ } } else { - throw new exception(__METHOD__ .":: invalid permission set."); + $extraInfo=""; + if(!is_array($perms)) { + $extraInfo = " (expected array, received ". gettype($perms) ." '". $perms ."')"; + } + throw new exception(__METHOD__ .":: invalid permission set". $extraInfo); } return($retval); }//end build_permission_string(); @@ -144,7 +184,7 @@ public function create_permission($name, $userId, $groupId, $permString) { if(is_string($name) && strlen($name) && is_numeric($userId) && $userId >= 0 && is_numeric($groupId) && $groupId >= 0) { $cleanStringArr = array( - 'object_name' => 'sql', + 'object_path' => 'sql', 'user_id' => 'numeric', 'group_id' => 'numeric', 'u_r' => 'bool', @@ -159,13 +199,11 @@ ); try{ $insertArr = $this->parse_permission_string($permString); - $insertArr['object_name'] = $this->gfObj->cleanString($name, 'sql', 0); + $insertArr['object_path'] = $this->gfObj->cleanString($name, 'sql', 0); $insertArr['user_id'] = $userId; $insertArr['group_id'] = $groupId; - $insertSql = $this->gfObj->string_from_array($insertArr, 'insert', null, $cleanStringArr); - $sql = "INSERT INTO ". self::objTable ." ". $insertSql; - $newId = $this->db->run_insert($sql, self::objSeq); + $newId = $this->dbTableHandler->create_record($insertArr); } catch(Exception $e) { throw new exception(__METHOD__ .":: failed to create new record, DETAILS::: ". $e->getMessage()); @@ -183,24 +221,11 @@ //============================================================================ /** - * Same as get_permission(). - */ - public function get_object($name) { - return($this->get_permission($name)); - }//end get_object() - //============================================================================ - - - - //============================================================================ - /** * Retrieves a permission object by name from the database, exception on failure. */ public function get_permission($name) { try { - $name = $this->gfObj->cleanString($name, 'sql', 0); - $sql = "SELECT * FROM ". self::objTable ." WHERE object_name='". $name ."'"; - $retval = $this->db->run_query($sql); + $retval = $this->dbTableHandler->get_single_record(array('object_path'=>$name)); } catch(Exception $e) { throw new exception(__METHOD__ .":: error while locating permission '". $name ."', DETAILS::: ". $e->getMessage()); @@ -214,24 +239,12 @@ //============================================================================ /** - * Same as get_permission_by_id(). - */ - public function get_object_by_id($objectId) { - return($this->get_permission_by_id($objectId)); - }//end get_object_by_id() - //============================================================================ - - - - //============================================================================ - /** * Retrieves a permission object from the database based on an ID. */ public function get_permission_by_id($permId) { try { if(!is_null($permId) && is_numeric($permId)) { - $sql = "SELECT * FROM ". self::objTable ." WHERE object_id='". $permId ."'"; - $retval = $this->db->run_query($sql); + $retval = $this->dbTableHandler->get_record_by_id($permId); } else { throw new exception(__METHOD__ .":: invalid permission ID (". $permId .")"); @@ -298,7 +311,6 @@ if(preg_match('/'. $type .'_[rwx]$/',$myKey)) { //chop the last character off (i.e. 'r' from 'u_r') $myPermChar = substr($myKey, -1); - #$retval[$myPermChar] = $this->gfObj->interpret_bool($permData[$myKey], array('f', 't')); $retval[$myPermChar] = $this->evaluate_perm_value($permData[$myKey], $type); } } Modified: trunk/0.4/setup/genericPermissions.pgsql.sql =================================================================== --- trunk/0.4/setup/genericPermissions.pgsql.sql 2011-01-27 05:04:56 UTC (rev 200) +++ trunk/0.4/setup/genericPermissions.pgsql.sql 2011-01-27 06:18:09 UTC (rev 201) @@ -40,13 +40,11 @@ -- -- Object table -- Unique set of names which should be chained together to create an object path; for a URL of "/member/blog/edit", the pieces would be created --- with ID's, such as "member"=1, "blog"=2, "edit"=3; the object path would then be ":1::2::3:"; an extra prefix element might be created to --- define a default, inheritable set of permissions, such as "{root}"=10; the path might then be ":10::1::2::3:". +-- with ID's, such as "member"=1, "blog"=2, "edit"=3; the object path would then be ":1::2::3:". -- CREATE TABLE cswal_object_table ( object_id integer NOT NULL PRIMARY KEY, object_name text NOT NULL UNIQUE, - is_hidden boolean NOT NULL DEFAULT FALSE, created TIMESTAMPTZ NOT NULL DEFAULT NOW() ); @@ -84,8 +82,8 @@ INSERT INTO cswal_group_table (group_name) VALUES ('blogs'); INSERT INTO cswal_group_table (group_name) VALUES ('admin'); -INSERT INTO cswal_object_table (object_id, object_name,is_hidden) VALUES (0, '/', true); -INSERT INTO cswal_object_table (object_id, object_name,is_hidden) VALUES (1, 'member', false); +INSERT INTO cswal_object_table (object_id, object_name) VALUES (0, '{APPURL}'); +INSERT INTO cswal_object_table (object_id, object_name) VALUES (1, 'member'); INSERT INTO cswal_permission_table (object_path,user_id, group_id) Modified: trunk/0.4/tests/testOfCSGenericPermissions.php =================================================================== --- trunk/0.4/tests/testOfCSGenericPermissions.php 2011-01-27 05:04:56 UTC (rev 200) +++ trunk/0.4/tests/testOfCSGenericPermissions.php 2011-01-27 06:18:09 UTC (rev 201) @@ -89,9 +89,9 @@ $groupList = $this->permObj->get_all_groups(); - foreach($groupList as $groupData) { - $this->assertEqual($this->permObj->get_group_by_id($groupData['group_id']), $groupData); - $this->assertEqual($this->permObj->get_group($groupData['group_name']), $groupData); + foreach($groupList as $groupId=>$groupData) { + $this->assertEqual($this->permObj->get_group_by_id($groupId), $groupData, "failed to get group (". $groupData['group_name'] .") by ID (". $groupId .")"); + $this->assertEqual($this->permObj->get_group($groupData['group_name']), $groupData, "failed to get group (". $groupData['group_name'] .") by name"); } } @@ -100,8 +100,7 @@ $newId = $this->permObj->create_user_group($this->validUsers[$myKey]['uid'],$newGroupId); $this->assertTrue(is_numeric($newId)); $this->assertTrue($this->permObj->is_group_member($this->validUsers[$myKey]['uid'],$newGroupId), "user (". - $this->validUsers[$myKey]['uid'] .") isn't member of group (". $newGroupId .") after ". - "being added to it... "); + $this->validUsers[$myKey]['uid'] .") isn't member of group (". $newGroupId .") after being added to it... "); $ugList = $this->permObj->get_user_groups($this->validUsers[$myKey]['uid']); $this->assertTrue(is_array($ugList)); @@ -175,9 +174,7 @@ //the method 'build_permissions_string()' should disregard extra indices in the array & build the string. $this->assertEqual($this->permObj->make_perm_string($this->permObj->get_permission_by_id($permId)), $usePermString); - $this->assertEqual($this->permObj->make_perm_string($this->permObj->get_object_by_id($permId)), $usePermString); $this->assertEqual($this->permObj->make_perm_string($this->permObj->get_permission($usePermName)), $usePermString); - $this->assertEqual($this->permObj->make_perm_string($this->permObj->get_object($usePermName)), $usePermString); //check to make sure individual permission requests work as expected. $this->assertTrue($this->permObj->has_read_permission($myUid, $usePermName)); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |