小枝和函数的问题


Problems with Twig and Functions

我试图检查权限级别,但唯一有效的功能是auth.perm_one,当perm_two, perm_three等。是一样的. .用户在数据库中的权限级别为10。

我在下面贴出了所有相关的代码

中间件文件:

<?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')
        ]);
    }
}
用户文件:

<?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);
    }
}

我想实现这个:

<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> 

我明白了。以下是受影响函数的新代码:

public function hasPermission($permission)
{
    $perm_level = intval($this->perm_level);
    $permission = $permission - 1;
    if ($perm_level > $permission) {return true;} else {return false;}
}