Source for file AuthorizationCache.class.php

Documentation is available at AuthorizationCache.class.php

  1. <?php
  2.  
  3. require_once(HARMONI.'oki2/authorization/HarmoniFunctionIterator.class.php');
  4.  
  5. /**
  6. * This class provides a mechanism for caching different authorization components and
  7. * also acts as an interface between the datastructures and the database.
  8. *
  9. * @package harmoni.osid_v2.authorization
  10. *
  11. * @copyright Copyright &copy; 2005, Middlebury College
  12. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  13. *
  14. * @version $Id: AuthorizationCache.class.php,v 1.33 2007/09/13 16:04:18 adamfranco Exp $
  15. */
  16. class AuthorizationCache {
  17.  
  18. /**
  19. * An array of the cached Qualifier objects. The index of each element is the Id
  20. * of the corresponding Qualifier.
  21. * @var array _qualifiers
  22. * @access protected
  23. */
  24. var $_qualifiers;
  25.  
  26.  
  27. /**
  28. * An array of the cached Function objects. The index of each element is the Id
  29. * of the corresponding Function.
  30. * @var array _functions
  31. * @access protected
  32. */
  33. var $_functions;
  34.  
  35.  
  36. /**
  37. * An array of the cached Authorization objects. The index of each element is the Id
  38. * of the corresponding Authorization.
  39. * @var array _authorizations
  40. * @access protected
  41. */
  42. var $_authorizations;
  43. /**
  44. * The database connection as returned by the DBHandler.
  45. * @var integer _dbIndex
  46. * @access protected
  47. */
  48. var $_dbIndex;
  49. /**
  50. * Constructor
  51. * @access protected
  52. */
  53. function AuthorizationCache($dbIndex) {
  54. // ** argument validation **
  55. ArgumentValidator::validate($dbIndex, IntegerValidatorRule::getRule(), true);
  56. // ** end of argument validation **
  57.  
  58. $this->_dbIndex = $dbIndex;
  59. $this->_qualifiers = array();
  60. $this->_functions = array();
  61. $this->_authorizations = array();
  62. }
  63. /**
  64. * Creates a new Authorization object, caches it, and inserts it into the database.
  65. * @access public
  66. * @param ref object agentId who is authorized to perform this Function for this Qualifer and its descendants
  67. * @param ref object functionId the Id of the Function for this Authorization
  68. * @param ref object qualifierId the Id of the Qualifier for this Authorization
  69. * @param object DateAndTime effectiveDate when the Authorization becomes effective
  70. * @param object DateAndTime expirationDate when the Authorization stops being effective
  71. * @return ref object Authorization
  72. ***/
  73. function createAuthorization($agentId, $functionId, $qualifierId, $effectiveDate = NULL, $expirationDate = NULL) {
  74. // ** parameter validation
  75. ArgumentValidator::validate($agentId, ExtendsValidatorRule::getRule("Id"), true);
  76. ArgumentValidator::validate($functionId, ExtendsValidatorRule::getRule("Id"), true);
  77. ArgumentValidator::validate($qualifierId, ExtendsValidatorRule::getRule("Id"), true);
  78. ArgumentValidator::validate($effectiveDate, OptionalRule::getRule(IntegerValidatorRule::getRule()), true);
  79. ArgumentValidator::validate($expirationDate, OptionalRule::getRule(IntegerValidatorRule::getRule()), true);
  80. // ** end of parameter validation
  81. // create the authorization object
  82. $idManager = Services::getService("Id");
  83. $id =$idManager->createId();
  84. $idValue = $id->getIdString();
  85. // figure out whether it's dated or not
  86. $dated = isset($effectiveDate) && isset($expirationDate);
  87. if ($dated)
  88. $authorization = new HarmoniAuthorization($idValue, $agentId, $functionId, $qualifierId, true, $this, $effectiveDate, $expirationDate);
  89. else
  90. $authorization = new HarmoniAuthorization($idValue, $agentId, $functionId, $qualifierId,true, $this);
  91. // now insert into database
  92. $dbHandler = Services::getService("DatabaseManager");
  93.  
  94. $query = new InsertQuery();
  95. $query->setTable("az_authorization");
  96. $columns = array();
  97. $columns[] = "authorization_id";
  98. $columns[] = "fk_agent";
  99. $columns[] = "fk_function";
  100. $columns[] = "fk_qualifier";
  101. if ($dated) {
  102. $columns[] = "authorization_effective_date";
  103. $columns[] = "authorization_expiration_date";
  104. }
  105. $query->setColumns($columns);
  106. $values = array();
  107. $values[] = "'".addslashes($idValue)."'";
  108. $values[] = "'".addslashes($agentId->getIdString())."'";
  109. $values[] = "'".addslashes($functionId->getIdString())."'";
  110. $values[] = "'".addslashes($qualifierId->getIdString())."'";
  111. if ($dated) {
  112. if (is_object($effectiveDate))
  113. $values[] =
  114. $dbHandler->toDBDate($effectiveDate, $this->_dbIndex);
  115. else
  116. $values[] = "NULL";
  117. if (is_object($expirationDate))
  118. $values[] =
  119. $dbHandler->toDBDate($expirationDate, $this->_dbIndex);
  120. else
  121. $values[] = "NULL";
  122. }
  123. $query->setValues($values);
  124. $queryResult =$dbHandler->query($query, $this->_dbIndex);
  125. if ($queryResult->getNumberOfRows() != 1) {
  126. $err = "Could not insert into database.";
  127. throwError(new Error($err, "authorization", true));
  128. }
  129.  
  130. $this->_authorizations[$idValue] =$authorization;
  131.  
  132. return $authorization;
  133. }
  134. /**
  135. * Creates a new Function, insertsi in the DB and caches it.
  136. * @param ref object functionId is externally defined
  137. * @param string displayName the name to display for this Function
  138. * @param string description the description of this Function
  139. * @param ref object functionType the Type of this Function
  140. * @param ref object qualifierHierarchyId the Id of the Qualifier Hierarchy associated with this Function
  141. * @return ref object Function
  142. */
  143. function createFunction($functionId, $displayName, $description, $functionType, $qualifierHierarchyId) {
  144. // ** parameter validation
  145. ArgumentValidator::validate($functionId, ExtendsValidatorRule::getRule("Id"), true);
  146. ArgumentValidator::validate($displayName, StringValidatorRule::getRule(), true);
  147. ArgumentValidator::validate($description, StringValidatorRule::getRule(), true);
  148. ArgumentValidator::validate($functionType, ExtendsValidatorRule::getRule("Type"), true);
  149. ArgumentValidator::validate($qualifierHierarchyId, ExtendsValidatorRule::getRule("Id"), true);
  150. // ** end of parameter validation
  151. // create the Function object
  152. $idManager = Services::getService("Id");
  153. $function = new HarmoniFunction($functionId, $displayName, $description,
  154. $functionType, $qualifierHierarchyId,
  155. $this->_dbIndex);
  156. // now insert into database
  157. $dbHandler = Services::getService("DatabaseManager");
  158. $idValue = $functionId->getIdString();
  159.  
  160. // 1. Insert the type
  161. $domain = $functionType->getDomain();
  162. $authority = $functionType->getAuthority();
  163. $keyword = $functionType->getKeyword();
  164. $functionTypeDescription = $functionType->getDescription();
  165.  
  166. // check whether the type is already in the DB, if not insert it
  167. $query = new SelectQuery();
  168. $query->addTable("type");
  169. $query->addColumn("type_id", "id", "type");
  170. $where = "type.type_domain = '".addslashes($domain)."'";
  171. $where .= " AND type.type_authority = '".addslashes($authority)."'";
  172. $where .= " AND type.type_keyword = '".addslashes($keyword)."'";
  173. $where .= " AND type.type_description = '".addslashes($functionTypeDescription)."'";
  174. $query->addWhere($where);
  175.  
  176. $queryResult =$dbHandler->query($query, $this->_dbIndex);
  177. if ($queryResult->getNumberOfRows() > 0) {// if the type is already in the database
  178. $functionTypeIdValue = $queryResult->field("id"); // get the id
  179. $queryResult->free();
  180. } else { // if not, insert it
  181. $query = new InsertQuery();
  182. $query->setTable("type");
  183. $query->setAutoIncrementColumn("type_id", "type_type_id_seq");
  184. $columns = array();
  185. $columns[] = "type_domain";
  186. $columns[] = "type_authority";
  187. $columns[] = "type_keyword";
  188. $columns[] = "type_description";
  189. $query->setColumns($columns);
  190. $values = array();
  191. $values[] = "'".addslashes($domain)."'";
  192. $values[] = "'".addslashes($authority)."'";
  193. $values[] = "'".addslashes($keyword)."'";
  194. $values[] = "'".addslashes($functionTypeDescription)."'";
  195. $query->setValues($values);
  196.  
  197. $queryResult =$dbHandler->query($query, $this->_dbIndex);
  198. $functionTypeIdValue = $queryResult->getLastAutoIncrementValue();
  199. }
  200. // 2. Now that we know the id of the type, insert in the DB
  201. $query = new InsertQuery();
  202. $query->setTable("az_function");
  203. $columns = array();
  204. $columns[] = "function_id";
  205. $columns[] = "function_reference_name";
  206. $columns[] = "function_description";
  207. $columns[] = "fk_qualifier_hierarchy";
  208. $columns[] = "fk_type";
  209. $query->setColumns($columns);
  210. $values = array();
  211. $values[] = "'".addslashes($idValue)."'";
  212. $values[] = "'".addslashes($displayName)."'";
  213. $values[] = "'".addslashes($description)."'";
  214. $values[] = "'".addslashes($qualifierHierarchyId->getIdString())."'";
  215. $values[] = "'".addslashes($functionTypeIdValue)."'";
  216. $query->setValues($values);
  217. $queryResult =$dbHandler->query($query, $this->_dbIndex);
  218. if ($queryResult->getNumberOfRows() != 1) {
  219. $err = "Could not insert into database.";
  220. throwError(new Error($err, "authorizarion", true));
  221. }
  222.  
  223. $this->_functions[$idValue] =$function;
  224.  
  225. return $function;
  226. }
  227.  
  228.  
  229. /**
  230. * Creates a new Qualifier in the Authorization Service that has no parent. This is different from making a new instance of a Qualifier object locally as the Qualifier will be inserted into the Authorization Service.
  231. * @param ref object qualifierId is externally defined
  232. * @param string displayName the name to display for this Qualifier
  233. * @param string description the description of this Qualifier
  234. * @param ref object qualifierType the Type of this Qualifier
  235. * @param ref object qualifierHierarchyId the Id of the Qualifier Hierarchy associated with this Qualifier
  236. * @return ref object Qualifier
  237. */
  238. function createRootQualifier($qualifierId, $displayName, $description, $qualifierType, $qualifierHierarchyId) {
  239. // ** parameter validation
  240. ArgumentValidator::validate($qualifierId, ExtendsValidatorRule::getRule("Id"), true);
  241. ArgumentValidator::validate($displayName, StringValidatorRule::getRule(), true);
  242. ArgumentValidator::validate($description, StringValidatorRule::getRule(), true);
  243. ArgumentValidator::validate($qualifierType, ExtendsValidatorRule::getRule("Type"), true);
  244. ArgumentValidator::validate($qualifierHierarchyId, ExtendsValidatorRule::getRule("Id"), true);
  245. // ** end of parameter validation
  246.  
  247. // create the node for the qualifier
  248. $hierarchyManager = Services::getService("Hierarchy");
  249. $hierarchy =$hierarchyManager->getHierarchy($qualifierHierarchyId);
  250. $node =$hierarchy->createRootNode($qualifierId, $qualifierType, $displayName, $description);
  251.  
  252. // now create the qualifier
  253. $qualifier = new HarmoniQualifier($node, $this);
  254. // and cache it
  255. $this->_qualifiers[$qualifierId->getIdString()] =$qualifier;
  256. return $qualifier;
  257. }
  258.  
  259.  
  260. /**
  261. * Creates a new Qualifier in the Authorization Service. This is different than making a new instance of a Qualifier object locally as the Qualifier will be inserted into the Authorization Service.
  262. * @param ref object qualifierId is externally defined
  263. * @param string displayName the name to display for this Qualifier
  264. * @param string description the description of this Qualifier
  265. * @param ref object qualifierType the Type of this Qualifier
  266. * @param ref object parentId the parent of this Qualifier
  267. * @return Qualifier
  268. * @throws osid.authorization.AuthorizationException An exception with one of the following messages defined in osid.authorization.AuthorizationException may be thrown: {@link AuthorizationException#OPERATION_FAILED OPERATION_FAILED}, {@link AuthorizationException#PERMISSION_DENIED PERMISSION_DENIED}, {@link AuthorizationException#CONFIGURATION_ERROR CONFIGURATION_ERROR}, {@link AuthorizationException#UNIMPLEMENTED UNIMPLEMENTED}, {@link AuthorizationException#NULL_ARGUMENT NULL_ARGUMENT}, {@link AuthorizationException#UNKNOWN_ID UNKNOWN_ID}, {@link AuthorizationException#UNKNOWN_TYPE UNKNOWN_TYPE}
  269. */
  270. function createQualifier($qualifierId, $displayName, $description, $qualifierType, $parentId) {
  271. // ** parameter validation
  272. ArgumentValidator::validate($qualifierId, ExtendsValidatorRule::getRule("Id"), true);
  273. ArgumentValidator::validate($displayName, StringValidatorRule::getRule(), true);
  274. ArgumentValidator::validate($description, StringValidatorRule::getRule(), true);
  275. ArgumentValidator::validate($qualifierType, ExtendsValidatorRule::getRule("Type"), true);
  276. ArgumentValidator::validate($parentId, ExtendsValidatorRule::getRule("Id"), true);
  277. // ** end of parameter validation
  278.  
  279. // create the node for the qualifier
  280. $hierarchyManager = Services::getService("Hierarchy");
  281. // get the hierarchy id from the node
  282. $parentNode =$hierarchyManager->getNode($parentId);
  283. // now get the hierarchy and create the node
  284. $hierarchy =$hierarchyManager->getHierarchyForNode($parentNode);
  285. $node =$hierarchy->createNode($qualifierId, $parentId, $qualifierType, $displayName, $description);
  286.  
  287. // now create the qualifier
  288. $qualifier = new HarmoniQualifier($node, $this);
  289. // and cache it
  290. $this->_qualifiers[$qualifierId->getIdString()] =$qualifier;
  291. return $qualifier;
  292. }
  293. /**
  294. * Get all the Function of the specified Type.
  295. * @param ref object functionType the Type of the Functions to return
  296. * @return ref object FunctionIterator
  297. */
  298. function getFunctionTypes() {
  299. if (!isset($this->_functionTypes)) {
  300. $dbHandler = Services::getService("DatabaseManager");
  301. $query = new SelectQuery();
  302. $query->addColumn("type_domain", "domain", "type");
  303. $query->addColumn("type_authority", "authority", "type");
  304. $query->addColumn("type_keyword", "keyword", "type");
  305. $query->addColumn("type_description", "type_description", "type");
  306. $query->addTable("az_function");
  307. $joinc = "fk_type = "."type.type_id";
  308. $query->addTable("type", INNER_JOIN, $joinc);
  309. $query->setGroupBy(array("type_domain", "type_authority", "type_keyword", "type_description"));
  310. $queryResult =$dbHandler->query($query, $this->_dbIndex);
  311. $this->_functionTypes = array();
  312. while ($queryResult->hasMoreRows()) {
  313. $row = $queryResult->getCurrentRow();
  314. // echo "<pre>";
  315. // print_r($row);
  316. // echo "</pre>";
  317. $this->_functionTypes[] = new HarmoniType($row['domain'], $row['authority'],
  318. $row['keyword'], $row['type_description']);
  319. $queryResult->advanceRow();
  320. }
  321. $queryResult->free();
  322. }
  323. $obj = new HarmoniTypeIterator($this->_functionTypes);
  324. return $obj;
  325. }
  326.  
  327. /**
  328. * Get all the Function of the specified Type.
  329. * @param ref object functionType the Type of the Functions to return
  330. * @return ref object FunctionIterator
  331. */
  332. function getFunctions($functionType) {
  333. // ** parameter validation
  334. ArgumentValidator::validate($functionType, ExtendsValidatorRule::getRule("Type"), true);
  335. // ** end of parameter validation
  336.  
  337. $typeString = Type::typeToString($functionType);
  338. if (!isset($this->_functions) || !isset($this->_functions[$typeString])) {
  339. if (!isset($this->_functions))
  340. $this->_functions = array();
  341. $this->_functions[$typeString] = array();
  342. $dbHandler = Services::getService("DatabaseManager");
  343. $query = new SelectQuery();
  344. $query->addColumn("function_id", "id");
  345. $query->addColumn("function_reference_name", "reference_name");
  346. $query->addColumn("function_description", "description");
  347. $query->addColumn("fk_qualifier_hierarchy", "hierarchy_id");
  348. $query->addColumn("type_domain", "domain", "type");
  349. $query->addColumn("type_authority", "authority", "type");
  350. $query->addColumn("type_keyword", "keyword", "type");
  351. $query->addColumn("type_description", "type_description", "type");
  352. $query->addTable("az_function");
  353. $joinc = "fk_type = "."type.type_id";
  354. $query->addTable("type", INNER_JOIN, $joinc);
  355. $where = "type.type_domain = '".addslashes($functionType->getDomain())."'";
  356. $query->addWhere($where);
  357. $where = "type.type_authority = '".addslashes($functionType->getAuthority())."'";
  358. $query->addWhere($where);
  359. $where = "type.type_keyword = '".addslashes($functionType->getKeyword())."'";
  360. $query->addWhere($where);
  361. $queryResult =$dbHandler->query($query, $this->_dbIndex);
  362.  
  363. while ($queryResult->hasMoreRows()) {
  364. $row = $queryResult->getCurrentRow();
  365. // echo "<pre>";
  366. // print_r($row);
  367. // echo "</pre>";
  368. $idValue = $row['id'];
  369. if (isset($this->_functions[$idValue])) {
  370. $function =$this->_functions[$idValue];
  371. }
  372. else {
  373. $type = new HarmoniType($row['domain'], $row['authority'],
  374. $row['keyword'], $row['type_description']);
  375. $idManager = Services::getService("Id");
  376. $functionId =$idManager->getId($row['id']);
  377. $hierarchyId =$idManager->getId($row['hierarchy_id']);
  378. $function = new HarmoniFunction($functionId, $row['reference_name'],
  379. $row['description'], $type, $hierarchyId ,
  380. $this->_dbIndex);
  381. $this->_functions[$idValue] =$function;
  382. }
  383. $this->_functions[$typeString][] =$function;
  384. $queryResult->advanceRow();
  385. }
  386. $queryResult->free();
  387. }
  388. $obj = new HarmoniFunctionIterator($this->_functions[$typeString]);
  389. return $obj;
  390. }
  391.  
  392.  
  393. /**
  394. * Returns the Function with the specified id.
  395. * @access public
  396. * @param string id The id of the function.
  397. * @return ref object The Function with the specified id.
  398. ***/
  399. function getFunction($idValue) {
  400. // ** parameter validation
  401. ArgumentValidator::validate($idValue, StringValidatorRule::getRule(), true);
  402. // ** end of parameter validation
  403. if (isset($this->_functions[$idValue]))
  404. return $this->_functions[$idValue];
  405.  
  406. $dbHandler = Services::getService("DatabaseManager");
  407. $query = new SelectQuery();
  408. $query->addColumn("function_id", "id");
  409. $query->addColumn("function_reference_name", "reference_name");
  410. $query->addColumn("function_description", "description");
  411. $query->addColumn("fk_qualifier_hierarchy", "hierarchy_id");
  412. $query->addColumn("type_domain", "domain", "type");
  413. $query->addColumn("type_authority", "authority", "type");
  414. $query->addColumn("type_keyword", "keyword", "type");
  415. $query->addColumn("type_description", "type_description", "type");
  416. $query->addTable("az_function");
  417. $joinc = "fk_type = type.type_id";
  418. $query->addTable("type", INNER_JOIN, $joinc);
  419. $where = "function_id = '".addslashes($idValue)."'";
  420. $query->addWhere($where);
  421. $queryResult =$dbHandler->query($query, $this->_dbIndex);
  422. if ($queryResult->getNumberOfRows() != 1) {
  423. $queryResult->free();
  424. $str = "Exactly one row must have been returned";
  425. throwError($str, "authorization", true);
  426. }
  427. $row = $queryResult->getCurrentRow();
  428. $queryResult->free();
  429. $type = new HarmoniType($row['domain'], $row['authority'],
  430. $row['keyword'], $row['type_description']);
  431. $idManager = Services::getService("Id");
  432. $functionId =$idManager->getId($row['id']);
  433. $hierarchyId =$idManager->getId($row['hierarchy_id']);
  434. $function = new HarmoniFunction($functionId, $row['reference_name'],
  435. $row['description'], $type, $hierarchyId ,
  436. $this->_dbIndex);
  437. $this->_functions[$idValue] =$function;
  438. return $function;
  439. }
  440. /**
  441. * Returns the Qualifier with the specified id.
  442. * @access public
  443. * @param string id The id of the Qualifier .
  444. * @return ref object The Qualifier with the specified id.
  445. ***/
  446. function getQualifier($qualifierId) {
  447. // ** parameter validation
  448. ArgumentValidator::validate($qualifierId, ExtendsValidatorRule::getRule("Id"), true);
  449. // ** end of parameter validation
  450. $idValue = $qualifierId->getIdString();
  451.  
  452. if (isset($this->_qualifiers[$idValue]))
  453. return $this->_qualifiers[$idValue];
  454.  
  455. // get the node for the qualifier
  456. $hierarchyManager = Services::getService("Hierarchy");
  457. $node =$hierarchyManager->getNode($qualifierId);
  458. // now create the qualifier
  459. $qualifier = new HarmoniQualifier($node, $this);
  460. $this->_qualifiers[$idValue] =$qualifier;
  461. return $qualifier;
  462.  
  463. }
  464. /**
  465. * Given a QualifierHierarchy Id, returns the Qualifier that is the root of
  466. * the tree of Qualifiers of this Type.
  467. *
  468. * @param object qualifierHierarchyId
  469. * @return object QualifierIterator
  470. */
  471. function getRootQualifiers($qualifierHierarchyId) {
  472. $hierarchyManager = Services::getService("Hierarchy", true);
  473. $hierarchy =$hierarchyManager->getHierarchy($qualifierHierarchyId);
  474. // create an array for our qualifiers
  475. $qualifiers = array();
  476. // Get the qualifier for each node
  477. $nodes =$hierarchy->getRootNodes();
  478. while ($nodes->hasNext()) {
  479. $node =$nodes->next();
  480. $nodeId =$node->getId();
  481. // Make sure that we have a qualifier for this node.
  482. if (!isset($this->_qualifiers[$nodeId->getIdString()])) {
  483. $this->_qualifiers[$nodeId->getIdString()] = new HarmoniQualifier($node, $this);
  484. }
  485. $qualifiers[] =$this->_qualifiers[$nodeId->getIdString()];
  486. }
  487. $obj = new HarmoniQualifierIterator($qualifiers);
  488. return $obj;
  489. }
  490.  
  491. /**
  492. * Returns the Qualifier with the specified id.
  493. * @access public
  494. * @param string id The id of the Qualifier .
  495. * @return ref object The Qualifier with the specified id.
  496. ***/
  497. function getQualifierDescendants($qualifierId) {
  498. // ** parameter validation
  499. ArgumentValidator::validate($qualifierId, ExtendsValidatorRule::getRule("Id"), true);
  500. // ** end of parameter validation
  501. $idValue = $qualifierId->getIdString();
  502.  
  503. // get the descendant nodes
  504. $hierarchyManager = Services::getService("Hierarchy");
  505. $node = $hierarchyManager->getNode($qualifierId);
  506. $hierarchy = $hierarchyManager->getHierarchyForNode($node);
  507. $nodes = $hierarchy->traverse($qualifierId, Hierarchy::TRAVERSE_MODE_DEPTH_FIRST(),
  508. Hierarchy::TRAVERSE_DIRECTION_DOWN(), Hierarchy::TRAVERSE_LEVELS_ALL());
  509.  
  510. // create the qualifiers
  511. $qualifiers = array();
  512. // get rid of the root node (it is not a descendant)
  513. $nodes->next();
  514. while ($nodes->hasNext()) {
  515. $node = $nodes->next();
  516. $nodeId = $node->getNodeId();
  517. $idValue = $nodeId->getIdString();
  518. if (isset($this->_qualifiers[$idValue]))
  519. $qualifier =$this->_qualifiers[$idValue];
  520. else {
  521. $qualifier = new HarmoniQualifier($hierarchy->getNode($nodeId), $this);
  522. $this->_qualifiers[$idValue] =$qualifier;
  523. }
  524. $qualifiers[] =$qualifier;
  525. }
  526.  
  527. $obj = new HarmoniQualifierIterator($qualifiers);
  528.  
  529. return $obj;
  530. }
  531.  
  532.  
  533.  
  534. /**
  535. * Deletes the given Authorization.
  536. * @access public
  537. * @param ref object authorization The Authorization to delete.
  538. ***/
  539. function deleteAuthorization($authorization) {
  540. // ** parameter validation
  541. ArgumentValidator::validate($authorization, ExtendsValidatorRule::getRule("Authorization"), true);
  542. // ** end of parameter validation
  543. // echo "<pre>\n";
  544. // print_r($authorization);
  545. // echo "</pre>\n";
  546.  
  547. // get the id
  548. $idValue = $authorization->_id;
  549. // now remove from database
  550. $dbHandler = Services::getService("DatabaseManager");
  551.  
  552. $query = new DeleteQuery();
  553. $query->setTable("az_authorization");
  554. $query->addWhere("authorization_id = '".addslashes($idValue)."'");
  555. $queryResult =$dbHandler->query($query, $this->_dbIndex);
  556. if ($queryResult->getNumberOfRows() != 1) {
  557. $err = "Zero or more than one Authorization were deleted (must have been exactly one).";
  558. throwError(new Error($err, "authorizarion", true));
  559. }
  560. // update cache
  561. $this->_authorizations[$idValue] = null;
  562. unset($this->_authorizations[$idValue]);
  563. }
  564. /**
  565. * Deletes the given Authorization.
  566. * @access public
  567. * @param ref object authorization The Authorization to delete.
  568. ***/
  569. function deleteQualifier($qualifierId) {
  570. // ** parameter validation
  571. ArgumentValidator::validate($qualifierId, ExtendsValidatorRule::getRule("Id"), true);
  572. // ** end of parameter validation
  573.  
  574. // get the id
  575. $idValue = $qualifierId->getIdString();
  576.  
  577. // create the node for the qualifier
  578. $hierarchyManager = Services::getService("Hierarchy");
  579.  
  580. $node =$hierarchyManager->getNode($qualifierId);
  581. $hierarchy =$hierarchyManager->getHierarchyForNode($node);
  582. $hierarchy->deleteNode($qualifierId);
  583. // update cache
  584. $this->_qualifiers[$idValue] = null;
  585. unset($this->_qualifiers[$idValue]);
  586. }
  587.  
  588. /**
  589. * Deletes the given Authorization.
  590. * @access public
  591. * @param ref object authorization The Authorization to delete.
  592. ***/
  593. function deleteFunction($functionId) {
  594. // ** parameter validation
  595. ArgumentValidator::validate($functionId, ExtendsValidatorRule::getRule("Id"), true);
  596. // ** end of parameter validation
  597.  
  598. // get the id
  599. $idValue = $functionId->getIdString();
  600. // now remove from database
  601. $dbHandler = Services::getService("DatabaseManager");
  602.  
  603. $query = new DeleteQuery();
  604. $query->setTable("az_function");
  605. $query->addWhere("function_id = '".addslashes($idValue)."'");
  606. $queryResult =$dbHandler->query($query, $this->_dbIndex);
  607. if ($queryResult->getNumberOfRows() != 1) {
  608. $err = "Zero or more than one function were deleted (must have been exactly one).";
  609. throwError(new Error($err, "authorizarion", true));
  610. }
  611. // update cache
  612. $this->_functions[$idValue] = null;
  613. unset($this->_functions[$idValue]);
  614. }
  615. /**
  616. * Auxilliary private function that returns Authorizations according to a
  617. * criteria. Null values are interpreted as wildmarks. Warning: $returnExplicitOnly = false
  618. * will increase the running time significantly - USE SPARINGLY!
  619. * @access public
  620. * @param string aId The string id of an agent.
  621. * @param string fId The string id of a function.
  622. * @param string qId The string id of a qualifier. This parameter can not be null
  623. * and used as a wildmark.
  624. * @param object fType The type of a function.
  625. * @param boolean returnExplicitOnly If True, only explicit Authorizations
  626. * will be returned.
  627. * @param boolean searchUp If true, the ancester nodes of the qualifier will
  628. * be checked as well
  629. * @param boolean isActiveNow If True, only active Authorizations will be returned.
  630. * @return ref object An AuthorizationIterator.
  631. ***/
  632. function getAZs($aId, $fId, $qId, $fType, $returnExplicitOnly, $searchUp, $isActiveNow, $groupIds = array()) {
  633. // printpre (func_get_args());
  634. // ** parameter validation
  635. $rule = StringValidatorRule::getRule();
  636. ArgumentValidator::validate($groupIds, ArrayValidatorRuleWithRule::getRule(OptionalRule::getRule($rule)), true);
  637. ArgumentValidator::validate($aId, OptionalRule::getRule($rule), true);
  638. ArgumentValidator::validate($fId, OptionalRule::getRule($rule), true);
  639. ArgumentValidator::validate($qId, OptionalRule::getRule($rule), true);
  640. ArgumentValidator::validate($fType, OptionalRule::getRule(ExtendsValidatorRule::getRule("Type")), true);
  641. ArgumentValidator::validate($returnExplicitOnly, BooleanValidatorRule::getRule(), true);
  642. ArgumentValidator::validate($isActiveNow,BooleanValidatorRule::getRule(), true);
  643. // ** end of parameter validation
  644. $idManager = Services::getService("Id");
  645. // the parameter that influences the result most is $returnExplicitOnly
  646. // 1) If $returnExplicitOnly is TRUE, then we only need to check for Authorizations
  647. // that have been explicitly created, i.e. no need to look for inherited
  648. // authorizations
  649. // 2) If $returnExplicitOnly is FALSE, then we need to include inherited Authorizations
  650. // as well.
  651. // this array will store the ids of all qualifiers to be checked for authorizations
  652. $qualifiers = array();
  653. // check all ancestors of given qualifier
  654. $hierarchyManager = Services::getService("Hierarchy");
  655.  
  656. if (isset($qId)) {
  657. $qualifierId =$idManager->getId($qId);
  658. $node =$hierarchyManager->getNode($qualifierId);
  659. $hierarchy =$hierarchyManager->getHierarchyForNode($node);
  660. if ($searchUp) {
  661. // these are the ancestor nodes
  662. $nodes =$hierarchy->traverse($qualifierId, Hierarchy::TRAVERSE_MODE_DEPTH_FIRST(),
  663. Hierarchy::TRAVERSE_DIRECTION_UP(), Hierarchy::TRAVERSE_LEVELS_ALL());
  664. // now get the id of each node and store in array
  665. while($nodes->hasNext()){
  666. $info =$nodes->next();
  667. $id =$info->getNodeId();
  668. $qualifiers[] = $id->getIdString();
  669. }
  670. } else {
  671. $qualifiers = array($qId);
  672. }
  673. }
  674. // print_r($qualifiers);
  675. // setup the query
  676. $dbHandler = Services::getService("DatabaseManager");
  677. $query = new SelectQuery();
  678. $query->addColumn("authorization_id", "id");
  679. $query->addColumn("fk_agent", "aid");
  680. $query->addColumn("fk_function", "fid");
  681. $query->addColumn("fk_qualifier", "qid");
  682. $query->addColumn("authorization_effective_date", "eff_date");
  683. $query->addColumn("authorization_expiration_date", "exp_date");
  684.  
  685. $query->addTable("az_authorization");
  686.  
  687. // now include criteria
  688. // the qualifiers criteria
  689. if (isset($qId)) {
  690. foreach (array_keys($qualifiers) as $key) {
  691. $qualifiers[$key] = addslashes($qualifiers[$key]);
  692. }
  693. $list = implode("','", $qualifiers);
  694. $list = "'".$list."'";
  695. $where = "az_authorization.fk_qualifier IN ($list)";
  696. $query->addWhere($where);
  697. }
  698. foreach (array_keys($groupIds) as $key) {
  699. $groupIds[$key] = addslashes($groupIds[$key]);
  700. }
  701. if (count($groupIds)) {
  702. $agentList = implode("','", $groupIds);
  703. $agentList = "'".addslashes($aId)."','".$agentList."'";
  704. } else {
  705. $agentList = "'".addslashes($aId)."'";
  706. }
  707. // the agent criteria
  708. if (isset($aId) || count($groupIds)) {
  709. // $joinc = "az_authorization.fk_agent = agent.agent_id";
  710. // $query->addTable("agent", INNER_JOIN, $joinc);
  711. $where = "az_authorization.fk_agent IN ($agentList)";
  712. $query->addWhere($where);
  713. }
  714. // the function criteria
  715. if (isset($fId)) {
  716. $joinc = "az_authorization.fk_function = az_function.function_id";
  717. $query->addTable("az_function", INNER_JOIN, $joinc);
  718. $where = "az_authorization.fk_function = '".addslashes($fId)."'";
  719. $query->addWhere($where);
  720. }
  721. // the function type criteria
  722. if (isset($fType)) {
  723. // do not join with az_function if we did already
  724. if (!isset($fId)) {
  725. $joinc = "az_authorization.fk_function = az_function.function_id";
  726. $query->addTable("az_function", INNER_JOIN, $joinc);
  727. }
  728. // now join with type
  729. $joinc = "az_function.fk_type = type.type_id";
  730. $query->addTable("type", INNER_JOIN, $joinc);
  731. $domain = $fType->getDomain();
  732. $authority = $fType->getAuthority();
  733. $keyword = $fType->getKeyword();
  734. $where = "type.type_domain = '".addslashes($domain)."'";
  735. $query->addWhere($where);
  736. $where = "type.type_authority = '".addslashes($authority)."'";
  737. $query->addWhere($where);
  738. $where = "type.type_keyword = '".addslashes($keyword)."'";
  739. $query->addWhere($where);
  740. }
  741. // the isActiveNow criteria
  742. if ($isActiveNow) {
  743. $where = "(authorization_effective_date IS NULL OR (NOW() >= authorization_effective_date))";
  744. $query->addWhere($where);
  745. $where = "(authorization_expiration_date IS NULL OR (NOW() < authorization_expiration_date))";
  746. $query->addWhere($where);
  747. }
  748. $query->addOrderBy("authorization_id");
  749. // echo "<pre>\n";
  750. // echo MySQL_SQLGenerator::generateSQLQuery($query);
  751. // echo "</pre>\n";
  752. $queryResult =$dbHandler->query($query, $this->_dbIndex);
  753. // this array will store the authorizations that will be returned
  754. $authorizations = array();
  755. // we only want to create one implicitAZ for a given Agent/Function/Qualifier
  756. // combo, so maintain a list of already created ones to skip
  757. $createdImplicitAZs = array();
  758. $i = 0;
  759. // process all rows and create the explicit authorizations
  760. while ($queryResult->hasMoreRows()) {
  761. $row = $queryResult->getCurrentRow();
  762. // printpre($row);
  763.  
  764. $idValue = $row['id'];
  765. $id =$idManager->getId($idValue);
  766. if (isset($this->_authorizations[$idValue])) {
  767. $authorization =$this->_authorizations[$idValue];
  768. }
  769. else {
  770. $agentId = $idManager->getId($row['aid']);
  771. $functionId =$idManager->getId($row['fid']);
  772. $explicitQualifierId =$idManager->getId($row['qid']);
  773. $effectiveDate =$dbHandler->fromDBDate($row['eff_date'], $this->_dbIndex);
  774. $expirationDate =$dbHandler->fromDBDate($row['exp_date'], $this->_dbIndex);
  775. // create the explicit authorization (each explicit authorization
  776. // has a corresponding row in the authorization db table)
  777. $authorization = new HarmoniAuthorization($idValue, $agentId,
  778. $functionId, $explicitQualifierId,
  779. true, $this,
  780. $effectiveDate,
  781. $expirationDate);
  782. $this->_authorizations[$idValue] =$authorization;
  783. }
  784. // Explicit AZ for ancestor qualifiers and groups should have
  785. // corresponding implicit AZs
  786. // in decendents, but not appear in their AZs directly.
  787. // Therefore, only add the explicit AZ if it is for the requested
  788. // qualifier and agent if we are fetching more than just the explicitAZs.
  789. if (($row['qid'] == $qId && $row['aid'] == $aId) || $returnExplicitOnly)
  790. $authorizations[] =$authorization;
  791.  
  792. // now create the implicit authorizations
  793. // the implicit authorizations will be created for all nodes
  794. // on the hierarchy path(s) between the node with the explicit authorization
  795. // and the node on which getAZs() was called.
  796. // If the row's qualifier and agent are what we asked for however,
  797. // then the AZ is explicit and doesn't need an implicit AZ as well.
  798. if ((!$returnExplicitOnly || $searchUp)
  799. && !($row['qid'] == $qId && $row['aid'] == $aId))
  800. {
  801. // printpre("Building Implicit AZs...");
  802. // var_dump($returnExplicitOnly);
  803. // var_dump($searchUp);
  804. // if this is an AZ that is implicit because of a group instead
  805. // of because of the hierarchy, create it.
  806. if ($row['qid'] == $qId && $row['aid'] != $aId) {
  807. // printpre("In first clause (AuthorizationCache)");
  808. $qualifierId =$idManager->getId($qId);
  809. // If we are getting implicit AZs for a given agent, make sure
  810. // that the implicit AZ has their Id.
  811. if ($aId)
  812. $agentId =$idManager->getId($aId);
  813. else
  814. $agentId =$authorization->getAgentId();
  815. $function =$authorization->getFunction();
  816. $functionId =$function->getId();
  817. $effectiveDate = $authorization->getEffectiveDate();
  818. $expirationDate = $authorization->getExpirationDate();
  819. $implicit = new HarmoniAuthorization(null,
  820. $agentId,
  821. $functionId,
  822. $qualifierId,
  823. false,
  824. $this,
  825. $effectiveDate,
  826. $expirationDate);
  827. $azHash = $agentId->getIdString()
  828. ."::".$functionId->getIdString()
  829. ."::".$qualifierId->getIdString();
  830. if (!in_array($azHash, $createdImplicitAZs)) {
  831. $authorizations[] =$implicit;
  832. $createdImplicitAZs[] = $azHash;
  833. }
  834. }
  835. // Otherwise, do what is necessary to create the implicit qualifier
  836. // based on the hierarchy
  837. // If we have a $qid then our results must only have been for it.
  838. // This means that if we got here, we have an authorization in
  839. // an ancestor somewhere, so we can savly return an implicit AZ.
  840. else if (!$returnExplicitOnly && $qId) {
  841. // printpre("In second clause (AuthorizationCache)");
  842. // If we are getting implicit AZs for a given agent, make sure
  843. // that the implicit AZ has their Id.
  844. if ($aId)
  845. $agentId =$idManager->getId($aId);
  846. else
  847. $agentId =$authorization->getAgentId();
  848. $function =$authorization->getFunction();
  849. $functionId =$function->getId();
  850. $effectiveDate = $authorization->getEffectiveDate();
  851. $expirationDate = $authorization->getExpirationDate();
  852. $implicitQualifierId =$idManager->getId($qId);
  853. $implicit = new HarmoniAuthorization(null, $agentId,
  854. $functionId, $implicitQualifierId,
  855. false, $this, $effectiveDate,
  856. $expirationDate);
  857. $azHash = $agentId->getIdString()
  858. ."::".$functionId->getIdString()
  859. ."::".$implicitQualifierId->getIdString();
  860. if (!in_array($azHash, $createdImplicitAZs)) {
  861. $authorizations[] =$implicit;
  862. $createdImplicitAZs[] = $azHash;
  863. }
  864. }
  865. // If we are are just asking for all authorizations, not with
  866. // a particular qualifier, then we must build a hierarchy down
  867. // to make implicit qualifiers for the decendents of the
  868. // explicit qualifier
  869. else if (!$returnExplicitOnly) {
  870. printpre($row);
  871. printpre("In third clause (AuthorizationCache)");
  872. $explicitQualifier =$authorization->getQualifier();
  873. $explicitQualifierId =$explicitQualifier->getId();
  874. // If we are getting implicit AZs for a given agent, make sure
  875. // that the implicit AZ has their Id.
  876. if ($aId)
  877. $agentId =$idManager->getId($aId);
  878. else
  879. $agentId =$authorization->getAgentId();
  880. $function =$authorization->getFunction();
  881. $functionId =$function->getId();
  882. $effectiveDate = $authorization->getEffectiveDate();
  883. $expirationDate = $authorization->getExpirationDate();
  884. // this is set 2
  885. $authZManager = Services::getService("AuthZ");
  886. $hierarchies =$authZManager->getQualifierHierarchies();
  887. while($hierarchies->hasNext()) {
  888. $hierarchyId =$hierarchies->next();
  889. $hierarchy =$hierarchyManager->getHierarchy($hierarchyId);
  890. $timer = new Timer;
  891. $timer->start();
  892. $nodes =$hierarchy->traverse(
  893. $explicitQualifierId,
  894. Hierarchy::TRAVERSE_MODE_DEPTH_FIRST(),
  895. Hierarchy::TRAVERSE_DIRECTION_DOWN(),
  896. Hierarchy::TRAVERSE_LEVELS_ALL());
  897. $timer->end();
  898. printf("LoadAZTime: %1.6f <br/>", $timer->printTime());
  899. // now get the id of each node and store in array
  900. $set2 = array();
  901. // skip the first node
  902. $nodes->next();
  903. while($nodes->hasNext()){
  904. $info =$nodes->next();
  905. $nodeId =$info->getNodeId();
  906. $implicit = new HarmoniAuthorization(null, $agentId,
  907. $functionId, $nodeId,
  908. false, $this, $effectiveDate,
  909. $expirationDate);
  910. $azHash = $agentId->getIdString()
  911. ."::".$functionId->getIdString()
  912. ."::".$nodeId->getIdString();
  913. // printpre($azHash);
  914. // Weird bugs were happening, with $createdImplicitAZs
  915. // but I can't figure out what is going on.
  916. if (!in_array($azHash, $createdImplicitAZs)) {
  917. $authorizations[] =$implicit;
  918. // $createdImplicitAZs[] = $azHash;
  919. }
  920. }
  921. }
  922. }
  923. }
  924. $queryResult->advanceRow();
  925. }
  926. $queryResult->free();
  927. return $authorizations;
  928. }
  929. }
  930. ?>

Documentation generated on Wed, 19 Sep 2007 10:21:40 -0400 by phpDocumentor 1.3.0RC3