partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

User.php (8621B)


      1 <?php
      2 
      3 namespace PartKeepr\AuthBundle\Entity;
      4 
      5 use Doctrine\ORM\Mapping as ORM;
      6 use PartKeepr\CoreBundle\Entity\BaseEntity;
      7 use PartKeepr\DoctrineReflectionBundle\Annotation\TargetService;
      8 use PartKeepr\DoctrineReflectionBundle\Annotation\VirtualField;
      9 use Symfony\Component\Security\Core\User\EquatableInterface;
     10 use Symfony\Component\Security\Core\User\UserInterface;
     11 use Symfony\Component\Serializer\Annotation\Groups;
     12 use Symfony\Component\Validator\Constraints as Assert;
     13 
     14 /**
     15  * @ORM\Entity
     16  * @ORM\Table(
     17  *      name="PartKeeprUser",
     18  *      uniqueConstraints={@ORM\UniqueConstraint(name="username_provider", columns={"username", "provider_id"})})
     19  * @TargetService(uri="/api/users")
     20  */
     21 class User extends BaseEntity implements UserInterface, EquatableInterface
     22 {
     23     /**
     24      * @ORM\Column(length=50)
     25      * @Groups({"default"})
     26      */
     27     private $username;
     28 
     29     /**
     30      * @Groups({"default"})
     31      * @ORM\Column(length=32,nullable=true)
     32      */
     33     private $password;
     34 
     35     /**
     36      * @Groups({"default"})
     37      * @VirtualField(type="string")
     38      *
     39      * @var string
     40      */
     41     private $newPassword;
     42 
     43     /**
     44      * @Assert\Email()
     45      * @Groups({"default"})
     46      * @ORM\Column(length=255,nullable=true)
     47      *
     48      * @var string
     49      */
     50     private $email;
     51 
     52     /**
     53      * @ORM\Column(type="boolean")
     54      */
     55     private $admin;
     56 
     57     /**
     58      * Marks a user as a legacy user (=old md5 auth).
     59      *
     60      * @ORM\Column(type="boolean")
     61      * @Groups({"default"})
     62      *
     63      * @var bool
     64      */
     65     private $legacy;
     66 
     67     /**
     68      * @ORM\Column(type="datetime",nullable=true)
     69      */
     70     private $lastSeen;
     71 
     72     /**
     73      * @ORM\ManyToOne(targetEntity="PartKeepr\AuthBundle\Entity\UserProvider")
     74      * @Groups({"default"})
     75      */
     76     private $provider;
     77 
     78     /**
     79      * @ORM\OneToMany(targetEntity="PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDayHistory", mappedBy="user", cascade={"remove"}, orphanRemoval=true)
     80      */
     81     private $tipHistories;
     82 
     83     /**
     84      * Holds the initial serialized user preferences.
     85      *
     86      * @VirtualField(type="string")
     87      * @Groups({"default"})
     88      *
     89      * @var string
     90      */
     91     private $initialUserPreferences;
     92 
     93     /**
     94      * Defines if the user is active.
     95      *
     96      * @ORM\Column(type="boolean")
     97      * @Groups({"default"})
     98      *
     99      * @var bool
    100      */
    101     private $active;
    102 
    103     /**
    104      * Defines if the user is protected. Protection defines that the user may not be changed or removed.
    105      *
    106      * @ORM\Column(type="boolean")
    107      * @Groups({"default"})
    108      *
    109      * @var bool
    110      */
    111     private $protected;
    112 
    113     /**
    114      * Creates a new user object.
    115      *
    116      * @param string       $username The username to set (optional)
    117      * @param UserProvider $provider The authentification provider
    118      *
    119      * @throws \Exception
    120      */
    121     public function __construct($username = null, $provider = null)
    122     {
    123         if ($username !== null) {
    124             $this->setUsername($username);
    125         }
    126 
    127         if ($provider !== null) {
    128             $this->setProvider($provider);
    129         }
    130 
    131         $this->setAdmin(false);
    132         $this->setActive(true);
    133         $this->setProtected(false);
    134     }
    135 
    136     /**
    137      * Sets the admin flag.
    138      *
    139      * @param bool $bAdmin True if the user is an admin, false otherwise
    140      */
    141     public function setAdmin($bAdmin)
    142     {
    143         $this->admin = (bool) $bAdmin;
    144     }
    145 
    146     /**
    147      * @return bool
    148      */
    149     public function isProtected()
    150     {
    151         return $this->protected;
    152     }
    153 
    154     /**
    155      * @param bool $protected
    156      */
    157     public function setProtected($protected)
    158     {
    159         $this->protected = $protected;
    160     }
    161 
    162     /**
    163      * Returns if the user is active.
    164      *
    165      * @return bool
    166      */
    167     public function isActive()
    168     {
    169         return $this->active;
    170     }
    171 
    172     /**
    173      * Sets if the user is active.
    174      *
    175      * @param bool $active
    176      */
    177     public function setActive($active)
    178     {
    179         $this->active = $active;
    180     }
    181 
    182     /**
    183      * @return string
    184      */
    185     public function getInitialUserPreferences()
    186     {
    187         return $this->initialUserPreferences;
    188     }
    189 
    190     /**
    191      * @param string $initialUserPreferences
    192      */
    193     public function setInitialUserPreferences($initialUserPreferences)
    194     {
    195         $this->initialUserPreferences = $initialUserPreferences;
    196     }
    197 
    198     /**
    199      * @return mixed
    200      */
    201     public function getTipHistories()
    202     {
    203         return $this->tipHistories;
    204     }
    205 
    206     /**
    207      * @param mixed $tipHistories
    208      */
    209     public function setTipHistories($tipHistories)
    210     {
    211         $this->tipHistories = $tipHistories;
    212     }
    213 
    214     /**
    215      * @return string
    216      */
    217     public function getEmail()
    218     {
    219         return $this->email;
    220     }
    221 
    222     /**
    223      * @param string $email
    224      */
    225     public function setEmail($email)
    226     {
    227         $this->email = $email;
    228     }
    229 
    230     /**
    231      * Returns the authentification provider.
    232      *
    233      * @return UserProvider
    234      */
    235     public function getProvider()
    236     {
    237         return $this->provider;
    238     }
    239 
    240     /**
    241      * Sets the authentification provider.
    242      *
    243      * @param UserProvider $provider
    244      */
    245     public function setProvider(UserProvider $provider)
    246     {
    247         $this->provider = $provider;
    248     }
    249 
    250     /**
    251      * Sets the raw username, without replacing any special chars.
    252      *
    253      * This method should only be used for building a temporary user
    254      * for login checks.
    255      *
    256      * @param string $username The raw username
    257      */
    258     public function setRawUsername($username)
    259     {
    260         $this->username = $username;
    261     }
    262 
    263     /**
    264      * Returns the admin flag.
    265      *
    266      * @return bool True if the user is an admin
    267      */
    268     public function isAdmin()
    269     {
    270         return $this->admin;
    271     }
    272 
    273     /**
    274      * @return string
    275      */
    276     public function getPassword()
    277     {
    278         return $this->password;
    279     }
    280 
    281     /**
    282      * Sets the user's password.
    283      *
    284      * @param string $password
    285      */
    286     public function setPassword($password)
    287     {
    288         $this->password = $password;
    289     }
    290 
    291     public function getNewPassword()
    292     {
    293         return $this->newPassword;
    294     }
    295 
    296     /**
    297      * Sets the new password. Used for password changes.
    298      *
    299      * @param string $password The new password
    300      */
    301     public function setNewPassword($password)
    302     {
    303         $this->newPassword = $password;
    304     }
    305 
    306     /**
    307      * Compares the given un-hashed password with the
    308      * object's hashed password.
    309      *
    310      *
    311      * @param string $password The unhashed password
    312      *
    313      * @return bool true if the passwords match, false otherwise
    314      */
    315     public function comparePassword($password)
    316     {
    317         return $this->compareHashedPassword(md5($password));
    318     }
    319 
    320     /**
    321      * Compares the given hashed password with the object's
    322      * hashed password.
    323      *
    324      * @param string $hashedPassword The md5-hashed password
    325      *
    326      * @return bool true if the passwords match, false otherwise
    327      */
    328     public function compareHashedPassword($hashedPassword)
    329     {
    330         if ($hashedPassword == $this->password) {
    331             return true;
    332         } else {
    333             return false;
    334         }
    335     }
    336 
    337     /**
    338      * Updates the last seen field to the current time.
    339      */
    340     public function updateSeen()
    341     {
    342         $this->lastSeen = new \DateTime('now');
    343     }
    344 
    345     /**
    346      * Returns if the user is a legacy user.
    347      *
    348      * @return bool
    349      */
    350     public function isLegacy()
    351     {
    352         return $this->legacy;
    353     }
    354 
    355     /**
    356      * Marks a user as a legacy user.
    357      *
    358      * @param bool|true $legacy
    359      */
    360     public function setLegacy($legacy = true)
    361     {
    362         $this->legacy = $legacy;
    363     }
    364 
    365     /**
    366      * Retrieve the last seen flag for a user.
    367      *
    368      * @return \DateTime
    369      */
    370     public function getLastSeen()
    371     {
    372         return $this->lastSeen;
    373     }
    374 
    375     public function getRoles()
    376     {
    377         return [];
    378     }
    379 
    380     public function getSalt()
    381     {
    382         return '';
    383     }
    384 
    385     public function eraseCredentials()
    386     {
    387     }
    388 
    389     public function isEqualTo(UserInterface $user)
    390     {
    391         if (!$user instanceof self) {
    392             return false;
    393         }
    394 
    395         if ($this->getUsername() != $user->getUsername()) {
    396             return false;
    397         }
    398 
    399         return true;
    400     }
    401 
    402     /**
    403      * Returns the username.
    404      *
    405      * @return string The username
    406      */
    407     public function getUsername()
    408     {
    409         return $this->username;
    410     }
    411 
    412     /**
    413      * Sets the username.
    414      *
    415      * @param string $username The username to set.
    416      */
    417     public function setUsername($username)
    418     {
    419         $this->username = $username;
    420     }
    421 }