efficiently / authority-controller

Authorization PHP package for Laravel 4, 5.0, 5.1, 5.2 and 5.3

Home Page:http://laravel.io/forum/02-03-2014-authority-controller-authorization-library-cancan-port

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

loadAndAuthorizeResource not correctly loading for edit/show etc

sintemaa opened this issue · comments

According to the documentation if you use loadAndAuthorizeResource in the __construct() method of your controller it should automatically populate the model you need in your method. However while this works ok for the methods that require all items (e.g. index) it doesn't work for my edit & show methods (and probably for all methods of the same type).

My route looks like this:

Route::get('users/{id}/edit', ['as'=>'users.edit', 'uses' =>'UsersController@edit']);

My edit method in UsersController looks like

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        return View::make('users.edit')->with('user', $this->user);
    }

I was expecting $this->user to contain the user with the id set in the route, but I get an error that $this->user is not defined.

I did some digging and found that setting $this->user depends on whether ControllerResource->isMemberAction() returns true. This brings me to ControllerResource->getIdParam(), which gets ControllerResource->getIdKey() and checks if this exists in $this->params.

ControllerResource->getIdKey() returns "id", which is what I was expecting, but for some reason $this->params does not contain a key called "id" with the value from the route, but instead a key called "id_id". I've no idea why this happens.

I did some further digging and ended up in Parameters->fillController() which seems to fill the params array. All seems ok until line 38, but then from line 39 to 49 there is some code that seems to deliberately replace my id parameter with id_id, with some comments about difference between laravel & rails. I don't know rails so this comment is unclear to me.

Is this a bug or am I doing something wrong? I can work around this by adding

$this->user = User::find($id); 

to the start of my edit method but it seems inefficient as I am using this package which should do this automagically.

As a temporary fix, can you try to use this route instead, please?

Route::get('users/{user}/edit', ['as'=>'users.edit', 'uses' =>'UsersController@edit']);

The Parameters class follow the conventions of Laravel controller/RESTful routes.

So you need to replace the {id} route's parameter by {user}.
But I'm agree with you, it's not really intuitive, specially if you add custom routes to your Laravel application. This issue need to be fixed.

By the way, your issue is very well documented, I think you spend some times to write it. Thank you, I appreciate that.
I'm going to push a fix today. Are you available to test it ?

@sintemaa I think this issue is fixed now, can you update this package in your application ?
With the master version, add this in your composer.json file:

{
  // ...
  "require": {
     //...
     "efficiently/authority-controller": "dev-master",
  //...
}

Then update it, like this:
composer update efficiently/authority-controller

If you have any problems, feel free to reopen this issue.

Otherwise, if it's OK, I'll tag a new version.

Have a good day,
Tortue Torche

Happy to provide the extensive issue documentation. I had done quite some digging in your code to figure out what I did wrong so I thought best to explain what I'd sone to help you understand the root cause, saving you time in fixing it.

The fix seems to work fine for me.
Thanks