Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace SoftUniBlogBundle\Entity;
- use Doctrine\Common\Collections\ArrayCollection;
- use Doctrine\ORM\Mapping as ORM;
- use Symfony\Component\Security\Core\User\UserInterface;
- /**
- * User
- *
- * @ORM\Table(name="users")
- * @ORM\Entity(repositoryClass="SoftUniBlogBundle\Repository\UserRepository")
- */
- class User implements UserInterface
- {
- /**
- * @var int
- *
- * @ORM\Column(name="id", type="integer")
- * @ORM\Id
- * @ORM\GeneratedValue(strategy="AUTO")
- */
- private $id;
- /**
- * @var string
- *
- * @ORM\Column(name="email", type="string", length=100, unique=true)
- */
- private $email;
- /**
- * @var string
- *
- * @ORM\Column(name="fullName", type="string", length=255)
- */
- private $fullName;
- /**
- * @var string
- *
- * @ORM\Column(name="password", type="string", length=255)
- */
- private $password;
- /**
- * @var ArrayCollection
- *
- * @ORM\ManyToMany(targetEntity="SoftUniBlogBundle\Entity\Role")
- * @ORM\JoinTable(name="users_roles",
- * joinColumns={@ORM\JoinColumn(name="userId", referencedColumnName="id")},
- * inverseJoinColumns={@ORM\JoinColumn(name="userRoles", referencedColumnName="id")})
- */
- private $roles;
- /**
- * @var ArrayCollection
- *
- * @ORM\OneToMany(targetEntity="\SoftUniBlogBundle\Entity\Article", mappedBy="authorId")
- */
- private $articles;
- /**
- * @return \Doctrine\Common\Collections\ArrayCollection
- */
- public function getArticles()
- {
- return $this->articles;
- }
- /**
- * @param \SoftUniBlogBundle\Entity\Article $article
- * @return User
- */
- public function addPost(Article $article)
- {
- $this->articles[] = $article;
- return $this;
- }
- /**
- * Get id
- *
- * @return int
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * Set email
- *
- * @param string $email
- *
- * @return User
- */
- public function setEmail($email)
- {
- $this->email = $email;
- return $this;
- }
- /**
- * Get email
- *
- * @return string
- */
- public function getEmail()
- {
- return $this->email;
- }
- /**
- * Set fullName
- *
- * @param string $fullName
- *
- * @return User
- */
- public function setFullName($fullName)
- {
- $this->fullName = $fullName;
- return $this;
- }
- /**
- * Get fullName
- *
- * @return string
- */
- public function getFullName()
- {
- return $this->fullName;
- }
- /**
- * Set password
- *
- * @param string $password
- *
- * @return User
- */
- public function setPassword($password)
- {
- $this->password = $password;
- return $this;
- }
- /**
- * Get password
- *
- * @return string
- */
- public function getPassword()
- {
- return $this->password;
- }
- /**
- * Returns the roles granted to the user.
- *
- * <code>
- * public function getRoles()
- * {
- * return array('ROLE_USER');
- * }
- * </code>
- *
- * Alternatively, the roles might be stored on a ``roles`` property,
- * and populated in any number of different ways when the user object
- * is created.
- *
- * @return (Role|string)[] The user roles
- */
- public function getRoles()
- {
- $stringRoles = [];
- foreach ($this->roles as $role)
- {
- /**@var $role Role */
- $stringRoles[] = is_string($role) ? $role : $role->getRole();
- }
- return $stringRoles;
- }
- /**
- * @param \SoftUniBlogBundle\Entity\Role $role
- *
- * @return $this
- */
- public function addRole(Role $role)
- {
- $this->roles[] = $role;
- return $this;
- }
- /**
- * @param array $roles
- * @return $this
- */
- public function setRoles(array $roles)
- {
- $this->roles = $roles;
- return $this;
- }
- /**
- * Returns the salt that was originally used to encode the password.
- *
- * This can return null if the password was not encoded using a salt.
- *
- * @return string|null The salt
- */
- public function getSalt()
- {
- return null;
- }
- /**
- * Returns the username used to authenticate the user.
- *
- * @return string The username
- */
- public function getUsername()
- {
- return $this->email;
- }
- /**
- * Removes sensitive data from the user.
- *
- * This is important if, at any given point, sensitive information like
- * the plain-text password is stored on this object.
- */
- public function eraseCredentials()
- {
- // TODO: Implement eraseCredentials() method.
- }
- function __toString()
- {
- return $this->fullName;
- }
- public function __construct()
- {
- $this->articles = new ArrayCollection();
- $this->roles = new ArrayCollection();
- }
- /**
- * @param Article $article
- * @return bool
- */
- public function isAuthor(Article $article)
- {
- return $this->getId() == $article->getAuthorId();
- }
- public function isAdmin() {
- return in_array("ROLE_ADMIN", $this->getRoles());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement