PageRenderTime 135ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/protected/modules/user/migrations/m110805_153437_installYiiUser.php

https://bitbucket.org/rohitrox/hotc
PHP | 212 lines | 198 code | 13 blank | 1 comment | 15 complexity | 86947d80fc9d881ccbf7be11873127a1 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. class m110805_153437_installYiiUser extends CDbMigration
  3. {
  4. protected $MySqlOptions = 'ENGINE=InnoDB CHARSET=utf8';
  5. private $_model;
  6. public function safeUp()
  7. {
  8. if (!Yii::app()->getModule('user')) {
  9. echo "\n\nAdd to console.php :\n"
  10. ."'modules'=>array(\n"
  11. ."...\n"
  12. ." 'user'=>array(\n"
  13. ." ... # copy settings from main config\n"
  14. ." ),\n"
  15. ."...\n"
  16. ."),\n"
  17. ."\n";
  18. return false;
  19. }
  20. Yii::import('user.models.User');
  21. //*
  22. switch ($this->dbType()) {
  23. case "mysql":
  24. $this->createTable(Yii::app()->getModule('user')->tableUsers, array(
  25. "id" => "pk",
  26. "username" => "varchar(20) NOT NULL DEFAULT ''",
  27. "password" => "varchar(128) NOT NULL DEFAULT ''",
  28. "email" => "varchar(128) NOT NULL DEFAULT ''",
  29. "activkey" => "varchar(128) NOT NULL DEFAULT ''",
  30. "createtime" => "int(10) NOT NULL DEFAULT 0",
  31. "lastvisit" => "int(10) NOT NULL DEFAULT 0",
  32. "superuser" => "int(1) NOT NULL DEFAULT 0",
  33. "status" => "int(1) NOT NULL DEFAULT 0",
  34. ), $this->MySqlOptions);
  35. $this->createIndex('user_username', Yii::app()->getModule('user')->tableUsers, 'username', true);
  36. $this->createIndex('user_email', Yii::app()->getModule('user')->tableUsers, 'email', true);
  37. $this->createTable(Yii::app()->getModule('user')->tableProfiles, array(
  38. 'user_id' => 'pk',
  39. 'first_name' => 'string',
  40. 'last_name' => 'string',
  41. ), $this->MySqlOptions);
  42. $this->addForeignKey('user_profile_id', Yii::app()->getModule('user')->tableProfiles, 'user_id', Yii::app()->getModule('user')->tableUsers, 'id', 'CASCADE', 'RESTRICT');
  43. $this->createTable(Yii::app()->getModule('user')->tableProfileFields, array(
  44. "id" => "pk",
  45. "varname" => "varchar(50) NOT NULL DEFAULT ''",
  46. "title" => "varchar(255) NOT NULL DEFAULT ''",
  47. "field_type" => "varchar(50) NOT NULL DEFAULT ''",
  48. "field_size" => "int(3) NOT NULL DEFAULT 0",
  49. "field_size_min" => "int(3) NOT NULL DEFAULT 0",
  50. "required" => "int(1) NOT NULL DEFAULT 0",
  51. "match" => "varchar(255) NOT NULL DEFAULT ''",
  52. "range" => "varchar(255) NOT NULL DEFAULT ''",
  53. "error_message" => "varchar(255) NOT NULL DEFAULT ''",
  54. "other_validator" => "text",
  55. "default" => "varchar(255) NOT NULL DEFAULT ''",
  56. "widget" => "varchar(255) NOT NULL DEFAULT ''",
  57. "widgetparams" => "text",
  58. "position" => "int(3) NOT NULL DEFAULT 0",
  59. "visible" => "int(1) NOT NULL DEFAULT 0",
  60. ), $this->MySqlOptions);
  61. break;
  62. case "sqlite":
  63. default:
  64. $this->createTable(Yii::app()->getModule('user')->tableUsers, array(
  65. "id" => "pk",
  66. "username" => "varchar(20) NOT NULL",
  67. "password" => "varchar(128) NOT NULL",
  68. "email" => "varchar(128) NOT NULL",
  69. "activkey" => "varchar(128) NOT NULL",
  70. "createtime" => "int(10) NOT NULL",
  71. "lastvisit" => "int(10) NOT NULL",
  72. "superuser" => "int(1) NOT NULL",
  73. "status" => "int(1) NOT NULL",
  74. ));
  75. $this->createIndex('user_username', Yii::app()->getModule('user')->tableUsers, 'username', true);
  76. $this->createIndex('user_email', Yii::app()->getModule('user')->tableUsers, 'email', true);
  77. $this->createTable(Yii::app()->getModule('user')->tableProfiles, array(
  78. 'user_id' => 'pk',
  79. 'first_name' => 'string',
  80. 'last_name' => 'string',
  81. ));
  82. $this->createTable(Yii::app()->getModule('user')->tableProfileFields, array(
  83. "id" => "pk",
  84. "varname" => "varchar(50) NOT NULL",
  85. "title" => "varchar(255) NOT NULL",
  86. "field_type" => "varchar(50) NOT NULL",
  87. "field_size" => "int(3) NOT NULL",
  88. "field_size_min" => "int(3) NOT NULL",
  89. "required" => "int(1) NOT NULL",
  90. "match" => "varchar(255) NOT NULL",
  91. "range" => "varchar(255) NOT NULL",
  92. "error_message" => "varchar(255) NOT NULL",
  93. "other_validator" => "text NOT NULL",
  94. "default" => "varchar(255) NOT NULL",
  95. "widget" => "varchar(255) NOT NULL",
  96. "widgetparams" => "text NOT NULL",
  97. "position" => "int(3) NOT NULL",
  98. "visible" => "int(1) NOT NULL",
  99. ));
  100. break;
  101. }//*/
  102. if (in_array('--interactive=0',$_SERVER['argv'])) {
  103. $this->_model->username = 'admin';
  104. $this->_model->email = 'webmaster@example.com';
  105. $this->_model->password = 'admin';
  106. } else {
  107. $this->readStdinUser('Admin login', 'username', 'admin');
  108. $this->readStdinUser('Admin email', 'email', 'webmaster@example.com');
  109. $this->readStdinUser('Admin password', 'password', 'admin');
  110. }
  111. $this->insert(Yii::app()->getModule('user')->tableUsers, array(
  112. "id" => "1",
  113. "username" => $this->_model->username,
  114. "password" => Yii::app()->getModule('user')->encrypting($this->_model->password),
  115. "email" => "webmaster@example.com",
  116. "activkey" => Yii::app()->getModule('user')->encrypting(microtime()),
  117. "createtime" => time(),
  118. "lastvisit" => "0",
  119. "superuser" => "1",
  120. "status" => "1",
  121. ));
  122. $this->insert(Yii::app()->getModule('user')->tableProfiles, array(
  123. "user_id" => "1",
  124. "first_name" => "Administrator",
  125. "last_name" => "Admin",
  126. ));
  127. $this->insert(Yii::app()->getModule('user')->tableProfileFields, array(
  128. "id" => "1",
  129. "varname" => "first_name",
  130. "title" => "First Name",
  131. "field_type" => "VARCHAR",
  132. "field_size" => "255",
  133. "field_size_min" => "3",
  134. "required" => "2",
  135. "match" => "",
  136. "range" => "",
  137. "error_message" => "Incorrect First Name (length between 3 and 50 characters).",
  138. "other_validator" => "",
  139. "default" => "",
  140. "widget" => "",
  141. "widgetparams" => "",
  142. "position" => "1",
  143. "visible" => "3",
  144. ));
  145. $this->insert(Yii::app()->getModule('user')->tableProfileFields, array(
  146. "id" => "2",
  147. "varname" => "last_name",
  148. "title" => "Last Name",
  149. "field_type" => "VARCHAR",
  150. "field_size" => "255",
  151. "field_size_min" => "3",
  152. "required" => "2",
  153. "match" => "",
  154. "range" => "",
  155. "error_message" => "Incorrect Last Name (length between 3 and 50 characters).",
  156. "other_validator" => "",
  157. "default" => "",
  158. "widget" => "",
  159. "widgetparams" => "",
  160. "position" => "2",
  161. "visible" => "3",
  162. ));
  163. }
  164. public function safeDown()
  165. {
  166. $this->dropTable(Yii::app()->getModule('user')->tableProfileFields);
  167. $this->dropTable(Yii::app()->getModule('user')->tableProfiles);
  168. $this->dropTable(Yii::app()->getModule('user')->tableUsers);
  169. }
  170. public function dbType()
  171. {
  172. list($type) = explode(':',Yii::app()->db->connectionString);
  173. echo "type db: ".$type."\n";
  174. return $type;
  175. }
  176. private function readStdin($prompt, $valid_inputs, $default = '') {
  177. while(!isset($input) || (is_array($valid_inputs) && !in_array($input, $valid_inputs)) || ($valid_inputs == 'is_file' && !is_file($input))) {
  178. echo $prompt;
  179. $input = strtolower(trim(fgets(STDIN)));
  180. if(empty($input) && !empty($default)) {
  181. $input = $default;
  182. }
  183. }
  184. return $input;
  185. }
  186. private function readStdinUser($prompt, $field, $default = '') {
  187. if (!$this->_model)
  188. $this->_model = new User;
  189. while(!isset($input) || !$this->_model->validate(array($field))) {
  190. echo $prompt.(($default)?" [$default]":'').': ';
  191. $input = (trim(fgets(STDIN)));
  192. if(empty($input) && !empty($default)) {
  193. $input = $default;
  194. }
  195. $this->_model->setAttribute($field,$input);
  196. }
  197. return $input;
  198. }
  199. }