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 .= '.';
|
2013-10-18 00:09:50 +02:00
|
|
|
list ($privilegeName, $subPrivilegeName) = explode('.', $key);
|
2013-10-07 23:17:33 +02:00
|
|
|
$privilegeName = TextHelper::camelCaseToKebabCase($privilegeName);
|
2013-10-18 00:09:50 +02:00
|
|
|
$subPrivilegeName = TextHelper::camelCaseToKebabCase($subPrivilegeName);
|
|
|
|
$key = rtrim($privilegeName . '.' . $subPrivilegeName, '.');
|
2013-10-07 23:17:33 +02:00
|
|
|
|
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-18 00:09:50 +02:00
|
|
|
public static function confirm($privilege, $subPrivilege = null)
|
2013-10-06 13:21:16 +02:00
|
|
|
{
|
2013-10-18 00:09:50 +02:00
|
|
|
$user = \Chibi\Registry::getContext()->user;
|
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];
|
|
|
|
}
|
2013-10-18 00:09:50 +02:00
|
|
|
if ($subPrivilege != null)
|
2013-10-07 23:17:33 +02:00
|
|
|
{
|
2013-10-18 00:09:50 +02:00
|
|
|
$key2 = $key . '.' . strtolower($subPrivilege);
|
2013-10-07 23:17:33 +02:00
|
|
|
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-18 00:09:50 +02:00
|
|
|
public static function confirmWithException($privilege, $subPrivilege = null)
|
2013-10-06 13:21:16 +02:00
|
|
|
{
|
2013-10-18 00:09:50 +02:00
|
|
|
if (!self::confirm($privilege, $subPrivilege))
|
2013-10-06 13:21:16 +02:00
|
|
|
{
|
|
|
|
throw new SimpleException('Insufficient privileges');
|
|
|
|
}
|
|
|
|
}
|
2013-10-16 18:07:23 +02:00
|
|
|
|
2013-10-18 00:09:50 +02:00
|
|
|
public static function getIdentitySubPrivilege($user)
|
|
|
|
{
|
2013-10-19 18:14:19 +02:00
|
|
|
if (!$user)
|
|
|
|
return false;
|
2013-10-18 00:09:50 +02:00
|
|
|
$userFromContext = \Chibi\Registry::getContext()->user;
|
|
|
|
return $user->id == $userFromContext->id ? 'own' : 'all';
|
|
|
|
}
|
|
|
|
|
2013-10-16 18:07:23 +02:00
|
|
|
public static function confirmEmail($user)
|
|
|
|
{
|
|
|
|
if (!$user->email_confirmed)
|
|
|
|
throw new SimpleException('Need e-mail address confirmation to continue');
|
|
|
|
}
|
2013-10-06 13:21:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PrivilegesHelper::init();
|