Skip to content

Problem with loading resource with changed 'idParam' #35

Description

@bartekmaciejewski

Hi,

I have a problem with loading resource after changing 'idParam'. Code samples:

  1. In ContractController:
    `
    public function __construct()
    {
    parent::__construct();

            $this->loadAndAuthorizeResource(['class' => 'App\Models\Contract', 'idParam' => 'id_contract']);
    

    }
    `

  2. I have only two methods in this controller (index and show) but no need to add to them code for authorization, 'show' method looks that:

`public function show(Contract $contract)
{
Log::info(METHOD);

    $this->data['contract'] = $contract;

	return view('customer.contract.show', $this->data);
}`
  1. My authority-controller:

`$serializer = new SuperClosure\Serializer;

return [

'initialize' => $serializer->serialize(function ($authority) {
    
    $user = $authority->getCurrentUser();
    
    foreach ($user->permissions as $perm) {
        if ($perm->type == 'allow') {
            $authority->allow($perm->action, $perm->resource, function ($self, $contract) {
                #just for test purpose
                dd('am i here?');
                if (isset($contract->account)) {
                    return $self->getCurrentUser()->id == $contract->account->id;
                } else {
                    return true;
                }
            });
        } else {
            $authority->deny($perm->action, $perm->resource);
        }
    }
})

];`

As a result, in 'show' method, proper object is not loaded (no object is loaded to be clear). After changing one method in authority-controller/src/Efficiently/AuthorityController/ControllerResource.php to

protected function getIdParam() { return array_key_exists($this->getIdKey(), $this->params) ? print_r($this->params['id'], true) : ""; }

the object is loaded but it's my poor hack, not solving problem... Of course allowing/denying works great, but I wanted to use closure and check loaded object.

I hope I followed your instructions and it's not just my mistake but deeper problem.

Greetings
Bartek

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions