4 * @file classes/security/AccessKeyManager.inc.php
6 * Copyright (c) 2000-2009 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
9 * @class AccessKeyManager
13 * @brief Class defining operations for AccessKey management.
16 // $Id: AccessKeyManager.inc.php,v 1.4 2009/04/08 21:34:54 asmecher Exp $
19 class AccessKeyManager
{
24 * Create a manager for access keys.
26 function AccessKeyManager() {
27 $this->accessKeyDao
=& DAORegistry
::getDAO('AccessKeyDAO');
28 $this->_performPeriodicCleanup();
32 * Generate a key hash from a key.
36 function generateKeyHash($key) {
41 * Validate an access key based on the supplied credentials.
42 * If $assocId is specified, it must match the associated ID of the
44 * @param $context string The context of the access key
45 * @param $key string The access key "passcode"
46 * @param $assocId string optional assoc ID to check against the keys in the database
48 function &validateKey($context, $userId, $keyHash, $assocId = null) {
49 $accessKey =& $this->accessKeyDao
->getAccessKeyByKeyHash($context, $userId, $keyHash, $assocId);
54 * Create an access key with the given information.
55 * @param $context string The context of the access key
56 * @param $userId int The ID of the effective user for this access key
57 * @param $assocId int The associated ID of the key
58 * @param $expiryDays int The number of days before this key expires
59 * @return accessKey string The generated passkey
61 function createKey($context, $userId, $assocId, $expiryDays) {
62 $accessKey = new AccessKey();
63 $accessKey->setContext($context);
64 $accessKey->setUserId($userId);
65 $accessKey->setAssocId($assocId);
66 $accessKey->setExpiryDate(Core
::getCurrentDate(time() +
(60 * 60 * 24 * $expiryDays)));
68 $key = Validation
::generatePassword();
69 $accessKey->setKeyHash($this->generateKeyHash($key));
71 $this->accessKeyDao
->insertAccessKey($accessKey);
77 * Periodically clean up expired keys.
79 function _performPeriodicCleanup() {
80 if (time() %
100 == 0) {
81 $accessKeyDao =& DAORegistry
::getDAO('AccessKeyDAO');
82 $accessKeyDao->deleteExpiredKeys();