[pLog-svn] r3986 - in plog/trunk/class: dao test/tests/dao

oscar at devel.lifetype.net oscar at devel.lifetype.net
Mon Sep 18 12:32:34 GMT 2006


Author: oscar
Date: 2006-09-18 12:32:34 +0000 (Mon, 18 Sep 2006)
New Revision: 3986

Added:
   plog/trunk/class/dao/permission.class.php
   plog/trunk/class/dao/permissions.class.php
   plog/trunk/class/test/tests/dao/permissions_test.class.php
Modified:
   plog/trunk/class/dao/daocacheconstants.properties.php
Log:
Added some code and tests as the foundation of the new user permissions framework. The idea is that the permissions table will 
hold a list of permissions that will be assigned to each user, and this list will be extensible. There will be a list of "core" permissions
that cannot be deleted (because they will be used by core code) and plugins can add new permissions. Action classes will 
know which permissions they require and reject requests of users without thoes capabilies, and the lt_users_permissions table will
be used to link between users and permissions.


Modified: plog/trunk/class/dao/daocacheconstants.properties.php
===================================================================
--- plog/trunk/class/dao/daocacheconstants.properties.php	2006-09-18 08:55:25 UTC (rev 3985)
+++ plog/trunk/class/dao/daocacheconstants.properties.php	2006-09-18 12:32:34 UTC (rev 3986)
@@ -105,4 +105,10 @@
 	 * locales
 	 */	
     define( "CACHE_LOCALES", "locales" );
+    
+    /**
+     * permissions
+     */
+    define( "CACHE_PERMISSIONS_ALL", "permissions_all" );
+    define( "CACHE_PERMISSIONS", "permissions" );
 ?>
\ No newline at end of file

Added: plog/trunk/class/dao/permission.class.php
===================================================================
--- plog/trunk/class/dao/permission.class.php	2006-09-18 08:55:25 UTC (rev 3985)
+++ plog/trunk/class/dao/permission.class.php	2006-09-18 12:32:34 UTC (rev 3986)
@@ -0,0 +1,73 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/dao/model.class.php" );
+    include_once( PLOG_CLASS_PATH."class/database/dbobject.class.php" );
+
+    /**
+     * \ingroup DAO
+     *
+     * Object that maps a row of the permissions table
+     */
+	class Permission extends DbObject
+	{
+		var $_id;
+		var $_name;
+		var $_description;
+	
+		/**
+		 * Constructor
+		 *
+		 * @param name Name of the permission
+		 * @param description Description of the permission. Please use a locale id/key
+		 * instead of a text string. Translation will be provided later on in the user interface
+		 * based on this key
+		 * @param id of the permission
+		 */
+		function Permission( $name, $description, $id = -1 )
+		{
+			$this->DbObject();
+			
+			$this->pk = "id";
+			
+			$this->_name = $name;
+			$this->_description = $description;
+			$this->_id = $id;
+			
+			$this->_fields = Array(
+				"permission" => "getName",
+				"description" => "getDescription",
+				"id" => "getId"
+			);
+		}
+		
+		function getId()
+		{
+			return( $this->_id );
+		}
+		
+		function setId( $id )
+		{
+			$this->_id = $id;	
+		}
+		
+		function getName()
+		{
+			return( $this->_name );
+		}
+		
+		function setName( $name )
+		{
+			$this->_name = $name;
+		}		
+		
+		function getDescription()
+		{
+			return( $this->_description );	
+		}
+		
+		function setDescription( $desc )
+		{
+			$this->_description = $desc;
+		}		
+	}
+?>
\ No newline at end of file

Added: plog/trunk/class/dao/permissions.class.php
===================================================================
--- plog/trunk/class/dao/permissions.class.php	2006-09-18 08:55:25 UTC (rev 3985)
+++ plog/trunk/class/dao/permissions.class.php	2006-09-18 12:32:34 UTC (rev 3986)
@@ -0,0 +1,108 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/dao/model.class.php" );
+    include_once( PLOG_CLASS_PATH."class/dao/permission.class.php" );	
+	include_once( PLOG_CLASS_PATH."class/dao/daocacheconstants.properties.php" );	
+    
+	/**
+	 * \ingroup DAO
+	 *
+	 * Model class that manages the 'permissions' table
+	 */ 
+	class Permissions extends Model
+	{
+	
+		function Permissions()
+		{
+			$this->Model();
+			$this->table = $this->getPrefix()."permissions";
+		}
+	
+		/**
+		 * Add a new permission
+		 *
+		 * @param perm A Permission object
+		 * @return True if successful or false otherwise
+		 */	
+		function addPermission( &$perm )
+		{
+			if( ($result = $this->add( $perm, Array( CACHE_PERMISSIONS => "getId" )))) {
+				$this->_cache->removeData( "_all_", CACHE_PERMISSIONS_ALL );
+			}
+				
+			return( $result );			
+		}
+		
+		/**
+		 * Retrieve a permission given its id
+		 *
+		 * @param id The id of the permission
+		 * @return A Permission object if successful or false otherwise
+		 */
+		function getPermission( $id )
+		{
+			return( $this->get( "id", $id, CACHE_PERMISSIONS ));			
+		}
+		
+		/**
+		 * Retrieve a permission given its name
+		 *
+		 * @param name The name of the permission
+		 * @return A Permission object if successful or false otherwise
+		 */		
+		function getPermissionByName( $name )
+		{
+			$perms = $this->getAllPermissions();
+			foreach( $perms as $perm ) {
+				if( $perm->getName() == $name ) {
+					return( $perm );	
+				}	
+			}
+			
+			return( false );
+		}
+		
+		/**
+		 * Deletes a permission given its id
+		 *
+		 * @param id The id of the permission
+		 * @return True if successful or false otherwise
+		 */		 
+		function deletePermission( $id )
+		{
+			if( ($result = $this->delete( "id", $id ))) {
+				$this->_cache->removeData( $id, CACHE_PERMISSIONS );
+				$this->_cache->removeData( "_all_", CACHE_PERMISSIONS_ALL );
+			}
+			
+			return( $result );			
+		}
+		
+		/**
+		 * Loads all permissions from the database
+		 *
+		 * @return An array of Permission objects, or an empty array if no
+		 * permissions were found
+		 */
+		function getAllPermissions()
+		{
+			$permissions = $this->getAll( "all", 
+			                             CACHE_PERMISSIONS_ALL, 
+			                             Array( CACHE_PERMISSIONS => "getId" ),
+			                             Array( "permission" => "ASC" ));			                            
+			if( !$permissions )
+				return( Array());				
+			                             			                             
+			return( $permissions );			
+		}
+		
+		/**
+		 * @private
+		 */
+		function mapRow( $row )
+		{
+			$perm = new Permission( $row["permission"], $row["description"], $row["id"] );
+			return( $perm  );	
+		}
+	}
+?>
\ No newline at end of file

Added: plog/trunk/class/test/tests/dao/permissions_test.class.php
===================================================================
--- plog/trunk/class/test/tests/dao/permissions_test.class.php	2006-09-18 08:55:25 UTC (rev 3985)
+++ plog/trunk/class/test/tests/dao/permissions_test.class.php	2006-09-18 12:32:34 UTC (rev 3986)
@@ -0,0 +1,110 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/test/helpers/lifetypetestcase.class.php" );
+	include_once( PLOG_CLASS_PATH."class/dao/permission.class.php" );
+	include_once( PLOG_CLASS_PATH."class/dao/permissions.class.php" );
+
+	/**
+	 * \ingroup Test
+	 *
+	 * Test cases for the Permissions class
+	 */
+	class Permissions_Test extends LifeTypeTestCase
+	{
+		function testAddPermission()
+		{
+			$perm = new Permission( "test_perm", "test_perm_desc" );
+			$perms = new Permissions();
+			
+			// check that it was correctly added
+			$this->assertTrue( $perms->addPermission( $perm ), "Error adding new permission" );
+			
+			// check that the object got its id set correctly
+			$this->assertTrue( ($perm->getId() != -1 ), "New permission still has id = -1" );
+			
+			// check that it can be loaded from the database
+			$newPerm = $perms->getPermission( $perm->getId());
+			$this->assertTrue( $newPerm, "Error loading the new category from the database" );
+			
+			// and that the object contains the same values
+			$this->assertEquals( $perm->getName(), $newPerm->getName(), "new permission is not the same as original" );
+			$this->assertEquals( $perm->getDescription(), $newPerm->getDescription(), "new permission is not the same as original" );
+		}
+		
+		function testGetPermission()
+		{
+			// first part of the test is the same as testAddPermission
+			$this->testAddPermission();
+			
+			// while in the second half we test for errors
+			$perms = new Permissions();
+			$this->assertFalse( $perms->getPermission( 12312312311 ), "Permission should not have been found" );	
+			$this->assertFalse( $perms->getPermission( "blah" ), "Permission should not have been found" );				
+		}
+		
+		function testGetPermissions()
+		{
+			// load all permissions and check at least that it's an array
+			// :TODO: We could probably improve this test
+			$perm = new Permission( "to_be_deleted", "this will be deleted" );
+			$perms = new Permissions();
+			
+			// check that it was correctly added
+			$this->assertTrue( $perms->addPermission( $perm ), "Error adding new permission" );
+			
+			// try to load all permissions, there should at least be one member in the array
+			$allPerms = $perms->getAllPermissions();
+			$this->assertTrue( (count($allPerms) > 0 ), "getAllPermissions did not return all permissions" );
+			// make sure that the one we just got is one of them
+			$found = false;
+			$i = 0;
+			while( !$found && $i < count($allPerms)) {
+				$found = ($allPerms[$i]->getId() == $perm->getId());
+				$i++;
+			}
+			$this->assertTrue( $found, "Could not locate new permission after calling getAllPermissions" );
+			
+			// we don't need it anymore, let's delete it
+			$this->assertTrue( $perms->deletePermission( $perm->getId()), "There was an error deleting the permission" );			
+		}
+		
+		function testDeletePermission()
+		{
+			$perm = new Permission( "to_be_deleted", "this will be deleted" );
+			$perms = new Permissions();
+			
+			// check that it was correctly added
+			$this->assertTrue( $perms->addPermission( $perm ), "Error adding new permission" );			
+			
+			// now try to delete it
+			$this->assertTrue( $perms->deletePermission( $perm->getId()), "There was an error deleting the permission" );
+			
+			// and finally check that it isn't there anymore
+			$this->assertFalse( $perms->getPermission( $perm->getId(), "Deleted permission was still loaded again" ));	
+		}
+		
+		function testGetPermissionByName()
+		{
+			// test that a non-existant permission returns false
+			$perms = new Permissions();
+			$this->assertFalse( $perms->getPermissionByName( "afadsfasdfasfasdfasfs" ), "This permission should not exist" );	
+			
+			// add a test permission and see that it can be retrieved later on by name
+			$perm = new Permission( "to_be_retrieved", "this will be deleted" );
+			$perms = new Permissions();
+			
+			// check that it was correctly added
+			$this->assertTrue( $perms->addPermission( $perm ), "Error adding new permission" );			
+			
+			// retrieve it by name
+			$newPerm = $perms->getPermissionByName( "to_be_retrieved" );
+			$this->assertTrue( $newPerm, "It was not possible to retrieve the permission by name" );
+			
+			// check that it is the same
+			$this->assertEquals( $perm->getId(), $newPerm->getId(), "Permission retrieved by name did not match" );
+			
+			// and delete it
+			$this->assertTrue( $perms->deletePermission( $perm->getId()), "There was an error deleting the permission" );			
+		}
+	}
+?>
\ No newline at end of file



More information about the pLog-svn mailing list