Last active
February 8, 2017 16:01
-
-
Save LavaToaster/8f8a15c978ca350a12e5105f3de4d713 to your computer and use it in GitHub Desktop.
Example LaravelDoctrine user entity and repository. Raw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Entities; | |
use Doctrine\ORM\Mapping as ORM; | |
use Illuminate\Contracts\Auth\Authenticatable; | |
use Illuminate\Contracts\Support\Jsonable; | |
use LaravelDoctrine\Passport\Contracts; | |
use LaravelDoctrine\Passport\Traits; | |
use LaravelDoctrine\ORM\Auth\Authenticatable as AuthenticatableTrait; | |
/** | |
* @ORM\Entity(repositoryClass="App\Repositories\UserRepository") | |
* @ORM\Table(name="users") | |
* @ORM\HasLifecycleCallbacks() | |
*/ | |
class User implements Contracts\OAuthUser, Authenticatable, Jsonable, \JsonSerializable | |
{ | |
use Traits\HasApiToken, Traits\Timestamps, AuthenticatableTrait, Traits\Serializable; | |
protected $attributes = [ | |
'id', | |
'name', | |
'email' | |
]; | |
/** | |
* @ORM\Id | |
* @ORM\GeneratedValue | |
* @ORM\Column(type="integer") | |
* | |
* @var int | |
*/ | |
protected $id; | |
/** | |
* @ORM\Column(type="string") | |
* | |
* @var string | |
*/ | |
protected $name; | |
/** | |
* @ORM\Column(type="string") | |
* | |
* @var string | |
*/ | |
protected $email; | |
/** | |
* @ORM\Column(type="string") | |
* | |
* @var string | |
*/ | |
protected $password; | |
public function __constructor() | |
{ | |
$this->bootHasApiToken(); | |
} | |
/** | |
* @return int | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* @param int $id | |
*/ | |
public function setId($id) | |
{ | |
$this->id = $id; | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return $this->name; | |
} | |
/** | |
* @param string $name | |
*/ | |
public function setName($name) | |
{ | |
$this->name = $name; | |
} | |
/** | |
* @return string | |
*/ | |
public function getEmail() | |
{ | |
return $this->email; | |
} | |
/** | |
* @param string $email | |
*/ | |
public function setEmail($email) | |
{ | |
$this->email = $email; | |
} | |
/** | |
* @return string | |
*/ | |
public function getPassword() | |
{ | |
return $this->password; | |
} | |
/** | |
* @param string $password | |
*/ | |
public function setPassword($password) | |
{ | |
$this->password = bcrypt($password); | |
} | |
/** | |
* Validates the given password against the user | |
* | |
* @param string $password | |
* @return bool | |
*/ | |
public function validateForPassportPasswordGrant($password) | |
{ | |
return \Hash::check($password, $this->getPassword()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Repositories; | |
use Doctrine\ORM\EntityRepository; | |
use LaravelDoctrine\Passport\Contracts\OAuthUser; | |
use LaravelDoctrine\Passport\Contracts\OAuthUserRepository; | |
class UserRepository extends EntityRepository implements OAuthUserRepository | |
{ | |
/** | |
* Find the a user based on the given identifier. | |
* | |
* @param string $identifier | |
* @return OAuthUser | |
*/ | |
public function findForPassport($identifier) | |
{ | |
return $this->findOneBy(['email' => $identifier]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment