Firewall
firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false secured: pattern: ^/ anonymous: ~ form_login: check_path: /login_check login_path: /login username_parameter: _username password_parameter: _password logout: path: /logout access_control: - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/admin, roles: ROLE_ADMIN } - { path: ^/mod, roles: ROLE_MOD } role_hierarchy: ROLE_MOD: [ROLE_CZ, ROLE_TRP, ROLE_TRD, ROLE_TRR, ROLE_TRX, ROLE_MC, ROLE_U] ROLE_ADMIN: ROLE_MOD ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
W bazie danych każdy użytkownik posiad jedną lub więcej z ról (ROLE_CZ, ROLE_TRP, ROLE_TRD, ROLE_TRR, ROLE_TRX, ROLE_MC, ROLE_U)
Klasa użytkownika
<?php namespace Mns\PytaniaBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; use Symfony\Component\Security\Core\User\UserInterface; /** * @ORM\MappedSuperclass */ abstract class Person implements UserInterface { /** * @ORM\Column(type="string", length=32, unique=true) */ protected $login; /** * @ORM\Column(type="string", length=100) */ protected $password; /** * @ORM\Column(type="string", length=32) */ protected $salt; /** * @ORM\ManyToMany(targetEntity="Group", inversedBy="users") */ protected $group; public function __construct($login, $password) { $this->login = $login; $this->password = $password; $this->group = new ArrayCollection(); } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set login * * @param string $login */ public function setLogin($login) { $this->login = $login; } /** * Get login * * @return string */ public function getLogin() { return $this->login; } /** * Set password * * @param string $password */ public function setPassword($password) { $this->password = $password; } /** * Get password * * @return string */ public function getPassword() { return $this->password; } public function equals(UserInterface $user) { if($this->login !== $user->getUsername()) return false; if ($this->password !== $user->getPassword()) return false; if ($this->salt !== $user->getSalt()) return false; return true; } public function eraseCredentials() { } public function getRoles() { return $this->group->toArray(); } public function getSalt() { return $this->salt; } public function getUsername() { $this->getLogin(); } public function getName() { $this->getLogin(); } /** * Add group * * @param Mns\PytaniaBundle\Entity\Group $group */ public function addGroup(\Mns\PytaniaBundle\Entity\User $group) { $this->group[] = $group; } /** * Get group * * @return Doctrine\Common\Collections\Collection */ public function getGroup() { return $this->group; } public function setGroup(\Doctrine\Common\Collections\ArrayCollection $group) { $this->group = $group; } }
Klasa ról
<?php namespace Mns\PytaniaBundle\Entity; use Symfony\Component\Security\Core\Role\RoleInterface; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table(name="roles") * @ORM\Entity() */ class Group implements RoleInterface { /** * @ORM\Column(name="id", type="integer") * @ORM\Id() * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(name="name", type="string", length=30) */ protected $name; /** * @ORM\Column(name="role", type="string", length=20, unique=true) */ protected $role; /** * @ORM\ManyToMany(targetEntity="Person", mappedBy="group") */ protected $users; public function __construct() { $this->users = new ArrayCollection(); } public function __toString() { return $this->getName(); } public function getRole() { return $this->role; } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set name * * @param string $name */ public function setName($name) { $this->name = $name; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set role * * @param string $role */ public function setRole($role) { $this->role = $role; } /** * Add users * * @param Mns\PytaniaBundle\Entity\User $users */ public function addUser(\Mns\PytaniaBundle\Entity\User $users) { $this->users[] = $users; } /** * Get users * * @return Doctrine\Common\Collections\Collection */ public function getUsers() { return $this->users; } }