PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Install/Console/Command/InstallShell.php

https://github.com/kareypowell/croogo
PHP | 223 lines | 184 code | 20 blank | 19 comment | 14 complexity | a647e79820d66bf92a4c183109275cc9 MD5 | raw file
  1. <?php
  2. App::uses('AppShell', 'Console/Command');
  3. App::uses('InstallManager', 'Install.Lib');
  4. App::uses('Install', 'Install.Model');
  5. App::uses('ComponentCollection', 'Controller');
  6. App::uses('AuthComponent', 'Controller/Component');
  7. /**
  8. * Install Shell
  9. *
  10. * @category Shell
  11. * @package Croogo.Install.Console.Command
  12. * @version 1.0
  13. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  14. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  15. * @link http://www.croogo.org
  16. */
  17. class InstallShell extends AppShell {
  18. /**
  19. * Display help/options
  20. */
  21. public function getOptionParser() {
  22. $drivers = array('Mysql', 'Postgres', 'Sqlite', 'Sqlserver');
  23. $parser = parent::getOptionParser();
  24. $parser->description(__d('croogo', 'Install Utilities'))
  25. ->addSubcommand('main', array(
  26. 'help' => 'Generate croogo.php, database.php, settings.json and create admin user.',
  27. 'parser' => array(
  28. 'description' => 'Generate croogo.php, database.php, settings.json and create admin user.',
  29. )
  30. ))
  31. ->addSubcommand('setup_acos', array(
  32. 'help' => 'Setup default ACOs for roles',
  33. 'parser' => array(
  34. 'description' => 'Generate default role settings during release',
  35. )
  36. ))
  37. ->addOption('datasource', array(
  38. 'help' => 'Database Driver',
  39. 'short' => 'd',
  40. 'required' => true,
  41. 'options' => $drivers,
  42. ))
  43. ->addOption('host', array(
  44. 'help' => 'Database Host',
  45. 'short' => 'h',
  46. 'required' => true,
  47. ))
  48. ->addOption('login', array(
  49. 'help' => 'Database User',
  50. 'short' => 'l',
  51. 'required' => true,
  52. ))
  53. ->addOption('password', array(
  54. 'help' => 'Database Password',
  55. 'short' => 'p',
  56. 'required' => true,
  57. ))
  58. ->addOption('database-name', array(
  59. 'help' => 'Database Name',
  60. 'short' => 'n',
  61. 'required' => true,
  62. ))
  63. ->addOption('port', array(
  64. 'help' => 'Database Port',
  65. 'short' => 't',
  66. 'required' => true,
  67. ))
  68. ->addOption('prefix', array(
  69. 'help' => 'Table Prefix',
  70. 'short' => 'x',
  71. 'required' => true,
  72. ))
  73. ->addArgument('admin-user', array(
  74. 'help' => 'Admin username',
  75. ))
  76. ->addArgument('admin-password', array(
  77. 'help' => 'Admin password',
  78. ));
  79. return $parser;
  80. }
  81. /**
  82. * Convenient wrapper for Shell::in() that gets the default from CLI options
  83. */
  84. protected function _in($prompt, $options = null, $default = null, $argument = null) {
  85. if (isset($this->params[$argument])) {
  86. return $this->params[$argument];
  87. }
  88. return $this->in($prompt, $options, $default);
  89. }
  90. /**
  91. * Convenient wrapper for Shell::in() that gets the default from CLI argument
  92. */
  93. protected function _args($prompt, $options = null, $default = null, $index = null) {
  94. if (isset($this->args[$index])) {
  95. return $this->args[$index];
  96. }
  97. return $this->in($prompt, $options, $default);
  98. }
  99. public function main() {
  100. $this->out();
  101. $this->out('Database settings:');
  102. $install['Install']['datasource'] = $this->_in(__d('croogo', 'DataSource'), array(
  103. 'Mysql',
  104. 'Sqlite',
  105. 'Postgres',
  106. 'Sqlserver'
  107. ), 'Mysql', 'datasource');
  108. $install['Install']['datasource'] = 'Database/' . $install['Install']['datasource'];
  109. $install['Install']['host'] = $this->_in(__d('croogo', 'Host'), null, 'localhost', 'host');
  110. $install['Install']['login'] = $this->_in(__d('croogo', 'Login'), null, 'root', 'login');
  111. $install['Install']['password'] = $this->_in(__d('croogo', 'Password'), null, '', 'password');
  112. $install['Install']['database'] = $this->_in(__d('croogo', 'Database'), null, 'croogo', 'database-name');
  113. $install['Install']['prefix'] = $this->_in(__d('croogo', 'Prefix'), null, '', 'prefix');
  114. $install['Install']['port'] = $this->_in(__d('croogo', 'Port'), null, null, 'port');
  115. $InstallManager = new InstallManager();
  116. $InstallManager->createDatabaseFile($install);
  117. $this->out('Setting up database objects. Please wait...');
  118. $Install = ClassRegistry::init('Install.Install');
  119. try {
  120. $result = $Install->setupDatabase();
  121. if ($result !== true) {
  122. $this->err($result);
  123. return $this->_stop();
  124. }
  125. } catch (Exception $e) {
  126. $this->err($e->getMessage());
  127. $this->err('Please verify you have the correct credentials');
  128. return $this->_stop();
  129. }
  130. $InstallManager->createCroogoFile();
  131. $this->out();
  132. if (empty($this->args)) {
  133. $this->out('Create Admin user:');
  134. }
  135. do {
  136. $username = $this->_args(__d('croogo', 'Username'), null, null, 0);
  137. if (empty($username)) {
  138. $this->err('Username must not be empty');
  139. }
  140. } while (empty($username));
  141. do {
  142. $password = $this->_args(__d('croogo', 'Password'), null, null, 1);
  143. if (empty($this->args)) {
  144. $verify = $this->_in(__d('croogo', 'Verify Password'), null, null, 1);
  145. $passwordsMatched = $password == $verify;
  146. if (!$passwordsMatched) {
  147. $this->err('Passwords do not match');
  148. }
  149. } else {
  150. $passwordsMatched = true;
  151. }
  152. if (empty($password)) {
  153. $this->err('Password must not be empty');
  154. }
  155. } while (empty($password) || !$passwordsMatched);
  156. $user['User']['username'] = $username;
  157. $user['User']['password'] = $password;
  158. $Install->addAdminUser($user);
  159. $InstallManager->createSettingsFile();
  160. $InstallManager->installCompleted();
  161. $this->out();
  162. $this->success('Congratulations, Croogo has been installed successfully.');
  163. }
  164. public function setup_acos() {
  165. $Role = ClassRegistry::init('Users.Role');
  166. $Role->Behaviors->attach('Croogo.Aliasable');
  167. $public = $Role->byAlias('public');
  168. $registered = $Role->byAlias('registered');
  169. $Permission = ClassRegistry::init('Acl.AclPermission');
  170. $setup = array(
  171. 'controllers/Comments/Comments/index' => array($public),
  172. 'controllers/Comments/Comments/add' => array($public),
  173. 'controllers/Comments/Comments/delete' => array($registered),
  174. 'controllers/Contacts/Contacts/view' => array($public),
  175. 'controllers/Nodes/Nodes/index' => array($public),
  176. 'controllers/Nodes/Nodes/term' => array($public),
  177. 'controllers/Nodes/Nodes/promoted' => array($public),
  178. 'controllers/Nodes/Nodes/search' => array($public),
  179. 'controllers/Nodes/Nodes/view' => array($public),
  180. 'controllers/Users/Users/index' => array($registered),
  181. 'controllers/Users/Users/add' => array($public),
  182. 'controllers/Users/Users/activate' => array($public),
  183. 'controllers/Users/Users/edit' => array($registered),
  184. 'controllers/Users/Users/forgot' => array($public),
  185. 'controllers/Users/Users/reset' => array($public),
  186. 'controllers/Users/Users/login' => array($public),
  187. 'controllers/Users/Users/logout' => array($registered),
  188. 'controllers/Users/Users/admin_logout' => array($registered),
  189. 'controllers/Users/Users/view' => array($registered),
  190. );
  191. foreach ($setup as $aco => $roles) {
  192. foreach ($roles as $roleId) {
  193. $aro = array(
  194. 'model' => 'Role',
  195. 'foreign_key' => $roleId,
  196. );
  197. if ($Permission->allow($aro, $aco)) {
  198. $this->success(__d('croogo', 'Permission %s granted to %s', $aco, $Role->byId($roleId)));
  199. }
  200. }
  201. }
  202. }
  203. }