├── README.md ├── composer.json ├── composer.lock ├── phpunit.xml ├── public └── .gitkeep ├── src ├── Mrterryh │ └── Permissions │ │ ├── Can.php │ │ ├── Permission.php │ │ ├── PermissionsServiceProvider.php │ │ ├── Role.php │ │ └── RolePermission.php └── migrations │ ├── .gitkeep │ ├── 2014_03_02_140137_create_role_table.php │ ├── 2014_03_02_140221_create_role_permission_table.php │ ├── 2014_03_02_140247_create_permission_table.php │ └── 2014_03_03_145311_add_role_id_to_user_table.php └── tests └── .gitkeep /README.md: -------------------------------------------------------------------------------- 1 | # IMPORTANT! 2 | > This package is outdated and no longer maintained. I plan on bringing it back up to date eventually, but until then there are plenty of decent alternatives. 3 | 4 | Permissions 5 | =========== 6 | 7 | Tiny Laravel 4 package for handling user roles and permissions. 8 | 9 | Installation 10 | ============ 11 | 12 | Add the following to the require key of your composer.json file: 13 | 14 | "mrterryh/permissions": "dev-master" 15 | 16 | 17 | Run `$ composer update`. 18 | 19 | Navigate to your `config/app.php` file and add `'Mrterryh\Permissions\PermissionsServiceProvider'` to the `$providers` array. 20 | 21 | Create the tables by running `$ php artisan migrate package="mrterryh/permissions"`. Ensure that your `users` table exists first. 22 | 23 | Navigate to your `models/User.php` file and add the `Mrterryh\Permissions\Can` trait below the class decloration line: 24 | 25 | class User extends Eloquent implements UserInterface, RemindableInterface { 26 | use Mrterryh\Permissions\Can; 27 | 28 | Usage 29 | ===== 30 | 31 | Create a new role: 32 | 33 | $role = new \Mrterryh\Permissions\Role(); 34 | $role->name = 'Administrator'; 35 | $role->save(); 36 | 37 | Create a new permission: 38 | 39 | $permission = new \Mrterryh\Permissions\Permission(); 40 | $permission->name = 'read_articles'; 41 | $permission->display_name ='Can read articles'; 42 | $permission->save(); 43 | 44 | Attach the permission to the role: 45 | 46 | $role->allow($permission); 47 | 48 | Create a user: 49 | 50 | $user = new User; 51 | $user->role_id = 1; 52 | $user->save(); 53 | 54 | And you're set! To check if a user has a permission: 55 | 56 | $user = User::find(1); 57 | 58 | if ($user->can('read_articles')) 59 | echo 'The user with the ID of "1" can read articles'; 60 | 61 | To check if the current authenticated user has a permission: 62 | 63 | if (Auth::user()->can('read_articles')) 64 | echo 'The current authenticated user can read articles'; 65 | 66 | License 67 | ======= 68 | 69 | Permissions is licensed under the [MIT license](http://opensource.org/licenses/MIT). 70 | 71 | Thank you! 72 | ========== 73 | 74 | Thank you for using my package. Should you encouter any problems, please submit them [here](https://github.com/mrterryh/Permissions/issues) and they shall be dealt with promptly. 75 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mrterryh/permissions", 3 | "description": "Tiny Laravel 4 package for handling user roles and permissions.", 4 | "keywords": ["laravel", "permissions", "auth", "roles"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Terry Harvey", 9 | "email": "mcstormify@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.4.0", 14 | "illuminate/support": "4.1.*" 15 | }, 16 | "autoload": { 17 | "classmap": [ 18 | "src/migrations" 19 | ], 20 | "psr-0": { 21 | "Mrterryh\\Permissions\\": "src/" 22 | } 23 | }, 24 | "minimum-stability": "stable" 25 | } 26 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" 5 | ], 6 | "hash": "27db5be13898f2b937e7ec1373d09ca8", 7 | "packages": [ 8 | { 9 | "name": "illuminate/support", 10 | "version": "v4.1.23", 11 | "target-dir": "Illuminate/Support", 12 | "source": { 13 | "type": "git", 14 | "url": "https://github.com/illuminate/support.git", 15 | "reference": "3134bebb9997be4963b320da8b7db2752d7cb937" 16 | }, 17 | "dist": { 18 | "type": "zip", 19 | "url": "https://api.github.com/repos/illuminate/support/zipball/3134bebb9997be4963b320da8b7db2752d7cb937", 20 | "reference": "3134bebb9997be4963b320da8b7db2752d7cb937", 21 | "shasum": "" 22 | }, 23 | "require": { 24 | "php": ">=5.3.0" 25 | }, 26 | "require-dev": { 27 | "jeremeamia/superclosure": "1.0.*", 28 | "mockery/mockery": "0.9.*", 29 | "patchwork/utf8": "1.1.*", 30 | "phpunit/phpunit": "3.7.*" 31 | }, 32 | "type": "library", 33 | "extra": { 34 | "branch-alias": { 35 | "dev-master": "4.1-dev" 36 | } 37 | }, 38 | "autoload": { 39 | "psr-0": { 40 | "Illuminate\\Support": "" 41 | }, 42 | "files": [ 43 | "Illuminate/Support/helpers.php" 44 | ] 45 | }, 46 | "notification-url": "https://packagist.org/downloads/", 47 | "license": [ 48 | "MIT" 49 | ], 50 | "authors": [ 51 | { 52 | "name": "Taylor Otwell", 53 | "email": "taylorotwell@gmail.com", 54 | "homepage": "https://github.com/taylorotwell", 55 | "role": "Developer" 56 | } 57 | ], 58 | "time": "2014-02-27 21:11:13" 59 | } 60 | ], 61 | "packages-dev": [ 62 | 63 | ], 64 | "aliases": [ 65 | 66 | ], 67 | "minimum-stability": "stable", 68 | "stability-flags": [ 69 | 70 | ], 71 | "platform": { 72 | "php": ">=5.3.0" 73 | }, 74 | "platform-dev": [ 75 | 76 | ] 77 | } 78 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /public/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrterryh/Permissions/569eca38fe7559c6b6b5161dcdbcd9bff06724dc/public/.gitkeep -------------------------------------------------------------------------------- /src/Mrterryh/Permissions/Can.php: -------------------------------------------------------------------------------- 1 | belongsTo('\Mrterryh\Permissions\Role', 'role_id'); 8 | } 9 | 10 | public function can($permission) 11 | { 12 | return $this->role->can($permission); 13 | } 14 | } -------------------------------------------------------------------------------- /src/Mrterryh/Permissions/Permission.php: -------------------------------------------------------------------------------- 1 | package('mrterryh/permissions'); 22 | } 23 | 24 | /** 25 | * Register the service provider. 26 | * 27 | * @return void 28 | */ 29 | public function register() 30 | { 31 | // 32 | } 33 | 34 | /** 35 | * Get the services provided by the provider. 36 | * 37 | * @return array 38 | */ 39 | public function provides() 40 | { 41 | return array(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/Mrterryh/Permissions/Role.php: -------------------------------------------------------------------------------- 1 | id); 10 | 11 | foreach ($rolePermissions as $rolePermission) { 12 | if ($rolePermission->permission->name == $permission) { 13 | return true; 14 | } 15 | } 16 | 17 | return false; 18 | } 19 | 20 | public function allow(\Mrterryh\Permissions\Permission $permission) 21 | { 22 | $rolePermission = new \Mrterryh\Permissions\RolePermission(); 23 | $rolePermission->role_id = $this->id; 24 | $rolePermission->permission_id = $permission->id; 25 | $rolePermission->save(); 26 | } 27 | 28 | public function deny(\Mrterryh\Permissions\Permission $permission) 29 | { 30 | $permission->delete(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Mrterryh/Permissions/RolePermission.php: -------------------------------------------------------------------------------- 1 | belongsTo('\Mrterryh\Permissions\Permission'); 11 | } 12 | 13 | public static function role($id) 14 | { 15 | if (!isset(static::$stored[$id])) 16 | static::$stored[$id] = RolePermission::with('permission')->where('role_id', '=', $id)->get(); 17 | 18 | return static::$stored[$id]; 19 | } 20 | } -------------------------------------------------------------------------------- /src/migrations/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrterryh/Permissions/569eca38fe7559c6b6b5161dcdbcd9bff06724dc/src/migrations/.gitkeep -------------------------------------------------------------------------------- /src/migrations/2014_03_02_140137_create_role_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | $table->string('name'); 19 | $table->timestamps(); 20 | }); 21 | } 22 | 23 | /** 24 | * Reverse the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function down() 29 | { 30 | Schema::drop('roles'); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/migrations/2014_03_02_140221_create_role_permission_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | $table->integer('role_id'); 19 | $table->integer('permission_id'); 20 | $table->timestamps(); 21 | }); 22 | } 23 | 24 | /** 25 | * Reverse the migrations. 26 | * 27 | * @return void 28 | */ 29 | public function down() 30 | { 31 | Schema::drop('role_permissions'); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/migrations/2014_03_02_140247_create_permission_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | $table->string('name'); 19 | $table->string('display_name'); 20 | $table->timestamps(); 21 | }); 22 | } 23 | 24 | /** 25 | * Reverse the migrations. 26 | * 27 | * @return void 28 | */ 29 | public function down() 30 | { 31 | Schema::drop('permissions'); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/migrations/2014_03_03_145311_add_role_id_to_user_table.php: -------------------------------------------------------------------------------- 1 | integer('role_id')->after('id')->default(0); 18 | }); 19 | } 20 | 21 | /** 22 | * Reverse the migrations. 23 | * 24 | * @return void 25 | */ 26 | public function down() 27 | { 28 | Schema::table('users', function($table) 29 | { 30 | $table->dropColumn('role_id'); 31 | }); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /tests/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrterryh/Permissions/569eca38fe7559c6b6b5161dcdbcd9bff06724dc/tests/.gitkeep --------------------------------------------------------------------------------