0
votes

Tried every possiblity for logging in using FOSUserBundle and EasyAdminBundle, but without success

Here is my security.yml:

security:
    encoders:
        MyApp\UserBundle\Entity\User:
            algorithm: sha1
            encode_as_base64: false
            iterations: 1
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_ADMIN:       [ROLE_USER, ROLE_ADMIN]
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        auth_area:
            pattern: ^/users/(login|register|uploadPhoto|forgotPassword)
            anonymous: ~

        admin_area:
            pattern: ^/admin
            form_login:
                provider: fos_userbundle
                csrf_parameter: _csrf_security_token
                csrf_token_id: alongstringhere
                login_path: fos_user_security_login
                check_path: fos_user_security_check
                default_target_path: admin_index
                always_use_default_target_path: true
            logout:
                path: fos_user_security_logout
                target: fos_user_security_login
            anonymous: ~
            stateless: true

        secured_area:
            pattern:    ^/
            anonymous: ~
            stateless: true
            simple_preauth:
                authenticator: MyApp.sessionkey_authenticator


    providers:
        administrators:
            entity: { class: MyAppUserBundle:User, property: email }
        fos_userbundle:
            id: fos_user.user_manager

    access_control:
        - { path: ^/api/doc, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/users/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/users/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/users/logout, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/users/uploadPhoto, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/users/forgotPassword, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/register$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin, roles: [ROLE_ADMIN, ROLE_USER] }
        - { path: ^/, roles: ROLE_USER }

config.yml for fos user:

fos_user:
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class: MyApp\UserBundle\Entity\Admin

used routes.yml:

easy_admin_bundle:
    resource: "@MyAppMainBundle/Controller/AdminController.php"
    type:     annotation
    prefix:   /admin

fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"
    prefix:   /admin

admin_login:
    pattern: /admin/login

admin_logout:
    pattern: /admin/logout

admin_index:
    pattern:  /admin

Admin entity:

namespace MyApp\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Admin
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="MyApp\UserBundle\Entity\Repository\AdminRepository")
 * @ORM\Entity
 */
class Admin extends BaseUser
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        $this->setUpdatedAt(new \DateTime(date('Y-m-d H:i:s')));

        if($this->getCreatedAt() == null)
        {
            $this->setCreatedAt(new \DateTime(date('Y-m-d H:i:s')));
        }
    }


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="updated_at", type="datetime")
     */
    private $updatedAt;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created_at", type="datetime")
     */
    private $createdAt;

    /**
     * Set updatedAt
     *
     * @param \DateTime $updatedAt
     *
     * @return Admin
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    /**
     * Get updatedAt
     *
     * @return \DateTime
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     *
     * @return Admin
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * Get createdAt
     *
     * @return \DateTime
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function updatedTimestamps()
    {
        $this->setUpdatedAt(new \DateTime(date('Y-m-d H:i:s')));

        if($this->getCreatedAt() == null)
        {
            $this->setCreatedAt(new \DateTime(date('Y-m-d H:i:s')));
        }
    }
}

When I try to log in, it doesn't throw any error, credentials are valid (user was created in console), but I keep being redirected to the login page, I cannot continue to the admin panel. Can anyone help on this? Thank you in advance!

1
I can't see anything too obvious. However in security.yml under access_control you are using roles: to describe single roles where you should be using role: - possibly this is causing an issue. Worth a try anyway! - Richard
I just tried that, but it's not changing anything - Fazakas Istvan
Using stateless: true with the form login doesn't make much sense imo as you will want to keep information about the authenticated user in the session. Why did you add it here? - xabbuh
It seems that this was the issue I was searching for. Thank you @xabbuh. Can you post it as an answer, so I can vote it up? - Fazakas Istvan
@FazakasIstvan just for the record, the EasyAdmin Demo app (github.com/javiereguiluz/easy-admin-demo) uses FOSUserBundle for handling the security. Next time you can check out if needed. - Javier Eguiluz

1 Answers

2
votes

Using stateless: true with the form login doesn't make much sense imo as you will want to keep information about the authenticated user in the session. You should set it to false in your config.