-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1804 from erikn69/teams_feature
Teams/Groups feature
- Loading branch information
Showing
23 changed files
with
689 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class AddTeamsFields extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
$teams = config('permission.teams'); | ||
$tableNames = config('permission.table_names'); | ||
$columnNames = config('permission.column_names'); | ||
|
||
if (! $teams) { | ||
return; | ||
} | ||
if (empty($tableNames)) { | ||
throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.'); | ||
} | ||
if (empty($columnNames['team_foreign_key'] ?? null)) { | ||
throw new \Exception('Error: team_foreign_key on config/permission.php not loaded. Run [php artisan config:clear] and try again.'); | ||
} | ||
|
||
Schema::table($tableNames['roles'], function (Blueprint $table) use ($tableNames, $columnNames) { | ||
if (! Schema::hasColumn($tableNames['roles'], $columnNames['team_foreign_key'])) { | ||
$table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable(); | ||
$table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index'); | ||
} | ||
}); | ||
|
||
Schema::table($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) { | ||
if (! Schema::hasColumn($tableNames['model_has_permissions'], $columnNames['team_foreign_key'])) { | ||
$table->unsignedBigInteger($columnNames['team_foreign_key'])->default('1');; | ||
$table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index'); | ||
|
||
$table->dropPrimary(); | ||
$table->primary([$columnNames['team_foreign_key'], 'permission_id', $columnNames['model_morph_key'], 'model_type'], | ||
'model_has_permissions_permission_model_type_primary'); | ||
} | ||
}); | ||
|
||
Schema::table($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) { | ||
if (! Schema::hasColumn($tableNames['model_has_roles'], $columnNames['team_foreign_key'])) { | ||
$table->unsignedBigInteger($columnNames['team_foreign_key'])->default('1');; | ||
$table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index'); | ||
|
||
$table->dropPrimary(); | ||
$table->primary([$columnNames['team_foreign_key'], 'role_id', $columnNames['model_morph_key'], 'model_type'], | ||
'model_has_roles_role_model_type_primary'); | ||
} | ||
}); | ||
|
||
app('cache') | ||
->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null) | ||
->forget(config('permission.cache.key')); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- | ||
title: Teams permissions | ||
weight: 3 | ||
--- | ||
|
||
NOTE: Those changes must be made before performing the migration. If you have already run the migration and want to upgrade your solution, you can run the artisan console command `php artisan permission:setup-teams`, to create a new migration file named [xxxx_xx_xx_xx_add_teams_fields.php](https://github.com/spatie/laravel-permission/blob/master/database/migrations/add_teams_fields.php.stub) and then run `php artisan migrate` to upgrade your database tables. | ||
|
||
When enabled, teams permissions offers you a flexible control for a variety of scenarios. The idea behind teams permissions is inspired by the default permission implementation of [Laratrust](https://laratrust.santigarcor.me/). | ||
|
||
|
||
Teams permissions can be enabled in the permission config file: | ||
|
||
```php | ||
// config/permission.php | ||
'teams' => true, | ||
``` | ||
|
||
Also, if you want to use a custom foreign key for teams you must change in the permission config file: | ||
```php | ||
// config/permission.php | ||
'team_foreign_key' => 'custom_team_id', | ||
``` | ||
|
||
## Working with Teams Permissions | ||
|
||
After implements on login a solution for select a team on authentication (for example set `team_id` of the current selected team on **session**: `session(['team_id' => $team->team_id]);` ), | ||
we can set global `team_id` from anywhere, but works better if you create a `Middleware`, example: | ||
|
||
```php | ||
namespace App\Http\Middleware; | ||
|
||
class TeamsPermission{ | ||
|
||
public function handle($request, \Closure $next){ | ||
if(!empty(auth()->user())){ | ||
// session value set on login | ||
app(\Spatie\Permission\PermissionRegistrar::class)->setPermissionsTeamId(session('team_id')); | ||
} | ||
// other custom ways to get team_id | ||
/*if(!empty(auth('api')->user())){ | ||
// `getTeamIdFromToken()` example of custom method for getting the set team_id | ||
app(\Spatie\Permission\PermissionRegistrar::class)->setPermissionsTeamId(auth('api')->user()->getTeamIdFromToken()); | ||
}*/ | ||
|
||
return $next($request); | ||
} | ||
} | ||
``` | ||
NOTE: You must add your custom `Middleware` to `$middlewarePriority` on `app/Http/Kernel.php`. | ||
|
||
## Roles Creating | ||
|
||
When creating a role you can pass the `team_id` as an optional parameter | ||
|
||
```php | ||
// with null team_id it creates a global role, global roles can be used from any team and they are unique | ||
Role::create(['name' => 'writer', 'team_id' => null]); | ||
|
||
// creates a role with team_id = 1, team roles can have the same name on different teams | ||
Role::create(['name' => 'reader', 'team_id' => 1]); | ||
|
||
// creating a role without team_id makes the role take the default global team_id | ||
Role::create(['name' => 'reviewer']); | ||
``` | ||
|
||
## Roles/Permissions Assignment & Removal | ||
|
||
The role/permission assignment and removal are the same, but they take the global `team_id` set on login for sync. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.