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

Eloquent user driver – roles are not assignable #1989

Closed
rdarcy1 opened this Issue May 31, 2018 · 0 comments

Comments

Projects
None yet
3 participants
@rdarcy1

rdarcy1 commented May 31, 2018

Describe the bug
When using the eloquent user driver, roles cannot be assigned to users. If roles are assigned manually in the database by adding a record to the user_roles table, these rows are deleted on logout (or I think whenever the user is saved).

To Reproduce
Steps to reproduce the behavior:

  1. Use eloquent user driver
  2. Try to add roles to a user in the CP
  3. No roles are added

And:

  1. Manually assign roles in database
  2. Log out
  3. Role assignments have been removed

Expected behavior
Roles should be assignable and persist through user logout.

Environment details (please complete the following information):

  • Statamic Version: 2.9.2
  • Fresh Install or Upgrade: Upgrade
  • Web Server: Valet
  • PHP Version: 7.1
  • Addons installed:

Fix
This is not a good long term solution, but I seem to have got it working with edits to the set() and updateRoles() methods in Data/Users/Eloquent/User.php.

The issue when storing roles is that they're not stored on the users table, so the roles field is removed when checking against table column names.

// Data/Users/Eloquent/User.php#92

public function set($key, $value)
{
    $columns = \Schema::getColumnListing($this->model()->getTable());

    // Store the roles on the model so they're picked up for saving later
    if (array_has(array_flip($columns), $key) || $key === 'roles') {
        $this->model()->$key = $value;
    }
}

The issue when updating roles is that the roles relationship is not automatically loaded from the database (when loading from a file there's no need to query related data).

// Data/Users/Eloquent/User.php#41

private function updateRoles()
{
    $roles = collect($this->get('roles', []));

    // If roles aren't found on the model already, try re-building from the database
    if ($roles->isEmpty()) {

        // $this->roles() returns a collection of Role objects; we want just a collection of UUIDs
        $roles = $this->roles()->map(function (Statamic\Permissions\File\Role $role) {
            return $role->uuid();
        });
    }

    $this->remove('roles');
    (new Roles($this))->sync($roles);
}

@jasonvarga jasonvarga changed the title from [2.9.2] Eloquent user driver – roles are not assignable to Eloquent user driver – roles are not assignable May 31, 2018

@jackmcdade jackmcdade closed this Jul 17, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment