generateRoleHierarchy

Generate Role Hierarchy structure by yml configuration

@param mixed  $params
@param string $parent

@return array
497507
private function generateRoleHierarchy($params, $parent = "")
    {
        if (is_array($params))
        {
            foreach ($params as $key => $child)
            {
                if ($parent != "" && is_array($child))
                {
                    foreach ($child as $subkey => $subchild)
                    {
                        $this->roleHierarchy[$parent][] = $subkey;
                        if (is_array($child))
                        {
                            $this->generateRoleHierarchy($subchild, $subkey);
                        }
                    }
                }
                elseif (is_array($child))
                {
                    $this->generateRoleHierarchy($child, $key);
                }
                elseif (!isset($this->roleHierarchy[$parent]) || !in_array($child, $this->roleHierarchy[$parent]))
                {
                    $this->roleHierarchy[$parent][] = $child;
                }
            }
        }
        return $this->roleHierarchy;
    }
661031
protected function getHierarchy($role, array $roleHierarchy)
    {
        $roles = [];
        if (array_key_exists($role, $roleHierarchy)) {
            $roleRoles = (array) $roleHierarchy[$role];
            foreach ($roleRoles as $role) {
                $roles = array_merge($roles, $this->getHierarchy($role, $roleHierarchy));
            }
            $roles = array_merge($roles, $roleRoles);
        }

        return $roles;
    }
1110568
public function getChildren($parent = null, array $options = [])
    {
        if ($parent instanceof HasNodeInterface) {
            $node = $this->publicNodeVersions->getNodeFor($parent);
        } elseif ($parent instanceof Category) {
            $node = $this->nodeRepository->find($parent->getNodeId());
        } elseif ($parent instanceof Node) {
            $node = $parent;
        } else {
            $node = null;
        }

        return $this->getNodeChildren(['parent' => $node] + $options);
    }
891784
public function getParentsList()
    {
        /** @var static $root */
        $root  = static::root();
        $items = [
            ['id' => '1', 'title' => 'Item Root'],
        ];
        $nodes = $root->descendants();
        if ($this->exists) {
            $nodes->where('navigation_id', $this->navigation_id)->where('id', '<>', $this->id);
        }

        foreach ($nodes->get() as $node) {
            foreach ($node->getDescendantsAndSelf()->toHierarchy() as $menu) {
                $this->appendMenu($menu, $items);
            }
        }

        return array_pluck($items, 'title', 'id');
    }
927663
public function getInheritanceList(): array
    {
        $roleList = [$this];
        $role = $this;
        while ($parentRole = $role->getParent()) {
            $roleList[] = $parentRole;
            $role = $parentRole;
        }

        return $roleList;
    }
74660
public function addParentRoles($role, array $parents)
    {
        $role = $this->checkRole($role);
        $roleId = $role->getRoleId();
        if ($this->hasRole($roleId)) {
            foreach ($parents as $parent) {
                $parent = $this->checkRole($parent);
                $parentId = $parent->getRoleId();
                if ($this->hasRole($parentId)) {
                    array_push($this->roles[$roleId][self::NODE_PARENTS_ROLE], $parentId);
                } else {
                    throw new RoleNotExistException(
                        sprintf(
                            'Role parent %s not stored',
                            $roleId
                        )
                    );
                }
            }
            return true;
        } else {
            throw new RoleNotExistException(
                sprintf(
                    'Role %s not stored',
                    $roleId
                )
            );
        }
    }
564554
private function buildRoleMap() {
		
		$this->mappedRoleNames = [];

		foreach ($this->hierarchy as $parent => $roles) {

			$roles = array_map('strtolower', $roles);
			$this->mappedRoleNames[$parent] = $roles;
			$additionalRoles = $roles;
			$foundParent = [];

			// check for every role, whether there are other roles below it

			while ($role = array_shift($additionalRoles)) {

				if (!isset($this->hierarchy[$role])) {
					
					// role has no further sub-roles defined

					continue;
				}
		
				$foundParent[] = $role;
		
				foreach ($this->hierarchy[$role] as $roleToAdd) {
					$this->mappedRoleNames[$parent][] = $roleToAdd;
				}

				foreach (array_diff($this->hierarchy[$role], $foundParent) as $roleToAdd) {

					$additionalRoles[] = $roleToAdd;

				}
			}
		
			$this->mappedRoleNames[$parent] = array_unique($this->mappedRoleNames[$parent]);

		}
		
	}
65701
public function setParentRoles(array $parentRoles)
    {
        $this->parentRoles = [];
        foreach ($parentRoles as $parentRole) {
            $this->addParentRole($parentRole);
        }
    }
774455
protected function normalizeListParentParameter($parent)
    {
        $separator = $this->getListParentSeparator();

        if (is_integer($parent) && $parent < 0 || is_string($parent) && preg_match('#-\d+#', $parent)) {
            $parent = (int) $parent;

            if ($parent < 0) {
                // The the parent that conforms to the index in the parent chain
                // This only works within the context of this model, so no cross-model logic is expected
                $index = count($this->listParents) + $parent - 1;

                if ($index >= 0 && isset($this->listParents[ $index ])) {
                    $parent = $this->listParents[ $index ]->relation . $separator . $this->listParents[ $index ]->key;
                } else {
                    $parent = null;
                }

            } else {
                $parent = null;
            }
        }

        if (is_string($parent) && false === strpos($parent, $separator)) {
            $parent = null;
        }

        return $parent;
    }
582395
public function getSubRoles(Role $role) {
		
		$parentName = $role->getRoleName();
		$subRoles = [];
		
		if(array_key_exists($role->getRoleName(), $this->mappedRoleNames)) {
			
			foreach($this->mappedRoleNames[$parentName] as $subRoleName) {
				$subRoles[] = new Role($subRoleName);
			}
			
		}
		
		return $subRoles;

	}
775699
protected static function getOrganizationUserRoles(OrganizationUserInterface $user, $roleHierarchy = null)
    {
        $roles = [];

        if ($user instanceof RoleableInterface && $user instanceof OrganizationUserInterface) {
            $org = $user->getOrganization();
            $roles = self::buildOrganizationUserRoles($roles, $user, $org->getName());
            $roles = self::buildOrganizationRoles($roles, $org);

            if ($roleHierarchy instanceof RoleHierarchyInterface) {
                $roles = RoleUtil::formatNames($roleHierarchy->getReachableRoles($roles));
            }
        }

        return $roles;
    }
248651
public function getAllAvailableRoles($roleHierarchy)
    {
        $roles = [];
        foreach ($roleHierarchy as $key => $value) {
            if (is_array($value)) {
                $roles = array_merge($roles, $this->getAllAvailableRoles($value));
            }
            if (is_string($key)) {
                $roles[] = $key;
            }
            if (is_string($value)) {
                $roles[] = $value;
            }
        }

        return $roles;
    }
72652
private function buildTree($account, &$data, $parent) {
    $data[] = array($account['name'], null, 1);
    if ($account['subAccounts']) {
      foreach($account['subAccounts'] as $subAccount) {
        $this->buildTree($subAccount, $data, $account['name']);
      }
    }
  }
669057
private function buildTree($rows, $parent_id = 0)
    {
        $containerAttributes = '';

        if (!$this->containerOptionsIsSet) {
            $containerAttributes = Html::renderTagAttributes($this->containerOptions);
            $this->containerOptionsIsSet = true;
        }

        $result = "<ul$containerAttributes>";

        foreach ($rows as $row) {
            if ($row['parent_id'] == $parent_id) {

                $value = call_user_func_array($this->value, ['model' => $row['model']]);
                $result .= "<li>$value";

                if ($this->hasChild($rows, $row['id'])) {
                    $result.= $this->buildTree($rows, $row['id']);
                }

                $result .= '</li>';
            }
        }

        $result .= '</ul>';

        return $result;
    }
625957
protected function _addRoles($roles)
    {
        foreach($roles as $role) {
            if (!$this->hasRole($role->getName())) {
                $parents = $role->getParents()->toArray();
                $parentNames = array();
                foreach($parents as $parent) {
                    $parentNames[] = $parent->getName();
                }
                $this->addRole(new Role($role->getName()), $parentNames);
            }
        }

        return $this;
    }
456887
private function getAllChildRoleIds($parentIds)
    {
        $allChildIds = [];

        while (count($parentIds) > 0) {
            $parentIds = $this->getChildIds($parentIds);
            $allChildIds = ArrayHelper::merge($allChildIds, $parentIds);
        };

        return $allChildIds;
    }
26187
public function addParentRole(Role $parentRole)
    {
        if (!$this->hasParentRole($parentRole)) {
            $parentRoleIdentifier = $parentRole->getIdentifier();
            $this->parentRoles[$parentRoleIdentifier] = $parentRole;
        }
    }