2011-12-19 15:22:56 +00:00
|
|
|
<?php
|
2013-01-19 19:36:40 +00:00
|
|
|
namespace chat\system\permission;
|
2011-12-19 15:22:56 +00:00
|
|
|
use \wcf\system\acl\ACLHandler;
|
|
|
|
use \wcf\system\package\PackageDependencyHandler;
|
|
|
|
use \wcf\system\WCF;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles chat-permissions.
|
2014-02-28 16:06:50 +00:00
|
|
|
*
|
2012-01-28 16:51:35 +00:00
|
|
|
* @author Tim Düsterhus, Marcel Werk
|
2014-02-27 22:05:09 +00:00
|
|
|
* @copyright 2010-2014 WoltLab GmbH
|
2011-12-19 15:22:56 +00:00
|
|
|
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
|
2013-01-19 19:36:40 +00:00
|
|
|
* @package be.bastelstu.chat
|
|
|
|
* @subpackage system.permission
|
2011-12-19 15:22:56 +00:00
|
|
|
*/
|
2013-01-19 19:36:40 +00:00
|
|
|
class PermissionHandler {
|
2012-10-20 14:23:00 +00:00
|
|
|
/**
|
|
|
|
* permissions set for the active user
|
|
|
|
* @var array<boolean>
|
|
|
|
*/
|
2011-12-19 15:22:56 +00:00
|
|
|
protected $chatPermissions = array();
|
2012-10-20 14:23:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* given user decorated in a user profile
|
|
|
|
* @var \wcf\data\user\UserProfile
|
|
|
|
*/
|
|
|
|
protected $user = null;
|
2011-12-19 15:22:56 +00:00
|
|
|
|
2012-10-19 15:44:30 +00:00
|
|
|
public function __construct(\wcf\data\user\User $user = null) {
|
|
|
|
if ($user === null) $user = WCF::getUser();
|
2012-10-20 14:54:41 +00:00
|
|
|
$this->user = new \wcf\data\user\UserProfile($user);
|
2012-10-19 15:44:30 +00:00
|
|
|
|
2013-01-19 19:36:40 +00:00
|
|
|
$packageID = \chat\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
|
2013-02-16 23:04:32 +00:00
|
|
|
$this->chatPermissions = \chat\system\cache\builder\PermissionCacheBuilder::getInstance()->getData($user->getGroupIDs());
|
2011-12-19 15:22:56 +00:00
|
|
|
|
|
|
|
// get user permissions
|
2012-10-19 15:44:30 +00:00
|
|
|
if ($user->userID) {
|
2011-12-19 15:22:56 +00:00
|
|
|
// get data from storage
|
2012-10-19 15:44:30 +00:00
|
|
|
$ush->loadStorage(array($user->userID), $packageID);
|
|
|
|
|
2011-12-19 15:22:56 +00:00
|
|
|
// get ids
|
2012-10-19 15:44:30 +00:00
|
|
|
$data = $ush->getStorage(array($user->userID), 'chatUserPermissions', $packageID);
|
|
|
|
|
2011-12-19 15:22:56 +00:00
|
|
|
// cache does not exist or is outdated
|
2012-10-19 15:44:30 +00:00
|
|
|
if ($data[$user->userID] === null) {
|
2011-12-19 15:22:56 +00:00
|
|
|
$userPermissions = array();
|
|
|
|
|
|
|
|
$conditionBuilder = new \wcf\system\database\util\PreparedStatementConditionBuilder();
|
2013-01-19 19:36:40 +00:00
|
|
|
$conditionBuilder->add('acl_option.objectTypeID = ?', array(ACLHandler::getInstance()->getObjectTypeID('be.bastelstu.chat.room')));
|
2011-12-19 15:22:56 +00:00
|
|
|
$conditionBuilder->add('option_to_user.optionID = acl_option.optionID');
|
2012-10-19 15:44:30 +00:00
|
|
|
$conditionBuilder->add('option_to_user.userID = ?', array($user->userID));
|
2011-12-19 15:22:56 +00:00
|
|
|
$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
|
2012-10-19 15:44:30 +00:00
|
|
|
$ush->update($user->userID, 'chatUserPermissions', serialize($userPermissions), $packageID);
|
2011-12-19 15:22:56 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-10-19 15:44:30 +00:00
|
|
|
$userPermissions = unserialize($data[$user->userID]);
|
2011-12-19 15:22:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($userPermissions as $roomID => $permissions) {
|
|
|
|
foreach ($permissions as $name => $value) {
|
|
|
|
$this->chatPermissions[$roomID][$name] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the given permission for the given room
|
2014-02-28 16:06:50 +00:00
|
|
|
*
|
2013-01-19 19:36:40 +00:00
|
|
|
* @param \chat\data\room\Room $room
|
|
|
|
* @param string $permission
|
2011-12-19 15:22:56 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-01-19 19:36:40 +00:00
|
|
|
public function getPermission(\chat\data\room\Room $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);
|
2012-10-19 15:44:30 +00:00
|
|
|
|
2012-10-20 14:23:00 +00:00
|
|
|
return $this->user->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() {
|
2013-01-19 19:36:40 +00:00
|
|
|
$packageID = \chat\util\ChatUtil::getPackageID();
|
2012-02-26 16:55:44 +00:00
|
|
|
$ush = \wcf\system\user\storage\UserStorageHandler::getInstance();
|
|
|
|
|
|
|
|
$ush->resetAll('chatUserPermissions', $packageID);
|
2013-02-04 18:27:03 +00:00
|
|
|
\chat\system\cache\builder\PermissionCacheBuilder::getInstance()->reset();
|
2012-02-26 16:55:44 +00:00
|
|
|
}
|
2012-12-20 17:46:38 +00:00
|
|
|
}
|