Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get all abilities of role model #280

Closed
FinnJanik opened this issue Apr 25, 2018 · 10 comments
Closed

Can't get all abilities of role model #280

FinnJanik opened this issue Apr 25, 2018 · 10 comments
Labels

Comments

@FinnJanik
Copy link

Hey again,

I got another interresting one... 😃
I created a custom role model in the App directory. I just extended the default Bouncer role model and added some functions and another package to log activity on the model (create, update, delete).
I implemented it like suggested here: #13

First I didn't register the Bouncer::useRoleModel() in the AppServiceProvider. Everything seemed to work fine, but the getAbilties() function returned an empty collection when used on my custom model.

//Empty collection returned
$role = Role::find(1);
$role->getAbilities();

//Returned full list of abilities
$role = Bouncer::role()::find(1);
$role->getAbilities();

Then I went on and registered the Bouncer::useRoleModel() in the AppServiceProvider. Now the getAbilities() function doesn't work on Bouncer anymore too.

//Empty collection returned
$role = Role::find(1);
$role->getAbilities();

//Now also an empty collection
$role = Bouncer::role()::find(1);
$role->getAbilities();

This time I have now clue how this happened. I actually tried a lot of things like implementing the Traits directly and so on, nothing worked..

Kind regards,
Finn Janik

@JosephSilber
Copy link
Owner

There isn't really enough information to go on here.

Can you show me your full Role model, as well as the full service provider that calls useRoleModel?

@FinnJanik
Copy link
Author

I have put the useRoleModel into the AppServiceProvider:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use Bouncer;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
        Bouncer::useRoleModel(\App\Role::class);
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

And my Role model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Silber\Bouncer\Database\Role as BouncerRole;
use \Dimsav\Translatable\Translatable;

class Role extends BouncerRole
{
    use Translatable;

    //Translations
    public $translatedAttributes = ['title'];

  
}

I also included Spaties activitylog here, but after extending the Bouncer Role model didn't work I put the log logic directly into the controller. I want to log the create, update and delete events of the model.

@JosephSilber
Copy link
Owner

Nothing I see here would suggest there's anything wrong. Are you sure your role actually has abilities assigned to it?

@FinnJanik
Copy link
Author

Yes I'm sure. As I said if I user the Bouncer model directly I get all the abilities. I tried it in the artisan console and in the controller. Both times with the same effect..
I just can't get the abilties of the role from my model.
How can we go on about that?

@JosephSilber
Copy link
Owner

To help you, I would need a clean, minimal reproduction of this problem.

  1. Create a brand new Laravel project on your computer.

  2. Install Bouncer.

  3. git init . in that project, and save all of it as the initial commit.

  4. Add your custom model and its registration, and commit it as the second commit.

  5. Create a test route that shows how you're trying to use Bouncer and getting wrong results, and save it as the 3rd commit.

  6. Push your project up to GitHub, and share it here.

In all, there should be exactly 3 commits, so that it's easy to read through it and see whats what. Enjoy!

@jalvjim
Copy link

jalvjim commented May 7, 2018

Hi!
I am dealing with the same problem, but i think this happens because the relations from DB model are statically linked to Bouncer's Role Model and your instance belongs to another different class. That's why you are not getting the same results

@tobias-kuendig
Copy link

I had a similar problem.

The entity_type in the permissions table was saved as roles. If I run a query a entity_type of App\Role was used.

In my case the problem was that I did not call useRoleModel. I know this is not the solution to your problem but it might help someone else.

// AppServiceProvider.php -> boot()
\Bouncer::useRoleModel(App\Role::class);

@JosephSilber
Copy link
Owner

I'm still not clear what the issue is here.

Anyway, it may be related to this. If so, try registering your custom model with the morph map:

use Bouncer;
use App\Role;
use Illuminate\Database\Eloquent\Relations\Relation;

public function boot()
{
    Bouncer::useRoleModel(Role::class);

    Relation::morphMap([Role::class]);
}

@JosephSilber
Copy link
Owner

Is this still an issue after registering your custom models with the morph map?

@shyammtp
Copy link

This works for me,
Relation::morphMap(['roles' => Role::class]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants