Home > Backend Development > PHP Tutorial > Laravel5.2 对后台用户ACL权限管理没有效果

Laravel5.2 对后台用户ACL权限管理没有效果

WBOY
Release: 2016-06-06 20:13:59
Original
1388 people have browsed it

Laravel5.1 ACL教程

按照上面的教程,用户表使用默认的user表的话,一切都是很顺利的,可是我最终的目的是后台的admin表进行权限管理,于是把教程里的user都换成了admin,这时候出了问题,在blade中@can('edit-post')@can('delete-post')全部没通过

于是我换到了控制器

<code>        $admin = Auth::guard('admin')->user();
        if($admin->can('edit-post')){
            echo 1;
        }</code>
Copy after login
Copy after login

通过,成功输出1,yeah!

于是我就想到了会不会是模版中的can是取的user表?于是我又把auth.php中的默认guard改成admin

<code>    'defaults' => [
        'guard' => 'admin',
        'passwords' => 'users',
    ],</code>
Copy after login
Copy after login

成功!yeah!
好了,问题来了,我不改auth.phpguard的默认值,怎样可以在模版中让can判断的是admin表的用户?

回复内容:

Laravel5.1 ACL教程

按照上面的教程,用户表使用默认的user表的话,一切都是很顺利的,可是我最终的目的是后台的admin表进行权限管理,于是把教程里的user都换成了admin,这时候出了问题,在blade中@can('edit-post')@can('delete-post')全部没通过

于是我换到了控制器

<code>        $admin = Auth::guard('admin')->user();
        if($admin->can('edit-post')){
            echo 1;
        }</code>
Copy after login
Copy after login

通过,成功输出1,yeah!

于是我就想到了会不会是模版中的can是取的user表?于是我又把auth.php中的默认guard改成admin

<code>    'defaults' => [
        'guard' => 'admin',
        'passwords' => 'users',
    ],</code>
Copy after login
Copy after login

成功!yeah!
好了,问题来了,我不改auth.phpguard的默认值,怎样可以在模版中让can判断的是admin表的用户?

看了下源码,似乎并不支持!!!

<code>    /**
     * Compile the can statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileCan($expression)
    {
        return "<?php if (Gate::check{$expression}): ?>";
    }</code>
Copy after login
<code>    /**
     * 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;
    }
    </code>
Copy after login

模板里可以这样写,只不过很长,我把if里边的存到一个公用函数中了

<code>@if(Gate::forUser(Auth::guard('admin')->user())->allows('$priv'))

@endif 

</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template