Jump to content

Creating and deleting permission settings


GuruMeditation
 Share

Recommended Posts

Hi,

I'm trying to work out how to create new permissions, and also to check for them before installation. I can't seem to find any info on how to do this. So far I have the following code to create a new permission:

$pm = new Permission();
$pm->name = 'new-permission';
$pm->title = 'A test permission?';
$pm = $pm->save();

Not sure if that's the right way to do it? Now to check if the permission already exists before installation, I use the following but it's returning true even when the permission doesn't exist.

if($this->permissions->get('new-permission'))
throw new WireException("The 'Permission already exists");

I know I'm probably doesn't something stupid, but I have no code to reference for this.

Link to comment
Share on other sites

I suspect that the permission returns an object (NullPage). So your if statement will be:
 

if (object) {} // returns always true


I guess that if you append ->id to your if statement, the outcome will be the integer 0 and thus false.
 

if($this->permissions->get('new-permission')->id) {
    // higher then 0, the permission (Page) does exists.
} else {
    // 0 If not exists, the ID comes from the NullPage
}

ps, I didn't checked what I typed here (not on my working machine right now)

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...