2011-12-19 15:22:56 +00:00
|
|
|
<?php
|
2012-02-26 16:55:44 +00:00
|
|
|
namespace wcf\system\chat\permission;
|
2011-12-19 15:22:56 +00:00
|
|
|
use \wcf\system\acl\ACLHandler;
|
2012-02-26 17:16:28 +00:00
|
|
|
use \wcf\system\cache\CacheHandler;
|
2011-12-19 15:22:56 +00:00
|
|
|
use \wcf\system\package\PackageDependencyHandler;
|
|
|
|
use \wcf\system\WCF;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles chat-permissions.
|
|
|
|
*
|
2012-01-28 16:51:35 +00:00
|
|
|
* @author Tim Düsterhus, Marcel Werk
|
2012-01-28 16:50:33 +00:00
|
|
|
* @copyright 2010-2012 WoltLab GmbH
|
2011-12-19 15:22:56 +00:00
|
|
|
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
|
2012-03-12 16:18:15 +00:00
|
|
|
* @package be.bastelstu.wcf.chat
|
2011-12-19 15:22:56 +00:00
|
|
|
* @subpackage system.chat.permissions
|
|
|
|
*/
|
|
|
|
class ChatPermissionHandler extends \wcf\system\SingletonFactory {
|
|
|
|
protected $chatPermissions = array();
|
2012-03-03 22:12:21 +00:00
|
|
|
protected static $defaults = array(
|
|
|
|
'user.canEnter' => true,
|
|
|
|
'user.canWrite' => true,
|
|
|
|
'mod.canAlwaysEnter' => false,
|
|
|
|
'mod.canAlwaysWrite' => false
|
|
|
|
);
|
2011-12-19 15:22:56 +00:00
|
|
|
|
|
|
|
/**
|
2012-02-26 16:55:44 +00:00
|
|
|
* @see \wcf\system\SingletonFactory::init()
|
2011-12-19 15:22:56 +00:00
|
|
|
*/
|
|
|
|
protected function init() {
|
2012-03-08 21:07:46 +00:00
|
|
|
$packageID = \wcf\util\ChatUtil::getPackageID();
|
2011-12-19 15:22:56 +00:00
|
|
|
$ush = \wcf\system\user\storage\UserStorageHandler::getInstance();
|
2012-02-26 17:16:28 +00:00
|
|
|
|
|
|
|
// get groups permissions
|
|
|
|
$groups = implode(',', WCF::getUser()->getGroupIDs());
|
|
|
|
$groupsFileName = \wcf\util\StringUtil::getHash(implode('-', WCF::getUser()->getGroupIDs()));
|
2012-03-05 15:49:45 +00:00
|
|
|
CacheHandler::getInstance()->addResource('chatPermission-'.$groups, WCF_DIR.'cache/cache.chatPermission-'.$groupsFileName.'.php', '\wcf\system\cache\builder\ChatPermissionCacheBuilder');
|
2012-02-26 17:16:28 +00:00
|
|
|
$this->chatPermissions = CacheHandler::getInstance()->get('chatPermission-'.$groups);
|
2011-12-19 15:22:56 +00:00
|
|
|
|
|
|
|
// get user permissions
|
|
|
|
if (WCF::getUser()->userID) {
|
|
|
|
// get data from storage
|
|
|
|
$ush->loadStorage(array(WCF::getUser()->userID), $packageID);
|
|
|
|
|
|
|
|
// get ids
|
|
|
|
$data = $ush->getStorage(array(WCF::getUser()->userID), 'chatUserPermissions', $packageID);
|
|
|
|
|
|
|
|
// cache does not exist or is outdated
|
|
|
|
if ($data[WCF::getUser()->userID] === null) {
|
|
|
|
$userPermissions = array();
|
|
|
|
|
|
|
|
$conditionBuilder = new \wcf\system\database\util\PreparedStatementConditionBuilder();
|
2012-03-05 15:49:45 +00:00
|
|
|
$conditionBuilder->add('acl_option.packageID IN (?)', array(PackageDependencyHandler::getInstance()->getDependencies()));
|
2012-03-12 16:18:15 +00:00
|
|
|
$conditionBuilder->add('acl_option.objectTypeID = ?', array(ACLHandler::getInstance()->getObjectTypeID('be.bastelstu.wcf.chat.room')));
|
2011-12-19 15:22:56 +00:00
|
|
|
$conditionBuilder->add('option_to_user.optionID = acl_option.optionID');
|
|
|
|
$conditionBuilder->add('option_to_user.userID = ?', array(WCF::getUser()->userID));
|
|
|
|
$sql = "SELECT option_to_user.objectID AS roomID, option_to_user.optionValue,
|
|
|
|
acl_option.optionName AS permission
|
|
|
|
FROM wcf".WCF_N."_acl_option acl_option,
|
|
|
|
wcf".WCF_N."_acl_option_to_user option_to_user
|
|
|
|
".$conditionBuilder;
|
2012-03-08 21:07:46 +00:00
|
|
|
$stmt = WCF::getDB()->prepareStatement($sql);
|
|
|
|
$stmt->execute($conditionBuilder->getParameters());
|
|
|
|
while ($row = $stmt->fetchArray()) {
|
2011-12-19 15:22:56 +00:00
|
|
|
$userPermissions[$row['roomID']][$row['permission']] = $row['optionValue'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// update cache
|
|
|
|
$ush->update(WCF::getUser()->userID, 'chatUserPermissions', serialize($userPermissions), $packageID);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$userPermissions = unserialize($data[WCF::getUser()->userID]);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($userPermissions as $roomID => $permissions) {
|
|
|
|
foreach ($permissions as $name => $value) {
|
|
|
|
$this->chatPermissions[$roomID][$name] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the given permission for the given room
|
|
|
|
*
|
|
|
|
* @param \wcf\data\chat\room\ChatRoom $room
|
|
|
|
* @param string $permission
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function getPermission(\wcf\data\chat\room\ChatRoom $room, $permission) {
|
2012-03-03 22:12:21 +00:00
|
|
|
if (!isset($this->chatPermissions[$room->roomID][$permission])) {
|
2012-03-23 16:45:26 +00:00
|
|
|
$permission = str_replace(array('user.', 'mod.'), array('user.chat.', 'mod.chat.'), $permission);
|
|
|
|
return WCF::getUser()->getPermission($permission);
|
2012-03-03 22:12:21 +00:00
|
|
|
}
|
2011-12-19 15:22:56 +00:00
|
|
|
return (boolean) $this->chatPermissions[$room->roomID][$permission];
|
|
|
|
}
|
2012-02-26 16:55:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the cache.
|
|
|
|
*/
|
|
|
|
public static function clearCache() {
|
2012-03-08 21:07:46 +00:00
|
|
|
$packageID = \wcf\util\ChatUtil::getPackageID();
|
2012-02-26 16:55:44 +00:00
|
|
|
$ush = \wcf\system\user\storage\UserStorageHandler::getInstance();
|
|
|
|
|
|
|
|
$ush->resetAll('chatUserPermissions', $packageID);
|
2012-02-26 17:16:28 +00:00
|
|
|
\wcf\system\cache\CacheHandler::getInstance()->clear(WCF_DIR.'cache', 'cache.chatPermission-[a-f0-9]{40}.php');
|
2012-02-26 16:55:44 +00:00
|
|
|
}
|
2011-12-19 15:22:56 +00:00
|
|
|
}
|