-
Notifications
You must be signed in to change notification settings - Fork 3
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 #1722 from bakaphp/abilities-module-template
refactor: debug test
- Loading branch information
Showing
15 changed files
with
422 additions
and
7 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
42 changes: 42 additions & 0 deletions
42
app/Console/Commands/AccessControl/UpdateAbilitiesCommand.php
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,42 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands\AccessControl; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Redis; | ||
use Kanvas\AccessControlList\Actions\CreateAbilitiesByModule; | ||
use Kanvas\Apps\Models\Apps; | ||
use Kanvas\AccessControlList\Enums\ModuleEnum; | ||
use Kanvas\AccessControlList\Repositories\RolesRepository; | ||
|
||
class UpdateAbilitiesCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'kanvas:update-abilities {app?}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Command description'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle() | ||
{ | ||
if ($key = $this->argument('app')) { | ||
$apps = Apps::where('key', $key)->get(); | ||
} else { | ||
$apps = Apps::all(); | ||
} | ||
foreach ($apps as $app) { | ||
(new CreateAbilitiesByModule($app))->execute(); | ||
} | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
database/migrations/2024_04_15_162222_abilities_modules_table.php
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,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class () extends Migration { | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('abilities_modules', function (Blueprint $table) { | ||
$table->id(); | ||
$table->integer('system_modules_id')->index('system_modules_id'); | ||
$table->integer('apps_id')->index('apps_id'); | ||
$table->integer('abilities_id')->index('abilities_id'); | ||
$table->integer('module_id')->index('module_id'); | ||
$table->string('scope'); | ||
$table->integer('is_deleted')->default(0); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('abilities_modules'); | ||
} | ||
}; |
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
55 changes: 55 additions & 0 deletions
55
src/Kanvas/AccessControlList/Actions/CreateAbilitiesByModule.php
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kanvas\AccessControlList\Actions; | ||
|
||
use Bouncer; | ||
use Kanvas\AccessControlList\Enums\RolesEnums; | ||
use Kanvas\AccessControlList\Models\Ability; | ||
use Kanvas\AccessControlList\Models\AbilitiesModules; | ||
use Kanvas\AccessControlList\Templates\ModulesRepositories; | ||
use Kanvas\Apps\Models\Apps; | ||
use Kanvas\SystemModules\Repositories\SystemModulesRepository; | ||
|
||
class CreateAbilitiesByModule | ||
{ | ||
public function __construct(protected ?Apps $app = null) | ||
{ | ||
$this->app = $app ?? app(Apps::class); | ||
} | ||
|
||
/** | ||
* Create abilities by module. | ||
* | ||
* @return void | ||
*/ | ||
public function execute() | ||
{ | ||
$scope = RolesEnums::getScope($this->app); | ||
Bouncer::scope()->to($scope); | ||
Bouncer::useAbilityModel(Ability::class); | ||
|
||
foreach (ModulesRepositories::getAbilitiesByModule() as $module => $subModule) { | ||
foreach ($subModule as $model => $abilities) { | ||
$systemModule = SystemModulesRepository::getByModelName($model); | ||
foreach ($abilities as $ability) { | ||
$ability = Bouncer::ability()->firstOrCreate([ | ||
'name' => $ability, | ||
'title' => ucfirst($ability), | ||
'entity_type' => $model, | ||
]); | ||
AbilitiesModules::firstOrCreate( | ||
[ | ||
'system_modules_id' => $systemModule->getId(), | ||
'abilities_id' => $ability->id, | ||
'scope' => $scope, | ||
'module_id' => $module, | ||
'apps_id' => $this->app->getId(), | ||
] | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kanvas\AccessControlList\Models; | ||
|
||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Kanvas\Models\BaseModel; | ||
use Kanvas\SystemModules\Models\SystemModules; | ||
use Silber\Bouncer\Database\Ability; | ||
|
||
class AbilitiesModules extends BaseModel | ||
{ | ||
protected $guarded = []; | ||
protected $table = 'abilities_modules'; | ||
|
||
public function systemModule(): BelongsTo | ||
{ | ||
return $this->belongsTo(SystemModules::class, 'system_modules_id'); | ||
} | ||
|
||
public function ability(): BelongsTo | ||
{ | ||
return $this->belongsTo(Ability::class, ['abilities_id', 'scope'], ['id', 'scope']); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kanvas\AccessControlList\Models; | ||
|
||
use Silber\Bouncer\Database\Ability as SilberAbility; | ||
|
||
class Ability extends SilberAbility | ||
{ | ||
protected $fillable = [ | ||
'name', | ||
'title', | ||
'entity_type', | ||
]; | ||
} |
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.