public function getCMSFields()
 {
     $fields = parent::getCMSFields();
     $fields->removeFieldFromTab('Root', 'Codes');
     $fields->removeFieldFromTab('Root', 'Groups');
     $fields->addFieldToTab('Root.Main', $permissionField = new PermissionCheckboxSetField('Codes', Permission::singleton()->i18n_plural_name(), 'SilverStripe\\Security\\PermissionRoleCode', 'RoleID'));
     $permissionField->setHiddenPermissions(Permission::config()->hidden_permissions);
     return $fields;
 }
 public function testSaveInto()
 {
     $group = $this->objFromFixture('SilverStripe\\Security\\Group', 'group');
     // tested group
     $untouchable = $this->objFromFixture('SilverStripe\\Security\\Group', 'untouchable');
     // group that should not change
     $field = new PermissionCheckboxSetField('Permissions', 'Permissions', 'SilverStripe\\Security\\Permission', 'GroupID', $group);
     // get the number of permissions before we start
     $baseCount = DataObject::get('SilverStripe\\Security\\Permission')->Count();
     // there are currently no permissions, save empty checkbox
     $field->saveInto($group);
     $group->flushCache();
     $untouchable->flushCache();
     $this->assertEquals($group->Permissions()->Count(), 0, 'The tested group has no permissions');
     $this->assertEquals($untouchable->Permissions()->Count(), 1, 'The other group has one permission');
     $this->assertEquals($untouchable->Permissions()->where("\"Code\"='ADMIN'")->Count(), 1, 'The other group has ADMIN permission');
     $this->assertEquals(DataObject::get('SilverStripe\\Security\\Permission')->Count(), $baseCount, 'There are no orphaned permissions');
     // add some permissions
     $field->setValue(array('ADMIN' => true, 'NON-ADMIN' => true));
     $field->saveInto($group);
     $group->flushCache();
     $untouchable->flushCache();
     $this->assertEquals($group->Permissions()->Count(), 2, 'The tested group has two permissions permission');
     $this->assertEquals($group->Permissions()->where("\"Code\"='ADMIN'")->Count(), 1, 'The tested group has ADMIN permission');
     $this->assertEquals($group->Permissions()->where("\"Code\"='NON-ADMIN'")->Count(), 1, 'The tested group has CMS_ACCESS_AssetAdmin permission');
     $this->assertEquals($untouchable->Permissions()->Count(), 1, 'The other group has one permission');
     $this->assertEquals($untouchable->Permissions()->where("\"Code\"='ADMIN'")->Count(), 1, 'The other group has ADMIN permission');
     $this->assertEquals(DataObject::get('SilverStripe\\Security\\Permission')->Count(), $baseCount + 2, 'There are no orphaned permissions');
     // remove permission
     $field->setValue(array('ADMIN' => true));
     $field->saveInto($group);
     $group->flushCache();
     $untouchable->flushCache();
     $this->assertEquals($group->Permissions()->Count(), 1, 'The tested group has 1 permission');
     $this->assertEquals($group->Permissions()->where("\"Code\"='ADMIN'")->Count(), 1, 'The tested group has ADMIN permission');
     $this->assertEquals($untouchable->Permissions()->Count(), 1, 'The other group has one permission');
     $this->assertEquals($untouchable->Permissions()->where("\"Code\"='ADMIN'")->Count(), 1, 'The other group has ADMIN permission');
     $this->assertEquals(DataObject::get('SilverStripe\\Security\\Permission')->Count(), $baseCount + 1, 'There are no orphaned permissions');
 }
 public function testHiddenPermissions()
 {
     $permissionCheckboxSet = new PermissionCheckboxSetField('Permissions', 'Permissions', 'SilverStripe\\Security\\Permission', 'GroupID');
     $this->assertContains('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field());
     Config::inst()->update('SilverStripe\\Security\\Permission', 'hidden_permissions', array('CMS_ACCESS_LeftAndMain'));
     $this->assertNotContains('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field());
     Config::inst()->remove('SilverStripe\\Security\\Permission', 'hidden_permissions');
     $this->assertContains('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field());
 }
 /**
  * Caution: Only call on instances, not through a singleton.
  * The "root group" fields will be created through {@link SecurityAdmin->EditForm()}.
  *
  * @return FieldList
  */
 public function getCMSFields()
 {
     $fields = new FieldList(new TabSet("Root", new Tab('Members', _t('SecurityAdmin.MEMBERS', 'Members'), new TextField("Title", $this->fieldLabel('Title')), $parentidfield = DropdownField::create('ParentID', $this->fieldLabel('Parent'), Group::get()->exclude('ID', $this->ID)->map('ID', 'Breadcrumbs'))->setEmptyString(' '), new TextareaField('Description', $this->fieldLabel('Description'))), $permissionsTab = new Tab('Permissions', _t('SecurityAdmin.PERMISSIONS', 'Permissions'), $permissionsField = new PermissionCheckboxSetField('Permissions', false, 'SilverStripe\\Security\\Permission', 'GroupID', $this))));
     $parentidfield->setDescription(_t('Group.GroupReminder', 'If you choose a parent group, this group will take all it\'s roles'));
     // Filter permissions
     // TODO SecurityAdmin coupling, not easy to get to the form fields through GridFieldDetailForm
     $permissionsField->setHiddenPermissions((array) Config::inst()->get('SilverStripe\\Admin\\SecurityAdmin', 'hidden_permissions'));
     if ($this->ID) {
         $group = $this;
         $config = GridFieldConfig_RelationEditor::create();
         $config->addComponent(new GridFieldButtonRow('after'));
         $config->addComponents(new GridFieldExportButton('buttons-after-left'));
         $config->addComponents(new GridFieldPrintButton('buttons-after-left'));
         /** @var GridFieldAddExistingAutocompleter $autocompleter */
         $autocompleter = $config->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldAddExistingAutocompleter');
         /** @skipUpgrade */
         $autocompleter->setResultsFormat('$Title ($Email)')->setSearchFields(array('FirstName', 'Surname', 'Email'));
         /** @var GridFieldDetailForm $detailForm */
         $detailForm = $config->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldDetailForm');
         $detailForm->setValidator(Member_Validator::create())->setItemEditFormCallback(function ($form, $component) use($group) {
             /** @var Form $form */
             $record = $form->getRecord();
             $groupsField = $form->Fields()->dataFieldByName('DirectGroups');
             if ($groupsField) {
                 // If new records are created in a group context,
                 // set this group by default.
                 if ($record && !$record->ID) {
                     $groupsField->setValue($group->ID);
                 } elseif ($record && $record->ID) {
                     // TODO Mark disabled once chosen.js supports it
                     // $groupsField->setDisabledItems(array($group->ID));
                     $form->Fields()->replaceField('DirectGroups', $groupsField->performReadonlyTransformation());
                 }
             }
         });
         $memberList = GridField::create('Members', false, $this->DirectMembers(), $config)->addExtraClass('members_grid');
         // @todo Implement permission checking on GridField
         //$memberList->setPermissions(array('edit', 'delete', 'export', 'add', 'inlineadd'));
         $fields->addFieldToTab('Root.Members', $memberList);
     }
     // Only add a dropdown for HTML editor configurations if more than one is available.
     // Otherwise Member->getHtmlEditorConfigForCMS() will default to the 'cms' configuration.
     $editorConfigMap = HTMLEditorConfig::get_available_configs_map();
     if (count($editorConfigMap) > 1) {
         $fields->addFieldToTab('Root.Permissions', new DropdownField('HtmlEditorConfig', 'HTML Editor Configuration', $editorConfigMap), 'Permissions');
     }
     if (!Permission::check('EDIT_PERMISSIONS')) {
         $fields->removeFieldFromTab('Root', 'Permissions');
     }
     // Only show the "Roles" tab if permissions are granted to edit them,
     // and at least one role exists
     if (Permission::check('APPLY_ROLES') && DataObject::get('SilverStripe\\Security\\PermissionRole')) {
         $fields->findOrMakeTab('Root.Roles', _t('SecurityAdmin.ROLES', 'Roles'));
         $fields->addFieldToTab('Root.Roles', new LiteralField("", "<p>" . _t('SecurityAdmin.ROLESDESCRIPTION', "Roles are predefined sets of permissions, and can be assigned to groups.<br />" . "They are inherited from parent groups if required.") . '<br />' . sprintf('<a href="%s" class="add-role">%s</a>', SecurityAdmin::singleton()->Link('show/root#Root_Roles'), _t('Group.RolesAddEditLink', 'Manage roles')) . "</p>"));
         // Add roles (and disable all checkboxes for inherited roles)
         $allRoles = PermissionRole::get();
         if (!Permission::check('ADMIN')) {
             $allRoles = $allRoles->filter("OnlyAdminCanApply", 0);
         }
         if ($this->ID) {
             $groupRoles = $this->Roles();
             $inheritedRoles = new ArrayList();
             $ancestors = $this->getAncestors();
             foreach ($ancestors as $ancestor) {
                 $ancestorRoles = $ancestor->Roles();
                 if ($ancestorRoles) {
                     $inheritedRoles->merge($ancestorRoles);
                 }
             }
             $groupRoleIDs = $groupRoles->column('ID') + $inheritedRoles->column('ID');
             $inheritedRoleIDs = $inheritedRoles->column('ID');
         } else {
             $groupRoleIDs = array();
             $inheritedRoleIDs = array();
         }
         $rolesField = ListboxField::create('Roles', false, $allRoles->map()->toArray())->setDefaultItems($groupRoleIDs)->setAttribute('data-placeholder', _t('Group.AddRole', 'Add a role for this group'))->setDisabledItems($inheritedRoleIDs);
         if (!$allRoles->count()) {
             $rolesField->setAttribute('data-placeholder', _t('Group.NoRoles', 'No roles found'));
         }
         $fields->addFieldToTab('Root.Roles', $rolesField);
     }
     $fields->push($idField = new HiddenField("ID"));
     $this->extend('updateCMSFields', $fields);
     return $fields;
 }