public function addAdmins(Request $request)
    {
        $admins = $request->request->get('admins', []);

        // Remove empty values
        $admins = array_filter($admins);

        if (!is_array($admins) || count($admins) === 0) {
            $this->app->abort(400, '"admins" parameter must contains at least one value.');
        }
        /** @var Authenticator $authenticator */
        $authenticator = $this->app->getAuthenticator();
        if (!in_array($authenticator->getUser()->getId(), $admins)) {
            $admins[] = $authenticator->getUser()->getId();
        }

        $userRepository = $this->getUserRepository();

        $demotedAdmins = [];

        foreach ($userRepository->findAdmins() as $admin) {
            if (!in_array($admin->getId(), $admins)) {
                $demotedAdmins[$admin->getId()] = $admin;
            }
        }

        $userRepository->findBy(['id' => $admins]);
        $admins = array_map(function ($usrId) use ($userRepository) {
            if (null === $user = $userRepository->find($usrId)) {
                throw new RuntimeException(sprintf('Invalid usrId %s provided.', $usrId));
            }

            return $user;
        }, $admins);

        /** @var UserManipulator $userManipulator */
        $userManipulator = $this->app['manipulator.user'];

        $userManipulator->demote($demotedAdmins);
        $userManipulator->promote($admins);

        /** @var ACLManipulator $aclManipulator */
        $aclManipulator = $this->app['manipulator.acl'];
        $aclManipulator->resetAdminRights($admins);

        return $this->app->redirectPath('admin_dashboard');
    }
public function addAdmins(Request $request)
    {
        $admins = $request->request->get('admins', []);

        // Remove empty values
        $admins = array_filter($admins);

        if (!is_array($admins) || count($admins) === 0) {
            $this->app->abort(400, '"admins" parameter must contains at least one value.');
        }
        /** @var Authenticator $authenticator */
        $authenticator = $this->app->getAuthenticator();
        if (!in_array($authenticator->getUser()->getId(), $admins)) {
            $admins[] = $authenticator->getUser()->getId();
        }

        $userRepository = $this->getUserRepository();

        $demotedAdmins = [];

        foreach ($userRepository->findAdmins() as $admin) {
            if (!in_array($admin->getId(), $admins)) {
                $demotedAdmins[$admin->getId()] = $admin;
            }
        }

        $userRepository->findBy(['id' => $admins]);
        $admins = array_map(function ($usrId) use ($userRepository) {
            if (null === $user = $userRepository->find($usrId)) {
                throw new RuntimeException(sprintf('Invalid usrId %s provided.', $usrId));
            }

            return $user;
        }, $admins);

        /** @var UserManipulator $userManipulator */
        $userManipulator = $this->app['manipulator.user'];

        $userManipulator->demote($demotedAdmins);
        $userManipulator->promote($admins);

        /** @var ACLManipulator $aclManipulator */
        $aclManipulator = $this->app['manipulator.acl'];
        $aclManipulator->resetAdminRights($admins);

        return $this->app->redirectPath('admin_dashboard');
    }
public function addAdmins ( Request $ request ) { $ admins = $ request -> request -> get ( 'admins' , [ ] ) ; // Remove empty values $ admins = array_filter ( $ admins ) ; if ( ! is_array ( $ admins ) || count ( $ admins ) === 0 ) { $ this -> app -> abort ( 400 , '"admins" parameter must contains at least one value.' ) ; } /** @var Authenticator $authenticator */ $ authenticator = $ this -> app -> getAuthenticator ( ) ; if ( ! in_array ( $ authenticator -> getUser ( ) -> getId ( ) , $ admins ) ) { $ admins [ ] = $ authenticator -> getUser ( ) -> getId ( ) ; } $ userRepository = $ this -> getUserRepository ( ) ; $ demotedAdmins = [ ] ; foreach ( $ userRepository -> findAdmins ( ) as $ admin ) { if ( ! in_array ( $ admin -> getId ( ) , $ admins ) ) { $ demotedAdmins [ $ admin -> getId ( ) ] = $ admin ; } } $ userRepository -> findBy ( [ 'id' => $ admins ] ) ; $ admins = array_map ( function ( $ usrId ) use ( $ userRepository ) { if ( null === $ user = $ userRepository -> find ( $ usrId ) ) { throw new RuntimeException ( sprintf ( 'Invalid usrId %s provided.' , $ usrId ) ) ; } return $ user ; } , $ admins ) ; /** @var UserManipulator $userManipulator */ $ userManipulator = $ this -> app [ 'manipulator.user' ] ; $ userManipulator -> demote ( $ demotedAdmins ) ; $ userManipulator -> promote ( $ admins ) ; /** @var ACLManipulator $aclManipulator */ $ aclManipulator = $ this -> app [ 'manipulator.acl' ] ; $ aclManipulator -> resetAdminRights ( $ admins ) ; return $ this -> app -> redirectPath ( 'admin_dashboard' ) ; }
Grant to an user admin rights

@param  Request          $request
@return RedirectResponse
Grant to an user admin rights