From a10390a31071f83a44dc86860705900b899ee16e Mon Sep 17 00:00:00 2001 From: Jason Irish Date: Fri, 10 Feb 2017 15:49:34 -0600 Subject: [PATCH] LocationCategory - permissions closes #133 --- code/objects/LocationCategory.php | 62 +++++++++++++++++++++++++- composer.json | 3 +- tests/LocationCategoryTest.php | 74 +++++++++++++++++++++++++++++++ tests/Locator_Test.yml | 4 ++ 4 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 tests/LocationCategoryTest.php diff --git a/code/objects/LocationCategory.php b/code/objects/LocationCategory.php index 4bc3861..3b8a840 100644 --- a/code/objects/LocationCategory.php +++ b/code/objects/LocationCategory.php @@ -9,7 +9,6 @@ */ class LocationCategory extends DataObject { - /** * @var array */ @@ -35,6 +34,7 @@ class LocationCategory extends DataObject * @var string */ private static $singular_name = 'Category'; + /** * @var string */ @@ -45,4 +45,64 @@ class LocationCategory extends DataObject */ private static $default_sort = 'Name'; + public function getCMSFields() + { + $fields = parent::getCMSFields(); + + $fields->removeByName([ + 'Locations', + ]); + + if ($this->ID) { + // Locations + $config = GridFieldConfig_RelationEditor::create(); + $config->removeComponentsByType('GridFieldAddExistingAutocompleter'); + $config->addComponent(new GridFieldAddExistingSearchButton()); + $config->removeComponentsByType('GridFieldAddNewButton'); + $locations = $this->Locations(); + $locationField = GridField::create('Locations', 'Locations', $locations, $config); + + $fields->addFieldsToTab('Root.Locations', array( + $locationField, + )); + } + + return $fields; + } + + /** + * @param null|Member $member + * @return bool + */ + public function canView($member = null) + { + return true; + } + + /** + * @param null|Member $member + * @return bool|int + */ + public function canEdit($member = null) + { + return Permission::check('Location_EDIT', 'any', $member); + } + + /** + * @param null|Member $member + * @return bool|int + */ + public function canDelete($member = null) + { + return Permission::check('Location_DELETE', 'any', $member); + } + + /** + * @param null|Member $member + * @return bool|int + */ + public function canCreate($member = null) + { + return Permission::check('Location_CREATE', 'any', $member); + } } diff --git a/composer.json b/composer.json index d0744a2..21ffc5a 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ "silverstripe/framework": "^3.4", "silverstripe/cms": "^3.4", "silverstripe-australia/addressable": "^1.1", - "muskie9/data-to-arraylist": "^1.0" + "muskie9/data-to-arraylist": "^1.0", + "silverstripe-australia/gridfieldextensions": "^1.2" }, "suggest": { "unclecheese/betterbuttons": "Adds new form actions and buttons to GridField detail form for usability enhancements.", diff --git a/tests/LocationCategoryTest.php b/tests/LocationCategoryTest.php new file mode 100644 index 0000000..90825ac --- /dev/null +++ b/tests/LocationCategoryTest.php @@ -0,0 +1,74 @@ +objFromFixture('LocationCategory', 'service'); + $fieldset = $object->getCMSFields(); + $this->assertTrue(is_a($fieldset, 'FieldList')); + } + + /** + * + */ + public function testCanView() + { + $object = $this->objFromFixture('LocationCategory', 'service'); + $this->assertTrue($object->canView()); + } + + /** + * + */ + public function testCanEdit() + { + $object = $this->objFromFixture('LocationCategory', 'service'); + + $admin = $this->objFromFixture('Member', 'locationedit'); + $this->assertTrue($object->canEdit($admin)); + + $member = $this->objFromFixture('Member', 'default'); + $this->assertFalse($object->canEdit($member)); + } + + /** + * + */ + public function testCanDelete() + { + $object = $this->objFromFixture('LocationCategory', 'service'); + + $admin = $this->objFromFixture('Member', 'locationdelete'); + $this->assertTrue($object->canDelete($admin)); + + $member = $this->objFromFixture('Member', 'default'); + $this->assertFalse($object->canDelete($member)); + } + + /** + * + */ + public function testCanCreate() + { + $object = $this->objFromFixture('LocationCategory', 'service'); + + $admin = $this->objFromFixture('Member', 'locationcreate'); + $this->assertTrue($object->canCreate($admin)); + + $member = $this->objFromFixture('Member', 'default'); + $this->assertFalse($object->canCreate($member)); + } +} diff --git a/tests/Locator_Test.yml b/tests/Locator_Test.yml index 68ca50a..5636890 100644 --- a/tests/Locator_Test.yml +++ b/tests/Locator_Test.yml @@ -34,6 +34,10 @@ Member: Surname: Location Email: delete@test.com Groups: =>Group.locationdelete + default: + FirstName: 'Default' + Surname: 'Member' + Email: 'member@test.com' LocationCategory: service: Name: Service