Advertisement
Guest User

User

a guest
Nov 9th, 2016
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.45 KB | None | 0 0
  1. <?php
  2.  
  3. namespace SoftUniBlogBundle\Entity;
  4.  
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8.  
  9. /**
  10.  * User
  11.  *
  12.  * @ORM\Table(name="users")
  13.  * @ORM\Entity(repositoryClass="SoftUniBlogBundle\Repository\UserRepository")
  14.  */
  15. class User implements UserInterface
  16. {
  17.     /**
  18.      * @var int
  19.      *
  20.      * @ORM\Column(name="id", type="integer")
  21.      * @ORM\Id
  22.      * @ORM\GeneratedValue(strategy="AUTO")
  23.      */
  24.     private $id;
  25.  
  26.     /**
  27.      * @var string
  28.      *
  29.      * @ORM\Column(name="email", type="string", length=100, unique=true)
  30.      */
  31.     private $email;
  32.  
  33.     /**
  34.      * @var string
  35.      *
  36.      * @ORM\Column(name="fullName", type="string", length=255)
  37.      */
  38.     private $fullName;
  39.  
  40.     /**
  41.      * @var string
  42.      *
  43.      * @ORM\Column(name="password", type="string", length=255)
  44.      */
  45.     private $password;
  46.  
  47.     /**
  48.      * @var ArrayCollection
  49.      *
  50.      * @ORM\ManyToMany(targetEntity="SoftUniBlogBundle\Entity\Role")
  51.      * @ORM\JoinTable(name="users_roles",
  52.      *      joinColumns={@ORM\JoinColumn(name="userId", referencedColumnName="id")},
  53.      *      inverseJoinColumns={@ORM\JoinColumn(name="userRoles", referencedColumnName="id")})
  54.      */
  55.     private $roles;
  56.  
  57.     /**
  58.      * @var ArrayCollection
  59.      *
  60.      * @ORM\OneToMany(targetEntity="\SoftUniBlogBundle\Entity\Article", mappedBy="authorId")
  61.      */
  62.     private $articles;
  63.  
  64.     /**
  65.      * @return \Doctrine\Common\Collections\ArrayCollection
  66.      */
  67.     public function getArticles()
  68.     {
  69.         return $this->articles;
  70.     }
  71.  
  72.     /**
  73.      * @param \SoftUniBlogBundle\Entity\Article $article
  74.      * @return User
  75.      */
  76.     public function addPost(Article $article)
  77.     {
  78.         $this->articles[] = $article;
  79.  
  80.         return $this;
  81.     }
  82.  
  83.     /**
  84.      * Get id
  85.      *
  86.      * @return int
  87.      */
  88.     public function getId()
  89.     {
  90.         return $this->id;
  91.     }
  92.  
  93.     /**
  94.      * Set email
  95.      *
  96.      * @param string $email
  97.      *
  98.      * @return User
  99.      */
  100.     public function setEmail($email)
  101.     {
  102.         $this->email = $email;
  103.  
  104.         return $this;
  105.     }
  106.  
  107.     /**
  108.      * Get email
  109.      *
  110.      * @return string
  111.      */
  112.     public function getEmail()
  113.     {
  114.         return $this->email;
  115.     }
  116.  
  117.     /**
  118.      * Set fullName
  119.      *
  120.      * @param string $fullName
  121.      *
  122.      * @return User
  123.      */
  124.     public function setFullName($fullName)
  125.     {
  126.         $this->fullName = $fullName;
  127.  
  128.         return $this;
  129.     }
  130.  
  131.     /**
  132.      * Get fullName
  133.      *
  134.      * @return string
  135.      */
  136.     public function getFullName()
  137.     {
  138.         return $this->fullName;
  139.     }
  140.  
  141.     /**
  142.      * Set password
  143.      *
  144.      * @param string $password
  145.      *
  146.      * @return User
  147.      */
  148.     public function setPassword($password)
  149.     {
  150.         $this->password = $password;
  151.  
  152.         return $this;
  153.     }
  154.  
  155.     /**
  156.      * Get password
  157.      *
  158.      * @return string
  159.      */
  160.     public function getPassword()
  161.     {
  162.         return $this->password;
  163.     }
  164.  
  165.     /**
  166.      * Returns the roles granted to the user.
  167.      *
  168.      * <code>
  169.      * public function getRoles()
  170.      * {
  171.      *     return array('ROLE_USER');
  172.      * }
  173.      * </code>
  174.      *
  175.      * Alternatively, the roles might be stored on a ``roles`` property,
  176.      * and populated in any number of different ways when the user object
  177.      * is created.
  178.      *
  179.      * @return (Role|string)[] The user roles
  180.      */
  181.     public function getRoles()
  182.     {
  183.         $stringRoles = [];
  184.         foreach ($this->roles as $role)
  185.         {
  186.             /**@var $role Role */
  187.             $stringRoles[] = is_string($role) ? $role : $role->getRole();
  188.         }
  189.         return $stringRoles;
  190.     }
  191.  
  192.     /**
  193.      * @param \SoftUniBlogBundle\Entity\Role $role
  194.      *
  195.      * @return $this
  196.      */
  197.     public function addRole(Role $role)
  198.     {
  199.         $this->roles[] = $role;
  200.  
  201.         return $this;
  202.     }
  203.  
  204.     /**
  205.      * @param array $roles
  206.      * @return $this
  207.      */
  208.     public function setRoles(array $roles)
  209.     {
  210.         $this->roles = $roles;
  211.  
  212.         return $this;
  213.     }
  214.  
  215.     /**
  216.      * Returns the salt that was originally used to encode the password.
  217.      *
  218.      * This can return null if the password was not encoded using a salt.
  219.      *
  220.      * @return string|null The salt
  221.      */
  222.     public function getSalt()
  223.     {
  224.         return null;
  225.     }
  226.  
  227.     /**
  228.      * Returns the username used to authenticate the user.
  229.      *
  230.      * @return string The username
  231.      */
  232.     public function getUsername()
  233.     {
  234.         return $this->email;
  235.     }
  236.  
  237.     /**
  238.      * Removes sensitive data from the user.
  239.      *
  240.      * This is important if, at any given point, sensitive information like
  241.      * the plain-text password is stored on this object.
  242.      */
  243.     public function eraseCredentials()
  244.     {
  245.         // TODO: Implement eraseCredentials() method.
  246.     }
  247.    
  248.     function __toString()
  249.     {
  250.         return $this->fullName;
  251.     }
  252.  
  253.     public function __construct()
  254.     {
  255.         $this->articles = new ArrayCollection();
  256.         $this->roles = new ArrayCollection();
  257.     }
  258.  
  259.     /**
  260.      * @param Article $article
  261.      * @return bool
  262.      */
  263.     public function isAuthor(Article $article)
  264.     {
  265.         return $this->getId() == $article->getAuthorId();
  266.     }
  267.  
  268.     public function isAdmin() {
  269.         return in_array("ROLE_ADMIN", $this->getRoles());
  270.     }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement