HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
RInstaller.php
Go to the documentation of this file.
1 <?php
10 class RInstaller extends CApplicationComponent
11 {
12  const ERROR_NONE = 0;
13  const ERROR_QUERY_FAILED = 1;
14 
18  public $defaultRoles;
30  public $guestName;
34  private $_authManager;
38  private $_installed;
39 
43  public $db;
44 
51  public function init()
52  {
53  parent::init();
54 
55  // Make sure the application is configured
56  // to use a valid authorization manager.
57  $authManager = Yii::app()->getAuthManager();
58  if (($authManager instanceof RDbAuthManager) === false) {
59  throw new CException(Rights::t('install', 'Application authorization manager must extend the RDbAuthManager class.'));
60  }
61 
62  // Make sure the application is configured
63  // to use a valid web user.
64  $user = Yii::app()->getUser();
65  if (($user instanceof RWebUser) === false) {
66  throw new CException(Rights::t('install', 'Application web user must extend the RWebUser class.'));
67  }
68 
69  $this->_authManager = $authManager;
70  $this->db = $this->_authManager->db;
71  }
72 
80  public function run()
81  {
82  // Get the table names.
83  $itemTable = $this->_authManager->itemTable;
84  $itemChildTable = $this->_authManager->itemChildTable;
85  $assignmentTable = $this->_authManager->assignmentTable;
86  $rightsTable = $this->_authManager->rightsTable;
87 
88  // Fetch the schema.
89  $schema = file_get_contents(dirname(__FILE__).'/../data/schema.sql');
90 
91  // Correct the table names.
92  $schema = strtr($schema, array(
93  'AuthItem' => $itemTable,
94  'AuthItemChild' => $itemChildTable,
95  'AuthAssignment' => $assignmentTable,
96  'Rights' => $rightsTable,
97  ));
98 
99  // Convert the schema into an array of sql queries.
100  $schema = preg_split("/;\s*/", trim($schema, ';'));
101 
102  // Start transaction
103  $txn = $this->db->beginTransaction();
104 
105  try {
106  // Execute each query in the schema.
107  foreach ($schema as $sql) {
108  $command = $this->db->createCommand($sql);
109  $command->execute();
110  }
111 
112  // Insert the necessary roles.
113  $roles = $this->getUniqueRoles();
114  foreach ($roles as $roleName) {
115  $sql = "INSERT INTO {$itemTable} (name, type, data)
116  VALUES (:name, :type, :data)";
117  $command = $this->db->createCommand($sql);
118  $command->bindValue(':name', $roleName);
119  $command->bindValue(':type', CAuthItem::TYPE_ROLE);
120  $command->bindValue(':data', 'N;');
121  $command->execute();
122  }
123 
124  // Assign the logged in user the superusers role.
125  $sql = "INSERT INTO {$assignmentTable} (itemname, userid, data)
126  VALUES (:itemname, :userid, :data)";
127  $command = $this->db->createCommand($sql);
128  $command->bindValue(':itemname', $this->superuserName);
129  $command->bindValue(':userid', Yii::app()->getUser()->id);
130  $command->bindValue(':data', 'N;');
131  $command->execute();
132 
133  // All commands executed successfully, commit.
134  $txn->commit();
135 
136  return self::ERROR_NONE;
137  } catch (CDbException $e) {
138  // Something went wrong, rollback.
139  $txn->rollback();
140 
142  }
143  }
144 
150  private function getUniqueRoles()
151  {
152  $roles = CMap::mergeArray($this->defaultRoles, array(
153  $this->superuserName,
154  /*$this->authenticatedName,
155  $this->guestName,*/
156  ));
157 
158  return array_unique($roles);
159  }
160 
164  public function getInstalled()
165  {
166  if ($this->_installed !== null) {
167  return $this->_installed;
168  } else {
169  $schema = array(
170  "SELECT COUNT(*) FROM {$this->_authManager->itemTable}",
171  "SELECT COUNT(*) FROM {$this->_authManager->itemChildTable}",
172  "SELECT COUNT(*) FROM {$this->_authManager->assignmentTable}",
173  "SELECT COUNT(*) FROM {$this->_authManager->rightsTable}",
174  );
175 
176  try {
177  foreach ($schema as $sql) {
178  $command = $this->db->createCommand($sql);
179  $command->queryScalar();
180  }
181 
182  $installed = true;
183  } catch (CDbException $e) {
184  $installed = false;
185  }
186 
187  return $this->_installed = $installed;
188  }
189  }
190 }