Reputation: 11
I am using FOSUserBundle for developing my website project and I would like to know (and how) set two different tables for user registration. I would like to do it in this way becase I consider it would be more easy to set administrators in a table and users in another one.
I keep open mind, so If you think I'm not right with my decisionm, it would be great if you provide me an alternative way to get it.
Thanks for reading.
Upvotes: 1
Views: 745
Reputation: 1760
I think you could do what you want by defining a custom user provider (a class that will retrieve your User entity, given a username : How to Create a custom User Provider
Having said that, I'm not sure this is a good way to do what you want, as you will have informations (email, passwords...) that will be split in several tables.
I would do something a bit different : create an Administrator entity which will store the settings about an admin (rights on this and that, etc) and make a relation between your User entity (which will alone store the username/password...) and the Administrator entity. If the user is a regular user, this relation will be NULL, but, if it is an administrator, it will be linked to the administrator entity. In more details :
I suppose you have a User entity set up according to FOSUserBundle documentation. Just add OneToOne relation with an Administrator entity.
<?php
// src/Acme/UserBundle/Entity/User.php
namespace Acme\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToOne(targetEntity="Acme\UserBundle\Entity\Administrator", inversedBy="user")
*/
private $administrator;
public function __construct()
{
parent::__construct();
// your own logic
}
public function getAdministrator()
{
return $this->administrator;
}
public function setAdministrator(Acme\UserBundle\Entity\Administrator $administrator)
{
$this->administrator = $administrator;
$adinistrator->setUser($this);
return $this;
}
}
The Administrator entity will hold all the fields you need to manage very thinly the rights on your application. For example, a boolean that states if this administrator can delete posts :
<?php
// src/Acme/UserBundle/Entity/Administrator.php
namespace Acme\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="Administrator")
*/
class Administrator
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToOne(targetEntity="Acme\UserBundle\Entity\User", mappedBy="administrator")
*/
private $user;
/**
* @var boolean
*
* @ORM\Column(name="canDeletePosts", type="boolean")
*/
private $canDeletePosts;
// And all the fields you need !
public function __construct()
{
parent::__construct();
// your own logic
}
public function getUser()
{
return $this->user;
}
public function setUser(Acme\UserBundle\Entity\User $user)
{
$this->user = $user;
return $this;
}
public function setCanDeletePosts($canDeletePosts)
{
$this->canDeletePosts = $canDeletePosts;
return $this;
}
public function getCanDeletePosts()
{
return $this->canDeletePosts;
}
}
When you promote a user to admin, don't forget to create an Administrator and link it to the User entity. Then, in your administration controller, if the User is an administrator, you can do things like this :
$canDeletePosts = $this->getUser()->getAdministrator()->getCanDeletePosts();
if ($canDeletePosts)
{
// Remove the post
}
Upvotes: 1