From f4a9690ec5ce364ef8deaa300228dd65d8df7528 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Tue, 22 Jan 2008 01:27:41 +0000 Subject: [PATCH] Added subsites restriction of asset folders --- _config.php | 1 + code/FileSubsites.php | 63 +++++++++++++++++++++++++++++++++++++++++++ code/test.php | 0 3 files changed, 64 insertions(+) create mode 100755 code/FileSubsites.php delete mode 100644 code/test.php diff --git a/_config.php b/_config.php index f19682b..eee62f4 100644 --- a/_config.php +++ b/_config.php @@ -11,6 +11,7 @@ new SiteTree(); Object::add_extension('LeftAndMain', 'LeftAndMainSubsites'); Object::add_extension('Group', 'GroupSubsites'); +Object::add_extension('File', 'FileSubsites'); Director::addRules(100, array( 'admin/subsites/$Action/$ID/$OtherID' => 'SubsiteAdmin', diff --git a/code/FileSubsites.php b/code/FileSubsites.php new file mode 100755 index 0000000..36c98c0 --- /dev/null +++ b/code/FileSubsites.php @@ -0,0 +1,63 @@ +owner->class == 'File') { + return array( + 'has_one' => array( + 'Subsite' => 'Subsite', + ), + ); + } + } + + /** + * Add subsites-specific fields to the folder editor. + */ + function updateCMSFields(FieldSet &$fields) { + if($this->owner instanceof Folder) { + $sites = Subsite::accessible_sites('CMS_ACCESS_AssetAdmin'); + $fields->addFieldToTab('Root.Details', new DropdownField("SubsiteID", "Subsite", $sites->toDropdownMap('ID', 'Title', "(Public)"))); + } + } + + /** + * Update any requests to limit the results to the current site + */ + function augmentSQL(SQLQuery &$query) { + // If you're querying by ID, ignore the sub-site - this is a bit ugly... + if(strpos($query->where[0], ".`ID` = ") === false && strpos($query->where[0], ".ID = ") === false) { + + if($context = DataObject::context_obj()) $subsiteID = (int)$context->SubsiteID; + else $subsiteID = (int)Subsite::currentSubsiteID(); + + // The foreach is an ugly way of getting the first key :-) + foreach($query->from as $tableName => $info) { + $query->where[] = "`$tableName`.SubsiteID IN (0, $subsiteID)"; + break; + } + + $query->orderby = 'SubsiteID' . ($query->orderby ? ', ' : '') . $query->orderby; + } + } + + function augmentBeforeWrite() { + if(!is_numeric($this->owner->ID) && !$this->owner->SubsiteID) $this->owner->SubsiteID = Subsite::currentSubsiteID(); + } + + function alternateCanEdit() { + // Check the CMS_ACCESS_SecurityAdmin privileges on the subsite that owns this group + $oldSubsiteID = Session::get('SubsiteID'); + + Session::set('SubsiteID', $this->owner->SubsiteID); + $access = Permission::check('CMS_ACCESS_SecurityAdmin'); + Session::set('SubsiteID', $oldSubsiteID); + + return $access; + } +} + diff --git a/code/test.php b/code/test.php deleted file mode 100644 index e69de29..0000000