How to implement user permissions in Laravel 4?

梦想与她 提交于 2019-12-07 17:32:07

问题


What I basically want is user permissions.

I've got an table called 'accounts' in my database. There is a column called 'group_id'. I want to set it when the 'group_id' = 3, then the user is admin. Then he can view special sites, buttons, and things like that. I've tried to implement something like that:

public function ($roleName) {
    $role = $this->roles;

    if ($role->name == $roleName) {
        return true;
    }

    return false;
}

Also, I don't know what and how the model is needed, do I need an new one and things like that.


回答1:


Old post, but maybe someone will find this useful

Add a method to your User model that returns true if the user is an admin. In our case here, it's simply "is our group_id equal to 3?"

// models/User.php
class User extends Eloquent 
{
    ... 
    public function isAdmin()
    {
        return $this->group_id == 3;
    }

}

Next add a filter that can be used to protect routes

// filters.php
Route::filter('admin', function($route, $request)
{
    if ( ! Auth::user()->isAdmin())
    {
        return App::abort(401, 'You are not authorized.');
    }
});

Finally use the filter to protect a group of routes. I this simplified case, only an admin user could access /admin

// routes.php
Route::group(array('before' => array('auth|admin')), function()
{
    Route::get('/admin', function()
    {
        return Response::make("You're an admin!");
    }
});

Based on this post: http://laravelsnippets.com/snippets/admin-route-filter




回答2:


I suggest Authority for Laravel 4




回答3:


I personally use Verify package for user management.



来源:https://stackoverflow.com/questions/17914302/how-to-implement-user-permissions-in-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!