Skip to content

Commit 6e44d0f

Browse files
committed
Laravel 5.1 and up user authorisation with roles and permissions
1 parent 7401aa9 commit 6e44d0f

27 files changed

+1539
-1
lines changed

CONTRIBUTING.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Contributing
2+
3+
Contributions are **welcome** and will be fully **credited**.
4+
5+
We accept contributions via Pull Requests on [Github](https://github.com/ribedesign/laravel-authorisation).
6+
7+
8+
## Pull Requests
9+
10+
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).
11+
12+
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
13+
14+
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
15+
16+
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
17+
18+
- **Create feature branches** - Don't ask us to pull from your master branch.
19+
20+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
21+
22+
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
23+
24+
25+
## Running Tests
26+
27+
``` bash
28+
$ phpunit
29+
```
30+
31+
32+
**Happy coding**!

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) Ribedesign <[email protected]>
4+
5+
> Permission is hereby granted, free of charge, to any person obtaining a copy
6+
> of this software and associated documentation files (the "Software"), to deal
7+
> in the Software without restriction, including without limitation the rights
8+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
> copies of the Software, and to permit persons to whom the Software is
10+
> furnished to do so, subject to the following conditions:
11+
>
12+
> The above copyright notice and this permission notice shall be included in
13+
> all copies or substantial portions of the Software.
14+
>
15+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
> THE SOFTWARE.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# laravel-authorisation
1+
# Laravel 5.1 and up user authorisation with roles and permissions

composer.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"name": "ribedesign/laravel-authorisation",
3+
"description": "Laravel 5.1 and up user authorisation with roles and permissions",
4+
"keywords": [
5+
"ribedesign",
6+
"laravel",
7+
"authorisation",
8+
"acl",
9+
"security"
10+
],
11+
"homepage": "https://github.com/ribedesign/laravel-authorisation",
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "Jaco Rietberg",
16+
"email": "[email protected]",
17+
"homepage": "https://www.ribedesign.nl",
18+
"role": "Developer"
19+
}
20+
],
21+
"require": {
22+
"php" : ">=5.6.0",
23+
"laravel/framework": "~5.1.11|~5.2.0|~5.3.0|~5.4.0|~5.5.0",
24+
"illuminate/contracts": "~5.1.0|~5.2.0|~5.3.0|~5.4.0|~5.5.0"
25+
},
26+
"require-dev": {
27+
"monolog/monolog": "^1.22",
28+
"orchestra/testbench": "~3.3.0|~3.4.0|~3.5.0",
29+
"phpunit/phpunit" : "^5.7.8|~6.0"
30+
},
31+
"autoload": {
32+
"psr-4": {
33+
"Ribedesign\\Authorisation\\": "src"
34+
}
35+
},
36+
"autoload-dev": {
37+
"psr-4": {
38+
"Ribedesign\\Authorisation\\Test\\": "tests"
39+
}
40+
},
41+
"scripts": {
42+
"test": "phpunit"
43+
},
44+
"config": {
45+
"sort-packages": true
46+
},
47+
"extra": {
48+
"laravel": {
49+
"providers": [
50+
"Ribedesign\\Authorisation\\AuthorisationServiceProvider"
51+
]
52+
}
53+
}
54+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
return [
4+
5+
'models' => [
6+
7+
/*
8+
* When using the "HasRoles" trait from this package, we need to know which
9+
* Eloquent model should be used to retrieve your roles. Of course, it
10+
* is often just the "Role" model but you may use whatever you like.
11+
*
12+
* The model you want to use as a Role model needs to implement the
13+
* `Ribedesign\Authorisation\Contracts\Role` contract.
14+
*/
15+
16+
'role' => Ribedesign\Authorisation\Models\Role::class,
17+
18+
/*
19+
* When using the "HasRoles" trait from this package, we need to know which
20+
* Eloquent model should be used to retrieve your permissions. Of course, it
21+
* is often just the "Permission" model but you may use whatever you like.
22+
*
23+
* The model you want to use as a Permission model needs to implement the
24+
* `Ribedesign\Authorisation\Contracts\Permission` contract.
25+
*/
26+
27+
'permission' => Ribedesign\Authorisation\Models\Permission::class,
28+
29+
/*
30+
* When using the "HasRoles" trait from this package, we need to know which
31+
* Eloquent model should be used to retrieve your permissions. Of course, it
32+
* is often just the "Object" model but you may use whatever you like.
33+
*
34+
* The model you want to use as a Object model needs to implement the
35+
* `Ribedesign\Authorisation\Contracts\Object` contract.
36+
*/
37+
38+
'object' => Ribedesign\Authorisation\Models\Object::class,
39+
40+
/*
41+
* When using the "HasRoles" trait from this package, we need to know which
42+
* Eloquent model should be used to retrieve your permissions. Of course, it
43+
* is often just the "Action" model but you may use whatever you like.
44+
*
45+
* The model you want to use as a Object model needs to implement the
46+
* `Ribedesign\Authorisation\Contracts\Action` contract.
47+
*/
48+
49+
'action' => Ribedesign\Authorisation\Models\Action::class,
50+
51+
],
52+
53+
'table_names' => [
54+
55+
/*
56+
* The table that your application uses for users. This table's model will
57+
* be using the "HasRoles" and "HasPermissions" traits.
58+
*/
59+
60+
'users' => 'users',
61+
62+
/*
63+
* When using the "HasRoles" trait from this package, we need to know which
64+
* table should be used to retrieve your roles. We have chosen a basic
65+
* default value but you may easily change it to any table you like.
66+
*/
67+
68+
'roles' => 'roles',
69+
70+
/*
71+
* When using the "HasRoles" trait from this package, we need to know which
72+
* table should be used to retrieve your permissions. We have chosen a basic
73+
* default value but you may easily change it to any table you like.
74+
*/
75+
76+
'permissions' => 'permissions',
77+
78+
/*
79+
* When using the "HasRoles" trait from this package, we need to know which
80+
* table should be used to retrieve your objects. We have chosen a basic
81+
* default value but you may easily change it to any table you like.
82+
*/
83+
84+
'objects' => 'objects',
85+
86+
/*
87+
* When using the "HasRoles" trait from this package, we need to know which
88+
* table should be used to retrieve your actions. We have chosen a basic
89+
* default value but you may easily change it to any table you like.
90+
*/
91+
92+
'actions' => 'actions',
93+
94+
/*
95+
* When using the "HasRoles" trait from this package, we need to know which
96+
* table should be used to retrieve your users permissions. We have chosen a
97+
* basic default value but you may easily change it to any table you like.
98+
*/
99+
100+
'user_has_permissions' => 'user_has_permissions',
101+
102+
/*
103+
* When using the "HasRoles" trait from this package, we need to know which
104+
* table should be used to retrieve your users roles. We have chosen a
105+
* basic default value but you may easily change it to any table you like.
106+
*/
107+
108+
'user_has_roles' => 'user_has_roles',
109+
110+
/*
111+
* When using the "HasRoles" trait from this package, we need to know which
112+
* table should be used to retrieve your roles permissions. We have chosen a
113+
* basic default value but you may easily change it to any table you like.
114+
*/
115+
116+
'role_has_permissions' => 'role_has_permissions',
117+
],
118+
119+
'foreign_keys' => [
120+
121+
/*
122+
* The name of the foreign key to the users table.
123+
*/
124+
'users' => 'user_id',
125+
],
126+
];
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
6+
class CreateAuthorisationTables extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
$tableNames = config('laravel-authorisation.table_names');
16+
$foreignKeys = config('laravel-authorisation.foreign_keys');
17+
18+
Schema::create($tableNames['roles'], function (Blueprint $table) {
19+
$table->increments('id');
20+
$table->integer('parent_id')->unsigned()->default(0);
21+
$table->string('name')->unique();
22+
$table->timestamps();
23+
});
24+
25+
Schema::create($tableNames['permissions'], function (Blueprint $table) {
26+
$table->increments('id');
27+
$table->string('name')->unique();
28+
$table->integer('object_id')->unsigned();
29+
$table->integer('action_id')->unsigned();
30+
$table->integer('enabled')->unsigned()->default(1);
31+
$table->timestamps();
32+
});
33+
34+
Schema::create($tableNames['objects'], function (Blueprint $table) {
35+
$table->increments('id');
36+
$table->string('name')->unique();
37+
$table->timestamps();
38+
});
39+
40+
Schema::create($tableNames['actions'], function (Blueprint $table) {
41+
$table->increments('id');
42+
$table->string('name')->unique();
43+
$table->timestamps();
44+
});
45+
46+
Schema::create($tableNames['user_has_permissions'], function (Blueprint $table) use ($tableNames, $foreignKeys) {
47+
$table->integer($foreignKeys['users'])->unsigned();
48+
$table->integer('permission_id')->unsigned();
49+
50+
$table->foreign($foreignKeys['users'])
51+
->references('id')
52+
->on($tableNames['users'])
53+
->onDelete('cascade');
54+
55+
$table->foreign('permission_id')
56+
->references('id')
57+
->on($tableNames['permissions'])
58+
->onDelete('cascade');
59+
60+
$table->primary([$foreignKeys['users'], 'permission_id']);
61+
});
62+
63+
Schema::create($tableNames['user_has_roles'], function (Blueprint $table) use ($tableNames, $foreignKeys) {
64+
$table->integer('role_id')->unsigned();
65+
$table->integer($foreignKeys['users'])->unsigned();
66+
67+
$table->foreign('role_id')
68+
->references('id')
69+
->on($tableNames['roles'])
70+
->onDelete('cascade');
71+
72+
$table->foreign($foreignKeys['users'])
73+
->references('id')
74+
->on($tableNames['users'])
75+
->onDelete('cascade');
76+
77+
$table->primary(['role_id', $foreignKeys['users']]);
78+
});
79+
80+
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
81+
$table->integer('permission_id')->unsigned();
82+
$table->integer('role_id')->unsigned();
83+
84+
$table->foreign('permission_id')
85+
->references('id')
86+
->on($tableNames['permissions'])
87+
->onDelete('cascade');
88+
89+
$table->foreign('role_id')
90+
->references('id')
91+
->on($tableNames['roles'])
92+
->onDelete('cascade');
93+
94+
$table->primary(['permission_id', 'role_id']);
95+
});
96+
}
97+
98+
/**
99+
* Reverse the migrations.
100+
*
101+
* @return void
102+
*/
103+
public function down()
104+
{
105+
$tableNames = config('laravel-authorisation.table_names');
106+
107+
Schema::drop($tableNames['role_has_permissions']);
108+
Schema::drop($tableNames['user_has_roles']);
109+
Schema::drop($tableNames['user_has_permissions']);
110+
Schema::drop($tableNames['roles']);
111+
Schema::drop($tableNames['permissions']);
112+
Schema::drop($tableNames['objects']);
113+
Schema::drop($tableNames['actions']);
114+
}
115+
}

0 commit comments

Comments
 (0)