Added privileges support

This commit is contained in:
Marcin Kurczewski 2013-10-06 13:21:16 +02:00
parent c345800716
commit af5650d4f6
5 changed files with 64 additions and 1 deletions

View file

@ -24,3 +24,6 @@ You received this e-mail because someone registered an user with this address at
Kind regards, Kind regards,
{host} registration engine" {host} registration engine"
[privileges]
uploadPost=registered

View 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();

View file

@ -16,4 +16,22 @@ class TextHelper
} }
return $text; 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
View file

@ -0,0 +1,5 @@
<?php
class Privilege
{
const UploadPost = 1;
}

View file

@ -10,6 +10,7 @@
<?php foreach ($this->context->stylesheets as $name): ?> <?php foreach ($this->context->stylesheets as $name): ?>
<link rel="stylesheet" type="text/css" href="<?php echo \Chibi\UrlHelper::absoluteUrl('/media/css/' . $name) ?>" /> <link rel="stylesheet" type="text/css" href="<?php echo \Chibi\UrlHelper::absoluteUrl('/media/css/' . $name) ?>" />
<?php endforeach ?> <?php endforeach ?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head> </head>
<body> <body>
@ -24,7 +25,9 @@
$preNav []= ['Browse', \Chibi\UrlHelper::route('post', 'list')]; $preNav []= ['Browse', \Chibi\UrlHelper::route('post', 'list')];
$preNav []= ['Comments', \Chibi\UrlHelper::route('comment', 'list')]; $preNav []= ['Comments', \Chibi\UrlHelper::route('comment', 'list')];
$preNav []= ['Favorites', \Chibi\UrlHelper::route('post', 'favorites')]; $preNav []= ['Favorites', \Chibi\UrlHelper::route('post', 'favorites')];
if (PrivilegesHelper::confirm($this->context->user, Privilege::UploadPost))
$preNav []= ['Upload', \Chibi\UrlHelper::route('post', 'upload')]; $preNav []= ['Upload', \Chibi\UrlHelper::route('post', 'upload')];
if (!$this->context->loggedIn) if (!$this->context->loggedIn)
{ {
$postNav []= ['Login', \Chibi\UrlHelper::route('auth', 'login')]; $postNav []= ['Login', \Chibi\UrlHelper::route('auth', 'login')];