Skip to content

Commit

Permalink
refactor(permission): Assign module to a each permision
Browse files Browse the repository at this point in the history
  • Loading branch information
gnovaro committed Mar 20, 2024
1 parent 9de1950 commit ae5ed23
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 32 deletions.
18 changes: 9 additions & 9 deletions database/seeders/ModuleSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ class ModuleSeeder extends Seeder
public function run(): void
{
DB::table('module')->upsert([
['name' => 'Lead'],
['name' => 'Customer'],
['name' => 'Product'],
['name' => 'Order'],
['name' => 'Supplier'],
['name' => 'Accounting'],
['name' => 'User'],
['name' => 'Company'],
['name' => 'Report'],
['name' => 'Lead'], //1
['name' => 'Customer'], //2
['name' => 'Product'], //3
['name' => 'Order'], //4
['name' => 'Supplier'], //5
['name' => 'Accounting'], //6
['name' => 'User'], //7
['name' => 'Company'], //8
['name' => 'Report'], //9
], ['name'], ['name']);
}
}
87 changes: 65 additions & 22 deletions database/seeders/PermissionSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,23 @@ public function run(): void
foreach ($this->permissions() as $permission) {
$roles = $permission['roles'];
$name = $permission['name'];
$guard_name = $permission['guard_name'];
$guardName = $permission['guard_name'];
$moduleId = $permission['module_id'] ?? null;
$permissionObject = null;

try {
$permissionObject = Permission::create([
'name' => $name,
'guard_name' => $guard_name,
]);
$permissionObject = Permission::firstOrCreate(
[
'name' => $name,
'guard_name' => $guardName,
'module_id' => $moduleId,
]
);
} catch (Throwable $throwable) {
$permissionObject = Permission::findByName($name, $guard_name);
$permissionObject = Permission::findByName($name, $guardName);
$permissionObject->module_id = $moduleId;
$permissionObject->updated_at = now();
$permissionObject->save();
}

foreach ($roles as $roleName) {
Expand All @@ -66,184 +73,220 @@ public function permissions(): array
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Seller', 'Support'],
'name' => 'read dashboard',
'guard_name' => 'web',
'module_id' => null,
],
// Lead
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Seller'],
'name' => 'create lead',
'guard_name' => 'web',
'module_id' => 1,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Seller'],
'name' => 'read lead',
'guard_name' => 'web',
'module_id' => 1,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Seller'],
'name' => 'update lead',
'guard_name' => 'web',
'module_id' => 1,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin'],
'name' => 'delete lead',
'guard_name' => 'web',
'module_id' => 1,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin'],
'name' => 'export lead',
'guard_name' => 'web',
'module_id' => 1,
],
// Customer
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Seller', 'Support'],
'name' => 'create customer',
'guard_name' => 'web',
'module_id' => 2,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Seller', 'Support'],
'name' => 'read customer',
'guard_name' => 'web',
'module_id' => 2,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Seller', 'Support'],
'name' => 'update customer',
'guard_name' => 'web',
'module_id' => 2,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin'],
'name' => 'delete customer',
'guard_name' => 'web',
'module_id' => 2,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin'],
'name' => 'export customer',
'guard_name' => 'web',
'module_id' => 2,
],
// Product
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Seller'],
'name' => 'create product',
'guard_name' => 'web',
'module_id' => 3,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Seller', 'Support'],
'name' => 'read product',
'guard_name' => 'web',
'module_id' => 3,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin'],
'name' => 'delete product',
'guard_name' => 'web',
'module_id' => 3,
],
// Order
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Seller', 'Support'],
'name' => 'create order',
'guard_name' => 'web',
'module_id' => 4,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Seller', 'Support'],
'name' => 'read order',
'guard_name' => 'web',
'module_id' => 4,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Seller', 'Support'],
'name' => 'update order',
'guard_name' => 'web',
'module_id' => 4,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin'],
'name' => 'delete order',
'guard_name' => 'web',
'module_id' => 4,
],
// Company
[
'roles' => ['SuperAdmin'], //Only SuperAdmin can create company
'name' => 'create company',
'guard_name' => 'web',
'module_id' => 8,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Support'],
'name' => 'read company',
'guard_name' => 'web',
'module_id' => 8,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin'],
'name' => 'update company',
'guard_name' => 'web',
'module_id' => 8,
],
[
'roles' => ['SuperAdmin'], //Only SuperAdmin can delete company
'name' => 'delete company',
'guard_name' => 'web',
'module_id' => 8,
],
// User
[
'roles' => ['SuperAdmin'], //Only SuperAdmin can create user
'name' => 'create user',
'guard_name' => 'web',
'module_id' => 7,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Support'],
'name' => 'read user',
'guard_name' => 'web',
'module_id' => 7,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Support'],
'name' => 'update user',
'guard_name' => 'web',
'module_id' => 7,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Support'],
'name' => 'delete user',
'guard_name' => 'web',
'module_id' => 7,
],
// Supplier
[
'roles' => ['SuperAdmin', 'CompanyAdmin'],
'name' => 'create supplier',
'guard_name' => 'web',
'module_id' => 5,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Support'],
'name' => 'read supplier',
'guard_name' => 'web',
'module_id' => 5,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin'],
'name' => 'update supplier',
'guard_name' => 'web',
'module_id' => 5,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin'],
'name' => 'delete supplier',
'guard_name' => 'web',
'module_id' => 5,
],
// Accounting
[
'roles' => ['SuperAdmin', 'CompanyAdmin'],
'name' => 'create accounting',
'guard_name' => 'web',
'module_id' => 6,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin'],
'name' => 'read accounting',
'guard_name' => 'web',
'module_id' => 6,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin'],
'name' => 'update accounting',
'guard_name' => 'web',
'module_id' => 6,
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin'],
'name' => 'delete accounting',
'guard_name' => 'web',
],
// Product
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Seller'],
'name' => 'create product',
'guard_name' => 'web',
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Seller', 'Support'],
'name' => 'read product',
'guard_name' => 'web',
],
[
'roles' => ['SuperAdmin', 'CompanyAdmin'],
'name' => 'delete product',
'guard_name' => 'web',
'module_id' => 6,
],
// Report
[
'roles' => ['SuperAdmin', 'CompanyAdmin', 'Seller', 'Support'],
'name' => 'read report',
'guard_name' => 'web',
'module_id' => 9,
],
// Message
[
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

const APP_VERSION = '3.9.2';
const APP_VERSION = '3.9.3';

0 comments on commit ae5ed23

Please sign in to comment.