HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
UserModule.php
Go to the documentation of this file.
1 <?php
13 class UserModule extends CWebModule
14 {
19  public $user_page_size = 10;
20 
25  public $fields_page_size = 10;
26 
31  public $hash = 'md5';
32 
37  public $sendActivationMail = true;
38 
43  public $loginNotActiv = false;
44 
49  public $activeAfterRegister = false;
50 
55  public $autoLogin = true;
56 
57  public $registrationUrl = array("/user/registration");
58  public $recoveryUrl = array("/user/recovery/recovery");
59  public $loginUrl = array("/user/login");
60  public $logoutUrl = array("/user/logout");
61  public $profileUrl = array("/user/profile");
62  public $returnUrl = array("/user/profile");
63  public $returnLogoutUrl = array("/user/login");
64 
69  public $rememberMeTime = 2592000; // 30 days
70 
71  public $fieldsMessage = '';
72 
79  public $relations = array();
80 
85  public $profileRelations = array();
86 
90  public $captcha = array('registration' => true);
91 
95  //public $cacheEnable = false;
96 
97  public $tableUsers = '{{users}}';
98  public $tableProfiles = '{{profiles}}';
99  public $tableProfileFields = '{{profiles_fields}}';
100 
101  public $defaultScope = array(
102  'with' => array('profile'),
103  );
104 
105  private static $_user;
106  private static $_users = array();
107  private static $_userByName = array();
108  private static $_admin;
109  private static $_admins;
110 
115  public $componentBehaviors = array();
116 
117  public function init()
118  {
119  // this method is called when the module is being created
120  // you may place code here to customize the module or the application
121 
122  // import the module-level models and components
123  $this->setImport(array(
124  'user.models.*',
125  'user.components.*',
126  ));
127  }
128 
129  public function getBehaviorsFor($componentName)
130  {
131  if (isset($this->componentBehaviors[$componentName])) {
132  return $this->componentBehaviors[$componentName];
133  } else {
134  return array();
135  }
136  }
137 
138  public function beforeControllerAction($controller, $action)
139  {
140  if (parent::beforeControllerAction($controller, $action)) {
141  // this method is called before any module controller action is performed
142  // you may place customized code here
143  return true;
144  } else {
145  return false;
146  }
147  }
148 
156  public static function t($str = '', $params = array(), $dic = 'user')
157  {
158  if (Yii::t("UserModule", $str) == $str) {
159  return Yii::t("UserModule.".$dic, $str, $params);
160  } else {
161  return Yii::t("UserModule", $str, $params);
162  }
163  }
164 
168  public static function encrypting($string = "")
169  {
170  $hash = Yii::app()->getModule('user')->hash;
171  if ($hash == "md5") {
172  return md5($string);
173  }
174  if ($hash == "sha1") {
175  return sha1($string);
176  } else {
177  return hash($hash, $string);
178  }
179  }
180 
186  public static function doCaptcha($place = '')
187  {
188  if (!extension_loaded('gd')) {
189  return false;
190  }
191  if (in_array($place, Yii::app()->getModule('user')->captcha)) {
192  return Yii::app()->getModule('user')->captcha[$place];
193  }
194 
195  return false;
196  }
197 
203  public static function isAdmin()
204  {
205  if (Yii::app()->user->isGuest) {
206  return false;
207  } else {
208  if (!isset(self::$_admin)) {
209  if (self::user()->superuser) {
210  self::$_admin = true;
211  } else {
212  self::$_admin = false;
213  }
214  }
215 
216  return self::$_admin;
217  }
218  }
219 
225  public static function getAdmins()
226  {
227  if (!self::$_admins) {
228  $admins = User::model()->active()->superuser()->findAll();
229  $return_name = array();
230  foreach ($admins as $admin) {
231  array_push($return_name, $admin->username);
232  }
233  self::$_admins = ($return_name) ? $return_name : array('');
234  }
235 
236  return self::$_admins;
237  }
238 
242  public static function sendMail($email, $subject, $message)
243  {
244  $adminEmail = Yii::app()->params['adminEmail'];
245  $headers = "MIME-Version: 1.0\r\nFrom: $adminEmail\r\nReply-To: $adminEmail\r\nContent-Type: text/html; charset=utf-8";
246  $message = wordwrap($message, 70);
247  $message = str_replace("\n.", "\n..", $message);
248 
249  return mail($email, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $headers);
250  }
251 
259  public static function user($id = 0, $clearCache = false)
260  {
261  if (!$id && !Yii::app()->user->isGuest) {
262  $id = Yii::app()->user->id;
263  }
264  if ($id) {
265  if (!isset(self::$_users[$id]) || $clearCache) {
266  self::$_users[$id] = User::model()->with(array('profile'))->findbyPk($id);
267  }
268 
269  return self::$_users[$id];
270  } else {
271  return false;
272  }
273  }
274 
282  public static function getUserByName($username)
283  {
284  if (!isset(self::$_userByName[$username])) {
285  $_userByName[$username] = User::model()->findByAttributes(array('username' => $username));
286  }
287 
288  return $_userByName[$username];
289  }
290 
298  public function users()
299  {
300  return User;
301  }
302 
308  public static function isGuest()
309  {
310  return Yii::app()->user->isGuest;
311  }
312 
318  public static function isUserTemp()
319  {
320  $user = self::user();
321  if (empty($user)) {
322  return false;
323  }
324  return $user->status == User::STATUS_TEMP;
325  }
326 
332  public static function isNotGuestAndUserTemp()
333  {
334  return (!self::isGuest() && !self::isUserTemp());
335  }
336 
342  public static function getUserId()
343  {
344  return Yii::app()->user->id;
345  }
346 
350  public static function createAndLoginUserTemp()
351  {
352  $sessionId = Yii::app()->getSession()->getSessionId();
353  $emailDomain = '@temp.com';
354  $password = '';
355 
356  $RegistrationForm = new RegistrationForm();
357  $User = User::model()->findByAttributes(array('username' => $sessionId));
358  if (empty($User)) {
359  $RegistrationForm->username = $sessionId;
360  $RegistrationForm->email = $sessionId.$emailDomain;
361  $RegistrationForm->password = UserModule::encrypting($password);
362  $RegistrationForm->status = User::STATUS_TEMP;
363 
364  if (!$RegistrationForm->save(false)) {
365  return false;
366  }
367 
368  $profile = new Profile();
369  $profile->user_id = $RegistrationForm->id;
370  $profile->lastname = '-';
371  $profile->firstname = '-';
372  $profile->save(false);
373 
374  $RegistrationForm->setDefaultTempUserAssignments($RegistrationForm->id);
375  } else {
376  $RegistrationForm = $User;
377  }
378 
379  $identity = new UserIdentity($sessionId, $password);
380  $identity->authenticate();
381  Yii::app()->user->login($identity, 0);
382 
383  $sessionId = Yii::app()->getSession()->getSessionId();
384  $RegistrationForm->username = $sessionId;
385  $RegistrationForm->email = $sessionId.$emailDomain;
386  $RegistrationForm->save(false);
387 
388  return true;
389  }
390 }