Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Auth/Eloquent/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function data($data = null)
{
if (func_num_args() === 0) {
$data = array_merge($this->model()->attributesToArray(), [
'roles' => $this->roles()->map->handle()->values()->all(),
'roles' => $this->explicitRoles()->map->handle()->values()->all(),
'groups' => $this->groups()->map->handle()->values()->all(),
]);

Expand Down Expand Up @@ -112,7 +112,7 @@ public function explicitRoles($roles = null)

protected function saveRoles()
{
$roles = $this->roles()->map->id();
$roles = $this->explicitRoles()->map->id();

(new Roles($this))->sync($roles);
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Auth/Eloquent/EloquentUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,24 @@ public function handle(?string $handle = null)
$this->assertSame([$user->email(), $userTwo->email(), $userThree->email(), $userFour->email()], Facades\User::query()->whereGroupIn(['a', 'b'])->orWhereGroupIn(['c'])->get()->map->email()->all());
}

#[Test]
public function it_doesnt_save_roles_inherited_from_groups_to_the_role_user_table()
{
$directRole = Facades\Role::make('direct');
$groupRole = Facades\Role::make('grouped');
$group = (new UserGroup)->handle('usergroup')->assignRole($groupRole);

Facades\Role::shouldReceive('find')->with('direct')->andReturn($directRole);
Facades\Role::shouldReceive('find')->with('grouped')->andReturn($groupRole);
Facades\UserGroup::shouldReceive('find')->with('usergroup')->andReturn($group);

$user = $this->createPermissible()->assignRole($directRole)->addToGroup($group);
$user->save();

$this->assertSame(['direct'], \DB::table(config('statamic.users.tables.role_user', 'role_user'))->where('user_id', $user->id())->pluck('role_id')->all());
$this->assertSame(['usergroup'], \DB::table(config('statamic.users.tables.group_user', 'group_user'))->where('user_id', $user->id())->pluck('group_id')->all());
}

public function makeUser()
{
return (new EloquentUser)
Expand Down
21 changes: 21 additions & 0 deletions tests/Auth/PermissibleContractTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,27 @@ public function handle(?string $handle = null)
$this->assertFalse($user->hasRole('b'));
}

#[Test]
public function it_doesnt_treat_roles_inherited_from_groups_as_explicitly_assigned()
{
$directRole = RoleAPI::make('direct');
$groupRole = RoleAPI::make('grouped');
$group = (new UserGroup)->handle('usergroup')->assignRole($groupRole);

RoleAPI::shouldReceive('find')->with('direct')->andReturn($directRole);
RoleAPI::shouldReceive('find')->with('grouped')->andReturn($groupRole);
RoleAPI::shouldReceive('all')->andReturn(collect([$directRole, $groupRole]));
UserGroupAPI::shouldReceive('find')->with('usergroup')->andReturn($group);
UserGroupAPI::shouldReceive('all')->andReturn(collect([$group]));

$user = $this->createPermissible()->assignRole($directRole)->addToGroup($group);
$user->save();

$this->assertEquals(['direct', 'grouped'], $user->roles()->map->handle()->values()->all());
$this->assertEquals(['direct'], $user->explicitRoles()->map->handle()->values()->all());
$this->assertEquals(['direct'], $user->data()->get('roles'));
}

#[Test]
public function it_gets_and_checks_permissions()
{
Expand Down
Loading