Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Models;
- use Models\Acl\Role,
- Doctrine\Common\Collections\ArrayCollection;
- /**
- * @Entity @Table(name="user")
- */
- class User
- {
- const NOT_FOUND = 1;
- const CREDENTIAL_INVALID = 2;
- /**
- * @Id @Column(name="id", type="integer")
- * @GeneratedValue(strategy="AUTO")
- */
- private $id;
- /**
- * @Column(name="created", type="datetime")
- */
- private $created;
- /**
- * @Column(name="updated", type="datetime")
- */
- private $updated;
- /**
- * @Column(name="last_online", type="datetime", nullable="false")
- */
- private $lastOnline;
- /** @Column(name="username", type="string", length=50,unique="true") */
- private $username;
- /** @Column(name="password", type="string", length=50) */
- private $password;
- /** @Column(name="salt", type="string", length=4) */
- private $salt;
- /** @Column(name="email", type="string", length=50) */
- private $email;
- /**
- * @OneToOne(targetEntity="Models\User\Details", inversedBy="user", cascade={"persist", "remove"})
- * @JoinColumn(name="details_id", referencedColumnName="id")
- */
- private $details;
- /**
- * @OneToOne(targetEntity="Models\Acl\Role")
- * @JoinColumn(name="role_id", referencedColumnName="id")
- */
- private $role;
- /**
- * @ManyToMany(targetEntity="Models\Viewer\Picture")
- * @JoinTable(name="user_join_viewer_picture",
- * joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
- * inverseJoinColumns={@JoinColumn(name="picture_id", referencedColumnName="id", unique=true)}
- * )
- */
- private $viewerPictures;
- public function __construct($username, $password)
- {
- // constructor is never called by Doctrine
- $this->created = $this->updated = new \DateTime("now");
- $this->setUsername($username);
- $this->setPassword($password);
- $this->viewerPictures = new ArrayCollection();
- }
- public function getCreated()
- {
- return $this->created;
- }
- /**
- * @PreUpdate
- */
- public function setUpdated()
- {
- $this->updated = new \DateTime("now");
- }
- public function getUpdated()
- {
- return $this->updated;
- }
- public function setLastOnline(\DateTime $date)
- {
- $this->lastOnline = $date;
- }
- public function getLastOnline()
- {
- return $this->lastOnline;
- }
- public function getId()
- {
- return $this->id;
- }
- public function getUsername()
- {
- return $this->username;
- }
- public function setUsername($username)
- {
- $this->username = $username;
- }
- public function setPassword($password)
- {
- $salt = "";
- for ($i=0;$i<4;$i++)
- $salt .= chr(rand(ord('a'), ord('z')));
- $this->salt = $salt;
- $this->password = md5(md5($salt).md5($password));
- }
- public function checkPassword($password)
- {
- if ($this->password == md5(md5($this->salt).md5($password)))
- return true;
- else
- return false;
- }
- public function getEmail()
- {
- return $this->username;
- }
- public function setEmail($email)
- {
- $this->email = $email;
- }
- public function setDetails(User\Details $details)
- {
- $details->setUser($this);
- $this->details = $details;
- }
- public function getDetails()
- {
- return $this->details;
- }
- public function setRole(Role $role)
- {
- $this->role = $role;
- }
- public function getRole()
- {
- return $this->role;
- }
- public function getViewerPictures()
- {
- return $this->viewerPictures;
- }
- /**
- *
- * @param string $username
- * @param string $password
- * @throws Exception
- * @return User
- */
- public static function authenticate($username, $password)
- {
- $em = \Zend_Registry::get('Doctrine_EntityManager');
- $user = $em->getRepository('Models\User')->findOneBy(array('username' => $username));
- if ($user)
- {
- if ($user->checkPassword($password))
- return $user->getId();
- throw new \Exception(self::CREDENTIAL_INVALID);
- }
- throw new \Exception(self::NOT_FOUND);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement