Skip to content

Commit

Permalink
LocationCategory - permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirish committed Feb 10, 2017
1 parent 2694c42 commit a10390a
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 2 deletions.
62 changes: 61 additions & 1 deletion code/objects/LocationCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
class LocationCategory extends DataObject
{

/**
* @var array
*/
Expand All @@ -35,6 +34,7 @@ class LocationCategory extends DataObject
* @var string
*/
private static $singular_name = 'Category';

/**
* @var string
*/
Expand All @@ -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);
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
74 changes: 74 additions & 0 deletions tests/LocationCategoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* Class LocationTest
*/
class LocationCategoryTest extends SapphireTest
{

/**
* @var string
*/
protected static $fixture_file = 'locator/tests/Locator_Test.yml';

/**
*
*/
public function testGetCMSFields()
{
$object = $this->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));
}
}
4 changes: 4 additions & 0 deletions tests/Locator_Test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Member:
Surname: Location
Email: [email protected]
Groups: =>Group.locationdelete
default:
FirstName: 'Default'
Surname: 'Member'
Email: '[email protected]'
LocationCategory:
service:
Name: Service
Expand Down

0 comments on commit a10390a

Please sign in to comment.