This repo is no longer being maintained. Since Laravel now requires PHP 5.4, using Laravel allows you to use traits. The Entrust package already supports this and has a much bigger community. I recommend the use of it instead.
Roles and permissions for Laravel 4.
Add searsaw/drawbridge as a requirement to your composer.json.
First, we need to export the migrations into the migrations directory so the tables for Drawbridge can be put into the database. Run the php artisan drawbridge:migrations command to export them. Then migrate the database with php artisan migrate.
For the roles and permissions to work, a Role model, a Permission model, and a User model need to exist. Luckily, Drawbridge ships with three models you can easily extend to add the functionality to your models. In your app/models directory, create the following two models.
There is also a BridgeUser model included in the package to give the functionality for adding roles to your User model. The below code adds the BridgeUser model functionality to the default User model that comes with Laravel.
All three models extend Magniloquent, a model validation package for Laravel models. Magniloquent extends Eloquent, giving your models all the functionality of Eloquent with the added benefit of model validation. For more information on Magniloquent, visit its Github page.
Adding Roles and Permissions
Roles are groups of permissions. Users can have different roles, giving them extended abilities depending on the roles they have.
To add roles to a user, use the addRole method on a user object. This method can take a role ID, a role name, or a role object. If the role object has not yet been saved to the database, it will be saved before being attached to the user. This method can also take an array of any combination of the previous three.
To add permissions to a role, use the addPermission method on a role object. This method works exactly as the addRole method mentioned above. This makes adding permissions as easy as possible.
To check if a user has a certain role, use the hasRole method on a user object. It can take an ID, a name, or a Role object itself. It returns true if the user has the role or false if the user does not.
To check if a role has a certain permission, use the hasPermission method on a role object. It can take an ID, a name, or a Permission object itself. It returns true if the role has the permission or false if the role does not.
You can also check to see if a user has a particular permission by using the the hasPermission method on a user object. It can take an ID, a name, or a Permission object itself. It returns true if the user has the permission or false if the user does not.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN