Skip to content

Commit

Permalink
Merge pull request #228 from Laravel-Backpack/4.1
Browse files Browse the repository at this point in the history
Added support for Backpack 4.1
  • Loading branch information
tabacitu authored May 8, 2020
2 parents a582caa + 2a5e855 commit 26021c6
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"prefer-stable": true,
"require": {
"spatie/laravel-permission": "^3.0",
"backpack/crud": "^4.0.0"
"backpack/crud": "^4.1.0"
},
"require-dev": {
"phpunit/phpunit" : "^9.0||^7.0",
Expand Down
3 changes: 3 additions & 0 deletions src/PermissionManagerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function boot()

// publish translation files
$this->publishes([__DIR__.'/resources/lang' => resource_path('lang/vendor/backpack')], 'lang');

// publish migration from Backpack 4.0 to Backpack 4.1
$this->publishes([__DIR__.'/database/migrations' => database_path('migrations')], 'migrations');
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/app/Http/Controllers/RoleCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ public function setupListOperation()
'label' => trans('backpack::permissionmanager.name'),
'type' => 'text',
]);
$this->crud->addColumn([ // select_multiple: n-n relationship (with pivot table)
'label' => trans('backpack::permissionmanager.users'), // Table column heading
'type' => 'relationship_count',
'name' => 'users', // the method that defines the relationship in your Model
'wrapper' => [
'href' => function ($crud, $column, $entry, $related_key) {
return backpack_url('user?role='.$entry->getKey());
},
],
'suffix' => ' users',
]);
if (config('backpack.permissionmanager.multiple_guards')) {
$this->crud->addColumn([
'name' => 'guard_name',
Expand Down
8 changes: 4 additions & 4 deletions src/app/Http/Controllers/UserCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public function setupUpdateOperation()
*/
public function store()
{
$this->crud->request = $this->crud->validateRequest();
$this->crud->request = $this->handlePasswordInput($this->crud->request);
$this->crud->setRequest($this->crud->validateRequest());
$this->crud->setRequest($this->handlePasswordInput($this->crud->getRequest()));
$this->crud->unsetValidation(); // validation has already been run

return $this->traitStore();
Expand All @@ -116,8 +116,8 @@ public function store()
*/
public function update()
{
$this->crud->request = $this->crud->validateRequest();
$this->crud->request = $this->handlePasswordInput($this->crud->request);
$this->crud->setRequest($this->crud->validateRequest());
$this->crud->setRequest($this->handlePasswordInput($this->crud->getRequest()));
$this->crud->unsetValidation(); // validation has already been run

return $this->traitUpdate();
Expand Down
2 changes: 1 addition & 1 deletion src/config/backpack/permissionmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

'models' => [
'user' => App\Models\BackpackUser::class,
'user' => App\User::class,
'permission' => Backpack\PermissionManager\app\Models\Permission::class,
'role' => Backpack\PermissionManager\app\Models\Role::class,
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class RemoveBackpackuserModel extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// establish the table names
$model_has_roles = config('permission.table_names.model_has_roles');
$model_has_permissions = config('permission.table_names.model_has_permissions');

// replace the BackpackUser model with User
if (\Illuminate\Support\Facades\Schema::hasTable($model_has_roles)) {
$this->replaceModels($model_has_roles);
}
if (\Illuminate\Support\Facades\Schema::hasTable($model_has_permissions)) {
$this->replaceModels($model_has_permissions);
}
}

public function replaceModels($table_name)
{
Log::info('Replacing BackpackUser model in '.$table_name);

// if you've ended up with duplicate entries (both for App\User and App\Models\BackpackUser)
// we can just delete them
$userEntries = DB::table($table_name)
->where('model_type', "App\User")
->get();

foreach ($userEntries as $entry) {
DB::table($table_name)
->where('role_id', $entry->role_id)
->where('model_type', 'App\Models\BackpackUser')
->where('model_id', $entry->model_id)
->delete();
}

// for the rest of them, we can just replace the BackpackUser model with User
DB::table($table_name)
->where('model_type', "App\Models\BackpackUser")
->update([
'model_type' => "App\User",
]);
}
}

0 comments on commit 26021c6

Please sign in to comment.