Skip to content

Commit

Permalink
Merge pull request #56 from jeff-99/fix-permissions-migration-for-ren…
Browse files Browse the repository at this point in the history
…amed-tables

Added the configured tables names in generation of permission migration
  • Loading branch information
RichardDominik authored Feb 20, 2020
2 parents 2b5b704 + 855da56 commit ba76bef
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions resources/views/permissions.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,37 @@ public function __construct()
*/
public function up(): void
{
DB::transaction(function () {
$tableNames = config('permission.table_names', [
'roles' => 'roles',
'permissions' => 'permissions',
'model_has_permissions' => 'model_has_permissions',
'model_has_roles' => 'model_has_roles',
'role_has_permissions' => 'role_has_permissions',
]);

DB::transaction(function () use($tableNames) {
foreach ($this->permissions as $permission) {
$permissionItem = DB::table('permissions')->where([
$permissionItem = DB::table($tableNames['permissions'])->where([
'name' => $permission['name'],
'guard_name' => $permission['guard_name']
])->first();
if ($permissionItem === null) {
DB::table('permissions')->insert($permission);
DB::table($tableNames['permissions'])->insert($permission);
}
}

foreach ($this->roles as $role) {
$permissions = $role['permissions'];
unset($role['permissions']);

$roleItem = DB::table('roles')->where([
$roleItem = DB::table($tableNames['roles'])->where([
'name' => $role['name'],
'guard_name' => $role['guard_name']
])->first();
if ($roleItem !== null) {
$roleId = $roleItem->id;

$permissionItems = DB::table('permissions')->whereIn('name', $permissions)->where(
$permissionItems = DB::table($tableNames['permissions'])->whereIn('name', $permissions)->where(
'guard_name',
$role['guard_name']
)->get();
Expand All @@ -99,9 +107,9 @@ public function up(): void
'permission_id' => $permissionItem->id,
'role_id' => $roleId
];
$roleHasPermissionItem = DB::table('role_has_permissions')->where($roleHasPermissionData)->first();
$roleHasPermissionItem = DB::table($tableNames['role_has_permissions'])->where($roleHasPermissionData)->first();
if ($roleHasPermissionItem === null) {
DB::table('role_has_permissions')->insert($roleHasPermissionData);
DB::table($tableNames['role_has_permissions'])->insert($roleHasPermissionData);
}
}
}
Expand All @@ -117,15 +125,23 @@ public function up(): void
*/
public function down(): void
{
DB::transaction(function () {
$tableNames = config('permission.table_names', [
'roles' => 'roles',
'permissions' => 'permissions',
'model_has_permissions' => 'model_has_permissions',
'model_has_roles' => 'model_has_roles',
'role_has_permissions' => 'role_has_permissions',
]);

DB::transaction(function () use ($tableNames){
foreach ($this->permissions as $permission) {
$permissionItem = DB::table('permissions')->where([
$permissionItem = DB::table($tableNames['permissions'])->where([
'name' => $permission['name'],
'guard_name' => $permission['guard_name']
])->first();
if ($permissionItem !== null) {
DB::table('permissions')->where('id', $permissionItem->id)->delete();
DB::table('model_has_permissions')->where('permission_id', $permissionItem->id)->delete();
DB::table($tableNames['permissions'])->where('id', $permissionItem->id)->delete();
DB::table($tableNames['model_has_permissions'])->where('permission_id', $permissionItem->id)->delete();
}
}
});
Expand Down

0 comments on commit ba76bef

Please sign in to comment.