Skip to content

Commit d27a093

Browse files
committed
Updated spatie permission to version 5 and added teams support
1 parent f04e9ef commit d27a093

File tree

9 files changed

+60
-102
lines changed

9 files changed

+60
-102
lines changed

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,26 @@ https://laravel.com/docs/6.x#installation
4848

4949
https://laravel.com/docs/6.x/authentication
5050

51-
#### Step 3. Install spatie roles & permissions package
51+
#### Step 3. Require the current package using composer:
52+
53+
```bash
54+
composer require venturedrake/laravel-crm:^0.7
55+
```
56+
57+
#### Step 4. Install spatie roles & permissions package
5258

5359
https://spatie.be/docs/laravel-permission/v4/installation-laravel
5460

55-
#### Step 4. Require the current package using composer:
61+
Adjust the settings in app/config/permission.php to the following:
5662

57-
```bash
58-
composer require venturedrake/laravel-crm:^0.7
63+
```php
64+
'permission' => VentureDrake\LaravelCrm\Models\Permission::class
65+
'role' => VentureDrake\LaravelCrm\Models\Role::class,
5966
```
6067

68+
If you are using the teams feature, follow this additional step:
69+
https://spatie.be/docs/laravel-permission/v5/basic-usage/teams-permissions
70+
6171
#### Step 5. Publish the migrations, config & assets
6272

6373
```bash
@@ -190,12 +200,10 @@ Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recen
190200

191201
## Roadmap
192202

193-
- Products
194203
- Notes
195204
- Tasks
196205
- Files / Documents
197206
- Calendar (Calls, Meetings, Reminders)
198-
- Dashboard
199207
- Custom Fields
200208
- Activity Feed / Timelines
201209
- CSV Import / Export

config/laravel-crm.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,12 @@
3333
|
3434
| For Jetstream see https://jetstream.laravel.com/2.x/features/teams.html
3535
|
36-
| For Spark see https://spark-classic.laravel.com/docs/11.0/teams
36+
| For Spark Classic see https://spark-classic.laravel.com/docs/11.0/teams
3737
|
38-
| IMPORTANT! This package uses the Spatie Permissions package to manage
39-
| user roles and permissions, which by default are system wide. Howwever
40-
| there is support added for roles & permissions at team level. To enable
41-
| you will need to adjust the settings in app/config/permission.php to
42-
| the following:
43-
|
44-
| 'permission' => VentureDrake\LaravelCrm\Models\Permission::class
45-
| 'role' => VentureDrake\LaravelCrm\Models\Role::class,
38+
| IMPORTANT! This package uses the Spatie Permissions package which as of
39+
| version 5 supports teams. PLease check you have version 5 or higher
40+
| installed and follow this additional step when installing the package:
41+
| https://spatie.be/docs/laravel-permission/v5/basic-usage/teams-permissions
4642
|
4743
*/
4844

database/migrations/add_fields_to_roles_permissions_tables.php.stub

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class AddFieldsToRolesPermissionsTables extends Migration
3535
if (! Schema::hasColumn('permissions', 'crm_permission'))
3636
{
3737
Schema::table('permissions', function (Blueprint $table) {
38-
3938
$table->boolean('crm_permission')->after('description')->default(0);
4039
});
4140
}

database/migrations/add_team_id_to_roles_permissions_tables.php.stub

Lines changed: 0 additions & 56 deletions
This file was deleted.

database/seeders/LaravelCrmTablesSeeder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,13 @@ public function run()
209209
Permission::firstOrCreate(['name' => 'edit crm fields', 'crm_permission' => 1]);
210210
Permission::firstOrCreate(['name' => 'delete crm fields', 'crm_permission' => 1]);
211211

212-
$role = Role::firstOrCreate(['name' => 'Owner', 'crm_role' => 1])
212+
$role = Role::firstOrCreate(['name' => 'Owner', 'crm_role' => 1, 'team_id' => null])
213213
->givePermissionTo(Permission::all());
214214

215-
$role = Role::firstOrCreate(['name' => 'Admin', 'crm_role' => 1])
215+
$role = Role::firstOrCreate(['name' => 'Admin', 'crm_role' => 1, 'team_id' => null])
216216
->givePermissionTo(Permission::all());
217217

218-
$role = Role::firstOrCreate(['name' => 'Manager', 'crm_role' => 1])
218+
$role = Role::firstOrCreate(['name' => 'Manager', 'crm_role' => 1, 'team_id' => null])
219219
->givePermissionTo([
220220
'create crm leads',
221221
'view crm leads',
@@ -239,7 +239,7 @@ public function run()
239239
'delete crm contacts',
240240
]);
241241

242-
$role = Role::firstOrCreate(['name' => 'Employee', 'crm_role' => 1])
242+
$role = Role::firstOrCreate(['name' => 'Employee', 'crm_role' => 1, 'team_id' => null])
243243
->givePermissionTo([
244244
'create crm leads',
245245
'view crm leads',
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
namespace VentureDrake\LaravelCrm\Http\Middleware;
3+
4+
use Closure;
5+
6+
class TeamsPermission
7+
{
8+
/**
9+
* Handle an incoming request.
10+
*
11+
* @param \Illuminate\Http\Request $request
12+
* @param \Closure $next
13+
* @return mixed
14+
*/
15+
public function handle($request, Closure $next)
16+
{
17+
if (auth()->guest()) {
18+
return $next($request);
19+
}
20+
21+
if (config('laravel-crm.teams')) {
22+
app(\Spatie\Permission\PermissionRegistrar::class)->setPermissionsTeamId(auth()->user()->currentTeam->id);
23+
}
24+
25+
return $next($request);
26+
}
27+
}

src/LaravelCrmServiceProvider.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use VentureDrake\LaravelCrm\Http\Middleware\LastOnlineAt;
2323
use VentureDrake\LaravelCrm\Http\Middleware\Settings;
2424
use VentureDrake\LaravelCrm\Http\Middleware\SystemCheck;
25+
use VentureDrake\LaravelCrm\Http\Middleware\TeamsPermission;
2526
use VentureDrake\LaravelCrm\Models\Email;
2627
use VentureDrake\LaravelCrm\Models\Lead;
2728
use VentureDrake\LaravelCrm\Models\Organisation;
@@ -82,6 +83,8 @@ class_alias('App\Models\Team', 'App\Team');
8283

8384
// Middleware
8485
$router->aliasMiddleware('auth.laravel-crm', Authenticate::class);
86+
$router->pushMiddlewareToGroup('crm', TeamsPermission::class);
87+
$router->pushMiddlewareToGroup('api', TeamsPermission::class);
8588
$router->pushMiddlewareToGroup('crm', Settings::class);
8689
$router->pushMiddlewareToGroup('api', Settings::class);
8790
$router->pushMiddlewareToGroup('crm', HasCrmAccess::class);
@@ -164,14 +167,13 @@ function ($perPage = 30, $page = null, $options = []) {
164167
__DIR__ . '/../database/migrations/create_laravel_crm_deal_products_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_laravel_crm_deal_products_table.php', 10),
165168
__DIR__ . '/../database/migrations/add_global_to_laravel_crm_settings_table.php.stub' => $this->getMigrationFileName($filesystem, 'add_global_to_laravel_crm_settings_table.php', 11),
166169
__DIR__ . '/../database/migrations/alter_fields_for_encryption_on_laravel_crm_tables.php.stub' => $this->getMigrationFileName($filesystem, 'alter_fields_for_encryption_on_laravel_crm_tables.php', 12),
167-
__DIR__ . '/../database/migrations/add_team_id_to_roles_permissions_tables.php.stub' => $this->getMigrationFileName($filesystem, 'add_team_id_to_roles_permissions_tables.php', 13),
168-
__DIR__ . '/../database/migrations/create_laravel_crm_address_types_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_laravel_crm_address_types_table.php', 14),
169-
__DIR__ . '/../database/migrations/alter_type_on_laravel_crm_phones_table.php.stub' => $this->getMigrationFileName($filesystem, 'alter_type_on_laravel_crm_phones_table.php', 15),
170-
__DIR__ . '/../database/migrations/add_description_to_laravel_crm_labels_table.php.stub' => $this->getMigrationFileName($filesystem, 'add_description_to_laravel_crm_labels_table.php', 16),
171-
__DIR__ . '/../database/migrations/add_name_to_laravel_crm_addresses_table.php.stub' => $this->getMigrationFileName($filesystem, 'add_name_to_laravel_crm_addresses_table.php', 17),
172-
__DIR__ . '/../database/migrations/create_laravel_crm_contacts_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_laravel_crm_contacts_table.php', 18),
173-
__DIR__ . '/../database/migrations/create_laravel_crm_contact_types_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_laravel_crm_contact_types_table.php', 19),
174-
__DIR__ . '/../database/migrations/create_laravel_crm_contact_contact_type_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_laravel_crm_contact_contact_type_table.php', 20),
170+
__DIR__ . '/../database/migrations/create_laravel_crm_address_types_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_laravel_crm_address_types_table.php', 13),
171+
__DIR__ . '/../database/migrations/alter_type_on_laravel_crm_phones_table.php.stub' => $this->getMigrationFileName($filesystem, 'alter_type_on_laravel_crm_phones_table.php', 14),
172+
__DIR__ . '/../database/migrations/add_description_to_laravel_crm_labels_table.php.stub' => $this->getMigrationFileName($filesystem, 'add_description_to_laravel_crm_labels_table.php', 15),
173+
__DIR__ . '/../database/migrations/add_name_to_laravel_crm_addresses_table.php.stub' => $this->getMigrationFileName($filesystem, 'add_name_to_laravel_crm_addresses_table.php', 16),
174+
__DIR__ . '/../database/migrations/create_laravel_crm_contacts_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_laravel_crm_contacts_table.php', 17),
175+
__DIR__ . '/../database/migrations/create_laravel_crm_contact_types_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_laravel_crm_contact_types_table.php', 18),
176+
__DIR__ . '/../database/migrations/create_laravel_crm_contact_contact_type_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_laravel_crm_contact_contact_type_table.php', 19),
175177
], 'migrations');
176178

177179
// Publishing the seeders

src/Models/Permission.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,11 @@
33
namespace VentureDrake\LaravelCrm\Models;
44

55
use Spatie\Permission\Models\Permission as SpatiePermission;
6-
use VentureDrake\LaravelCrm\Scopes\BelongsToTeamsScope;
7-
use VentureDrake\LaravelCrm\Traits\BelongsToTeams;
86

97
class Permission extends SpatiePermission
108
{
11-
use BelongsToTeams;
12-
139
public function scopeCrm($query)
1410
{
1511
return $query->where('crm_permission', 1);
1612
}
17-
18-
public function scopeAllTeams($query)
19-
{
20-
return $query->withoutGlobalScope(BelongsToTeamsScope::class);
21-
}
2213
}

src/Models/Role.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
namespace VentureDrake\LaravelCrm\Models;
44

55
use Spatie\Permission\Models\Role as SpatieRole;
6-
use VentureDrake\LaravelCrm\Scopes\BelongsToTeamsScope;
7-
use VentureDrake\LaravelCrm\Traits\BelongsToTeams;
86

97
class Role extends SpatieRole
108
{
11-
use BelongsToTeams;
12-
139
public function scopeCrm($query)
1410
{
1511
return $query->where('crm_role', 1);
@@ -19,9 +15,4 @@ public function scopeCrmNotOwner($query)
1915
{
2016
return $query->where('crm_role', 1)->where('name', '<>', 'Owner');
2117
}
22-
23-
public function scopeAllTeams($query)
24-
{
25-
return $query->withoutGlobalScope(BelongsToTeamsScope::class);
26-
}
2718
}

0 commit comments

Comments
 (0)