Twig 和函数的问题

问题描述 投票:0回答:1

我正在尝试检查权限级别,但唯一有效的功能是

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> 
php function authentication twig slim
1个回答
1
投票

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

public function hasPermission($permission)
{

    $perm_level = intval($this->perm_level);
    $permission = $permission - 1;

    return $perm_level > $permission;

}
© www.soinside.com 2019 - 2024. All rights reserved.