Version: current

     1 <?php
     2 /**
     3  * @package sapphire
     4  * @subpackage tasks
     5  */
     6 class UpgradeSiteTreePermissionSchemaTask extends BuildTask {
     7     static $allowed_actions = array(
     8         '*' => 'ADMIN'
     9     );
    10
    11     protected $title = 'Upgrade SiteTree Permissions Schema';
    12
    13     protected $description = "Move data from legacy columns to new schema introduced in SilverStripe 2.1.<br />
    14         SiteTree->Viewers to SiteTree->CanViewType<br />
    15         SiteTree->Editors to SiteTree->CanEditType<br />
    16         SiteTree->ViewersGroup to SiteTree->ViewerGroups (has_one to many_many)<br />
    17         SiteTree->Editorsroup to SiteTree->EditorGroups (has_one to many_many)<br />
    18         See http://open.silverstripe.com/ticket/2847
    19     ";
    20
    21     function run($request) {
    22         // transfer values for changed column name
    23         foreach(array('SiteTree','SiteTree_Live','SiteTree_versions') as $table) {
    24             DB::query("UPDATE \"{$table}\" SET \"CanViewType\" = 'Viewers';");
    25             DB::query("UPDATE \"{$table}\" SET \"CanEditType\" = 'Editors';");
    26         }
    27         //Debug::message('Moved SiteTree->Viewers to SiteTree->CanViewType');
    28         //Debug::message('Moved SiteTree->Editors to SiteTree->CanEditType');
    29
    30         // convert has_many to many_many
    31         $pageIDs = DB::query("SELECT ID FROM SiteTree")->column('ID');
    32         foreach($pageIDs as $pageID) {
    33             $page = DataObject::get_by_id('SiteTree', $pageID);
    34             if($page->ViewersGroup && DataObject::get_by_id("Group", $page->ViewersGroup)) $page->ViewerGroups()->add($page->ViewersGroup);
    35             if($page->EditorsGroup && DataObject::get_by_id("Group", $page->EditorsGroup)) $page->EditorGroups()->add($page->EditorsGroup);
    36
    37             $page->destroy();
    38             unset($page);
    39         }
    40         //Debug::message('SiteTree->ViewersGroup to SiteTree->ViewerGroups (has_one to many_many)');
    41         //Debug::message('SiteTree->EditorsGroup to SiteTree->EditorGroups (has_one to many_many)');
    42
    43         // rename legacy columns
    44         foreach(array('SiteTree','SiteTree_Live','SiteTree_versions') as $table) {
    45             foreach(array('Viewers','Editors','ViewersGroup','EditorsGroup') as $field) {
    46                  DB::getConn()->dontRequireField($table, $field);
    47             }
    48         }
    49     }
    50 }
    51 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -