Add ACL & helper methods

This commit is contained in:
Nicolas Le Goff
2013-08-25 00:09:28 +02:00
parent 7909bc5688
commit 78ee48803c
2 changed files with 91 additions and 0 deletions

View File

@@ -218,6 +218,11 @@ class User
*/
private $updated;
/**
* @var \ACL
*/
private $acl;
/**
* @return integer
*/
@@ -792,4 +797,54 @@ class User
$this->updated = $updated;
}
/**
* @param Application $app
*
* @return \ACL
*/
public function ACL(Application $app)
{
if (!$this->acl instanceof \ACL) {
$this->acl = new \ACL($this, $app);
}
return $this->acl;
}
/**
* @return boolean
*/
public function isTemplate()
{
return null !== $this->modelOf;
}
/**
* @return boolean
*/
public function isSpecial()
{
return in_array($this->login, array('invite', 'autoregister'));
}
/**
* @return string
*/
public function getDisplayName()
{
if ($this->isTemplate()) {
return sprintf(_('modele %s'), $this->getLogin());
}
if (trim($this->lastName) !== '' || trim($this->firstName) !== '') {
return $this->firstName . ('' !== $this->firstName && '' !== $this->lastName ? ' ' : '') . $this->lastName;
}
if (trim($this->email) !== '') {
return $this->email;
}
return _('Unnamed user');
}
}

View File

@@ -81,6 +81,42 @@ class UserTest extends \PHPUnit_Framework_TestCase
$this->user->setGeonameId(-1);
}
public function testGetDisplayName()
{
$this->user->setLogin('login');
$this->user->setFirstName('firstname');
$this->user->setLastName('lastname');
$this->user->setEmail('email@email.com');
$this->assertEquals($this->user->getDisplayName(), 'firstname lastname');
$this->user->setLastName('');
$this->assertEquals($this->user->getDisplayName(), 'firstname');
$this->user->setFirstName('');
$this->assertEquals($this->user->getDisplayName(), 'email@email.com');
$this->user->setEmail(null);
$this->assertEquals($this->user->getDisplayName(), 'Unnamed user');
$this->user->setLastName('lastname');
$this->assertEquals($this->user->getDisplayName(), 'lastname');
}
public function testIsTemplate()
{
$this->assertFalse($this->user->isTemplate());
$this->user->setModelOf(1);
$this->assertTrue($this->user->isTemplate());
}
public function testIsSpecial()
{
$this->user->setLogin('login');
$this->assertFalse($this->user->isSpecial());
$this->user->setLogin('invite');
$this->assertTrue($this->user->isSpecial());
$this->user->setLogin('login');
$this->assertFalse($this->user->isSpecial());
$this->user->setLogin('autoregister');
$this->assertTrue($this->user->isSpecial());
}
public function genderProvider()
{
return array(