Added privileges support
This commit is contained in:
parent
c345800716
commit
af5650d4f6
5 changed files with 64 additions and 1 deletions
|
@ -24,3 +24,6 @@ You received this e-mail because someone registered an user with this address at
|
|||
|
||||
Kind regards,
|
||||
{host} registration engine"
|
||||
|
||||
[privileges]
|
||||
uploadPost=registered
|
||||
|
|
34
src/Helpers/PrivilegesHelper.php
Normal file
34
src/Helpers/PrivilegesHelper.php
Normal file
|
@ -0,0 +1,34 @@
|
|||
<?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])
|
||||
? AccessRank::Admin
|
||||
: self::$privileges[$privilege];
|
||||
return $user->access_rank >= $minAccessRank;
|
||||
}
|
||||
|
||||
public static function confirmWithException($user, $privilege)
|
||||
{
|
||||
if (!self::confirm($user, $privilege))
|
||||
{
|
||||
throw new SimpleException('Insufficient privileges');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PrivilegesHelper::init();
|
|
@ -16,4 +16,22 @@ class TextHelper
|
|||
}
|
||||
return $text;
|
||||
}
|
||||
|
||||
public static function resolveConstant($constantName, $className = null)
|
||||
{
|
||||
//convert from kebab-case to CamelCase
|
||||
$constantName = preg_split('/-/', $constantName);
|
||||
$constantName = array_map('trim', $constantName);
|
||||
$constantName = array_map('ucfirst', $constantName);
|
||||
$constantName = join('', $constantName);
|
||||
if ($className !== null)
|
||||
{
|
||||
$constantName = $className . '::' . $constantName;
|
||||
}
|
||||
if (!defined($constantName))
|
||||
{
|
||||
throw new Exception('Undefined constant: ' . $constantName);
|
||||
}
|
||||
return constant($constantName);
|
||||
}
|
||||
}
|
||||
|
|
5
src/Models/Privilege.php
Normal file
5
src/Models/Privilege.php
Normal file
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
class Privilege
|
||||
{
|
||||
const UploadPost = 1;
|
||||
}
|
|
@ -10,6 +10,7 @@
|
|||
<?php foreach ($this->context->stylesheets as $name): ?>
|
||||
<link rel="stylesheet" type="text/css" href="<?php echo \Chibi\UrlHelper::absoluteUrl('/media/css/' . $name) ?>" />
|
||||
<?php endforeach ?>
|
||||
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
@ -24,7 +25,9 @@
|
|||
$preNav []= ['Browse', \Chibi\UrlHelper::route('post', 'list')];
|
||||
$preNav []= ['Comments', \Chibi\UrlHelper::route('comment', 'list')];
|
||||
$preNav []= ['Favorites', \Chibi\UrlHelper::route('post', 'favorites')];
|
||||
$preNav []= ['Upload', \Chibi\UrlHelper::route('post', 'upload')];
|
||||
if (PrivilegesHelper::confirm($this->context->user, Privilege::UploadPost))
|
||||
$preNav []= ['Upload', \Chibi\UrlHelper::route('post', 'upload')];
|
||||
|
||||
if (!$this->context->loggedIn)
|
||||
{
|
||||
$postNav []= ['Login', \Chibi\UrlHelper::route('auth', 'login')];
|
||||
|
|
Loading…
Reference in a new issue