JosephSilber / bouncer

Laravel Eloquent roles and abilities.
MIT License
3.45k stars 333 forks source link

laravel spark integration #296

Closed scottybo closed 6 years ago

scottybo commented 6 years ago

Does this package work in conjunction with Laravel Spark? If so, are there any 'gotchas' or special steps required?

JosephSilber commented 6 years ago

I don't have any experience with Spark, so you'll have to figure that out yourself.

scottybo commented 6 years ago

Fair enough!

scottybo commented 6 years ago

So far so good on Spark, however.. When trying to use $user->is('admin') I get the error:

Type error: Argument 1 passed to Illuminate\Database\Eloquent\Model::is() m  
  ust be an instance of Illuminate\Database\Eloquent\Model, string given, cal  
  led in /var/www/html/holly-spark/app/Console/Commands/SetUsersAssignedRole.  
  php on line 81 

Everything works fine if I use Bouncer::is($user)->a('admin')

My User Model looks like this:

namespace App;

use Laravel\Spark\CanJoinTeams;
use Laravel\Spark\User as SparkUser;

use Silber\Bouncer\Database\HasRolesAndAbilities;

use Config;

class User extends SparkUser
{
    use CanJoinTeams, HasRolesAndAbilities;

It would be nice to use the shorter $user->is() command - any ideas?

JosephSilber commented 6 years ago

The docs show how to do this:

$user->isAn('admin');

BTW, I would strongly advise against checking the user's role directly. As outlined in the docs:

Note: Generally speaking, you should not have a need to check roles directly. It is better to allow a role certain abilities, then check for those abilities instead. If what you need is very general, you can create very broad abilities. For example, an access-dashboard ability is always better than checking for admin or editor roles directly. For the rare occasion that you do want to check a role, that functionality is available here.

scottybo commented 6 years ago

@JosephSilber thanks - this is a special case (we're checking for a superadmin - i.e. me!)

JosephSilber commented 6 years ago

@scottybo is that a question?