dynamitem
dynamitem

Reputation: 1669

How to implement user permissions in Laravel 4?

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.

Upvotes: 3

Views: 11991

Answers (3)

Jason Macgowan
Jason Macgowan

Reputation: 524

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

Upvotes: 6

Andreyco
Andreyco

Reputation: 22872

I suggest Authority for Laravel 4

Upvotes: 4

Melvin
Melvin

Reputation: 6008

I personally use Verify package for user management.

Upvotes: 1

Related Questions