[Fusionforge-commits] r9865 - trunk/gforge/common/include

Roland Mas lolando at libremir.placard.fr.eu.org
Fri May 21 11:26:03 CEST 2010


Author: lolando
Date: 2010-05-21 11:26:03 +0200 (Fri, 21 May 2010)
New Revision: 9865

Modified:
   trunk/gforge/common/include/RBAC.php
   trunk/gforge/common/include/RBACEngine.class.php
Log:
Cache roles in order to go back to a slightly more reasonable number of SQL queries per page

Modified: trunk/gforge/common/include/RBAC.php
===================================================================
--- trunk/gforge/common/include/RBAC.php	2010-05-21 08:57:54 UTC (rev 9864)
+++ trunk/gforge/common/include/RBAC.php	2010-05-21 09:26:03 UTC (rev 9865)
@@ -652,25 +652,27 @@
 class RoleAnonymous extends BaseRole implements PFO_RoleAnonymous {
 	// This role is implemented as a singleton
 	private static $_instance ;
-	private static $_role_id ;
+	private $_role_id ;
 	public static function getInstance() {
-		$c = __CLASS__ ;
-		if (!isset(self::$_instance)) {
-			self::$_instance = new $c ;
+		if (isset(self::$_instance)) {
+			return self::$_instance ;
 		}
 
+		$c = __CLASS__ ;
+		self::$_instance = new $c ;
+		
 		$res = db_query_params ('SELECT r.role_id FROM pfo_role r, pfo_role_class c WHERE r.role_class = c.class_id AND c.class_name = $1',
 					array ('PFO_RoleAnonymous')) ;
 		if (!$res || !db_numrows($res)) {
 			throw new Exception ("No PFO_RoleAnonymous role in the database") ;
 		}
-		self::$_role_id = db_result ($res, 0, 'class_name') ;
+		self::$_instance->_role_id = db_result ($res, 0, 'role_id') ;
 
 		return self::$_instance ;
 	}
 
 	public function getID () {
-		return self::$_role_id ;
+		return $this->_role_id ;
 	}
 	public function isPublic () {
 		return true ;
@@ -692,25 +694,27 @@
 class RoleLoggedIn extends BaseRole implements PFO_RoleLoggedIn {
 	// This role is implemented as a singleton
 	private static $_instance ;
-	private static $_role_id ;
+	private $_role_id ;
 	public static function getInstance() {
-		$c = __CLASS__ ;
-		if (!isset(self::$_instance)) {
-			self::$_instance = new $c ;
+		if (isset(self::$_instance)) {
+			return self::$_instance ;
 		}
 
+		$c = __CLASS__ ;
+		self::$_instance = new $c ;
+		
 		$res = db_query_params ('SELECT r.role_id FROM pfo_role r, pfo_role_class c WHERE r.role_class = c.class_id AND c.class_name = $1',
 					array ('PFO_RoleLoggedIn')) ;
 		if (!$res || !db_numrows($res)) {
 			throw new Exception ("No PFO_RoleLoggedIn role in the database") ;
 		}
-		self::$_role_id = db_result ($res, 0, 'class_name') ;
+		self::$_instance->_role_id = db_result ($res, 0, 'role_id') ;
 
 		return self::$_instance ;
 	}
 
 	public function getID () {
-		return self::$_role_id ;
+		return $this->_role_id ;
 	}
 	public function isPublic () {
 		return true ;

Modified: trunk/gforge/common/include/RBACEngine.class.php
===================================================================
--- trunk/gforge/common/include/RBACEngine.class.php	2010-05-21 08:57:54 UTC (rev 9864)
+++ trunk/gforge/common/include/RBACEngine.class.php	2010-05-21 09:26:03 UTC (rev 9865)
@@ -26,6 +26,7 @@
 
 class RBACEngine extends Error implements PFO_RBACEngine {
 	private static $_instance ;
+	private $_cached_roles = array () ;
 
 	public static function getInstance() {
 		if (!isset(self::$_instance)) {
@@ -105,6 +106,9 @@
 	}
 
 	public function getRoleById ($role_id) {
+		if (array_key_exists ($role_id, $this->_cached_roles)) {
+			return $this->_cached_roles[$role_id] ;
+		}
 		if (USE_PFO_RBAC) {
 			$res = db_query_params ('SELECT c.class_name, r.home_group_id FROM pfo_role r, pfo_role_class c WHERE r.role_class = c.class_id AND r.role_id = $1',
 						array ($role_id)) ;
@@ -117,11 +121,14 @@
 			case 'PFO_RoleExplicit':
 				$group_id = db_result ($res, 0, 'home_group_id') ;
 				$group = group_get_object ($group_id) ;
-				return new Role ($group, $role_id) ;
+				$this->_cached_roles[$role_id] = new Role ($group, $role_id) ;
+				return $this->_cached_roles[$role_id] ;
 			case 'PFO_RoleAnonymous':
-				return RoleAnonymous::getInstance() ;
+				$this->_cached_roles[$role_id] = RoleAnonymous::getInstance() ;
+				return $this->_cached_roles[$role_id] ;
 			case 'PFO_RoleLoggedIn':
-				return RoleLoggedIn::getInstance() ;
+				$this->_cached_roles[$role_id] = RoleLoggedIn::getInstance() ;
+				return $this->_cached_roles[$role_id] ;
 			default:
 				throw new Exception ("Not implemented") ;
 			}




More information about the Fusionforge-commits mailing list