diff --git a/lib/Doctrine/Entities/User.php b/lib/Doctrine/Entities/User.php index bcd95b2cc1..24ab40e56a 100644 --- a/lib/Doctrine/Entities/User.php +++ b/lib/Doctrine/Entities/User.php @@ -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'); + } } diff --git a/tests/Doctrine/Tests/Entities/UserTest.php b/tests/Doctrine/Tests/Entities/UserTest.php index f71d356da3..1016453696 100644 --- a/tests/Doctrine/Tests/Entities/UserTest.php +++ b/tests/Doctrine/Tests/Entities/UserTest.php @@ -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(