Laravel5.2 對後台使用者ACL許可權管理沒有效果

來源:互聯網
上載者:User
關鍵字 php laravel
Laravel5.1 ACL教程

按照上面的教程,使用者表使用預設的user表的話,一切都是很順利的,可是我最終的目的是背景admin表進行許可權管理,於是把教程裡的user都換成了admin,這時候出了問題,在blade中@can('edit-post')@can('delete-post')全部沒通過

於是我換到了控制器

        $admin = Auth::guard('admin')->user();        if($admin->can('edit-post')){            echo 1;        }

通過,成功輸出1,yeah!

於是我就想到了會不會是模版中的can是取的user表?於是我又把auth.php中的預設guard改成admin

    'defaults' => [        'guard' => 'admin',        'passwords' => 'users',    ],

成功!yeah!
好了,問題來了,我不改auth.phpguard的預設值,怎樣可以在模版中讓can判斷的是admin表的使用者?

回複內容:

Laravel5.1 ACL教程

按照上面的教程,使用者表使用預設的user表的話,一切都是很順利的,可是我最終的目的是背景admin表進行許可權管理,於是把教程裡的user都換成了admin,這時候出了問題,在blade中@can('edit-post')@can('delete-post')全部沒通過

於是我換到了控制器

        $admin = Auth::guard('admin')->user();        if($admin->can('edit-post')){            echo 1;        }

通過,成功輸出1,yeah!

於是我就想到了會不會是模版中的can是取的user表?於是我又把auth.php中的預設guard改成admin

    'defaults' => [        'guard' => 'admin',        'passwords' => 'users',    ],

成功!yeah!
好了,問題來了,我不改auth.phpguard的預設值,怎樣可以在模版中讓can判斷的是admin表的使用者?

看了下源碼,似乎並不支援!!!

    /**     * Compile the can statements into valid PHP.     *     * @param  string  $expression     * @return string     */    protected function compileCan($expression)    {        return "";    }
    /**     * Determine if the given ability should be granted for the current user.     *     * @param  string  $ability     * @param  array|mixed  $arguments     * @return bool     */    public function check($ability, $arguments = [])    {        try {            $result = $this->raw($ability, $arguments);        } catch (AuthorizationException $e) {            return false;        }        return (bool) $result;    }    

模板裡可以這樣寫,只不過很長,我把if裡邊的存到一個公用函數中了

@if(Gate::forUser(Auth::guard('admin')->user())->allows('$priv'))@endif 
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.