Skip to content

Commit

Permalink
Merge pull request #109 from bezhanSalleh/fix/auth-provider-policy-ge…
Browse files Browse the repository at this point in the history
…neration

Fix/auth provider policy generation
  • Loading branch information
bezhanSalleh committed Aug 28, 2022
2 parents 7476fb5 + 5c590f7 commit f32f021
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Commands/MakeShieldGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ protected function generateForResources(array $resources): Collection
->values()
->each(function ($entity) {
if ($this->generatorOption === 'policies_and_permissions') {
$this->copyStubToApp('DefaultPolicy', $this->generatePolicyPath($entity), $this->generatePolicyStubVariables($entity));
$this->copyStubToApp(static::getPolicyStub($entity['model']), $this->generatePolicyPath($entity), $this->generatePolicyStubVariables($entity));
FilamentShield::generateForResource($entity['resource']);
}

if ($this->generatorOption === 'policies') {
$this->copyStubToApp('DefaultPolicy', $this->generatePolicyPath($entity), $this->generatePolicyStubVariables($entity));
$this->copyStubToApp(static::getPolicyStub($entity['model']), $this->generatePolicyPath($entity), $this->generatePolicyStubVariables($entity));
}

if ($this->generatorOption === 'permissions') {
Expand Down Expand Up @@ -265,4 +265,13 @@ protected function widgetInfo(array $widgets): void
})
);
}

protected static function getPolicyStub(string $model): string
{
if (Str::is(Str::of(Utils::getAuthProviderFQCN())->afterLast('\\'), $model)) {
return (string) 'UserPolicy';
}

return (string) 'DefaultPolicy';
}
}
144 changes: 144 additions & 0 deletions stubs/UserPolicy.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

namespace {{ namespace }};

use {{ auth_model_fqcn }};

use Illuminate\Auth\Access\HandlesAuthorization;

class {{ modelPolicy }}
{
use HandlesAuthorization;

/**
* Determine whether the {{ auth_model_variable }} can view any models.
*
* @param \{{ auth_model_fqcn }} ${{ auth_model_variable }}
* @return \Illuminate\Auth\Access\Response|bool
*/
public function viewAny({{ auth_model_name }} ${{ auth_model_variable }})
{
return ${{ auth_model_variable }}->can('{{ ViewAny }}');
}

/**
* Determine whether the {{ auth_model_variable }} can view the model.
*
* @param \{{ auth_model_fqcn }} ${{ auth_model_variable }}
* @return \Illuminate\Auth\Access\Response|bool
*/
public function view({{ auth_model_name }} ${{ auth_model_variable }})
{
return ${{ auth_model_variable }}->can('{{ View }}');
}

/**
* Determine whether the {{ auth_model_variable }} can create models.
*
* @param \{{ auth_model_fqcn }} ${{ auth_model_variable }}
* @return \Illuminate\Auth\Access\Response|bool
*/
public function create({{ auth_model_name }} ${{ auth_model_variable }})
{
return ${{ auth_model_variable }}->can('{{ Create }}');
}

/**
* Determine whether the {{ auth_model_variable }} can update the model.
*
* @param \{{ auth_model_fqcn }} ${{ auth_model_variable }}
* @return \Illuminate\Auth\Access\Response|bool
*/
public function update({{ auth_model_name }} ${{ auth_model_variable }})
{
return ${{ auth_model_variable }}->can('{{ Update }}');
}

/**
* Determine whether the {{ auth_model_variable }} can delete the model.
*
* @param \{{ auth_model_fqcn }} ${{ auth_model_variable }}
* @return \Illuminate\Auth\Access\Response|bool
*/
public function delete({{ auth_model_name }} ${{ auth_model_variable }})
{
return ${{ auth_model_variable }}->can('{{ Delete }}');
}

/**
* Determine whether the {{ auth_model_variable }} can bulk delete.
*
* @param \{{ auth_model_fqcn }} ${{ auth_model_variable }}
* @return \Illuminate\Auth\Access\Response|bool
*/
public function deleteAny({{ auth_model_name }} ${{ auth_model_variable }})
{
return ${{ auth_model_variable }}->can('{{ DeleteAny }}');
}

/**
* Determine whether the {{ auth_model_variable }} can permanently delete.
*
* @param \{{ auth_model_fqcn }} ${{ auth_model_variable }}
* @return \Illuminate\Auth\Access\Response|bool
*/
public function forceDelete({{ auth_model_name }} ${{ auth_model_variable }})
{
return ${{ auth_model_variable }}->can('{{ ForceDelete }}');
}

/**
* Determine whether the {{ auth_model_variable }} can permanently bulk delete.
*
* @param \{{ auth_model_fqcn }} ${{ auth_model_variable }}
* @return \Illuminate\Auth\Access\Response|bool
*/
public function forceDeleteAny({{ auth_model_name }} ${{ auth_model_variable }})
{
return ${{ auth_model_variable }}->can('{{ ForceDeleteAny }}');
}

/**
* Determine whether the {{ auth_model_variable }} can restore.
*
* @param \{{ auth_model_fqcn }} ${{ auth_model_variable }}
* @return \Illuminate\Auth\Access\Response|bool
*/
public function restore({{ auth_model_name }} ${{ auth_model_variable }})
{
return ${{ auth_model_variable }}->can('{{ Restore }}');
}

/**
* Determine whether the {{ auth_model_variable }} can bulk restore.
*
* @param \{{ auth_model_fqcn }} ${{ auth_model_variable }}
* @return \Illuminate\Auth\Access\Response|bool
*/
public function restoreAny({{ auth_model_name }} ${{ auth_model_variable }})
{
return ${{ auth_model_variable }}->can('{{ RestoreAny }}');
}

/**
* Determine whether the {{ auth_model_variable }} can bulk restore.
*
* @param \{{ auth_model_fqcn }} ${{ auth_model_variable }}
* @return \Illuminate\Auth\Access\Response|bool
*/
public function replicate({{ auth_model_name }} ${{ auth_model_variable }})
{
return ${{ auth_model_variable }}->can('{{ Replicate }}');
}

/**
* Determine whether the {{ auth_model_variable }} can reorder.
*
* @param \{{ auth_model_fqcn }} ${{ auth_model_variable }}
* @return \Illuminate\Auth\Access\Response|bool
*/
public function reorder({{ auth_model_name }} ${{ auth_model_variable }})
{
return ${{ auth_model_variable }}->can('{{ Reorder }}');
}
}

0 comments on commit f32f021

Please sign in to comment.