HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
User.php
Go to the documentation of this file.
1 <?php
2 
3 class User extends CActiveRecord
4 {
5  const STATUS_NOACTIVE = 0;
6  const STATUS_ACTIVE = 1;
7  const STATUS_BANNED = -1;
8  const STATUS_TEMP = 2;
9 
10  //TODO: Delete for next version (backward compatibility)
11  const STATUS_BANED = -1;
12 
34  public static function model($className = __CLASS__)
35  {
36  return parent::model($className);
37  }
38 
42  public function tableName()
43  {
44  return Yii::app()->getModule('user')->tableUsers;
45  }
46 
50  public function rules()
51  {
52  // NOTE: you should only define rules for those attributes that
53  // will receive user inputs.CConsoleApplication
54  return ((get_class(Yii::app()) == 'CConsoleApplication' || (get_class(Yii::app()) != 'CConsoleApplication' && Yii::app()->getModule('user')->isAdmin())) ? array(
55  array('username', 'length', 'max' => 20, 'min' => 3, 'message' => UserModule::t("Incorrect username (length between 3 and 20 characters).")),
56  array('password', 'length', 'max' => 128, 'min' => 4, 'message' => UserModule::t("Incorrect password (minimal length 4 symbols).")),
57  array('email', 'email'),
58  array('username', 'unique', 'message' => UserModule::t("This user's name already exists.")),
59  array('email', 'unique', 'message' => UserModule::t("This user's email address already exists.")),
60  array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u', 'message' => UserModule::t("Incorrect symbols (A-z0-9).")),
61  array('status', 'in', 'range' => array(self::STATUS_NOACTIVE, self::STATUS_ACTIVE, self::STATUS_BANNED, self::STATUS_TEMP)),
62  array('superuser', 'in', 'range' => array(0, 1)),
63  array('create_at', 'default', 'value' => date('Y-m-d H:i:s'), 'setOnEmpty' => true, 'on' => 'insert'),
64  array('lastvisit_at', 'default', 'value' => '0000-00-00 00:00:00', 'setOnEmpty' => true, 'on' => 'insert'),
65  array('username, email, superuser, status', 'required'),
66  array('superuser, status', 'numerical', 'integerOnly' => true),
67  array('id, username, password, email, activkey, create_at, lastvisit_at, superuser, status', 'safe', 'on' => 'search'),
68  ) : ((Yii::app()->user->id == $this->id) ? array(
69  array('username, email', 'required'),
70  array('username', 'length', 'max' => 20, 'min' => 3, 'message' => UserModule::t("Incorrect username (length between 3 and 20 characters).")),
71  array('email', 'email'),
72  array('username', 'unique', 'message' => UserModule::t("This user's name already exists.")),
73  array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u', 'message' => UserModule::t("Incorrect symbols (A-z0-9).")),
74  array('email', 'unique', 'message' => UserModule::t("This user's email address already exists.")),
75  ) : array()));
76  }
77 
81  public function relations()
82  {
83  $relations = Yii::app()->getModule('user')->relations;
84  if (!isset($relations['profile'])) {
85  $relations['profile'] = array(self::HAS_ONE, 'Profile', 'user_id');
86  }
87 
88  return $relations;
89  }
90 
94  public function attributeLabels()
95  {
96  return array(
97  'id' => UserModule::t("Id"),
98  'username' => UserModule::t("username"),
99  'password' => UserModule::t("password"),
100  'verifyPassword' => UserModule::t("Retype Password"),
101  'email' => UserModule::t("E-mail"),
102  'verifyCode' => UserModule::t("Verification Code"),
103  'activkey' => UserModule::t("activation key"),
104  'createtime' => UserModule::t("Registration date"),
105  'create_at' => UserModule::t("Registration date"),
106 
107  'lastvisit_at' => UserModule::t("Last visit"),
108  'superuser' => UserModule::t("Superuser"),
109  'status' => UserModule::t("Status"),
110  );
111  }
112 
113  public function scopes()
114  {
115  return array(
116  'active' => array(
117  'condition' => 'status='.self::STATUS_ACTIVE,
118  ),
119  'notactive' => array(
120  'condition' => 'status='.self::STATUS_NOACTIVE,
121  ),
122  'banned' => array(
123  'condition' => 'status='.self::STATUS_BANNED,
124  ),
125  'banned' => array(
126  'condition' => 'status='.self::STATUS_TEMP,
127  ),
128  'superuser' => array(
129  'condition' => 'superuser=1',
130  ),
131  'notsafe' => array(
132  'select' => 'id, username, password, email, activkey, create_at, lastvisit_at, superuser, status',
133  ),
134  );
135  }
136 
137  public function defaultScope()
138  {
139  return CMap::mergeArray(Yii::app()->getModule('user')->defaultScope, array(
140  'alias' => 'user',
141  'select' => 'user.id, user.username, user.email, user.create_at, user.lastvisit_at, user.superuser, user.status, user.token, user.activkey',
142  ));
143  }
144 
145  public static function itemAlias($type, $code = null)
146  {
147  $_items = array(
148  'UserStatus' => array(
149  self::STATUS_NOACTIVE => UserModule::t('Not active'),
150  self::STATUS_ACTIVE => UserModule::t('Active'),
151  self::STATUS_BANNED => UserModule::t('Banned'),
152  self::STATUS_TEMP => UserModule::t('Temp'),
153  ),
154  'AdminStatus' => array(
155  '0' => UserModule::t('No'),
156  '1' => UserModule::t('Yes'),
157  ),
158  );
159  if (isset($code)) {
160  return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
161  } else {
162  return isset($_items[$type]) ? $_items[$type] : false;
163  }
164  }
165 
171  public function search()
172  {
173  // Warning: Please modify the following code to remove attributes that
174  // should not be searched.
175 
176  $criteria = new CDbCriteria();
177 
178  $criteria->compare('id', $this->id);
179  $criteria->compare('username', $this->username, true);
180  $criteria->compare('password', $this->password);
181  $criteria->compare('email', $this->email, true);
182  $criteria->compare('activkey', $this->activkey);
183  $criteria->compare('create_at', $this->create_at);
184  $criteria->compare('lastvisit_at', $this->lastvisit_at);
185  $criteria->compare('superuser', $this->superuser);
186  $criteria->compare('status', $this->status);
187 
188  return new CActiveDataProvider(get_class($this), array(
189  'criteria' => $criteria,
190  'pagination' => array(
191  'pageSize' => Yii::app()->getModule('user')->user_page_size,
192  ),
193  ));
194  }
195 
196  public function getCreatetime()
197  {
198  return strtotime($this->create_at);
199  }
200 
201  public function setCreatetime($value)
202  {
203  $this->create_at = date('Y-m-d H:i:s', $value);
204  }
205 
206  public function getLastvisit()
207  {
208  return strtotime($this->lastvisit_at);
209  }
210 
211  public function setLastvisit($value)
212  {
213  $this->lastvisit_at = date('Y-m-d H:i:s', $value);
214  }
215 
221  public function behaviors()
222  {
223  return array(
224  'CBase64Behavior' => array(
225  'class' => 'CBase64Behavior',
226  'serialAttributes' => array('token'),
227  ),
228  );
229  }
230 }