You are here

public function UserSaveHandler::beforeSync in Services Client 7.2

Add role mapping.

Overrides EventHandler::beforeSync

File

include/event.inc, line 1412

Class

UserSaveHandler

Code

public function beforeSync($object) {
  if (!empty($this->config['user_map_roles'])) {
    $account = $this
      ->getEntity();

    // Build map of role to format [local_role_id] => [remote_role_name]
    $map = array();
    foreach ($this->config['user_map_roles_map'] as $id => $row) {
      $map[$row['local']] = $row['remote'];
    }

    // Load remote roles in format [remote_role_name] => [remote_role_id]
    $remote_roles = array_flip($this
      ->getRemoteRoles());

    // Build list of roles that will be attached to object
    $roles = array();

    // First we need to make sure that we have roles to map and also remote_roles
    // list isn't empty.
    if (!empty($map) && !empty($remote_roles)) {
      foreach ($map as $local_rid => $remote_role) {

        // Foreach local role, check if role is attached to local user account
        // and if we have remote role.
        if (isset($account->roles[$local_rid]) && isset($remote_roles[$remote_role])) {
          $roles[$remote_roles[$remote_role]] = $remote_role;
        }
      }
    }

    // If some mapping was created, attach roles.
    if (!empty($roles)) {
      $object->roles = $roles;
    }
  }
}