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;
|
2014-02-20 18:44:51 +01:00
|
|
|
|
|
|
|
if (!isset(self::$privileges[$privilegeName]) or
|
|
|
|
self::$privileges[$privilegeName] > $minAccessRank)
|
|
|
|
{
|
|
|
|
self::$privileges[$privilegeName] = $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-30 22:38:59 +01:00
|
|
|
if (php_sapi_name() == 'cli')
|
|
|
|
return true;
|
|
|
|
|
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-12-18 15:10:53 +01:00
|
|
|
return intval($user->accessRank) >= $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)
|
2013-10-27 20:39:32 +01:00
|
|
|
return 'all';
|
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)
|
|
|
|
{
|
2013-12-18 15:10:53 +01:00
|
|
|
if (!$user->emailConfirmed)
|
2013-10-16 18:07:23 +02:00
|
|
|
throw new SimpleException('Need e-mail address confirmation to continue');
|
|
|
|
}
|
2013-10-30 16:22:46 +01:00
|
|
|
|
|
|
|
public static function getAllowedSafety()
|
|
|
|
{
|
2013-10-30 22:38:59 +01:00
|
|
|
if (php_sapi_name() == 'cli')
|
|
|
|
return PostSafety::getAll();
|
|
|
|
|
2013-10-30 16:22:46 +01:00
|
|
|
$context = \Chibi\Registry::getContext();
|
|
|
|
return array_filter(PostSafety::getAll(), function($safety) use ($context)
|
|
|
|
{
|
|
|
|
return PrivilegesHelper::confirm(Privilege::ListPosts, PostSafety::toString($safety)) and
|
|
|
|
$context->user->hasEnabledSafety($safety);
|
|
|
|
});
|
|
|
|
}
|
2013-10-06 13:21:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PrivilegesHelper::init();
|