Passed
Push — master ( 1155ca...3f5943 )
by Damian
08:13
created

GridFieldGroupDeleteAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Forms\GridField;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\ORM\ValidationException;
7
use SilverStripe\Security\Member;
8
use SilverStripe\Security\Permission;
9
use SilverStripe\Security\Security;
10
11
/**
12
 * Adds a delete action for the gridfield to remove a relationship from group.
13
 * This is a special case where it captures whether the current user is the record being removed and
14
 * prevents removal from happening.
15
 */
16
class GridFieldGroupDeleteAction extends GridFieldDeleteAction
17
{
18
    /**
19
     * @var int
20
     */
21
    protected $groupID;
22
23
    public function __construct($groupID)
24
    {
25
        $this->groupID = $groupID;
26
        parent::__construct(true);
27
    }
28
29
    /**
30
     *
31
     * @param GridField $gridField
32
     * @param DataObject $record
33
     * @param string $columnName
34
     * @return string the HTML for the column
35
     */
36
    public function getColumnContent($gridField, $record, $columnName)
37
    {
38
        if ($this->canUnlink($record)) {
39
            return parent::getColumnContent($gridField, $record, $columnName);
40
        }
41
        return null;
42
    }
43
44
    /**
45
     * Get the ActionMenu group (not related to Member group)
46
     * @param GridField $gridField
47
     * @param DataObject $record
48
     * @param $columnName
49
     * @return null|string
50
     */
51
    public function getGroup($gridField, $record, $columnName)
52
    {
53
        if (!$this->canUnlink($record)) {
54
            return null;
55
        }
56
57
        return parent::getGroup($gridField, $record, $columnName);
58
    }
59
60
    /**
61
     * Handle the actions and apply any changes to the GridField
62
     *
63
     * @param GridField $gridField
64
     * @param string $actionName
65
     * @param array $arguments
66
     * @param array $data Form data
67
     * @throws ValidationException
68
     */
69
    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
70
    {
71
        $record = $gridField->getList()->find('ID', $arguments['RecordID']);
72
73
        if (!$record || !$actionName == 'unlinkrelation' || $this->canUnlink($record)) {
74
            parent::handleAction($gridField, $actionName, $arguments, $data);
75
            return;
76
        }
77
78
        throw new ValidationException(
79
            _t(__CLASS__ . '.UnlinkSelfFailure', 'Cannot remove yourself from this group, you will lose admin rights')
80
        );
81
    }
82
83
    /**
84
     * @param $record - the record of the User to unlink with
0 ignored issues
show
Documentation Bug introduced by
The doc comment - at position 0 could not be parsed: Unknown type name '-' at position 0 in -.
Loading history...
85
     * @return bool
86
     */
87
    protected function canUnlink($record)
88
    {
89
        $currentUser = Security::getCurrentUser();
90
        if ($currentUser
91
            && $record instanceof Member
92
            && (int)$record->ID === (int)$currentUser->ID
93
            && Permission::checkMember($record, 'ADMIN')
94
        ) {
95
            $adminGroups = array_intersect(
96
                $record->Groups()->column(),
97
                Permission::get_groups_by_permission('ADMIN')->column()
98
            );
99
100
            if (count($adminGroups) === 1 && array_search($this->groupID, $adminGroups) !== false) {
101
                return false;
102
            }
103
        }
104
        return true;
105
    }
106
}
107