Removed user->userData relationship (userData->user still must exist!)

This commit is contained in:
Polonkai Gergely 2012-07-17 18:24:54 +02:00
parent 36e5590436
commit 56de160dc1
4 changed files with 217 additions and 19 deletions

View File

@ -26,7 +26,7 @@ class UserData
/** /**
* @var KekRozsak\SecurityBundle\Entity\User $user * @var KekRozsak\SecurityBundle\Entity\User $user
* @ORM\Id * @ORM\Id
* @ORM\OneToOne(targetEntity="KekRozsak\SecurityBundle\Entity\User", inversedBy="userData") * @ORM\OneToOne(targetEntity="KekRozsak\SecurityBundle\Entity\User")
* @ORM\JoinColumn(name="user_id") * @ORM\JoinColumn(name="user_id")
*/ */
protected $user; protected $user;

View File

@ -77,8 +77,13 @@ class DefaultController extends Controller
if ($form->isValid(array('registration'))) if ($form->isValid(array('registration')))
{ {
$roleRepo = $this->getDoctrine()->getRepository('KekRozsakSecurityBundle:Role');
$defaultRoles = $roleRepo->findByDefault(true);
$user->setRegisteredAt(new \DateTime('now')); $user->setRegisteredAt(new \DateTime('now'));
$user->setPassword($this->get('security.encoder_factory')->getEncoder($user)->encodePassword($user->getPassword(), $user->getSalt())); $user->setPassword($this->get('security.encoder_factory')->getEncoder($user)->encodePassword($user->getPassword(), $user->getSalt()));
foreach ($defaultRoles as $role)
$user->addRole($role);
$em = $this->getDoctrine()->getEntityManager(); $em = $this->getDoctrine()->getEntityManager();
$em->persist($user); $em->persist($user);
$em->flush(); $em->flush();

View File

@ -0,0 +1,172 @@
<?php
namespace KekRozsak\SecurityBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\Role\RoleInterface;
use KekRozsak\SecurityBundle\Entity\User;
/**
* KekRozsak\SecurityBundle\Entity\Role
*
* @ORM\Entity
* @ORM\Table(name="roles")
*/
class Role implements RoleInterface
{
/**
* @var integer $id
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @var string name
* @ORM\Column(type="string", length=50, unique=true, nullable=false)
*/
protected $name;
/**
* Set name
*
* @param string $name
* @return Role
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @var boolean $default
* @ORM\Column(type="boolean", nullable=false)
*/
protected $default;
/**
* Set default
*
* @param boolean $default
*/
public function setDefault($default)
{
$this->default = $default;
return $this;
}
/**
* @var boolean $admin
* @ORM\Column(type="boolean", nullable=false)
*/
protected $admin;
/**
* Set admin
*
* @param boolean $admin
* @return Role
*/
public function setAdmin($admin)
{
$this->admin = $admin;
return $this;
}
/**
* Get admin
*
* @return boolean
*/
public function isAdmin()
{
return $this->admin;
}
/**
* @var boolean $superadmin
* @ORM\Column(type="boolean", nullable=false)
*/
protected $superAdmin;
/**
* Set superadmin
*
* @param boolean $superadmin
* @return Role
*/
public function setSuperadmin($superadmin)
{
$this->superadmin = $superadmin;
return $this;
}
/**
* Get superadmin
*
* @return boolean
*/
public function getSuperadmin()
{
return $this->superadmin;
}
/**
* @var text description
* @ORM\Column(type="string", length=150, nullable=true)
*/
protected $description;
/**
* Set description
*
* @param string $description
* @return Role
*/
public function setDescription($description = null)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/* Here comes the rest of RoleInterface's implementation */
public function getRole()
{
return $this->name;
}
}

View File

@ -25,6 +25,7 @@ class User implements UserInterface, AdvancedUserInterface
public function __construct() public function __construct()
{ {
$this->groups = new ArrayCollection(); $this->groups = new ArrayCollection();
$this->roles = new ArrayCollection();
} }
/** /**
@ -33,7 +34,7 @@ class User implements UserInterface, AdvancedUserInterface
* @ORM\GeneratedValue(strategy="AUTO") * @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer") * @ORM\Column(type="integer")
*/ */
private $id; protected $id;
/** /**
* Get id * Get id
@ -50,7 +51,7 @@ class User implements UserInterface, AdvancedUserInterface
* @ORM\Column(type="string", length=50, nullable=false, unique=true) * @ORM\Column(type="string", length=50, nullable=false, unique=true)
* @Assert\NotBlank(groups="registration") * @Assert\NotBlank(groups="registration")
*/ */
private $username; protected $username;
/** /**
* Set username * Set username
@ -79,7 +80,7 @@ class User implements UserInterface, AdvancedUserInterface
* @ORM\Column(type="string", length=50, nullable=false) * @ORM\Column(type="string", length=50, nullable=false)
* @Assert\NotBlank(groups="registration") * @Assert\NotBlank(groups="registration")
*/ */
private $password; protected $password;
/** /**
* Set password * Set password
@ -107,7 +108,7 @@ class User implements UserInterface, AdvancedUserInterface
* @var string $displayName * @var string $displayName
* @ORM\Column(type="string", length=50, unique=true, name="display_name") * @ORM\Column(type="string", length=50, unique=true, name="display_name")
*/ */
private $displayName; protected $displayName;
/** /**
* Set displayName * Set displayName
@ -135,7 +136,7 @@ class User implements UserInterface, AdvancedUserInterface
* @var string $email * @var string $email
* @ORM\Column(type="string", length=100, nullable=false, unique=true) * @ORM\Column(type="string", length=100, nullable=false, unique=true)
*/ */
private $email; protected $email;
/** /**
* Set email * Set email
@ -163,7 +164,7 @@ class User implements UserInterface, AdvancedUserInterface
* @var DateTime $registeredAt * @var DateTime $registeredAt
* @ORM\Column(type="datetime", nullable=false, name="registered_at") * @ORM\Column(type="datetime", nullable=false, name="registered_at")
*/ */
private $registeredAt; protected $registeredAt;
/** /**
* Set registeredAt * Set registeredAt
@ -192,7 +193,7 @@ class User implements UserInterface, AdvancedUserInterface
* @ORM\ManyToOne(targetEntity="User") * @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(name="accepted_by_id") * @ORM\JoinColumn(name="accepted_by_id")
*/ */
private $acceptedBy; protected $acceptedBy;
/** /**
* Set acceptedBy * Set acceptedBy
@ -220,7 +221,7 @@ class User implements UserInterface, AdvancedUserInterface
* @var DateTime $lastLoginAt * @var DateTime $lastLoginAt
* @ORM\Column(type="datetime", nullable=true, name="last_login_at") * @ORM\Column(type="datetime", nullable=true, name="last_login_at")
*/ */
private $lastLoginAt; protected $lastLoginAt;
/** /**
* Set lastLoginAt; * Set lastLoginAt;
@ -246,10 +247,10 @@ class User implements UserInterface, AdvancedUserInterface
/** /**
* @var \KekRozsak\FrontBundle\Entity\UserData $userData * @var \KekRozsak\FrontBundle\Entity\UserData $userData
* @ORM\OneToOne(targetEntity="KekRozsak\FrontBundle\Entity\UserData", mappedBy="user", fetch="LAZY", cascade={"persist"}) * @ORM\OneToMany(targetEntity="KekRozsak\FrontBundle\Entity\UserData", fetch="LAZY", cascade={"persist"}, mappedBy="user")
* @ORM\JoinColumn(name="id", referencedColumnName="user_id") * @ORM\JoinColumn(name="id", referencedColumnName="user_id")
*/ */
private $userData; protected $userData;
/** /**
@ -260,7 +261,7 @@ class User implements UserInterface, AdvancedUserInterface
*/ */
public function setUserData(\KekRozsak\FrontBundle\Entity\UserData $userData = null) public function setUserData(\KekRozsak\FrontBundle\Entity\UserData $userData = null)
{ {
$this->userData = $userData; $this->userData = new ArrayCollection(array($userData));
$userData->setUser($this); $userData->setUser($this);
return $this; return $this;
} }
@ -272,7 +273,7 @@ class User implements UserInterface, AdvancedUserInterface
*/ */
public function getUserData() public function getUserData()
{ {
return $this->userData; return $this->userData->get(1);
} }
/** /**
@ -280,7 +281,7 @@ class User implements UserInterface, AdvancedUserInterface
* @ORM\OneToMany(targetEntity="KekRozsak\FrontBundle\Entity\UserGroupMembership", mappedBy="user") * @ORM\OneToMany(targetEntity="KekRozsak\FrontBundle\Entity\UserGroupMembership", mappedBy="user")
* @ORM\JoinColumn(referencedColumnName="user_id") * @ORM\JoinColumn(referencedColumnName="user_id")
*/ */
private $groups; protected $groups;
/** /**
* Add group * Add group
@ -304,16 +305,36 @@ class User implements UserInterface, AdvancedUserInterface
return $this->groups; return $this->groups;
} }
/* Here comes the remaining part of UserInterface implementation */ /**
* @var Doctrine\Common\Collections\ArrayCollection $roles
* @ORM\ManyToMany(targetEntity="Role")
*/
protected $roles;
/**
* Add a role
*
* @param KekRozsak\SecurityBundle\Entity\Role $role
* @return User
*/
public function addRole(\KekRozsak\SecurityBundle\Entity\Role $role)
{
$this->roles[] = $role;
return $this;
}
/**
* Get all roles
*
* @return array
*/
public function getRoles() public function getRoles()
{ {
/* As we use ACLs instead of roles, every user get the return $this->roles->toArray();
* ROLE_USER role, and nothing else
*/
return array('ROLE_USER');
} }
/* Here comes the remaining part of UserInterface implementation */
public function getSalt() public function getSalt()
{ {
/* As we use crypt() to encode passwords, salt is always the /* As we use crypt() to encode passwords, salt is always the