You are here

class GroupRoleSynchronizer in Group 8

Same name and namespace in other branches
  1. 2.0.x src/GroupRoleSynchronizer.php \Drupal\group\GroupRoleSynchronizer

Synchronizes user roles to group roles.

Hierarchy

Expanded class hierarchy of GroupRoleSynchronizer

1 file declares its use of GroupRoleSynchronizer
GroupRoleSynchronizerTest.php in tests/src/Unit/GroupRoleSynchronizerTest.php
1 string reference to 'GroupRoleSynchronizer'
group.services.yml in ./group.services.yml
group.services.yml
1 service uses GroupRoleSynchronizer
group_role.synchronizer in ./group.services.yml
Drupal\group\GroupRoleSynchronizer

File

src/GroupRoleSynchronizer.php, line 13

Namespace

Drupal\group
View source
class GroupRoleSynchronizer implements GroupRoleSynchronizerInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new GroupRoleSynchronizer.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function getGroupRoleId($group_type_id, $role_id) {

    // The maximum length of a group role's machine name.
    //
    // Group role IDs consist of two parts separated by a dash:
    // - The group type ID.
    // - The machine name of the group role; unique per group type.
    //
    // Therefore, the maximum length of a group role machine name is determined
    // by subtracting the group type ID length from the entity type ID length
    // and leaving room for a dash character.
    $machine_name_max_length = EntityTypeInterface::ID_MAX_LENGTH - GroupTypeInterface::ID_MAX_LENGTH - 1;

    // Generate an MD5 hash to use as the group role machine name.
    $machine_name = substr(md5('group_role_sync.' . $role_id), 0, $machine_name_max_length);
    return "{$group_type_id}-{$machine_name}";
  }

  /**
   * {@inheritdoc}
   */
  public function getGroupRoleIdsByGroupType($group_type_id) {
    return $this
      ->getGroupRoleIdsByGroupTypes([
      $group_type_id,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getGroupRoleIdsByGroupTypes($group_type_ids) {
    $group_role_ids = [];
    $role_ids = $this->entityTypeManager
      ->getStorage('user_role')
      ->getQuery()
      ->execute();
    foreach ($role_ids as $role_id) {
      foreach ($group_type_ids as $group_type_id) {
        $group_role_ids[] = $this
          ->getGroupRoleId($group_type_id, $role_id);
      }
    }
    return $group_role_ids;
  }

  /**
   * {@inheritdoc}
   */
  public function getGroupRoleIdsByUserRole($role_id) {
    return $this
      ->getGroupRoleIdsByUserRoles([
      $role_id,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getGroupRoleIdsByUserRoles($role_ids) {
    $group_role_ids = [];
    $group_type_ids = $this->entityTypeManager
      ->getStorage('group_type')
      ->getQuery()
      ->execute();
    foreach ($group_type_ids as $group_type_id) {
      foreach ($role_ids as $role_id) {
        $group_role_ids[] = $this
          ->getGroupRoleId($group_type_id, $role_id);
      }
    }
    return $group_role_ids;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GroupRoleSynchronizer::$entityTypeManager protected property The entity type manager.
GroupRoleSynchronizer::getGroupRoleId public function Generates an ID for a synchronized group role. Overrides GroupRoleSynchronizerInterface::getGroupRoleId
GroupRoleSynchronizer::getGroupRoleIdsByGroupType public function Retrieves all synchronized group role IDs for a group type. Overrides GroupRoleSynchronizerInterface::getGroupRoleIdsByGroupType
GroupRoleSynchronizer::getGroupRoleIdsByGroupTypes public function Retrieves all synchronized group role IDs for a list of group types. Overrides GroupRoleSynchronizerInterface::getGroupRoleIdsByGroupTypes
GroupRoleSynchronizer::getGroupRoleIdsByUserRole public function Retrieves all synchronized group role IDs for a user role. Overrides GroupRoleSynchronizerInterface::getGroupRoleIdsByUserRole
GroupRoleSynchronizer::getGroupRoleIdsByUserRoles public function Retrieves all synchronized group role IDs for a list of user roles. Overrides GroupRoleSynchronizerInterface::getGroupRoleIdsByUserRoles
GroupRoleSynchronizer::__construct public function Constructs a new GroupRoleSynchronizer.