Create and returns an array of new role

@param $rolename
@param $machine_name

@return $array
497966
private function createRole($rolename, $machine_name)
    {
        $role = Role::create(
            [
                'id' => $machine_name,
                'label' => $rolename,
                'originalId' => $machine_name
            ]
        );

        $result = [];

        try {
            $role->save();

            $result['success'][] = [
                'role-id' => $role->id(),
                'role-name' => $role->get('label')
            ];
        } catch (\Exception $e) {
            $result['error'] = [
                'vid' => $role->id(),
                'name' => $role->get('label'),
                'error' => 'Error: ' . get_class($e) . ', code: ' . $e->getCode() . ', message: ' . $e->getMessage()
            ];
        }

        return $result;
    }
761892
public function createPlatformRoleAction()
    {
        $form = $this->formFactory->create(FormFactory::TYPE_ROLE_TRANSLATION);
        $form->handleRequest($this->request);

        if ($form->isValid()) {
            $translationKey = $form->get('translationKey')->getData();
            $role = $this->roleManager->createPlatformRoleAction($translationKey);

            return new JsonResponse(
                array(
                    'id' => $role->getId(),
                    'maxUsers' => $role->getMaxUsers(),
                    'translationKey' => $role->getTranslationKey(),
                    'count' => 0
                )
            );
        }

        return array('form' => $form->createView());
    }
1003628
protected function getRole($roleName)
    {
        if (isset($this->roles[$roleName])) {
            return $this->roles[$roleName];
        }

        // If no config, we create a simple role with no permission
        if (!isset($this->rolesConfig[$roleName])) {
            $role = new Role($roleName);
            $this->roles[$roleName] = $role;
            return $role;
        }

        $roleConfig = $this->rolesConfig[$roleName];

        if (isset($roleConfig['children'])) {
            $role = new HierarchicalRole($roleName);
            $childRoles = (array)$roleConfig['children'];
            foreach ($childRoles as $childRole) {
                $childRole = $this->getRole($childRole);
                $role->addChild($childRole);
            }
        } else {
            $role = new Role($roleName);
        }

        $permissions = isset($roleConfig['permissions']) ? $roleConfig['permissions'] : [];
        foreach ($permissions as $permission) {
            $role->addPermission($permission);
        }

        $this->roles[$roleName] = $role;

        return $role;
    }
1294241
static function create( $roleName, $version = 0 )
    {
        $row = array( 'id' => null,
                      'name' => $roleName,
                      'version' => 0 );
        $role = new eZRole( $row );
        return $role;
    }
825383
public function newAction() {

        $entity = new Role();

        $form = $this->createCreateForm($entity);

        return array(
            'entity' => $entity,
            'form' => $form->createView(),
        );
    }
274862
protected function mapMachineDataResult(array $machineData): array
    {
        $result = [];
        foreach ($machineData as $data) {
            $result[] = MachineData::createFromArray($data);
        }
        return $result;
    }
539196
public function createRole(
        ?RoleCreateData $data = null,
        ?string $name = null
    ): FormInterface {
        $name = $name ?: StringUtil::fqcnToBlockPrefix(RoleCreateType::class);

        return $this->formFactory->createNamed($name, RoleCreateType::class, $data);
    }
1214786
public static function findByName(string $name, $guardName = null): RoleContract
    {
        $guardName = $guardName ?? config('auth.defaults.guard');

        $role = static::where('name', $name)->where('guard_name', $guardName)->first();

        if (!$role) {
            throw RoleDoesNotExist::create($name);
        }

        return $role;
    }
875629
public function userCreationFormAction()
    {
        $roleUser = $this->roleManager->getRoleByName('ROLE_USER');
        $isAdmin = ($this->authorization->isGranted('ROLE_ADMIN')) ? true : false;
        $roles = $this->roleManager->getAllPlatformRoles();
        $unavailableRoles = [];

        foreach ($roles as $role) {
            $isAvailable = $this->roleManager->validateRoleInsert(new User(), $role);

            if (!$isAvailable) {
                $unavailableRoles[] = $role;
            }
        }
        $profileType = new ProfileCreationType(
            $this->localeManager,
            array($roleUser),
            $isAdmin,
            $this->authenticationManager->getDrivers()
        );
        $form = $this->formFactory->create($profileType);

        $error = null;

        if (!$this->mailManager->isMailerAvailable()) {
            $error = 'mail_not_available';
        }

        return array(
            'form_complete_user' => $form->createView(),
            'error' => $error,
            'unavailableRoles' => $unavailableRoles
        );
    }
1049921
public function roleFetchById($role_ids)
    {
        $multi = is_array($role_ids);
        if (!$role_ids) {
            return [];
        }
        $role_ids = (array)$role_ids;

        $query = "
            SELECT
                r.role_id, r.`name`,r. description, r.added_on, r.updated_on,
                GROUP_CONCAT(p.permission_id) as _permission_ids
            FROM
                auth_role r
            LEFT OUTER JOIN
                auth_role_permissions p ON p.role_id = r.role_id
            WHERE
                r.role_id IN('" . join("','", $role_ids) . "')
            GROUP BY p.role_id
        ";
        try {
            $roles = $this->db->query($query)->fetchAll(PDO::FETCH_CLASS, self::CLASS_ROLE);
            if (!$multi) {
                $roles = (sizeof($roles) > 0) ? $roles[0] : false;
            }
        } catch (PDOException $db_err) {
            if ($this->logger) {
                $this->logger->error("Failed executing fetch role by id query", ['exception' => $db_err]);
            }
            $roles = [];
        }

        return $roles;
    }
911022
public static function checkRole($roleName)
    {
        return function ($params) use ($roleName) {
            \Yii::$app->user->can($roleName, $params);
        };
    }
1137277
@Transactional(readOnly = true)
    public E findByRoleName(String roleName) {

        SimpleExpression eqRoleName =
            Restrictions.eq("name", roleName);

        E role = dao.findByUniqueCriteria(eqRoleName);

        return role;
    }
891222
public function loadRoleAssignmentsByRoleId($roleId)
    {
        $data = $this->roleGateway->loadRoleAssignmentsByRoleId($roleId);

        if (empty($data)) {
            return array();
        }

        return $this->mapper->mapRoleAssignments($data);
    }
395561
public function hasRole($roleName)
    {
        if (! is_null($this->getUser())) {
            return $this->getUser()->hasRole($roleName);
        }

        return false;
    }
102610
public function getConnectionRoles($connection = null, $server = null)
    {
        $credentials = $this->credentials->getServerCredentials($connection, $server);

        return Arr::get($credentials, 'roles', []);
    }
314152
public function createUserAndMembership($roles = [Membership::ROLE_READ], $organization = '')
    {
        if (\is_string($roles)) {
            $roles = [$roles];
        }
        $roles = array_map(function ($role) {
            return strtolower($role);
        }, $roles);
        $contents = $this
            ->call('POST',
                implode('/', [self::ORGANIZATION_RESOURCE, $this->getOrganizationName($organization), 'memberships']),
                ['json' => [
                    'roles' => $roles,
            ]])
            ->getBody()
            ->getContents();

        return $this->getSingleMemberShipFromJsonResponse($contents);
    }
986661
protected function buildRolesTree(RoleManagerInterface $role_manager)
    {
        $hierarchy = [];
        $roles = $role_manager->getRoles();
        foreach ($roles as $role) {
            if ($role instanceof RoleInterface) {
                if ($role->getParent()) {
                    if (!isset($hierarchy[$role->getParent()->getName()])) {
                        $hierarchy[$role->getParent()->getName()] = [];
                    }
                    $hierarchy[$role->getParent()->getName()][] = $role->getName();
                } else {
                    if (!isset($hierarchy[$role->getName()])) {
                        $hierarchy[$role->getName()] = [];
                    }
                }
            }
        }

        return $hierarchy;
    }