douke6857 2015-10-26 00:58
浏览 18
已采纳

树枝和功能的问题

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> 
  • 写回答

1条回答 默认 最新

  • dongxi3859 2015-10-26 20:16
    关注

    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;
    
        if ($perm_level > $permission) {return true;} else {return false;}
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥15 this signal is connected to multiple drivers怎么解决
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus