6 class SetupController
extends AppController
{
8 var $uses = array('DbConnection');
10 var $skipComponents = array('start', 'db_connection', 'db_schema');
13 * @var SessionComponent
25 function db_connection($action = '') {
26 if (!empty($this->data
)) {
27 $this->DbConnection
->save($this->data
);
28 $this->redirect(array('action' => 'db_connection'));
31 if (!$this->DbConnection
->exists()) {
32 $connection = array( 'DbConnection' => array(
34 'persistent' => false,
35 'host' => 'localhost',
39 'database' => strtolower(Configure
::read('Settings.name')),
43 $this->DbConnection
->save($connection);
44 $this->redirect(array('action' => 'db_connection'));
47 $connection = $this->DbConnection
->read();
48 $this->data
= $this->DbConnection
->read();
50 $connected = $this->_checkDbConnection();
52 $edit = ($action == 'edit') ||
!$connected;
54 $this->set(compact('connected', 'connection', 'edit'));
57 function db_schema($action = null) {
60 if ($action == 'create') {
64 uses('file', 'model' . DS
. 'connection_manager', 'model' . DS
. 'schema');
70 $this->Schema
=& new CakeSchema(compact('name', 'path', 'file'));
72 $options = array('name' => ucfirst(Configure
::read('Settings.name')), 'file' => $this->Schema
->file
);
73 $dbSchema = $this->Schema
->load($options);
75 $db =& ConnectionManager
::getDataSource($this->Schema
->connection
);
77 $drop = $create = array();
79 foreach ($dbSchema->tables
as $table => $fields) {
80 $drop[$table] = $db->dropSchema($dbSchema, $table);
81 $create[$table] = $db->createSchema($dbSchema, $table);
89 foreach($drop as $query) {
93 foreach($create as $query) {
97 if (!$this->__runSql($drop, 'drop')) {
101 if (!$this->__runSql($create, 'create')) {
105 $this->set('querys', $querys);
106 $this->set('errors', $this->__sqlErrors
);
108 $this->set(compact('success', 'run'));
111 function create_users() {
112 if (App
::import('model', 'User')) {
115 $this->Session
->setFlash('An error has occurred. Check your setup.');
116 $this->redirect(array('action' => 'db_connection'));
119 if ($User->find('count')) {
120 $this->Session
->setFlash('A user already exists in the database. Cannot create another in the setup.');
121 $this->redirect(array('action' => 'end'));
124 if(!empty($this->data
)) {
125 App
::import('component', 'Security');
127 $userdata = $this->data
['User'];
128 $userdata['enabled'] = '1';
129 $userdata['password'] = Security
::hash(Configure
::read('Security.salt') . $userdata['password']);
131 if ($User->save($userdata)) {
132 $this->_setupACL($User->id
);
133 $this->redirect(array('action' => 'end'));
139 $setup = Configure
::read('Setup');
140 $setup['has_run'] = true;
142 $this->_storeConfig('Setup', $setup, true);
146 * Checks database connection
150 function _checkDbConnection($datasource = 'default') {
151 uses('model' . DS
. 'connection_manager');
152 $db =& ConnectionManager
::getInstance();
153 $ds =& $db->getDataSource($datasource);
155 return $ds->isConnected();
158 function __runSql($contents, $event) {
160 if (empty($contents)) {
164 Configure
::write('debug', 2);
166 $db =& ConnectionManager
::getDataSource($this->Schema
->connection
);
167 $db->fullDebug
= true;
171 foreach($contents as $table => $sql) {
173 if (!$this->Schema
->before(array($event => $table))) {
176 if (!$db->_execute($sql)) {
177 $error = $db->lastError();
181 $this->Schema
->after(array($event => $table, 'errors' => $errors));
185 $this->__sqlErrors
= $errors;
187 return empty($errors);
190 function _setupAcl($id) {
191 if (App
::import('Component', 'Acl')) {
192 $this->Acl
=& new AclComponent();
193 $this->Acl
->startup($this);
196 $options = Configure
::read('Settings.auth.options');
197 $actionPath = $options['actionPath'];
201 $aco = $this->Acl
->Aco
;
202 if ($actionPath != '/') {
203 $actionPath = str_replace('/', '', $actionPath);
204 $aco->create(array('alias' => $actionPath));
206 $parentId = $aco->id
;
209 Configure
::load('controllers');
211 $controllers = Configure
::read('Controllers');
213 foreach ($controllers as $controller => $actions) {
214 $aco->create(array('alias' => $controller, 'parent_id' => $parentId));
216 $controllerId = $aco->id
;
217 foreach ($actions as $action) {
218 $aco->create(array('alias' => $action, 'parent_id' => $controllerId));
223 $this->Acl
->allow(array('model' => 'User', 'foreign_key' => $id), $actionPath);
226 function _storeConfig($name, $data = array(), $reRead = false) {
229 foreach ($data as $key => $value) {
230 $content .= sprintf("\$config['%s']['%s'] = %s;\n", $name, $key, var_export($value, true));
233 $content = "<?php\n".$content."?>";
236 $name = strtolower($name);
237 $file = new File(CONFIGS
.$name.'.php');
238 if ($file->open('w')) {
239 $file->append($content);
244 Configure
::load($name);
248 function _initComponents() {
249 if(!Configure
::read('Setup.has_run')) {
250 $components = array_flip($this->components
);
251 $blackList = array('Acl', 'Auth');
252 foreach ($blackList as $key) {
253 if (isset($components[$key])) {
254 unset($components[$key]);
258 $this->components
= array_flip($components);
260 return parent
::_initComponents();