-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cdae22a
Showing
15 changed files
with
532 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 4 | ||
trim_trailing_whitespace = true |
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,8 @@ | ||
.idea | ||
vendor | ||
composer.lock | ||
node_modules | ||
.phpunit.result.cache | ||
.php-cs-fixer.cache | ||
app | ||
reports |
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,9 @@ | ||
MIT License | ||
|
||
Copyright (c) Danil Shutsky | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,60 @@ | ||
## MoonShine Permission | ||
|
||
### Requirements | ||
|
||
- MoonShine v2.0+ | ||
|
||
### Installation | ||
|
||
```shell | ||
composer require moonshine/permissions | ||
``` | ||
|
||
```shell | ||
php artisan migrate | ||
``` | ||
|
||
### Get started | ||
|
||
1. Change MoonShineUser model in app/moonshine.php | ||
|
||
```php | ||
use MoonShine\Permissions\Models\MoonshineUser; | ||
|
||
return [ | ||
// ... | ||
'auth' => [ | ||
// ... | ||
'providers' => [ | ||
'moonshine' => [ | ||
'driver' => 'eloquent', | ||
'model' => MoonshineUser::class, | ||
], | ||
], | ||
], | ||
// ... | ||
]; | ||
``` | ||
|
||
Or add trait HasChangeLog to user model | ||
|
||
```php | ||
use MoonShine\Permissions\Traits\HasMoonShinePermissions; | ||
|
||
class MoonShineUser extends Model | ||
{ | ||
use HasMoonShinePermissions; | ||
} | ||
``` | ||
|
||
2. Add trait to resource | ||
|
||
```php | ||
use MoonShine\Permissions\Traits\WithPermissions; | ||
|
||
class PostResource extends ModelResource | ||
{ | ||
use WithPermissions; | ||
} | ||
``` | ||
|
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,56 @@ | ||
{ | ||
"name": "moonshine/permissions", | ||
"description": "User permissions for MoonShine", | ||
"keywords": ["permissions", "moonshine"], | ||
"type": "library", | ||
"homepage": "https://moonshine-laravel.com", | ||
"license": "MIT", | ||
"support": { | ||
"issues": "https://github.com/moonshine-software/permissions/issues", | ||
"source": "https://github.com/moonshine-software/permissions" | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Danil Shutsky", | ||
"email": "thecutcode@gmail.com", | ||
"homepage": "https://moonshine-laravel.com" | ||
} | ||
], | ||
"require": { | ||
"php": "^8.1|^8.2", | ||
"ext-curl": "*", | ||
"ext-json": "*" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.5.8", | ||
"mockery/mockery": "^1.4.4", | ||
"phpstan/phpstan": "^1.4.7", | ||
"orchestra/testbench": "^4.0|^5.0|^6.0|^7.0|^8.0", | ||
"brianium/paratest": "^6.8" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"MoonShine\\Permissions\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"MoonShine\\Permissions\\Tests\\": "tests/", | ||
"MoonShine\\Permissions\\Database\\Factories\\": "database/factories/" | ||
} | ||
}, | ||
"conflict": { | ||
"moonshine/moonshine": "<2.0" | ||
}, | ||
"scripts": { | ||
"test": "vendor/bin/phpunit", | ||
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes" | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"MoonShine\\Permissions\\PermissionsServiceProvider" | ||
] | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
database/migrations/2023_01_20_173629_create_moonshine_user_permissions_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,25 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration { | ||
public function up(): void | ||
{ | ||
Schema::create('moonshine_user_permissions', static function (Blueprint $table): void { | ||
$table->id(); | ||
|
||
$table->foreignId('moonshine_user_id'); | ||
|
||
$table->json('permissions'); | ||
|
||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::dropIfExists('moonshine_user_permissions'); | ||
} | ||
}; |
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,9 @@ | ||
@if($item->exists) | ||
<x-moonshine::divider /> | ||
|
||
<x-moonshine::title class="mb-6"> | ||
{{ $label }} | ||
</x-moonshine::title> | ||
|
||
{{ $form->render() }} | ||
@endif |
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,126 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MoonShine\Permissions\Components; | ||
|
||
use Closure; | ||
use Illuminate\Database\Eloquent\Model; | ||
use MoonShine\Components\FormBuilder; | ||
use MoonShine\Components\MoonshineComponent; | ||
use MoonShine\Decorations\Column; | ||
use MoonShine\Decorations\Divider; | ||
use MoonShine\Decorations\Grid; | ||
use MoonShine\Fields\Switcher; | ||
use MoonShine\Resources\ModelResource; | ||
use MoonShine\Traits\HasResource; | ||
use MoonShine\Traits\WithLabel; | ||
|
||
/** | ||
* @method static static make(Closure|string $label, ModelResource $resource) | ||
*/ | ||
final class Permissions extends MoonshineComponent | ||
{ | ||
use HasResource; | ||
use WithLabel; | ||
|
||
protected string $view = 'moonshine-permissions::components.permissions'; | ||
|
||
protected Model $item; | ||
|
||
protected $except = [ | ||
'getItem', | ||
'getResource', | ||
'getForm', | ||
]; | ||
|
||
public function __construct( | ||
Closure|string $label, | ||
ModelResource $resource | ||
) { | ||
$this->setResource($resource); | ||
$this->setLabel($label); | ||
} | ||
|
||
public function getItem(): Model | ||
{ | ||
return $this->getResource()->getItemOrInstance(); | ||
} | ||
|
||
public function getForm(): FormBuilder | ||
{ | ||
$url = $this->getResource() | ||
->route('permissions', $this->getItem()->getKey()); | ||
|
||
$elements = []; | ||
$values = []; | ||
$all = true; | ||
|
||
foreach (moonshine()->getResources() as $resource) { | ||
$checkboxes = []; | ||
$class = 'ps_' . class_basename($resource::class); | ||
$allSections = true; | ||
|
||
foreach ($resource->gateAbilities() as $ability) { | ||
$values['permissions'][$resource::class][$ability] = $this->getItem()->isHavePermission( | ||
$resource::class, | ||
$ability | ||
); | ||
|
||
if (! $values['permissions'][$resource::class][$ability]) { | ||
$allSections = false; | ||
$all = false; | ||
} | ||
|
||
$checkboxes[] = Switcher::make( | ||
$ability, | ||
"permissions." . $resource::class . ".$ability" | ||
) | ||
->customAttributes(['class' => 'permission_switcher ' . $class]) | ||
->setName("permissions[" . $resource::class . "][$ability]"); | ||
} | ||
|
||
$elements[] = Column::make([ | ||
Switcher::make($resource->title())->customAttributes([ | ||
'class' => 'permission_switcher_section', | ||
'@change' => "document | ||
.querySelectorAll('.$class') | ||
.forEach((el) => {el.checked = parseInt(event.target.value); el.dispatchEvent(new Event('change'))})", | ||
])->setValue($allSections)->hint('Toggle off/on all'), | ||
|
||
...$checkboxes, | ||
Divider::make(), | ||
])->columnSpan(6); | ||
} | ||
|
||
return FormBuilder::make($url) | ||
->fields([ | ||
Switcher::make('All')->customAttributes([ | ||
'@change' => <<<'JS' | ||
document | ||
.querySelectorAll('.permission_switcher, .permission_switcher_section') | ||
.forEach((el) => {el.checked = parseInt(event.target.value); el.dispatchEvent(new Event('change'))}) | ||
JS | ||
, | ||
])->setValue($all), | ||
Divider::make(), | ||
Grid::make( | ||
$elements | ||
), | ||
]) | ||
->fill($values) | ||
->submit(__('moonshine::ui.save')); | ||
} | ||
|
||
protected function viewData(): array | ||
{ | ||
return [ | ||
'label' => $this->label(), | ||
'form' => $this->getItem()?->exists | ||
? $this->getForm() | ||
: '', | ||
'item' => $this->getItem(), | ||
'resource' => $this->getResource(), | ||
]; | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MoonShine\Permissions\Http\Controllers; | ||
|
||
use Illuminate\Http\RedirectResponse; | ||
use MoonShine\Http\Controllers\MoonShineController; | ||
use MoonShine\Permissions\Http\Requests\PermissionFormRequest; | ||
use MoonShine\Permissions\Models\MoonshineUserPermission; | ||
|
||
class PermissionController extends MoonShineController | ||
{ | ||
public function __invoke(PermissionFormRequest $request): RedirectResponse | ||
{ | ||
$item = $request | ||
->getResource() | ||
->getItem(); | ||
|
||
if (! $request->has('permissions')) { | ||
$item->moonshineUserPermission()->delete(); | ||
} else { | ||
MoonshineUserPermission::query()->updateOrCreate( | ||
['moonshine_user_id' => $item->getKey()], | ||
request() | ||
->merge(['moonshine_user_id' => $item->getKey()]) | ||
->only(['moonshine_user_id', 'permissions']) | ||
); | ||
} | ||
|
||
$this->toast( | ||
__('moonshine::ui.saved'), | ||
'success' | ||
); | ||
|
||
return back(); | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MoonShine\Permissions\Http\Requests; | ||
|
||
use MoonShine\Http\Requests\MoonshineFormRequest; | ||
|
||
final class PermissionFormRequest extends MoonshineFormRequest | ||
{ | ||
public function authorize(): bool | ||
{ | ||
if (! in_array( | ||
'update', | ||
$this->getResource()->getActiveActions(), | ||
true | ||
)) { | ||
return false; | ||
} | ||
|
||
return $this->getResource()->can('update'); | ||
} | ||
|
||
/** | ||
* @return array{permissions: string[]} | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'permissions' => ['array'], | ||
]; | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MoonShine\Permissions\Models; | ||
|
||
use MoonShine\Models\MoonshineUser as User; | ||
use MoonShine\Permissions\Traits\HasMoonShinePermissions; | ||
|
||
class MoonshineUser extends User | ||
{ | ||
use HasMoonShinePermissions; | ||
} |
Oops, something went wrong.