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