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 ROS Turtlebot3 多机协同自主探索环境时遇到的多机任务分配问题,explore节点
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题