1

I'm doing an symfony application. I try to implement a user system.

3 type of person can log in: student, teacher and secretary

enter image description here

If I modelise so, how can I done a login class?

In my Student entity I have:

/**
 * @var \essaiBundle\Entity\User
 *
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="NONE")
 * @ORM\OneToOne(targetEntity="essaiBundle\Entity\User")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="id", referencedColumnName="id")
 * })
 */
private $id;

That reference to the user id.

How can I get the role for the user log in?

My security file:

security:
    providers:
        our_db_provider:
            entity: { class: essaiBundle\Entity\User, property: username}

    access_control:
        - { path: ^/admin, roles: ROLE_ADMIN }
    firewalls:
        login_firewall:
            pattern:   ^/login$
            anonymous: ~
        default:
            anonymous: ~
            http_basic: ~
            form_login:
                login_path: /login
                check_path: /login_check
                csrf_provider: security.csrf.token_manager
                default_target_path: admin
            provider: our_db_provider


    encoders:
        essaiBundle\Entity\User:
            algorithm: bcrypt
            cost: 12

Thanks

4
  • 1
    What is the difference between Student, Teacher and Secretary entities? Is role mechanism not enough in this case? Also look at Doctrine inheritance, if you have to separate your entities. Commented Nov 15, 2015 at 16:37
  • I need the 3 entities because of the other part of the application. An Teacher has not the same attribute that the student, etc. And also the relationships with other entities are different. Commented Nov 15, 2015 at 17:55
  • You are able to use multiple user entities when you use multiple user providers: symfony.com/doc/current/cookbook/security/… But you should consider to use only one user entity and multiple roles like chapay suggested. Commented Nov 15, 2015 at 21:51
  • I want to use multiple roles, but with multiples tables. A Student has a role, A Teache has a role, a Secretary has a role. All are users (table user). Commented Nov 15, 2015 at 21:56

1 Answer 1

2

The problem is in the application architecture. I think you started to design the application from the database side. You need to change the approach to application design. Doctrine can handle himself with the base, you don't have to worry about base to much anymore.

This is your new class diagram

You don't need one-to-one relations (with User and Student for example). Student, Teacher, Secretary extends from the User. This allows you to simplify work with logging, because you have to configure the User class only

User entity:

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"user" = "User", "student" = "Student", "teacher" = "Teacher", "secretary" = "Secretary"})
 */
class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * Name
     *
     * @var string
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

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

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }
}

Student entity:

use Doctrine\ORM\Mapping as ORM;

/**
 * Student
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Student extends User
{
    private $teachers; // extra field for student
}

Teacher class:

use Doctrine\ORM\Mapping as ORM;

/**
 * Teacher
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Teacher extends User
{
    private $students; // extra field for teacher
}

Secretary entity:

use Doctrine\ORM\Mapping as ORM;

/**
 * Secretary
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Secretary extends User
{
    private $sercretaryBar; // extra field for secretary
}

Here is link to Doctrine doc, that will help you.

DB:

When you change your entities, you can check what changes you need to apply in DB:

app/console doctrine:schema:update --dump-sql

to force change in DB:

app/console doctrine:schema:update --force

I suggests use the migrations (doctrine/doctrine-migrations-bundle on packagist.org), if you do not use so far.

Sign up to request clarification or add additional context in comments.

7 Comments

You're completely right. I have done the database model before. I think your solution must be a good solution, but a login classe must also extend the user class? How can I access to the login which extend the user class directly from the security.yml file?
You don't need Login class. You use User class instead, for all login procedures. Please refer to symfony doc
OK, the proble is that in a database, an attribute cannot be null (it can but it's not good). Because in my application the user can not have an login and password (he cannot log in), then I use another entity with this information. But ok, if I use a normal solution and I put the username and the password in an attribute in the user class. Then how can I know if an user has the student role, the secretary role or the teacher role?
I recommend you to read about fosuser bundle. One important think in this issue is that you write roles in variable Role and this is very important policy.
I think you don't understand my problem. I try to explain better. Now I have a class user and 3 class that extend this. In the class Teacher for example, I have for example a role variable. In the class Student another role variable and in the class Secretary another role variable. But now, I have to get back in the security file the user specifications (login, pass and role), login and pass I would get it, but the role is in the chlid one not in the user class, how to get it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.