jenssegers / laravel-mongodb-sentry

An extension for Laravel-MongoDB that lets you work with Sentry
54 stars 13 forks source link

$user->inGroup($group) #2

Closed bencolson closed 10 years ago

bencolson commented 10 years ago

$user->inGroup($group) isn't returning true when it should.

In Mongo, db.users.find() shows an array of group_ids that match up to db.groups.find(), but the pivot table users_groups is empty, and calling $user->addGroup($group) only seems to populate group_ids in users, not the pivot.

Also, you can call $user->addGroup($group) as many times as you like, and db.users.group_ids can be filled with as many duplicate group ids as you'd like.

public function run()
{

    $adminGroup = Sentry::findGroupByName('Admins');

    $user = Sentry::findUserByLogin('email@email.com');

    if ( !$user->inGroup($adminGroup))
    {
        $user->addGroup($adminGroup);
    }

}

print_r($user->getGroups()); shows the following:

Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( ) )

bencolson commented 10 years ago

On closer inspection, they IDs between group_ids and db.groups are subtly different:

print_r( $adminGroup->toArray());

Array
(
    [_id] => 536b08f5e85b8966160041a8
    [name] => Admins
    [permissions] => Array
        (
            [admin] => 1
            [agents] => 1
        )

    [updated_at] => 2014-05-08 14:32:53
    [created_at] => 2014-05-08 14:32:53
)

and

print_r( $user->toArray()['group_ids']);

Array
(
    [0] => 536afd8ee85b894a140041a7
    [1] => 536afd8ee85b894a140041a8
    [2] => 536afd8fe85b894b140041a7
    [3] => 536afd8fe85b894b140041a8
    [4] => 536afe23e85b8967140041a7
    [5] => 536afe23e85b8967140041a8
    [6] => 536b068ae85b89a8150041a7
    [7] => 536b068ae85b89a8150041a8
    [8] => 536b0714e85b89ec150041a7
    [9] => 536b0714e85b89ec150041a8
    [10] => 536b074ce85b89fb150041a7
    [11] => 536b074ce85b89fb150041a8
    [12] => 536b0767e85b8908160041a7
    [13] => 536b0767e85b8908160041a8
    [14] => 536b0774e85b8917160041a7
    [15] => 536b0774e85b8917160041a8
    [16] => 536b07e2e85b8926160041a7
    [17] => 536b07e2e85b8926160041a8
    [18] => 536b081be85b8927160041a7
    [19] => 536b081be85b8927160041a8
    [20] => 536b0839e85b8937160041a7
    [21] => 536b0839e85b8937160041a8
    [22] => 536b0870e85b8957160041a7
    [23] => 536b0870e85b8957160041a8
)
bencolson commented 10 years ago

Scratch all that. Issue resolved.

If you're seeding your DB with Sentry credentials, don't drop the groups collection and then regenerate groups because you'll end up chasing your tail for 2 hours. Just add try/catch to your seeds so as not to re-seed for existing groups and users.