Changeset 11641

Show
Ignore:
Timestamp:
08/20/07 16:39:59 (1 year ago)
Author:
juhana
Message:

Implemented topic creation and acl privilege setting

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/midgard/data/midgard_admin_sitewizard_cli.php

    r11619 r11641  
    1919    $structure_creator = $sitewizard->initialize_structure_creation('983e66725acd11db845a197adaa843af43af'); 
    2020    $structure_creator->read_config('structure_config.inc'); 
    21     $structure_creator->set_creation_root_topic('6d3af3384be911dcb7b0b3bf4b275d0d5d0d'); 
     21    //$structure_creator->set_creation_root_topic('6d3af3384be911dcb7b0b3bf4b275d0d5d0d'); 
     22    $structure_creator->create_creation_root_topic('6d3af3384be911dcb7b0b3bf4b275d0d5d0d', "test", "Test", "net.nehmer.static", array("koe", "koe", "koe")); 
     23    //$structure_creator->set_creation_root_group('94f058364f0f11dc93f803ebc4b67c0c7c0c');  
     24    $structure_creator->create_creation_root_group('94f058364f0f11dc93f803ebc4b67c0c7c0c', "testgroup8"); 
    2225    $structure_creator->execute();  
    2326} 
  • trunk/midgard/data/midgard_admin_sitewizard_creator.php

    r11619 r11641  
    99abstract class midgard_admin_sitewizard_creator 
    1010{ 
    11         protected $varbose
     11        protected $varbose = false
    1212 
    13         protected $parent_link;  
    14          
    15         public function __construct() 
     13        protected $parent_link = null;   
     14 
     15        protected $next_link = null; 
     16 
     17        protected $start_chain = true; 
     18 
     19        public function __construct($parent_link = null) 
    1620        { 
    17             $this->verbose = false; 
    18             $this->parent_link = null; 
     21            $this->parent_link = $parent_link; 
    1922        } 
    2023 
     
    2225        { 
    2326            unset($this->parent_link); 
     27            unset($this->next_link); 
    2428        } 
    2529 
    26         //abstract protected function shutdown(); 
     30        abstract protected function cleanup(); 
    2731 
    2832        abstract protected function read_config($path); 
     
    3943        } 
    4044         
     45        protected function start_chain($start) 
     46        { 
     47            $this->start_chain = $start; 
     48        } 
     49 
    4150        protected function get_parent_link() 
    4251        { 
  • trunk/midgard/data/midgard_admin_sitewizard_creator_structure.php

    r11619 r11641  
    1212class midgard_admin_sitewizard_creator_structure extends midgard_admin_sitewizard_creator 
    1313{ 
    14     private $host; 
    15  
    16     private $host_creator; 
    17  
    18     private $root_topic; 
    19  
    20     private $root_page; 
    21  
    22     private $creation_root_topic; 
    23  
    24     private $sitegroup; 
     14    private $host = null; 
     15 
     16    private $host_creator = null; 
     17 
     18    private $root_topic = null; 
     19 
     20    private $root_page = null; 
     21 
     22    private $creation_root_topic = null; 
     23 
     24    private $creation_root_group = null; 
     25 
     26    private $sitegroup = null; 
    2527         
    26     private $config
     28    private $config = null
    2729         
    28     private $start_chain; 
    29          
    30          
     30    private $structure = null; 
     31 
     32    private $created_groups = array(); 
     33 
    3134    public function __construct($host_creator = null) 
    3235    { 
    3336        // Always a good idea to run parent constructor 
    34         parent::__construct(); 
    35         $this->parent_link = $host_creator; 
    36         $this->start_chain = true; 
    37         $this->config = null; 
     37        parent::__construct($host_creator); 
    3838    } 
    3939 
     
    4141    { 
    4242        parent::__destruct(); 
     43    } 
     44 
     45    protected function cleanup() 
     46    { 
     47        $this->verbose('Cleaning up!'); 
     48 
    4349    } 
    4450  
     
    8995    } 
    9096         
    91     public function disable_start_chain() 
    92     { 
    93         $this->start_chain = false; 
    94     } 
    95          
    9697    public function read_config($path) 
    9798    { 
     
    113114    } 
    114115 
    115     public function set_midcom_privilege() 
    116     { 
    117  
     116    public function set_creation_root_group($group_guid) 
     117    { 
     118        if (!$this->creation_root_group = new midgard_group($group_guid)) 
     119        { 
     120            throw new midgard_admin_sitewizard_exception("Failed to set root group for structure creation GUID: "  
     121                . $group_guid . " Reason: " . mgd_errstr());  
     122         
     123            return true; 
     124        } 
     125        else 
     126        { 
     127            $this->verbose("Setting group for structure creation GUID: " . $group_guid); 
     128 
     129            return true; 
     130        } 
    118131    } 
    119132 
     
    135148    } 
    136149 
    137     // TODO 
    138     public function create_creation_root_topic($topic_guid, $topic_title = "", $component = "", $parameters = array()) 
     150    public function create_creation_root_group($owner_guid, $group_name) 
     151    { 
     152        $this->verbose("Creating a root group for group structure creation under group GUID: " .  $owner_guid); 
     153 
     154        $owner_group = new midgard_group($owner_guid); 
     155 
     156        $qb = new midgard_query_builder('midgard_group'); 
     157        $qb->add_constraint('sitegroup', '=', $this->sitegroup->id); 
     158        $qb->add_constraint('owner', '=', $owner_group->id); 
     159        $qb->add_constraint('name', '=', $group_name); 
     160 
     161        $groups = $qb->execute(); 
     162 
     163        if (count($groups) > 0) 
     164        { 
     165            $this->verbose("Group \"" . $group_name . "\" GUID: " . $groups[0]->guid  
     166                . " already exists. Setting as root group for structure creation."); 
     167 
     168            $this->creation_root_group = $groups[0]; 
     169 
     170            return true; 
     171        } 
     172        else 
     173        { 
     174            $this->creation_root_group = new midgard_group(); 
     175            $this->creation_root_group->owner = $owner_group->id; 
     176            $this->creation_root_group->sitegroup = $this->sitegroup->id; 
     177            $this->creation_root_group->name = $group_name; 
     178 
     179            if (!$this->creation_root_group->create()) 
     180            { 
     181                throw new midgard_admin_sitewizard_exception("Failed to create root group for group structure creation under group GUID: "  
     182                    . $this->creation_root_group->guid); 
     183            } 
     184            else 
     185            { 
     186             
     187                return true; 
     188            } 
     189        } 
     190    } 
     191 
     192    public function create_creation_root_topic($topic_guid, $topic_name = "", $topic_title = "", $component = "", $parameters = array()) 
    139193    { 
    140194        $this->verbose('Creating a root topic for structure creation'); 
     
    143197 
    144198        $this->creation_root_topic = new midgard_topic(); 
    145         $this->creation_root_topic->title = "Koe"
    146         $this->creation_root_topic->name = "koe"
     199        $this->creation_root_topic->title = $topic_title
     200        $this->creation_root_topic->name = $topic_name
    147201        $this->creation_root_topic->sitegroup = $this->sitegroup->id; 
     202        $this->creation_root_topic->component = $component; 
     203        $this->creation_root_topic->up = $topic->id; 
    148204 
    149205        if (!$this->creation_root_topic->create()) 
     
    155211        { 
    156212            $this->verbose("Created root topic for structure creation GUID: " . $this->ceration_root_topic->guid); 
    157         } 
     213             
     214            // Setting additional parameters 
     215            foreach ($parameters as $domain => $parameter) 
     216            { 
     217                foreach ($parameter as $name => $value) 
     218                { 
     219                    $this->creation_root_topic->parameter($domain, $name, $value); 
     220                } 
     221            } 
     222        } 
     223    } 
     224 
     225    private function replace($string) 
     226    { 
     227        $result = str_replace('__HOSTNAME__', $this->host->name, $string); 
     228        $result = str_replace('__HOSTTITLE__', $this->root_page->title, $result); 
     229 
     230        return $result; 
     231    } 
     232 
     233    private function create_topic_structure_recursive($nodes, $parent_node) 
     234    { 
     235        foreach ($nodes as $node) 
     236        { 
     237            $this->verbose("Creating topic \"" . $this->replace($node['title']) . "\" under parent topic GUID: "  
     238                . $parent_node->guid); 
     239 
     240            $topic = new midgard_topic(); 
     241            $topic->up = $parent_node->id; 
     242            $topic->name = $this->replace($node['name']); 
     243            $topic->title = $this->replace($node['title']); 
     244            $topic->sitegroup = $this->sitegroup->id; 
     245            $topic->component = $node['component']; 
     246 
     247            if (!$topic->create()) 
     248            { 
     249                throw new midagard_admin_sitewizard_exception("Failed to create topic \"" . $topic->title . "\" under parent GUID: "  
     250                    . $parent_node->guid); 
     251            } 
     252            else 
     253            { 
     254                // Setting additional parameters 
     255                if (isset($node['parameters']) && count($node['parameters']) > 0) 
     256                { 
     257                    $this->verbose("Starting to set additional parameters for topic GUID: " . $topic->guid); 
     258                     
     259                    $this->set_parameters($node, $topic); 
     260                } 
     261 
     262                // Setting midcom acl privileges 
     263               if (isset($node['acl']) && count($node['acl']) > 0) 
     264               { 
     265                   $this->verbose("Starting to set midcom acl privileges for topic GUID: " . $topic->guid); 
     266 
     267                   $this->set_privileges($node, $this->creation_root_topic); 
     268               } 
     269            } 
     270 
     271            if (isset($node['nodes']) && count($node['nodes']) > 0) 
     272            { 
     273                $this->create_topic_structure_recursive($node['nodes']); 
     274            } 
     275        } 
     276    } 
     277 
     278    private function set_parameters($config_node, $object) 
     279    { 
     280        foreach ($config_node['parameters'] as $domain => $parameter) 
     281        { 
     282            foreach ($parameter as $name => $value) 
     283            { 
     284                $this->verbose("Setting parameter (" . $domain . "," . $name . "," . $value .") for object GUID: "  
     285                    . $object->guid); 
     286                 
     287                $object->parameter($domain, $name, $value); 
     288            } 
     289        } 
     290        return true; 
     291    } 
     292 
     293    private function set_privileges($config_node, $object) 
     294    { 
     295        foreach($config_node['acl'] as $group_name => $privilege) 
     296        { 
     297            foreach($privilege as $name => $value) 
     298            { 
     299                if (array_key_exists($group_name, $this->created_groups)) 
     300                { 
     301                    $acl = new midcom_core_privilege_db();  
     302                    $acl->objectguid = $object->guid; 
     303                    $acl->name = $name; 
     304                    $acl->sitegroup = $this->sitegroup->id; 
     305                    $acl->assignee = "group:" . $this->created_groups[$group_name]->guid; 
     306                    $acl->value = $value; 
     307                 
     308                    if (!$acl->create()) 
     309                    { 
     310                        throw new midgard_admin_sitewizard_exception(); 
     311                    } 
     312                    else 
     313                    { 
     314                        $this->verbose("ACL (" . $acl->name . "," . $acl->assignee . "," . $acl->value . " set for object GUID: "  
     315                            . $object->guid); 
     316                         
     317                        return true; 
     318                    } 
     319                } 
     320            }  
     321        } 
    158322    } 
    159323 
    160324    public function create_topic_structure() 
    161325    { 
    162         $this->verbose("Starting to create topic structure under topic GUID: " . $creation_root_topic->guid); 
     326        $this->verbose("Starting to create topic structure under topic GUID: " . $this->creation_root_topic->guid); 
     327 
     328        foreach ($this->config as $structure) 
     329        { 
     330             $this->structure = $structure; 
     331        } 
     332 
     333        // Creating root topic from configuration if it hasn't been set already 
     334        if ($this->creation_root_topic == null) 
     335        { 
     336            $this->verbose('Creating root topic from configuration'); 
     337 
     338            $this->creation_root_topic = midgard_topic(); 
     339            $this->creation_root_topic->up = 0; 
     340            $this->creation_root_topic->sitegroup = $this->sitegroup->id; 
     341            $this->creation_root_topic->title = $this->structure['root']['title'];  
     342            $this->creation_root_topic->name = $this->structure['root']['name']; 
     343 
     344            if (!$this->creation_root_topic->create()) 
     345            { 
     346                throw new midgard_admin_sitewizard_exception("Failed to create root topic \"" . $this->structure['root']['name']  
     347                    . "\" for structure creation"); 
     348 
     349                return false;     
     350            } 
     351            else 
     352            { 
     353                // Setting additional parameters 
     354                if (isset($this->structure['root']['parameters']) && count($this->structure['root']['parameters']) > 0) 
     355                { 
     356                    $this->set_parameters($this->structure['root'], $this->creation_root_topic); 
     357                } 
     358 
     359                // Setting midcom acl privileges 
     360                if (isset($this->structure['root']['acl']) && count($this->structure['root']['acl']) > 0) 
     361                { 
     362                    $this->set_privileges($this->structure['root'], $this->creation_root_topic); 
     363                } 
     364            } 
     365        } 
    163366 
    164367        // Making sure the topic is empty before creating a structure 
    165368        $qb = new midgard_query_builder('midgard_topic'); 
    166369        $qb->add_constraint('up', '=', $this->creation_root_topic->id); 
     370        $qb->add_constraint('sitegroup', '=', $this->sitegroup->id); 
    167371        $topics = $qb->execute(); 
    168372         
    169373        if (count($topics) != 0) 
    170374        { 
    171             throw new midgard_admin_sitewizard_exception("Creation root topic is not empty!"); 
    172         } 
    173         else 
    174         { 
    175             //TODO: create structure 
    176              
    177             foreach ($this->config as $structure) 
    178             { 
    179                 // TODO: create root topic 
     375            throw new midgard_admin_sitewizard_exception('Creation root topic is not empty!'); 
     376        } 
     377        else 
     378        { 
     379            try 
     380            { 
     381                $this->create_topic_structure_recursive($this->structure['root']['nodes'], $this->creation_root_topic); 
     382            } 
     383            catch (midgard_admin_sitewizard_exception $e) 
     384            { 
     385                $e->error(); 
     386 
     387                throw new midgard_admin_sitewizard_exception("Failed to create topic structure under topic GUID: "  
     388                    . $this->creation_root_topic->guid . "Reason: " . mgd_errstr()); 
    180389             
    181                 foreach ($structure['root']['nodes'] as $node) 
    182                 { 
    183                     //echo $node['name'] . "\n";                  
    184                 } 
    185                  
    186390            } 
    187391        } 
     
    190394    public function create_group_structure() 
    191395    { 
    192         $this->verbose("Starting to create groups under admingroup ID: " . $this->sitegroup->admingroup); 
     396        if ($this->creation_root_group != null) 
     397        {    
     398            $group_owner_id = $this->creation_root_group->id; 
     399        } 
     400        else 
     401        { 
     402            $group_owner_id = $this->sitegroup->admingroup; 
     403        } 
     404 
     405        $this->verbose("Starting to create groups under group ID: " . $group_owner_id); 
    193406 
    194407        $qb = new midgard_query_builder('midgard_group'); 
    195408        $qb->add_constraint('sitegroup', '=', $this->sitegroup->id); 
    196         $qb->add_constraint('owner', '=', $this->sitegroup->admingroup); 
     409        $qb->add_constraint('owner', '=', $group_owner_id); 
    197410        $groups = $qb->execute(); 
    198411 
     
    213426                        if ($group->name == $candidate_group_name) 
    214427                        { 
     428                            $this->created_groups[$group->name] = $group; 
    215429                            $group_match++;  
    216430                        } 
     
    226440                    
    227441                        $new_group = new midgard_group(); 
     442                        $new_group->owner = $group_owner_id; 
     443                        $new_group->sitegroup = $this->sitegroup->id; 
    228444                        $new_group->name = $candidate_group_name; 
    229                         $new_group->sitegroup = $this->sitegroup->id; 
    230                         $new_group->owner = $this->sitegroup->admingroup; 
    231  
    232                         if (!$new_group->create()) 
     445                         
     446                        if (!$new_group->create()) 
    233447                        { 
    234448                            throw new midgard_admin_sitewizard("Failed to create group \"" . $candidate_group_name  
     
    237451                        else 
    238452                        { 
     453                            $this->created_groups[$new_group->name] = $new_group;  
    239454                            return true; 
    240455                        } 
     
    256471                    $new_group->name = $candidate_group_name; 
    257472                    $new_group->sitegroup = $this->sitegroup->id; 
    258                     $new_group->owner = $this->sitegroup->admingroup
     473                    $new_group->owner = $group_owner_id
    259474 
    260475                    if (!$new_group->create()) 
     
    286501            catch (midgard_admin_sitewizard_exception $e) 
    287502            { 
     503                $e->error(); 
     504 
    288505                throw new midgard_admin_sitewizard_exception("Failed to execute parent creator"); 
    289506            } 
     
    294511            $this->create_group_structure(); 
    295512            $this->create_topic_structure(); 
     513 
     514            $this->verbose("Sitewizard created website structure successfully. Now cleaning up..."); 
     515 
     516            $this->cleanup(); 
    296517        } 
    297518        catch(midgard_admin_sitewizard_exception $e) 
    298519        { 
    299520            $e->error(); 
    300             // TODO: Shutdown 
     521             
     522            $this->cleanup(); 
     523 
    301524            throw new midgard_admin_sitewizard_exception("Failed to create structure"); 
     525         
     526            return false; 
    302527        } 
    303528    }