How to set permissions in 5.6 from code, or launch the dialog window for permissions from a modal?

Permalink 2 users found helpful
I'm trying to update our Hutman News add on for 5.6.0, and wondering how to set the permissions. Right now we have a drop down that is kind of a hold over from when this was ported from our in-house CMS. It has public / private / disabled options which set different permissions:

Public sets it to readable by guest and registered
Private = only administrators
Disabled does hide from page list and hide from autonav

I'm using code like this to set the permissions:

$pxml->guests['canRead'] = false;
$pxml->registered['canRead'] = false;
$pxml->group[0]['gID'] = ADMIN_GROUP_ID;
$pxml->group[0]['canRead'] = true;
$pxml->group[0]['canWrite'] = true;
$pxml->group[0]['canApproveVersions'] = true;
$pxml->group[0]['canReadVersions'] = true;
$pxml->group[0]['canDelete'] = true;
$pxml->group[0]['canAdmin'] = true;
$newsPage->assignPermissionSet($pxml);


Obviously, this doesn't work in 5.6.0 because the permissions have been completely changed.

I'm wondering if anyone can help me to figure out how to add a link in the edit modal that would launch the page permissions dialog like what happens when you click the permissions link for the page in the site map? It seems like that would be the best way to go about managing permissions on the news pages but I have no idea how exactly to get that to work.

Alternatively if there's a way to just assign these permissions from code I'd like to know how to do that, too, but I'd rather do it the other way.

Thanks for any insight!

hutman
View Replies:
hutman replied on at Permalink Reply
hutman
Bump - does anyone know how to do this?

Another thing I was thinking about was the fact that when we 'add' a page instead of editing it, the page doesn't actually exist when we display the edit form. I'm not sure if opening the popup would work at that point - we may need to add the page to a temporary place when the dialog first opens up, then move it to the correct location after the content is populated. Will this affect the page permissions at all?

Thanks for any help, I'd really prefer to keep the ability to set page permissions right there in the edit form, rather than having to set them from the actual page or the site map.
brianvandelden replied on at Permalink Reply
brianvandelden
Here is an example, where $p is a Page Object.
$u = new User();
            /* GUEST (GUEST_GROUP_ID) PERMISSIONS */
            $pkHandles = array('view_page');
            $p->assignPermissions(Group::getByID(GUEST_GROUP_ID), $pkHandles);
            /* USER (REGISTERED_GROUP_ID) PERMISSIONS */
            $pkHandles = array('view_page');
            $pkHandles[] = 'edit_page_properties';
            $pkHandles[] = 'edit_page_contents';
            $pkHandles[] = 'approve_page_versions';
            $pkHandles[] = 'delete_page';
            $pkHandles[] = 'delete_page_versions';
            $p->assignPermissions(Group::getByID(REGISTERED_GROUP_ID), $pkHandles);
            /* ADMIN (3) PERMISSIONS */
            $pkHandles = array('view_page');
            $pkHandles[] = 'view_page_versions';


This are all the options
$pkHandles = array();
      if ($node['canRead'] == '1') {
         $pkHandles[] = 'view_page';
      }
      if ($node['canWrite'] == '1') {
         $pkHandles[] = 'view_page_versions';
         $pkHandles[] = 'edit_page_properties';
         $pkHandles[] = 'edit_page_contents';
         $pkHandles[] = 'approve_page_versions';
         $pkHandles[] = 'move_or_copy_page';
         $pkHandles[] = 'preview_page_as_user';
         $pkHandles[] = 'add_subpage';
      }
      if ($node['canAdmin'] == '1') {
         $pkHandles[] = 'edit_page_speed_settings';
.
hutman replied on at Permalink Reply
hutman
Finally had a chance to try this out, seems to work for actually assigning permissions like to read for guests and registered users. The thing I don't get is how to _remove_ those permissions after they are set. I tried passing in an empty array, but that didn't seem to work.
hutman replied on at Permalink Reply
hutman
Nevermind, found it:

$newsPage->assignPermissions(Group::getByID(GUEST_GROUP_ID), $pkHandles, -1);


You have to pass in -1 to make it the 'exclude' permission type.
glockops replied on at Permalink Reply
glockops
Word of warning, if the page object (in this case $newsPage) is not an actual object, then this command will apply to the home page (cID=1) of the site.

I just spent 15 minutes trying to figure out why the entire website disappeared, and it's because I set Guest view permissions to exclude for the homepage - all because I had a typo when assigning permissions to a single page via controller. Yay for development environments!
glockops replied on at Permalink Reply
glockops
You can remove all inherited permissions using the following
// In package controller install for single page
$manage = SinglePage::add('/manage',$pkg);
// ...
$manage->setPermissionsToManualOverride();
$manage->clearPagePermissions();

Then you can add the groups and exactly what they have access to do. This prevents you from needing to exclude guests.
$manage->assignPermissions(Group::getbyName('Administrators'), array('view_page'));
hereNT replied on at Permalink Reply
hereNT
I found one thing that can be a gotcha on doing this.

If you set view_page as excluded for guest or registered, but enabled for another group, the other group can't see it.

If you just don't specify that guest or registered _can_ see it, then it works. But if they are explicitly excluded, then only the super admin can view.