2013-10-06 13:21:16 +02:00
|
|
|
<?php
|
|
|
|
class PrivilegesHelper
|
|
|
|
{
|
|
|
|
private static $privileges = [];
|
|
|
|
|
|
|
|
public static function init()
|
|
|
|
{
|
2013-10-07 23:17:33 +02:00
|
|
|
self::$privileges = [];
|
|
|
|
foreach (\Chibi\Registry::getConfig()->privileges as $key => $minAccessRankName)
|
2013-10-06 13:21:16 +02:00
|
|
|
{
|
2013-10-07 23:17:33 +02:00
|
|
|
if (strpos($key, '.') === false)
|
|
|
|
$key .= '.';
|
|
|
|
list ($privilegeName, $flag) = explode('.', $key);
|
|
|
|
$privilegeName = TextHelper::camelCaseToKebabCase($privilegeName);
|
|
|
|
$flag = TextHelper::camelCaseToKebabCase($flag);
|
|
|
|
$key = rtrim($privilegeName . '.' . $flag, '.');
|
|
|
|
|
2013-10-06 13:21:16 +02:00
|
|
|
$minAccessRank = TextHelper::resolveConstant($minAccessRankName, 'AccessRank');
|
2013-10-07 23:17:33 +02:00
|
|
|
self::$privileges[$key] = $minAccessRank;
|
2013-10-06 13:21:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-07 23:17:33 +02:00
|
|
|
public static function confirm($user, $privilege, $flag = null)
|
2013-10-06 13:21:16 +02:00
|
|
|
{
|
2013-10-07 23:17:33 +02:00
|
|
|
$minAccessRank = AccessRank::Admin;
|
|
|
|
|
|
|
|
$key = TextHelper::camelCaseToKebabCase(Privilege::toString($privilege));
|
|
|
|
if (isset(self::$privileges[$key]))
|
|
|
|
{
|
|
|
|
$minAccessRank = self::$privileges[$key];
|
|
|
|
}
|
|
|
|
if ($flag != null)
|
|
|
|
{
|
|
|
|
$key2 = $key . '.' . strtolower($flag);
|
|
|
|
if (isset(self::$privileges[$key2]))
|
|
|
|
{
|
|
|
|
$minAccessRank = self::$privileges[$key2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-06 19:20:54 +02:00
|
|
|
return intval($user->access_rank) >= $minAccessRank;
|
2013-10-06 13:21:16 +02:00
|
|
|
}
|
|
|
|
|
2013-10-07 23:17:33 +02:00
|
|
|
public static function confirmWithException($user, $privilege, $flag = null)
|
2013-10-06 13:21:16 +02:00
|
|
|
{
|
2013-10-07 23:17:33 +02:00
|
|
|
if (!self::confirm($user, $privilege, $flag))
|
2013-10-06 13:21:16 +02:00
|
|
|
{
|
|
|
|
throw new SimpleException('Insufficient privileges');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PrivilegesHelper::init();
|