Skip to content

Instantly share code, notes, and snippets.

@LavaToaster
Last active February 8, 2017 16:01
Show Gist options
  • Save LavaToaster/8f8a15c978ca350a12e5105f3de4d713 to your computer and use it in GitHub Desktop.
Save LavaToaster/8f8a15c978ca350a12e5105f3de4d713 to your computer and use it in GitHub Desktop.
Example LaravelDoctrine user entity and repository. Raw
<?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());
}
}
<?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