function testMemberGroupRelationForm() { Session::set('loggedInAs', $this->idFromFixture('GroupTest_Member', 'admin')); $adminGroup = $this->objFromFixture('Group', 'admingroup'); $parentGroup = $this->objFromFixture('Group', 'parentgroup'); $childGroup = $this->objFromFixture('Group', 'childgroup'); // Test single group relation through checkboxsetfield $form = new GroupTest_MemberForm($this, 'Form'); $member = $this->objFromFixture('GroupTest_Member', 'admin'); $form->loadDataFrom($member); $checkboxSetField = $form->Fields()->fieldByName('Groups'); $checkboxSetField->setValue(array($adminGroup->ID => $adminGroup->ID, $parentGroup->ID => $parentGroup->ID)); $form->saveInto($member); $updatedGroups = $member->Groups(); $controlGroups = new Member_GroupSet($adminGroup, $parentGroup); $this->assertEquals($updatedGroups->Map('ID', 'ID'), $controlGroups->Map('ID', 'ID'), "Adding a toplevel group works"); // Test unsetting relationship $form->loadDataFrom($member); $checkboxSetField = $form->Fields()->fieldByName('Groups'); $checkboxSetField->setValue(array($adminGroup->ID => $adminGroup->ID)); $form->saveInto($member); $member->flushCache(); $updatedGroups = $member->Groups(); $controlGroups = new Member_GroupSet($adminGroup); $this->assertEquals($updatedGroups->Map('ID', 'ID'), $controlGroups->Map('ID', 'ID'), "Removing a previously added toplevel group works"); // Test adding child group }
/** * Overload {@link ManyManyList::removeByID()} so we can log * when a Member is removed from a Group. */ public function removeByID($itemID) { parent::removeByID($itemID); if ($this->getJoinTable() == 'Group_Members') { $currentMember = \Member::currentUser(); if (!($currentMember && $currentMember->exists())) { return; } $group = \Group::get()->byId($itemID); $member = \Member::get()->byId($this->getForeignID()); if (!$group) { return; } if (!$member) { return; } $this->getAuditLogger()->info(sprintf('"%s" (ID: %s) removed Member "%s" (ID: %s) from Group "%s" (ID: %s)', $currentMember->Email ?: $currentMember->Title, $currentMember->ID, $member->Email ?: $member->Title, $member->ID, $group->Title, $group->ID)); } }
/** * Get a "many-to-many" map that holds for all members their group * memberships * * @return Member_GroupSet Returns a map holding for all members their * group memberships. */ public function Groups() { $groups = $this->getManyManyComponents("Groups"); $groupIDs = $groups->column(); $collatedGroups = array(); if ($groups) { foreach ($groups as $group) { $collatedGroups = array_merge((array) $collatedGroups, $group->collateAncestorIDs()); } } $table = "Group_Members"; if (count($collatedGroups) > 0) { $collatedGroups = implode(", ", array_unique($collatedGroups)); $unfilteredGroups = singleton('Group')->instance_get("\"Group\".\"ID\" IN ({$collatedGroups})", "\"Group\".\"ID\"", "", "", "Member_GroupSet"); $result = new ComponentSet(); // Only include groups where allowedIPAddress() returns true $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null; foreach ($unfilteredGroups as $group) { if ($group->allowedIPAddress($ip)) { $result->push($group); } } } else { $result = new Member_GroupSet(); } $result->setComponentInfo("many-to-many", $this, "Member", $table, "Group"); return $result; }
/** * Get a "many-to-many" map that holds for all members their group memberships, * including any parent groups where membership is implied. * Use {@link DirectGroups()} to only retrieve the group relations without inheritance. * * @todo Push all this logic into Member_GroupSet's getIterator()? */ public function Groups() { $groups = Member_GroupSet::create('Group', 'Group_Members', 'GroupID', 'MemberID'); $groups = $groups->forForeignID($this->ID); $this->extend('updateGroups', $groups); return $groups; }
/** * Get a "many-to-many" map that holds for all members their group * memberships * * @return Member_GroupSet Returns a map holding for all members their * group memberships. */ public function Groups() { $groups = $this->getManyManyComponents("Groups"); $unsecure = DataObject::get("Group_Unsecure", ""); if ($unsecure) { foreach ($unsecure as $unsecureItem) { $groups->push($unsecureItem); } } $groupIDs = $groups->column(); $collatedGroups = array(); foreach ($groups as $group) { $collatedGroups = array_merge((array) $collatedGroups, $group->collateAncestorIDs()); } $table = "Group_Members"; if (count($collatedGroups) > 0) { $collatedGroups = implode(", ", array_unique($collatedGroups)); $result = singleton('Group')->instance_get("`ID` IN ({$collatedGroups})", "ID", "", "", "Member_GroupSet"); } else { $result = new Member_GroupSet(); } $result->setComponentInfo("many-to-many", $this, "Member", $table, "Group"); return $result; }
/** * Get a "many-to-many" map that holds for all members their group memberships, * including any parent groups where membership is implied. * Use {@link DirectGroups()} to only retrieve the group relations without inheritance. * * @todo Push all this logic into Member_GroupSet's getIterator()? */ public function Groups() { $groups = new Member_GroupSet('Group', 'Group_Members', 'GroupID', 'MemberID'); $groups->setForeignID($this->ID); $this->extend('updateGroups', $groups); return $groups; }
/** * Get a "many-to-many" map that holds for all members their group * memberships * * @todo Push all this logic into Member_GroupSet's getIterator()? */ public function Groups() { $groups = new Member_GroupSet('Group', 'Group_Members', 'GroupID', 'MemberID'); if ($this->ID) { $groups->setForeignID($this->ID); } // Filter out groups that aren't allowed from this IP $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null; $disallowedGroups = array(); foreach ($groups as $group) { if (!$group->allowedIPAddress($ip)) { $disallowedGroups[] = $groupID; } } if ($disallowedGroups) { $group->where("\"Group\".\"ID\" NOT IN (" . implode(',', $disallowedGroups) . ")"); } return $groups; }