SSbits - Home page
Site by Carbon Crayon
Submit a Post >

Snippets - Little bits of code to make you happy

Create and use a Permission code

Have you wanted to add an extra permission code that you can add to groups and then use that to test whether a particular user has that permission? Here's how it's done. The permission code it self is added via the Controller class, then you use a permission check anywhere in your site code to check the current user. Like so:

 

class Page_Controller extends ContentController implements PermissionProvider {
	
	function providePermissions(){
		return array(
			"USER" => "Just a regular user",
			"SUPER_USER" => "A super-duper mega user"
		);
	}
}

Now, let's say we only wanted users with permission code SUPER_USER to be able to edit a particular page, we could do a Permission::check() inside the canEdit() function like so:

 

function canEdit($Member = null)
{
	return Permission::check('SUPER_USER');
}

For more info on using canEdit() and the other permission functions see this post.

Aram Balakjian avatar

Aram Balakjian

Aram is a web developer running London based agency Aab Web. He has a strong passion for developing attractive, usable sites around the SilverStripe CMS.

No one has commented on this page yet.

Post a comment ...

You cannot post comments until you have logged in. Login Here.

Advertisement

Site of the Month

Find SSbits on

Top Contributers

Rank Avatar Name
1 article image Aram Balakjian
2 article image Daniel Hensby
3 article image Marcus Dalgren
4 article image Hamish Campbell
5 article image Ty Barho
6 article image Martijn van Nieuwenhoven
7 article image Darren-Lee
8 article image Roman Schmid
9 article image Matt Clegg
10 article image dalesaurus

View full leaderboard


Advertisement