I am trying to check permission levels, but the only function that is working is auth.perm_one
, when perm_two
, perm_three
etc.. are identical.. The user has a permission level of 10 in the database.
I have posted all relevant code below.
Middleware File:
<?php
namespace BSA\Middleware;
use Slim\Middleware;
class BeforeMiddleware extends Middleware
{
public function call()
{
$this->app->hook('slim.before', [$this, 'run']);
$this->next->call();
}
public function run()
{
if (isset($_SESSION[$this->app->config->get('auth.session')])){
$this->app->auth = $this->app->user->where('id', $_SESSION[$this->app->config->get('auth.session')])->first();
}
$this->app->view()->appendData([
'auth' => $this->app->auth,
'baseUrl' => $this->app->config->get('app.url')
]);
}
}
User File:
<?php
namespace BSA\User;
use Illuminate\Database\Eloquent\Model as Eloquent;
class User extends Eloquent
{
protected $table = 'users';
protected $fillable = [
'email',
'username',
'password',
'perm_level',
'active',
'active_hash',
'remember_identifier',
'remember_token',
];
public function getFullName()
{
if (!$this->first_name || !$this->last_name) {
return null;
}
return "{$this->first_name} {$this->last_name}";
}
public function getFirstName()
{
if (!$this->first_name) {
return null;
}
return "{$this->first_name}";
}
public function getLastName()
{
if (!$this->last_name) {
return null;
}
return "{$this->last_name}";
}
public function getFullNameOrUsername()
{
return $this->getFullName() ?: $this->username;
}
public function getFirstNameOrUsername()
{
return $this->getFirstName() ?: $this->username;
}
public function activateAccount()
{
$this->update([
'active' => true,
'active_hash' => null
]);
}
public function hasPermission($permission)
{
$permission = $permission - 1;
return (bool) $this->perm_level > $permission;
}
public function perm_one()
{
return $this->hasPermission(1);
}
public function perm_two()
{
return $this->hasPermission(2);
}
public function perm_three()
{
return $this->hasPermission(3);
}
public function perm_four()
{
return $this->hasPermission(4);
}
public function perm_five()
{
return $this->hasPermission(5);
}
public function perm_six()
{
return $this->hasPermission(6);
}
public function perm_seven()
{
return $this->hasPermission(7);
}
public function perm_eight()
{
return $this->hasPermission(8);
}
public function perm_nine()
{
return $this->hasPermission(9);
}
public function perm_ten()
{
return $this->hasPermission(10);
}
}
Where I am trying to implement this:
<aside class="sidebar">
{% if auth %}
<article>
<h2>Hello, {{ auth.getFirstNameOrUsername }}!</h2>
<h2><a href="{{ urlFor('logout') }}">Logout</a></h2>
</article>
{% else %}
<article>
<h2>Welcome!</h2>
<div class="registerorlogin"><h2><a href="{{ urlFor('register') }}">Register</a></h2>
<h3 class="center">or</h3>
<h2><a href="{{ urlFor('login') }}">Login</a></h2></div>
</article>
{% endif %}
{% if auth.perm_one %}
<article>
<h2>Test</h2>
</article>
{% endif %}
{% if auth.perm_two %}
<h2>TEST 2</h2>
{% endif %}
</aside>
I figured it out. Here is the new code for the affected function:
public function hasPermission($permission)
{
$perm_level = intval($this->perm_level);
$permission = $permission - 1;
return $perm_level > $permission;
}