Laravel:Sentry权限无法正常工作

时间:2014-07-06 06:05:06

标签: php laravel laravel-4 cartalyst-sentry

我的哨兵播种机中有以下内容:

<?php

use App\Models\User;

class SentrySeeder extends Seeder {

    public function run()
    {
        DB::table('users')->delete();
        DB::table('groups')->delete();
        DB::table('users_groups')->delete();

        Sentry::getUserProvider()->create(array(
            'email'       => 'admin@admin.com',
            'password'    => "admin@admin.com",
            'first_name'  => 'Kamran',
            'last_name'   => 'Ahmed',
            'activated'   => 1,
        ));

        Sentry::getUserProvider()->create(array(
            'email'       => 'user@user.com',
            'password'    => "user@user.com",
            'first_name'  => 'New',
            'last_name'   => 'User',
            'activated'   => 1,
        ));

        Sentry::getGroupProvider()->create(array(
            'name'        => 'Admin',
            'permissions' => array(
                'blog' => 1
            ),
        ));

        Sentry::getGroupProvider()->create(array(
            'name'        => 'Blogger',
            'permissions' => array(
                'blog.add' => 1,
                'blog.update' => 1,
                'blog.trash' => 1,
                'blog.remove' => 1
            ),
        ));

        // Assign user permissions
        $adminUser  = Sentry::getUserProvider()->findByLogin('admin@admin.com');
        $adminGroup = Sentry::getGroupProvider()->findByName('Admin');

        $normalUser  = Sentry::getUserProvider()->findByLogin('user@user.com');
        $normalGroup = Sentry::getGroupProvider()->findByName('Blogger');

        $adminUser->addGroup($adminGroup);
    }

}

如您所见,我定义了两个组AdminBloggerAdmin拥有blog定义的所有权限,而博主只能blog.addblog.updateblog.trashblog.remove。在我的帖子视图中,我有一个名为Delete Permanently的按钮,我使用了blog.remove权限。我想仅在Sentry::getUser()->hasAnyAccess(array('blog', 'blog.remove'))

时显示此按钮
@if (Sentry::getUser()->hasAnyAccess(array('blog', 'blog.remove')))
    <a class="btn btn-danger" href="{{URL::to('post/delete/' . $post->id)}}">Delete Permanently</a>
@endif

现在,当我使用admin@admin.com登录时,它正常工作,即删除按钮按预期显示,因为admin可以访问blog权限。但是,当我使用user@user.com登录时,按钮未显示。虽然我已将blog.remove的权限分配给user@user.com,但仍未显示该按钮的原因是什么。我还做了一个var_dump(..)并且它返回false。任何人都可以告诉我我的实施有什么问题吗?为什么user@user.com用户没有显示删除按钮?

1 个答案:

答案 0 :(得分:0)

在播种机中,您不会将用户分配到blogger组。

$normalUser->addGroup($normalGroup);