Source for file HarmoniAuthorization.class.php

Documentation is available at HarmoniAuthorization.class.php

  1. <?php
  2.  
  3. require_once(OKI2."/osid/authorization/Authorization.php");
  4. require_once(HARMONI."Primitives/Chronology/include.php");
  5.  
  6. /**
  7. * Authorization indicates what an agentId can do a Function in a Qualifier
  8. * context.
  9. *
  10. * <p>
  11. * OSID Version: 2.0
  12. * </p>
  13. *
  14. * @package harmoni.osid_v2.authorization
  15. *
  16. * @copyright Copyright &copy; 2005, Middlebury College
  17. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  18. *
  19. * @version $Id: HarmoniAuthorization.class.php,v 1.18 2007/09/04 20:25:38 adamfranco Exp $
  20. */
  21. class HarmoniAuthorization
  22. extends Authorization
  23. {
  24.  
  25. /**
  26. * The date when this Authorization starts being effective.
  27. * @var object _effectiveDate
  28. * @access private
  29. */
  30. var $_effectiveDate;
  31. /**
  32. * The Id of this Authorization (string).
  33. * @var string _id
  34. * @access protected
  35. */
  36. var $_id;
  37. /**
  38. * The Id of the agent.
  39. * @var object _agentId
  40. * @access private
  41. */
  42. var $_agentId;
  43. /**
  44. * The Id of the function.
  45. * @var object _functionId
  46. * @access private
  47. */
  48. var $_functionId;
  49. /**
  50. * The Id of the qualifier.
  51. * @var object _qualifierId
  52. * @access private
  53. */
  54. var $_qualifierId;
  55. /**
  56. * The date when the authorization expires.
  57. * @var object _expirationDate
  58. * @access private
  59. */
  60. var $_expirationDate;
  61. /**
  62. * Specifies whether this Authorization is explicit or not.
  63. * @var boolean _explicit
  64. * @access private
  65. */
  66. var $_explicit;
  67.  
  68. /**
  69. * The AuthorizationCache object.
  70. * @var object _cache
  71. * @access private
  72. */
  73. var $_cache;
  74. /**
  75. * The constructor.
  76. * @param object id The id of this Authorization
  77. * @param ref object agentId The Id of the agent.
  78. * @param ref object functionId The Id of the function.
  79. * @param ref object qualifierId The Id of the qualifier.
  80. * @param integer effectiveDate The date when the authorization becomes effective.
  81. * @param integer expirationDate The date when the authorization expires.
  82. * @param boolean explicit Specifies whether this Authorization is explicit or not.
  83. * @access public
  84. */
  85. function HarmoniAuthorization($id, $agentId, $functionId, $qualifierId, $explicit, $cache, $effectiveDate = NULL, $expirationDate = NULL) {
  86.  
  87. // ** parameter validation
  88. $extendsRule = ExtendsValidatorRule::getRule("Id");
  89. ArgumentValidator::validate($id, OptionalRule::getRule(StringValidatorRule::getRule()), true);
  90. ArgumentValidator::validate($agentId, $extendsRule, true);
  91. ArgumentValidator::validate($functionId, $extendsRule, true);
  92. ArgumentValidator::validate($qualifierId, $extendsRule, true);
  93. ArgumentValidator::validate($effectiveDate, OptionalRule::getRule(HasMethodsValidatorRule::getRule('asDateAndTime')), true);
  94. ArgumentValidator::validate($expirationDate, OptionalRule::getRule(HasMethodsValidatorRule::getRule('asDateAndTime')), true);
  95. ArgumentValidator::validate($explicit, BooleanValidatorRule::getRule(), true);
  96. ArgumentValidator::validate($cache, ExtendsValidatorRule::getRule("AuthorizationCache"), true);
  97. // ** end of parameter validation
  98. // make sure effective date is before expiration date
  99. if (isset($effectiveDate) && isset($expirationDate))
  100. if ($effectiveDate > $expirationDate) {
  101. $str = "The effective date must be before the expiration date.";
  102. throwError(new Error($str, "Authorization", true));
  103. }
  104.  
  105. $this->_id = $id;
  106. $this->_agentId =$agentId;
  107. $this->_functionId =$functionId;
  108. $this->_qualifierId =$qualifierId;
  109. if (isset($effectiveDate))
  110. $this->_effectiveDate =$effectiveDate;
  111. if (isset($expirationDate))
  112. $this->_expirationDate =$expirationDate;
  113. $this->_explicit = $explicit;
  114. $this->_cache =$cache;
  115. }
  116. /**
  117. * Get the date when this Authorization starts being effective.
  118. *
  119. * @return int
  120. *
  121. * @throws object AuthorizationException An exception with
  122. * one of the following messages defined in
  123. * org.osid.authorization.AuthorizationException may be thrown:
  124. * {@link }
  125. * org.osid.authorization.AuthorizationException#OPERATION_FAILED
  126. * OPERATION_FAILED}, {@link }
  127. * org.osid.authorization.AuthorizationException#PERMISSION_DENIED
  128. * PERMISSION_DENIED}, {@link }
  129. * org.osid.authorization.AuthorizationException#CONFIGURATION_ERROR
  130. * CONFIGURATION_ERROR}, {@link }
  131. * org.osid.authorization.AuthorizationException#UNIMPLEMENTED
  132. * UNIMPLEMENTED}
  133. *
  134. * @access public
  135. */
  136. function getEffectiveDate () {
  137. return $this->_effectiveDate;
  138. }
  139.  
  140. /**
  141. * Get the date when this Authorization stops being effective.
  142. *
  143. * @return int
  144. *
  145. * @throws object AuthorizationException An exception with
  146. * one of the following messages defined in
  147. * org.osid.authorization.AuthorizationException may be thrown:
  148. * {@link }
  149. * org.osid.authorization.AuthorizationException#OPERATION_FAILED
  150. * OPERATION_FAILED}, {@link }
  151. * org.osid.authorization.AuthorizationException#PERMISSION_DENIED
  152. * PERMISSION_DENIED}, {@link }
  153. * org.osid.authorization.AuthorizationException#CONFIGURATION_ERROR
  154. * CONFIGURATION_ERROR}, {@link }
  155. * org.osid.authorization.AuthorizationException#UNIMPLEMENTED
  156. * UNIMPLEMENTED}
  157. *
  158. * @access public
  159. */
  160. function getExpirationDate () {
  161. return $this->_expirationDate;
  162. }
  163.  
  164. /**
  165. * Get the Id of the agent that modified this Authorization.
  166. *
  167. * @return object Id
  168. *
  169. * @throws object AuthorizationException An exception with
  170. * one of the following messages defined in
  171. * org.osid.authorization.AuthorizationException may be thrown:
  172. * {@link }
  173. * org.osid.authorization.AuthorizationException#OPERATION_FAILED
  174. * OPERATION_FAILED}, {@link }
  175. * org.osid.authorization.AuthorizationException#PERMISSION_DENIED
  176. * PERMISSION_DENIED}, {@link }
  177. * org.osid.authorization.AuthorizationException#CONFIGURATION_ERROR
  178. * CONFIGURATION_ERROR}, {@link }
  179. * org.osid.authorization.AuthorizationException#UNIMPLEMENTED
  180. * UNIMPLEMENTED}
  181. *
  182. * @access public
  183. */
  184. function getModifiedBy () {
  185. throwError(new Error(AuthorizationExeption::UNIMPLEMENTED(), "Authorization", true));
  186. }
  187.  
  188. /**
  189. * Get the date when this Authorization was modified.
  190. *
  191. * @return int
  192. *
  193. * @throws object AuthorizationException An exception with
  194. * one of the following messages defined in
  195. * org.osid.authorization.AuthorizationException may be thrown:
  196. * {@link }
  197. * org.osid.authorization.AuthorizationException#OPERATION_FAILED
  198. * OPERATION_FAILED}, {@link }
  199. * org.osid.authorization.AuthorizationException#PERMISSION_DENIED
  200. * PERMISSION_DENIED}, {@link }
  201. * org.osid.authorization.AuthorizationException#CONFIGURATION_ERROR
  202. * CONFIGURATION_ERROR}, {@link }
  203. * org.osid.authorization.AuthorizationException#UNIMPLEMENTED
  204. * UNIMPLEMENTED}
  205. *
  206. * @access public
  207. */
  208. function getModifiedDate () {
  209. throwError(new Error(AuthorizationExeption::UNIMPLEMENTED(), "Authorization", true));
  210. }
  211.  
  212. /**
  213. *
  214. *
  215. * @return object Function
  216. *
  217. * @throws object AuthorizationException An exception with
  218. * one of the following messages defined in
  219. * org.osid.authorization.AuthorizationException may be thrown:
  220. * {@link }
  221. * org.osid.authorization.AuthorizationException#OPERATION_FAILED
  222. * OPERATION_FAILED}, {@link }
  223. * org.osid.authorization.AuthorizationException#PERMISSION_DENIED
  224. * PERMISSION_DENIED}, {@link }
  225. * org.osid.authorization.AuthorizationException#CONFIGURATION_ERROR
  226. * CONFIGURATION_ERROR}, {@link }
  227. * org.osid.authorization.AuthorizationException#UNIMPLEMENTED
  228. * UNIMPLEMENTED}
  229. *
  230. * @access public
  231. */
  232. function getFunction () {
  233. $idValue = $this->_functionId->getIdString();
  234. $result =$this->_cache->getFunction($idValue);
  235. return $result;
  236. }
  237.  
  238. /**
  239. *
  240. *
  241. * @return object Qualifier
  242. *
  243. * @throws object AuthorizationException An exception with
  244. * one of the following messages defined in
  245. * org.osid.authorization.AuthorizationException may be thrown:
  246. * {@link }
  247. * org.osid.authorization.AuthorizationException#OPERATION_FAILED
  248. * OPERATION_FAILED}, {@link }
  249. * org.osid.authorization.AuthorizationException#PERMISSION_DENIED
  250. * PERMISSION_DENIED}, {@link }
  251. * org.osid.authorization.AuthorizationException#CONFIGURATION_ERROR
  252. * CONFIGURATION_ERROR}, {@link }
  253. * org.osid.authorization.AuthorizationException#UNIMPLEMENTED
  254. * UNIMPLEMENTED}
  255. *
  256. * @access public
  257. */
  258. function getQualifier () {
  259. $result =$this->_cache->getQualifier($this->_qualifierId);
  260. return $result;
  261. }
  262.  
  263. /**
  264. * Get the agentid associated with this Authorization.
  265. *
  266. * @return object Id
  267. *
  268. * @throws object AuthorizationException An exception with
  269. * one of the following messages defined in
  270. * org.osid.authorization.AuthorizationException may be thrown:
  271. * {@link }
  272. * org.osid.authorization.AuthorizationException#OPERATION_FAILED
  273. * OPERATION_FAILED}, {@link }
  274. * org.osid.authorization.AuthorizationException#PERMISSION_DENIED
  275. * PERMISSION_DENIED}, {@link }
  276. * org.osid.authorization.AuthorizationException#CONFIGURATION_ERROR
  277. * CONFIGURATION_ERROR}, {@link }
  278. * org.osid.authorization.AuthorizationException#UNIMPLEMENTED
  279. * UNIMPLEMENTED}
  280. *
  281. * @access public
  282. */
  283. function getAgentId () {
  284. return $this->_agentId;
  285. }
  286.  
  287. /**
  288. * Return true if this Authorization is effective; false otherise;
  289. *
  290. * @return boolean
  291. *
  292. * @throws object AuthorizationException An exception with
  293. * one of the following messages defined in
  294. * org.osid.authorization.AuthorizationException may be thrown:
  295. * {@link }
  296. * org.osid.authorization.AuthorizationException#OPERATION_FAILED
  297. * OPERATION_FAILED}, {@link }
  298. * org.osid.authorization.AuthorizationException#PERMISSION_DENIED
  299. * PERMISSION_DENIED}, {@link }
  300. * org.osid.authorization.AuthorizationException#CONFIGURATION_ERROR
  301. * CONFIGURATION_ERROR}, {@link }
  302. * org.osid.authorization.AuthorizationException#UNIMPLEMENTED
  303. * UNIMPLEMENTED}
  304. *
  305. * @access public
  306. */
  307. function isActiveNow () {
  308. if (!is_object($this->_effectiveDate) || !is_object($this->_expirationDate))
  309. // a non-dated Authorization is always active
  310. return true;
  311. // if current time is after the effective date and before the expiration
  312. // date, then the Authorization is active.
  313. if ($this->_effectiveDate->isLessThanOrEqualTo(DateAndTime::now())
  314. && $this->_expirationDate->isGreaterThan(DateAndTime::now()))
  315. {
  316. return true;
  317. } else
  318. return false;
  319. }
  320.  
  321. /**
  322. * Some Authorizations are explicitly stored and others are implied, so use
  323. * this method to determine if the Authorization is explicit and can be
  324. * modified or deleted.
  325. *
  326. * @return boolean
  327. *
  328. * @throws object AuthorizationException An exception with
  329. * one of the following messages defined in
  330. * org.osid.authorization.AuthorizationException may be thrown:
  331. * {@link }
  332. * org.osid.authorization.AuthorizationException#OPERATION_FAILED
  333. * OPERATION_FAILED}, {@link }
  334. * org.osid.authorization.AuthorizationException#PERMISSION_DENIED
  335. * PERMISSION_DENIED}, {@link }
  336. * org.osid.authorization.AuthorizationException#CONFIGURATION_ERROR
  337. * CONFIGURATION_ERROR}, {@link }
  338. * org.osid.authorization.AuthorizationException#UNIMPLEMENTED
  339. * UNIMPLEMENTED}
  340. *
  341. * @access public
  342. */
  343. function isExplicit () {
  344. return $this->_explicit;
  345. }
  346.  
  347. /**
  348. * Modify the date when this Authorization starts being effective.
  349. *
  350. * @param object DateAndTime $expirationDate
  351. *
  352. * @throws object AuthorizationException An exception with
  353. * one of the following messages defined in
  354. * org.osid.authorization.AuthorizationException may be thrown:
  355. * {@link }
  356. * org.osid.authorization.AuthorizationException#OPERATION_FAILED
  357. * OPERATION_FAILED}, {@link }
  358. * org.osid.authorization.AuthorizationException#PERMISSION_DENIED
  359. * PERMISSION_DENIED}, {@link }
  360. * org.osid.authorization.AuthorizationException#CONFIGURATION_ERROR
  361. * CONFIGURATION_ERROR}, {@link }
  362. * org.osid.authorization.AuthorizationException#UNIMPLEMENTED
  363. * UNIMPLEMENTED}, {@link }
  364. * org.osid.authorization.AuthorizationException#NULL_ARGUMENT
  365. * NULL_ARGUMENT}, {@link }
  366. * org.osid.authorization.AuthorizationException#EFFECTIVE_PRECEDE_EXPIRATION}
  367. *
  368. * @access public
  369. */
  370. function updateExpirationDate ( $expirationDate ) {
  371. if (!$this->isExplicit()) {
  372. $str = "Cannot modify an implicit Authorization.";
  373. throwError(new Error(AuthorizationException::OPERATION_FAILED(),
  374. "Authorization", true));
  375. }
  376. // ** parameter validation
  377. ArgumentValidator::validate($expirationDate,
  378. HasMethodsValidatorRule::getRule("asDateAndTime"), true);
  379. // ** end of parameter validation
  380.  
  381. // make sure effective date is before expiration date
  382. if ($this->_effectiveDate->isGreaterThan($expirationDate)) {
  383. throwError(new Error(AuthorizationException::EFFECTIVE_PRECEDE_EXPIRATION(),
  384. "Authorization", true));
  385. }
  386.  
  387. if ($this->_expirationDate->isEqualTo($expirationDate))
  388. return; // nothing to update
  389.  
  390. // update the object
  391. $this->_expirationDate = $expirationDate;
  392.  
  393. // update the database
  394. $dbHandler = Services::getService("DatabaseManager");
  395. $dbPrefix = $this->_cache->_authzDB.".az_authorization";
  396. $query = new UpdateQuery();
  397. $query->setTable($dbPrefix);
  398. $idValue = $this->_id;
  399. $where = "{$dbPrefix}.authorization_id = '{$idValue}'";
  400. $query->setWhere($where);
  401. $query->setColumns(array("{$dbPrefix}.authorization_expiration_date"));
  402. $timestamp = $dbHandler->toDBDate($expirationDate, $this->_cache->_dbIndex);
  403. $query->setValues(array($timestamp));
  404. $queryResult =$dbHandler->query($query, $this->_cache->_dbIndex);
  405. if ($queryResult->getNumberOfRows() == 0)
  406. throwError(new Error(AuthorizationException::OPERATION_FAILED(),"Authorization",true));
  407. if ($queryResult->getNumberOfRows() > 1)
  408. throwError(new Error(AuthorizationException::OPERATION_FAILED() ,"Authorization",true));
  409. }
  410. /**
  411. * the date when this Authorization stops being effective.
  412. *
  413. * @param object DateAndTime $effectiveDate
  414. *
  415. * @throws object AuthorizationException An exception with
  416. * one of the following messages defined in
  417. * org.osid.authorization.AuthorizationException may be thrown:
  418. * {@link }
  419. * org.osid.authorization.AuthorizationException#OPERATION_FAILED
  420. * OPERATION_FAILED}, {@link }
  421. * org.osid.authorization.AuthorizationException#PERMISSION_DENIED
  422. * PERMISSION_DENIED}, {@link }
  423. * org.osid.authorization.AuthorizationException#CONFIGURATION_ERROR
  424. * CONFIGURATION_ERROR}, {@link }
  425. * org.osid.authorization.AuthorizationException#UNIMPLEMENTED
  426. * UNIMPLEMENTED}, {@link }
  427. * org.osid.authorization.AuthorizationException#NULL_ARGUMENT
  428. * NULL_ARGUMENT}, {@link }
  429. * org.osid.authorization.AuthorizationException#EFFECTIVE_PRECEDE_EXPIRATION}
  430. *
  431. * @access public
  432. */
  433. function updateEffectiveDate ( $effectiveDate ) {
  434. if (!$this->isExplicit()) {
  435. // "Cannot modify an implicit Authorization."
  436. throwError(new Error(AuthorizationException::OPERATION_FAILED(),
  437. "Authorization", true));
  438. }
  439. // ** parameter validation
  440. ArgumentValidator::validate($effectiveDate,
  441. HasMethodsValidatorRule::getRule("asDateAndTime"), true);
  442. // ** end of parameter validation
  443.  
  444. // make sure effective date is before expiration date
  445. if ($effectiveDate->isGreaterThan($this->_expirationDate)) {
  446. throwError(new Error(AuthorizationException::EFFECTIVE_PRECEDE_EXPIRATION(),
  447. "Authorization", true));
  448. }
  449.  
  450. if ($this->_effectiveDate->isEqualTo($effectiveDate))
  451. return; // nothing to update
  452.  
  453. // update the object
  454. $this->_effectiveDate =$effectiveDate;
  455.  
  456. // update the database
  457. $dbHandler = Services::getService("DatabaseManager");
  458. $dbPrefix = $this->_cache->_authzDB.".az_authorization";
  459. $query = new UpdateQuery();
  460. $query->setTable($dbPrefix);
  461. $idValue = $this->_id;
  462. $where = "{$dbPrefix}.authorization_id = '{$idValue}'";
  463. $query->setWhere($where);
  464. $query->setColumns(array("{$dbPrefix}.authorization_effective_date"));
  465. $timestamp = $dbHandler->toDBDate($effectiveDate, $this->_cache->_dbIndex);
  466. $query->setValues(array($timestamp));
  467. $queryResult =$dbHandler->query($query, $this->_cache->_dbIndex);
  468. if ($queryResult->getNumberOfRows() == 0)
  469. throwError(new Error(AuthorizationException::OPERATION_FAILED(),"Authorization",true));
  470. if ($queryResult->getNumberOfRows() > 1)
  471. throwError(new Error(AuthorizationException::OPERATION_FAILED() ,"Authorization",true));
  472. }
  473.  
  474.  
  475. }
  476.  
  477. ?>

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