Skip to content

Instantly share code, notes, and snippets.

@basz

basz/Identity Secret

Created September 26, 2016 11:58
Show Gist options
  • Save basz/898f6981ac12a6a43c3edc19d607a29c to your computer and use it in GitHub Desktop.
Save basz/898f6981ac12a6a43c3edc19d607a29c to your computer and use it in GitHub Desktop.
<?php
declare(strict_types = 1);
/**
* Project 'Healthy Feet' by Podolab Hoeksche Waard
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://plhw.nl/
* @copyright Copyright (c) 2010 - 2016 bushbaby multimedia. (https://bushbaby.nl)
* @author Bas Kamer <basatbushbabydotnl>
* @license Proprietary License
*/
namespace HF\Model\Entity\Authentication;
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use ZfcRbac\Identity\IdentityInterface;
use ZfrOAuth2\Server\Model\TokenOwnerInterface;
/**
* Identity
*/
class Identity implements TokenOwnerInterface, IdentityInterface
{
/**
* @var string
*/
private $userId;
/**
* @var string
*/
private $emailAddress;
/**
* @var string
*/
private $passwordHash;
/**
* @var string
*/
private $displayName;
/**
* @var DateTime
*/
private $registrationDate;
/**
* @var string
*/
private $emailStatus;
/**
* @var string
*/
private $accountStatus;
/**
* @var string[]
*/
private $roles;
/**
* @var Collection
*/
private $preferences;
/**
* Constructor
*/
public function __construct()
{
$this->roles = [];
$this->preferences = new ArrayCollection();
}
/**
* @return string
*/
public function getUserId()
{
return $this->userId;
}
/**
* @param string $userId
*/
public function setUserId(string $userId)
{
$this->userId = $userId;
}
/**
* @return string
*/
public function getEmailAddress()
{
return $this->emailAddress;
}
/**
* @param string $emailAddress
*/
public function setEmailAddress(string $emailAddress)
{
$this->emailAddress = $emailAddress;
}
/**
* @return string
*/
public function getPasswordHash()
{
return $this->passwordHash;
}
/**
* @param string $passwordHash
*/
public function setPasswordHash($passwordHash)
{
$this->passwordHash = $passwordHash;
}
/**
* @return string
*/
public function getDisplayName()
{
return $this->displayName;
}
/**
* @param string $displayName
*/
public function setDisplayName(string $displayName)
{
$this->displayName = $displayName;
}
/**
* @return DateTimeInterface
*/
public function getRegistrationDate(): DateTimeInterface
{
return $this->registrationDate;
}
/**
* @param DateTimeInterface $registrationDate
*/
public function setRegistrationDate(DateTimeInterface $registrationDate)
{
// Convert DateTimeImmutable to DateTime
$registrationDate = DateTime::createFromFormat(
DateTime::ISO8601,
$registrationDate->format(DateTime::ISO8601),
$registrationDate->getTimezone()
);
$this->registrationDate = $registrationDate;
}
/**
* @return string
*/
public function getEmailStatus()
{
return $this->emailStatus;
}
/**
* @param string $emailStatus
*/
public function setEmailStatus(string $emailStatus)
{
$this->emailStatus = $emailStatus;
}
/**
* @return string
*/
public function getAccountStatus()
{
return $this->accountStatus;
}
/**
* @param string $accountStatus
*/
public function setAccountStatus(string $accountStatus)
{
$this->accountStatus = $accountStatus;
}
/**
* Add role
*
* @param string $role
*
* @return Identity
*/
public function addRole(string $role)
{
if (!in_array($role, $this->roles, true)) {
$this->roles[] = $role;
}
return $this;
}
/**
* Get roles
*
* @return array
*/
public function getRoles(): array
{
return $this->roles;
}
/**
* @return Collection
*/
public function getPreferences(): Collection
{
return $this->preferences;
}
/**
* @param Preference $preference
*/
public function addPreference(Preference $preference)
{
if ($this->preferences->contains($preference)) {
return;
}
$this->preferences->add($preference);
$preference->setIdentity($this);
}
/**
* @param Preference $preference
*/
public function removePreference(Preference $preference)
{
if (!$this->preferences->contains($preference)) {
return;
}
$this->preferences->removeElement($preference);
$preference->setIdentity(null);
}
/**
* Get the id of the token owner
*
* @return mixed
*/
public function getTokenOwnerId(): string
{
return $this->getUserId();
}
}
<?php
namespace HF\Api\Infrastructure\Api\Mappings\Identity;
use HF\Model\Entity\Authentication\Identity;
use NilPortugues\Api\Mappings\JsonApiMapping;
class IdentityMapping implements JsonApiMapping
{
/**
* {@inhertidoc}
*/
public function getClass()
{
return Identity::class;
}
/**
* {@inheritdoc}
*/
public function getAlias()
{
return 'identity/user';
}
/**
* {@inheritdoc}
*/
public function getIdProperties()
{
return ['userId'];
}
/**
* {@inheritdoc}
*/
public function getAliasedProperties()
{
return ['userId'=>'id'];
}
/**
* {@inheritdoc}
*/
public function getHideProperties()
{
return ['passwordHash'];
}
/**
* {@inheritdoc}
*/
public function getUrls()
{
return [
'self' => 'http://example.com',
];
}
/**
* {@inheritdoc}
*/
public function getRelationships()
{
return ['preferences'];
}
/**
* {@inheritdoc}
*/
public function getRequiredProperties()
{
return [];
}
}
<?php
declare(strict_types = 1);
/**
* Project 'Healthy Feet' by Podolab Hoeksche Waard
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://plhw.nl/
* @copyright Copyright (c) 2010 - 2016 bushbaby multimedia. (https://bushbaby.nl)
* @author Bas Kamer <basatbushbabydotnl>
* @license Proprietary License
*/
namespace HF\Model\Entity\Authentication;
/**
* Preference
*/
class Preference
{
/**
* @var string
*/
private $preferenceId;
/**
* @var Identity
*/
private $identity;
/**
* @var string
*/
private $name;
/**
* @var mixed
*/
private $value;
/**
* @param string $preferenceId
*/
public function setPreferenceId(string $preferenceId)
{
$this->preferenceId = $preferenceId;
return $this;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
*/
public function setName(string $name)
{
$this->name = $name;
return $this;
}
/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}
/**
* @param mixed $value
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* @param Identity $identity
* @return $this
*/
public function setIdentity(Identity $identity = null)
{
if ($identity) {
$this->identity = $identity;
$identity->addPreference($this);
} else {
$this->identity = null;
$identity->removePreference($this);
}
return $this;
}
}
<?php
namespace HF\Api\Infrastructure\Api\Mappings\Identity;
use HF\Model\Entity\Authentication\Preference;
use NilPortugues\Api\Mappings\JsonApiMapping;
class PreferenceMapping implements JsonApiMapping
{
/**
* {@inhertidoc}
*/
public function getClass()
{
return Preference::class;
}
/**
* {@inheritdoc}
*/
public function getAlias()
{
return 'identity/preference';
}
/**
* {@inheritdoc}
*/
public function getIdProperties()
{
return ['preferenceId'];
}
/**
* {@inheritdoc}
*/
public function getAliasedProperties()
{
return ['preferenceId' => 'id'];
}
/**
* {@inheritdoc}
*/
public function getHideProperties()
{
return [];
}
/**
* {@inheritdoc}
*/
public function getUrls()
{
return [
'self' => 'http://example.com',
];
}
/**
* {@inheritdoc}
*/
public function getRelationships()
{
return [];
}
/**
* {@inheritdoc}
*/
public function getRequiredProperties()
{
return [];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment