Skip to content

Instantly share code, notes, and snippets.

@bwaidelich
Created August 21, 2013 09:20
Show Gist options
  • Save bwaidelich/6292221 to your computer and use it in GitHub Desktop.
Save bwaidelich/6292221 to your computer and use it in GitHub Desktop.
Simple example for a custom User domain object with some convenience methods in TYPO3 Flow
<?php
namespace Some\Package\Domain\Model;
use TYPO3\Flow\Annotations as Flow;
use Doctrine\ORM\Mapping as ORM;
use TYPO3\Flow\Security\Policy\Role;
use TYPO3\Party\Domain\Model\AbstractParty;
/**
* A User
*
* @Flow\Entity
*/
class User extends AbstractParty {
/**
* @var string
*/
protected $phoneNumber = '';
/**
* @var string
* @Flow\Validate(type="NotEmpty")
*/
protected $firstName;
/**
* @var string
* @Flow\Validate(type="NotEmpty")
*/
protected $lastName;
/**
* @var string
* @Flow\Validate(type="NotEmpty")
* @Flow\Validate(type="EmailAddress")
*/
protected $emailAddress;
/**
* @var string
*/
protected $company = '';
/**
* @var \DateTime
* @ORM\Column(nullable=true)
*/
protected $lastLogin;
/**
* Expiration date of this user, updated when password changes
* @var \DateTime
* @ORM\Column(nullable=true)
*/
protected $expirationDate;
/**
* @Flow\Inject
* @var \TYPO3\Flow\Security\Cryptography\HashService
*/
protected $hashService;
/**
* @Flow\Inject
* @var \TYPO3\Flow\Security\Policy\PolicyService
*/
protected $policyService;
/**
* Constructor
*/
public function __construct() {
parent::__construct();
$account = new \TYPO3\Flow\Security\Account();
$account->setAuthenticationProviderName('DefaultProvider');
$this->addAccount($account);
}
/**
* Get the first role identifier of the user account
*
* @return string
*/
public function getRole() {
$roles = $this->getPrimaryAccount()->getRoles();
if ($roles === array()) {
return NULL;
}
return (string)reset($roles);
}
/**
* @param string $roleIdentifier
* @return void
* @throws \InvalidArgumentException
*/
public function setRole($roleIdentifier) {
if (strpos($roleIdentifier, 'Some.Package:') !== 0) {
throw new \InvalidArgumentException(sprintf('Invalid Role identifier "%s"', $roleIdentifier), 1377076692);
}
$role = $this->policyService->getRole($roleIdentifier);
$account = $this->getPrimaryAccount();
$account->setRoles(array($role));
}
/**
* @return boolean
*/
public function isAdministrator() {
return $this->getRole() === 'Some.Package:Administrator';
}
/**
* @param boolean $administrator
* @return void
*/
public function setAdministrator($administrator) {
if ($administrator) {
$this->setRole('Some.Package:Administrator');
} else {
$this->setRole('Some.Package:Member');
}
}
/**
* @param \DateTime $expirationDate
* @return void
*/
public function setExpirationDate(\DateTime $expirationDate) {
$this->expirationDate = $expirationDate;
}
/**
* @return \DateTime
*/
public function getExpirationDate() {
return $this->expirationDate;
}
/**
* @return boolean
*/
public function isExpired() {
if ($this->expirationDate === NULL) {
return FALSE;
}
return ($this->expirationDate <= new \DateTime());
}
/**
* @param string $firstName
* @return void
*/
public function setFirstName($firstName) {
$this->firstName = $firstName;
}
/**
* @return string
*/
public function getFirstName() {
return $this->firstName;
}
/**
* @param string $lastName
* @return void
*/
public function setLastName($lastName) {
$this->lastName = $lastName;
}
/**
* @return string
*/
public function getLastName() {
return $this->lastName;
}
/**
* @return string
*/
public function getName() {
return sprintf('%s %s', $this->firstName, $this->lastName);
}
/**
* @param string $emailAddress
* @return void
*/
public function setEmailAddress($emailAddress) {
$this->emailAddress = $emailAddress;
}
/**
* @return string
*/
public function getEmailAddress() {
return $this->emailAddress;
}
/**
* @param string $phoneNumber
* @return void
*/
public function setPhoneNumber($phoneNumber) {
$this->phoneNumber = $phoneNumber;
}
/**
* @return string
*/
public function getPhoneNumber() {
return $this->phoneNumber;
}
/**
* @param string $company
* @return void
*/
public function setCompany($company) {
$this->company = $company;
}
/**
* @return string
*/
public function getCompany() {
return $this->company;
}
/**
* @param \DateTime $lastLogin
* @return void
*/
public function setLastLogin(\DateTime $lastLogin) {
$this->lastLogin = $lastLogin;
}
/**
* @return \DateTime
*/
public function getLastLogin() {
return $this->lastLogin;
}
/**
* @return string
*/
public function getUsername() {
return $this->getPrimaryAccount()->getAccountIdentifier();;
}
/**
* @param string $username
* @return void
*/
public function setUsername($username) {
$this->getPrimaryAccount()->setAccountIdentifier($username);
}
/**
* @param string $password
* @return void
*/
public function setPassword($password) {
$this->getPrimaryAccount()->setCredentialsSource($this->hashService->hashPassword($password));
}
/**
* @return \TYPO3\Flow\Security\Account
*/
public function getPrimaryAccount() {
if (count($this->accounts) > 0) {
return $this->accounts->first();
}
return NULL;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment