Skip to content

Instantly share code, notes, and snippets.

@fat4lix
Created October 22, 2019 11:54
Show Gist options
  • Select an option

  • Save fat4lix/b6e7b5add4a6e5757f9a35ac225cbd2d to your computer and use it in GitHub Desktop.

Select an option

Save fat4lix/b6e7b5add4a6e5757f9a35ac225cbd2d to your computer and use it in GitHub Desktop.
<?php
abstract class Action
{
/**
* Slug name for action
* @return string
*/
abstract public static function name();
/** Title for action
* @return string
*/
abstract public static function title();
/** Icon class witch used for this action
* @return string
*/
abstract public static function iconClass();
abstract public static function iconColor();
/**
* Vue component name witch used for this action
* @return string
*/
abstract public static function component();
abstract public static function run($model, $payload = null);
abstract public static function runBatch($model, $payload = null);
/**
* If ability not setup in target action class, action will authorize by default
* @return string
*/
public static function authorizeAbility(): string {
return '';
}
/**
* Get meta options for this action^ to using in frontend
* @return array
*/
public static function meta($model): array {
return [];
}
/**
* @param Model $model
* @return bool
*/
public static function authorize($model): bool {
if($ability = static::authorizeAbility()) {
return Gate::allows($ability, $model);
} else {
return true;
}
}
}
<?php
class ActionController extends Controller
{
use ResolveTableResource;
public function getActions(ActionsRequests $request, $domain, $resource) {
$validatedParams = $request->validated();
$model = $this->resolveModel($validatedParams);
$foundResource = $this->findResource($resource);
if(is_null($model || !$foundResource)) {
return response()->json([], 404);
}
$resourceActions = $foundResource::actions();
$actionsArray = collect();
foreach ($resourceActions as $action) {
if (is_countable($model)) {
$checked = true;
foreach ($model as $modelItem) {
if(! $action::authorize($modelItem)) {
$checked = false;
}
}
if(!$checked) continue;
unset($checked);
} else {
if(! $action::authorize($model)) {
continue;
}
}
$actionInfo = [
'name' => $action::name(),
'title' => $action::title(),
'iconClass' => $action::iconClass(),
'iconColor' => $action::iconColor(),
'component' => $action::component(),
'class' => $action,
'meta' => $action::meta($model)
];
$actionsArray->push($actionInfo);
unset($actionInfo);
}
return response()->json($actionsArray->toArray());
}
public function runAction(FireActionsRequests $request, $domain, $resource) {
$validatedParams = $request->validated();
$model = $this->resolveModel($validatedParams);
$foundResource = $this->findResource($resource);
if(is_null($model || !$foundResource)) {
return response()->json([], 404);
}
$resourceActions = $foundResource::actions();
$foundedAction = collect($resourceActions)->first(function ($action) use ($validatedParams) {
return $action == $validatedParams['actionClass'];
});
if(!$foundedAction) {
return response()->json([], 404);
}
if(is_countable($model)) {
$checked = true;
foreach ($model as $modelItem) {
if(! $foundedAction::authorize($modelItem)) {
$checked = false;
}
}
if (!$checked) {
return response()->json([], 403);
} else {
return $foundedAction::runBatch($model, $validatedParams['payload']);
}
} else {
if(! $foundedAction::authorize($model)) {
return response()->json([], 403);
} else {
return $foundedAction::run($model, $validatedParams['payload']);
}
}
}
private function resolveModel($inputParams) {
$resolved = null;
if(isset($inputParams['ids']) && count($inputParams['ids']) > 1) {
$class = $inputParams['class'];
/**
* @var Model $class
*/
$resolved = $class::query()->whereIn('id', $inputParams['ids'])->get();
if(!$resolved->isNotEmpty()) {
$resolved = null;
}
} else {
$resolved = $inputParams['class']::findOrFail($inputParams['id']);
}
return $resolved;
}
}
<?php
namespace Nightkit\Modules\Main\Components\Tables\Actions;
use Illuminate\Support\Facades\Auth;
use Nightkit\Modules\Engine\Components\Providers\TeamProvider;
class DeletePlayerAction extends Action
{
public static function authorizeAbility(): string
{
return 'canRemovePlayersFromTeam';
}
public static function name()
{
return 'delete_player';
}
public static function title()
{
return 'Исключить';
}
public static function iconClass()
{
return 'trash-alt';
}
public static function iconColor()
{
return 'red';
}
public static function component()
{
return 'button-action';
}
public static function run($model, $payload = null)
{
$team = resolve(TeamProvider::class)->loadFor(Auth::user())->getTeam();
$team->players()->detach($model->id);
return response()->json([
'notify' => [
'title' => 'Пользователь исключен из команды!',
'message' => 'Пользователь ' . $model->name . ' успешно исключен из команды!'
]
], 200);
}
public static function runBatch($model, $payload = null)
{
$team = resolve(TeamProvider::class)->loadFor(Auth::user())->getTeam();
$team->players()->detach($model->pluck('id')->toArray());
return response()->json([
'notify' => [
'title' => 'Пользователи исключены из команды!',
'message' => 'Выбранные пользователи успешно исключены из команды!'
]
], 200);
}
}
<?php
namespace Nightkit\Modules\Main\Policies;
use Nightkit\Modules\Engine\Components\Providers\TeamProvider;
use Nightkit\Modules\Engine\Models\Player;
use Nightkit\Modules\Main\Models\User;
class PlayersPolicy
{
public function canRemovePlayersFromTeam(User $user, Player $player) {
if($user->cannot('delete_users_from_team')) {
return false;
}
$teamProvider = resolve(TeamProvider::class)->loadFor($user)->loadPlayers();
$players = $teamProvider->getPlayers();
if(is_null($foundedPlayer = $players->find($player))) {
return false;
}
if($foundedPlayer->pivot->role->name == 'captain') {
return false;
}
return true;
}
public function canBlockPlayer(User $user, Player $player) {
if($user->cannot('block_users_in_team')) {
return false;
}
$teamProvider = resolve(TeamProvider::class)->loadFor($user)->loadPlayers();
$players = $teamProvider->getPlayers();
if(is_null($foundedPlayer = $players->find($player))) {
return false;
}
if($foundedPlayer->pivot->role->name == 'blocked'
|| $foundedPlayer->pivot->role->name == 'captain') {
return false;
}
return true;
}
public function canUnblockPlayer(User $user, Player $player) {
if($user->cannot('block_users_in_team')) {
return false;
}
$teamProvider = resolve(TeamProvider::class)->loadFor($user)->loadPlayers();
$players = $teamProvider->getPlayers();
if(is_null($foundedPlayer = $players->find($player))) {
return false;
}
if($foundedPlayer->pivot->role->name != 'blocked'
|| $foundedPlayer->pivot->role->name == 'captain') {
return false;
}
return true;
}
public function canChangeRole(User $user, Player $player) {
if($user->cannot('edit_team_roles')) {
return false;
}
$teamProvider = resolve(TeamProvider::class)->loadFor($user)->loadPlayers();
$players = $teamProvider->getPlayers();
if(is_null($foundedPlayer = $players->find($player))) {
return false;
}
if($foundedPlayer->pivot->role->name == 'captain') {
return false;
}
return true;
}
}
@fat4lix
Copy link
Copy Markdown
Author

fat4lix commented Oct 22, 2019

ActionController.php
Action.php -> DeletePlayerAction.php
PlayerPolicy.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment