Advertisement
Guest User

Untitled

a guest
May 30th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.12 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Models;
  4.  
  5. use Models\Acl\Role,
  6.     Doctrine\Common\Collections\ArrayCollection;
  7.  
  8. /**
  9.  * @Entity @Table(name="user")
  10.  */
  11.  
  12. class User
  13. {
  14.  
  15.     const NOT_FOUND             = 1;
  16.     const CREDENTIAL_INVALID    = 2;
  17.  
  18.     /**
  19.      * @Id @Column(name="id", type="integer")
  20.      * @GeneratedValue(strategy="AUTO")
  21.      */
  22.     private $id;
  23.    
  24.     /**
  25.      * @Column(name="created", type="datetime")
  26.      */
  27.     private $created;
  28.  
  29.     /**
  30.      * @Column(name="updated", type="datetime")
  31.      */
  32.     private $updated;
  33.    
  34.     /**
  35.      * @Column(name="last_online", type="datetime", nullable="false")
  36.      */
  37.     private $lastOnline;       
  38.    
  39.     /** @Column(name="username", type="string", length=50,unique="true") */
  40.     private $username;
  41.    
  42.     /** @Column(name="password", type="string", length=50) */
  43.     private $password;
  44.    
  45.     /** @Column(name="salt", type="string", length=4) */
  46.     private $salt;
  47.    
  48.     /** @Column(name="email", type="string", length=50) */
  49.     private $email;
  50.          
  51.     /**
  52.      * @OneToOne(targetEntity="Models\User\Details", inversedBy="user", cascade={"persist", "remove"})
  53.      * @JoinColumn(name="details_id", referencedColumnName="id")
  54.      */
  55.     private $details;  
  56.    
  57.     /**
  58.      * @OneToOne(targetEntity="Models\Acl\Role")
  59.      * @JoinColumn(name="role_id", referencedColumnName="id")
  60.      */
  61.     private $role;     
  62.    
  63.     /**
  64.     * @ManyToMany(targetEntity="Models\Viewer\Picture")
  65.     * @JoinTable(name="user_join_viewer_picture",
  66.     *      joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
  67.     *      inverseJoinColumns={@JoinColumn(name="picture_id", referencedColumnName="id", unique=true)}
  68.     *      )
  69.     */   
  70.     private $viewerPictures;   
  71.            
  72.     public function __construct($username, $password)
  73.     {
  74.         // constructor is never called by Doctrine
  75.         $this->created = $this->updated = new \DateTime("now");
  76.         $this->setUsername($username);
  77.         $this->setPassword($password);
  78.         $this->viewerPictures = new ArrayCollection();
  79.     }  
  80.    
  81.     public function getCreated()
  82.     {
  83.         return $this->created;
  84.     }
  85.    
  86.     /**
  87.      * @PreUpdate
  88.      */
  89.     public function setUpdated()
  90.     {
  91.         $this->updated = new \DateTime("now");
  92.     }
  93.    
  94.     public function getUpdated()
  95.     {
  96.         return $this->updated;
  97.     }
  98.    
  99.     public function setLastOnline(\DateTime $date)
  100.     {
  101.         $this->lastOnline = $date;
  102.     }  
  103.    
  104.     public function getLastOnline()
  105.     {
  106.         return $this->lastOnline;
  107.     }  
  108.        
  109.     public function getId()
  110.     {
  111.         return $this->id;
  112.     }
  113.  
  114.     public function getUsername()
  115.     {
  116.         return $this->username;
  117.     }
  118.  
  119.     public function setUsername($username)
  120.     {
  121.         $this->username = $username;
  122.     }
  123.    
  124.     public function setPassword($password)
  125.     {
  126.         $salt = "";
  127.         for ($i=0;$i<4;$i++)
  128.             $salt .= chr(rand(ord('a'), ord('z')));
  129.        
  130.         $this->salt = $salt;
  131.         $this->password = md5(md5($salt).md5($password));              
  132.     }
  133.    
  134.     public function checkPassword($password)
  135.     {  
  136.         if ($this->password == md5(md5($this->salt).md5($password)))
  137.             return true;
  138.         else
  139.             return false;
  140.     }
  141.  
  142.     public function getEmail()
  143.     {
  144.         return $this->username;
  145.     }
  146.  
  147.     public function setEmail($email)
  148.     {
  149.         $this->email = $email;
  150.     }
  151.        
  152.    
  153.     public function setDetails(User\Details $details)
  154.     {
  155.         $details->setUser($this);
  156.         $this->details = $details;
  157.     }
  158.    
  159.     public function getDetails()
  160.     {
  161.         return $this->details;
  162.     }
  163.    
  164.     public function setRole(Role $role)
  165.     {
  166.         $this->role = $role;
  167.     }
  168.    
  169.     public function getRole()
  170.     {
  171.         return $this->role;
  172.     }
  173.    
  174.     public function getViewerPictures()
  175.     {
  176.         return $this->viewerPictures;
  177.     }
  178.    
  179.     /**
  180.      *
  181.      * @param string $username
  182.      * @param string $password
  183.      * @throws Exception
  184.      * @return User
  185.      */
  186.    
  187.     public static function authenticate($username, $password)
  188.     {
  189.         $em = \Zend_Registry::get('Doctrine_EntityManager');
  190.         $user = $em->getRepository('Models\User')->findOneBy(array('username' => $username));
  191.         if ($user)
  192.         {
  193.             if ($user->checkPassword($password))
  194.                 return $user->getId();
  195.                
  196.             throw new \Exception(self::CREDENTIAL_INVALID);
  197.         }
  198.        
  199.         throw new \Exception(self::NOT_FOUND);
  200.                
  201.     }
  202.    
  203.    
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement