API Add role-helpers for templates

This commit is contained in:
Damian Mooyman 2015-04-09 15:45:12 +12:00
parent 32c18515c1
commit 0dd2d2504b
3 changed files with 50 additions and 0 deletions

View File

@ -154,6 +154,25 @@ class Blog extends Page implements PermissionProvider {
return $isContributor;
}
/**
* Determine the role of the given member
* Call be called via template to determine the current user
*
* E.g. `Hello $RoleOf($CurrentMember.ID)`
*
* @param Member|integer $member
* @return string|null Author, Editor, Writer, Contributor, or null if no role
*/
public function RoleOf($member) {
if(is_numeric($member)) $member = DataObject::get_by_id('Member', $member);
if(!$member) return null;
// Check each role
if($this->isEditor($member)) return _t('Blog.EDITOR', 'Editor');
if($this->isWriter($member)) return _t('Blog.WRITER', 'Writer');
if($this->isContributor($member)) return _t('Blog.CONTRIBUTOR', 'Contributor');
}
/**
* Determine if the given member belongs to the given subrelation
*

View File

@ -83,6 +83,27 @@ class BlogPost extends Page {
return $list->byID($member->ID) !== null;
}
/**
* Determine the role of the given member
* Call be called via template to determine the current user
*
* E.g. `Hello $RoleOf($CurrentMember.ID)`
*
* @param Member|integer $member
* @return string|null Author, Editor, Writer, Contributor, or null if no role
*/
public function RoleOf($member) {
if(is_numeric($member)) $member = DataObject::get_by_id('Member', $member);
if(!$member) return null;
// Check if this member is an author
if($this->isAuthor($member)) return _t("BlogPost.AUTHOR", "Author");
// Check parent role
$parent = $this->Parent();
if($parent instanceof Blog) return $parent->RoleOf($member);
}
public function getCMSFields() {
Requirements::css(BLOGGER_DIR . '/css/cms.css');

View File

@ -109,6 +109,16 @@ class BlogTest extends SapphireTest {
$contributor = $this->objFromFixture('Member', 'contributor');
$visitor = $this->objFromFixture('Member', 'visitor');
// Check roleof
$this->assertEquals('Editor', $blog->RoleOf($editor));
$this->assertEquals('Contributor', $blog->RoleOf($contributor));
$this->assertEquals('Writer', $blog->RoleOf($writer));
$this->assertEmpty($blog->RoleOf($visitor));
$this->assertEquals('Author', $postA->RoleOf($writer));
$this->assertEquals('Author', $postA->RoleOf($contributor));
$this->assertEquals('Editor', $postA->RoleOf($editor));
$this->assertEmpty($postA->RoleOf($visitor));
// Check that editors have all permissions on their own blog
$this->assertTrue($blog->canEdit($editor));
$this->assertFalse($blog2->canEdit($editor));