2016-08-19 00:51:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Security;
|
|
|
|
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\ManyManyList;
|
2018-02-09 14:52:07 +01:00
|
|
|
use SilverStripe\ORM\Queries\SQLDelete;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\ORM\Queries\SQLSelect;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a set of Groups attached to a member.
|
|
|
|
* Handles the hierarchy logic.
|
|
|
|
*/
|
|
|
|
class Member_GroupSet extends ManyManyList
|
|
|
|
{
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
protected function linkJoinTable()
|
|
|
|
{
|
|
|
|
// Do not join the table directly
|
|
|
|
if ($this->extraFields) {
|
|
|
|
user_error('Member_GroupSet does not support many_many_extraFields', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Link this group set to a specific member.
|
|
|
|
*
|
|
|
|
* Recursively selects all groups applied to this member, as well as any
|
|
|
|
* parent groups of any applied groups
|
|
|
|
*
|
2018-09-27 16:40:23 +02:00
|
|
|
* @param array|int|string|null $id (optional) An ID or an array of IDs - if not provided, will use the current
|
2016-11-29 00:31:16 +01:00
|
|
|
* ids as per getForeignID
|
|
|
|
* @return array Condition In array(SQL => parameters format)
|
|
|
|
*/
|
|
|
|
public function foreignIDFilter($id = null)
|
|
|
|
{
|
|
|
|
if ($id === null) {
|
|
|
|
$id = $this->getForeignID();
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
// Find directly applied groups
|
|
|
|
$manyManyFilter = parent::foreignIDFilter($id);
|
2018-09-27 16:40:23 +02:00
|
|
|
$query = SQLSelect::create('"Group_Members"."GroupID"', '"Group_Members"', $manyManyFilter);
|
2016-11-29 00:31:16 +01:00
|
|
|
$groupIDs = $query->execute()->column();
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
// Get all ancestors, iteratively merging these into the master set
|
2018-09-27 16:40:23 +02:00
|
|
|
$allGroupIDs = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
while ($groupIDs) {
|
|
|
|
$allGroupIDs = array_merge($allGroupIDs, $groupIDs);
|
2018-09-27 16:40:23 +02:00
|
|
|
$groupIDs = DataObject::get(Group::class)->byIDs($groupIDs)->column("ParentID");
|
2016-11-29 00:31:16 +01:00
|
|
|
$groupIDs = array_filter($groupIDs);
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
// Add a filter to this DataList
|
|
|
|
if (!empty($allGroupIDs)) {
|
|
|
|
$allGroupIDsPlaceholders = DB::placeholders($allGroupIDs);
|
2018-09-27 16:40:23 +02:00
|
|
|
return ["\"Group\".\"ID\" IN ($allGroupIDsPlaceholders)" => $allGroupIDs];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2018-09-27 16:40:23 +02:00
|
|
|
|
|
|
|
return ['"Group"."ID"' => 0];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
public function foreignIDWriteFilter($id = null)
|
|
|
|
{
|
|
|
|
// Use the ManyManyList::foreignIDFilter rather than the one
|
|
|
|
// in this class, otherwise we end up selecting all inherited groups
|
|
|
|
return parent::foreignIDFilter($id);
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
public function add($item, $extraFields = null)
|
|
|
|
{
|
|
|
|
// Get Group.ID
|
|
|
|
$itemID = null;
|
|
|
|
if (is_numeric($item)) {
|
|
|
|
$itemID = $item;
|
|
|
|
} else {
|
|
|
|
if ($item instanceof Group) {
|
|
|
|
$itemID = $item->ID;
|
|
|
|
}
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
// Check if this group is allowed to be added
|
2018-09-27 16:40:23 +02:00
|
|
|
if ($this->canAddGroups([$itemID])) {
|
2016-11-29 00:31:16 +01:00
|
|
|
parent::add($item, $extraFields);
|
|
|
|
}
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2018-02-09 14:52:07 +01:00
|
|
|
public function removeAll()
|
|
|
|
{
|
|
|
|
// Remove the join to the join table to avoid MySQL row locking issues.
|
|
|
|
$query = $this->dataQuery();
|
|
|
|
$foreignFilter = $query->getQueryParam('Foreign.Filter');
|
|
|
|
$query->removeFilterOn($foreignFilter);
|
|
|
|
|
|
|
|
// Select ID column
|
|
|
|
$selectQuery = $query->query();
|
|
|
|
$dataClassIDColumn = DataObject::getSchema()->sqlColumnForField($this->dataClass(), 'ID');
|
|
|
|
$selectQuery->setSelect($dataClassIDColumn);
|
|
|
|
|
|
|
|
$from = $selectQuery->getFrom();
|
|
|
|
unset($from[$this->joinTable]);
|
|
|
|
$selectQuery->setFrom($from);
|
|
|
|
$selectQuery->setOrderBy(); // ORDER BY in subselects breaks MS SQL Server and is not necessary here
|
|
|
|
$selectQuery->setDistinct(false);
|
|
|
|
|
|
|
|
// Use a sub-query as SQLite does not support setting delete targets in
|
|
|
|
// joined queries.
|
2018-09-27 16:40:23 +02:00
|
|
|
$delete = SQLDelete::create();
|
2018-02-09 14:52:07 +01:00
|
|
|
$delete->setFrom("\"{$this->joinTable}\"");
|
|
|
|
$delete->addWhere(parent::foreignIDFilter());
|
|
|
|
$subSelect = $selectQuery->sql($parameters);
|
2018-09-27 16:40:23 +02:00
|
|
|
$delete->addWhere([
|
2018-02-09 14:52:07 +01:00
|
|
|
"\"{$this->joinTable}\".\"{$this->localKey}\" IN ($subSelect)" => $parameters
|
2018-09-27 16:40:23 +02:00
|
|
|
]);
|
2018-02-09 14:52:07 +01:00
|
|
|
$delete->execute();
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Determine if the following groups IDs can be added
|
|
|
|
*
|
|
|
|
* @param array $itemIDs
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
protected function canAddGroups($itemIDs)
|
|
|
|
{
|
|
|
|
if (empty($itemIDs)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$member = $this->getMember();
|
|
|
|
return empty($member) || $member->onChangeGroups($itemIDs);
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Get foreign member record for this relation
|
|
|
|
*
|
|
|
|
* @return Member
|
|
|
|
*/
|
|
|
|
protected function getMember()
|
|
|
|
{
|
|
|
|
$id = $this->getForeignID();
|
|
|
|
if ($id) {
|
2017-06-09 05:07:35 +02:00
|
|
|
return DataObject::get_by_id(Member::class, $id);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
}
|