szurubooru/src/Helpers/PrivilegesHelper.php

35 lines
875 B
PHP
Raw Normal View History

2013-10-06 13:21:16 +02:00
<?php
class PrivilegesHelper
{
private static $privileges = [];
public static function init()
{
$privileges = \Chibi\Registry::getConfig()->privileges;
foreach ($privileges as $privilegeName => $minAccessRankName)
{
$privilege = TextHelper::resolveConstant($privilegeName, 'Privilege');
$minAccessRank = TextHelper::resolveConstant($minAccessRankName, 'AccessRank');
self::$privileges[$privilege] = $minAccessRank;
}
}
public static function confirm($user, $privilege)
{
$minAccessRank = isset(self::$privileges[$privilege])
2013-10-06 19:20:54 +02:00
? self::$privileges[$privilege]
: AccessRank::Admin;
return intval($user->access_rank) >= $minAccessRank;
2013-10-06 13:21:16 +02:00
}
public static function confirmWithException($user, $privilege)
{
if (!self::confirm($user, $privilege))
{
throw new SimpleException('Insufficient privileges');
}
}
}
PrivilegesHelper::init();