Skip to content

Debugging Authority rules

Tortue Torche edited this page Mar 17, 2015 · 3 revisions

What do you do when permissions you defined in the Authority class don't seem to be working properly? First try to duplicate this problem in the php artisan tinker or better yet, see Testing Authority rules.

Debugging Member Actions

// In Laravel tinker(console) or test
$user = User::orderBy('id')->first(); // fetch any user you want to test authority rules on
Auth::login($user);
$project = Project::orderBy('id')->first(); // any model you want to test against
$authority = App::make('authority');
$authority->can('create', $project); // see if it returns the expected behavior for that action

Note: this assumes that the model instance is being loaded properly. If you are only using authorizeResource it will not have an instance to work with so it will use the class.

Authority::can('create', 'Project');

Debugging index Action

// In Laravel tinker(console) or test
$user = User::orderBy('id')->first(); // fetch any user you want to test authority rules on
Auth::login($user);
$authority = App::make('authority');
$authority->can('index', 'Project'); // see if user can access the class
Project::all(); // see if returns the records the user can access
Project::query()->toSql(); // see what the generated SQL looks like to help determine why it's not fetching the records you want

Logging AccessDenied Exception

If you think the Efficiently\AuthorityController\Exceptions\AccessDenied exception is being raised and you are not sure why, you can log this behavior to help debug what is triggering it.

For Laravel 5.0

// in app/Exceptions/Handler.php

  /**
   * Render an exception into an HTTP response.
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  \Exception  $e
   * @return \Illuminate\Http\Response
   */
	public function render($request, Exception $exception)
    {
        if ($exception instanceof \Efficiently\AuthorityController\Exceptions\AccessDenied) {
            $msg = $exception->getMessage();
            \Log::info("Access denied on ".$exception->action." ".print_r($exception->subject), true);
            //...
        }
        //...
    }
    //code...

For Laravel 4.*

// in app/start/global.php
App::error(function(Efficiently\AuthorityController\Exceptions\AccessDenied $exception, $code, $fromConsole)
{
    Log::info("Access denied on ".$exception->action." ".print_r($exception->subject), true);
    // ...
});

Issue Tracker

If you are still unable to resolve the issue, please post on the issue tracker

Additional Docs