How to provide and manage your own permissions

At some point on your journey through the jungle that is SilverStripe, you will want to create permissions and assign users to them. You might have created a Page Category model admin to categorise your pages, and setup the relations between pages and their category. By default when you create a category as an admin they will not be visible to any content authors and any content authors that can access the model admin can't create any new items.

The solution to this dilemma is to create a new permission (for example PROJECT_CategoryAdmin) and then let people with that permission mange the category items. This is how to go about it:

Create the permission

You will need to tell SilverStripe you are implementing permission by changing the class declaration as follows (changes in bold):

class Category extends DataObject implements PermissionProvider

To create the new permission you will have to write a providePermissions() method for the Category class:

public function providePermissions() {
return array(
"PROJECT_CategoryAdmin" => array(
'name' => 'Administer categories',
'category' => 'PROJECT',
'help' => 'Manage categories.'
)
);
}

Finally, implement the permission by declaring the canCreate, canDelete, canEdit and canView methods to your liking:

public function canCreate($member = null) {
return Permission::check('PROJECT_CategoryAdmin');
}

public function canDelete($member = null) {
return Permission::check('PROJECT_CategoryAdmin');
}

public function canEdit($member = null) {
return Permission::check('PROJECT_CategoryAdmin');
}

public function canView($member = null) {
return Permission::check('PROJECT_CategoryAdmin') ;
}

Don't forget to create a Role and/or assign the permission to the right Group(s), to make sure the right users can manage your categories.