Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
UserModule.php
1 <?php
2 /**
3  * Yii-User module
4  *
5  * @author Mikhail Mangushev <mishamx@gmail.com>
6  * @link http://yii-user.googlecode.com/
7  * @license http://www.opensource.org/licenses/bsd-license.php
8  * @version $Id: UserModule.php 105 2011-02-16 13:05:56Z mishamx $
9  */
10 
11 class UserModule extends CWebModule
12 {
13  /**
14  * @var int
15  * @desc items on page
16  */
17  public $user_page_size = 10;
18 
19  /**
20  * @var int
21  * @desc items on page
22  */
23  public $fields_page_size = 10;
24 
25  /**
26  * @var string
27  * @desc hash method (md5,sha1 or algo hash function http://www.php.net/manual/en/function.hash.php)
28  */
29  public $hash = 'md5';
30 
31  /**
32  * @var boolean
33  * @desc use email for activation user account
34  */
35  public $sendActivationMail = true;
36 
37  //Subject of mail in activation mail. site_name - replaced by value 'name' in main.php
38  public $activationMailSubject = 'You registered from {site_name}';
39 
40  /**
41  * @var string email which is displayed in 'Send from:" in activation mail
42  */
43  public $activationEmail;
44 
45  /**
46  * @var boolean
47  * @desc allow auth for is not active user
48  */
49  public $loginNotActiv = false;
50 
51  /**
52  * @var boolean
53  * @desc activate user on registration (only $sendActivationMail = false)
54  */
55  public $activeAfterRegister = false;
56 
57  /**
58  * Amount of bad login attemps after which you couldnt login for $loginBlockTime
59  */
60  public $loginBadAttemps = 3;
61 
62  /**
63  * Time in seconds for blocking ability to login after $loginBadAttemps times
64  *
65  * @public string
66  **/
67  public $loginBlockTime = 300;
68 
69  /**
70  * If login widget using Ajax for field validation
71  */
72  public $loginAjax = false;
73 
74  /**
75  * @var boolean
76  * @desc login after registration (need loginNotActiv or activeAfterRegister = true)
77  */
78  public $autoLogin = true;
79 
80  private $_registrationUrl = array(
81  'en' => array("/user/registration")
82  );
83  private $_recoveryUrl = array(
84  'en' => "/user/recovery/recovery"
85  );
86  private $_logoutUrl = array(
87  'en' => "/user/logout"
88  );
89  private $_profileUrl = array(
90  'en' => "/user/profile"
91  );
92  private $_returnUrl = array(
93  'en' => "/user/profile"
94  );
95  private $_returnLogoutUrl = array(
96  'en' => "/user/login"
97  );
98  private $_noPermissionsUrl = array(
99  'en' => '/site/noPermissions'
100  );
101  private $_loginUrl = array(
102  'en' => "/user/login"
103  );
104  private $_homePageUrl = array(
105  'en' => ''
106  );
107  private $_friendListUrl = array();
108  private $_recoveryChangePasswordUrl = array();
109  /**
110  * @var string
111  * @desc link to the CMS page where UserRecoveryWidget is placed. Will be used in userLoginWidget as a href to corresponding page.
112  */
113  public $fieldsMessage = '';
114 
115  /**
116  * @var array
117  * @desc User model relation from other models
118  * @see http://www.yiiframework.com/doc/guide/database.arr
119  */
120  public $relations = array();
121 
122  /**
123  * @var array
124  * @desc Profile model relation from other models
125  */
126  public $profileRelations = array();
127 
128  /**
129  * @var boolean
130  */
131  public $captcha = array('registration' => true);
132 
133  /**
134  * @var int number of seconds that the user can remain in logged-in status. Defaults to 0, meaning login till the user closes the browser.
135  * If greater than 0, cookie-based login will be used.
136  */
137  public $loggedInDuration = 0;
138 
139  /**
140  * @var int minimum length of username field
141  */
142  public $usernameMin = 4;
143 
144  /**
145  * @var string SendFrom email for password forgotten mails
146  */
147  public $passwordForgottenEmail;
148 
149  /**
150  * @var int minimum length of password field
151  */
152  public $passwordMin = 4;
153 
154  public $userLoginWidgetFlashSuccess = 'UserLoginWidget-success';
155 
156  public $userLoginWidgetFlashError = 'UserLoginWidget-error';
157 
158  /**
159  * @var string if value is not defined admin will not be informed about new user is registered
160  */
161  public $registrationNotifyAdminEmail = null;
162  /**
163  * @var string name of the template file which is used to notify administrator
164  */
165  public $registrationNotifyAdminTemplate = 'notifyAdminAboutRegister';
166  /**
167  * @var string url to the page where placed UserProfileEditWidget (for approving emails)
168  */
169  public $profileEditUrl = false;
170 
171  /**
172  * @var string define which user field will be used in login process. 'email' or 'username'. If field != 'email' then username will be used
173  */
174  private $_authField = 'email';
175 
176  private $_assetsUrl;
177 
178  /**
179  * Public setter
180  *
181  * @param string $value value
182  *
183  * @throws Exception
184  * @return void
185  */
186  public function setAuthField($value)
187  {
188  if (!in_array($value, array('username', 'email'))) {
189  throw new Exception('authField can have value "username" or "email"');
190  }
191  $this->_authField = $value;
192  }
193 
194  /**
195  * Public getter
196  *
197  * @return string
198  */
199  public function getAuthField()
200  {
201  return $this->_authField;
202  }
203 
204  /**
205  * @param array $value
206  */
207  public function setLoginUrl(array $value)
208  {
209  $this->_loginUrl = $value;
210  }
211 
212  /**
213  * @param array $value
214  */
215  public function setRegistrationUrl(array $value)
216  {
217  $this->_registrationUrl = $value;
218  }
219 
220  /**
221  * @param array $value
222  */
223  public function setRecoveryUrl(array $value)
224  {
225  $this->_recoveryUrl = $value;
226  }
227 
228  /**
229  * @param array $value
230  */
231  public function setReturnUrl(array $value)
232  {
233  $this->_returnUrl = $value;
234  }
235 
236  /**
237  * @param array $value
238  */
239  public function setReturnLogoutUrl(array $value)
240  {
241  $this->_returnLogoutUrl = $value;
242  }
243 
244  /**
245  * @param array $value
246  */
247  public function setHomePageUrl(array $value)
248  {
249  $this->_homePageUrl = $value;
250  }
251 
252  /**
253  * @param array $value
254  */
255  public function setFriendListUrl(array $value)
256  {
257  $this->_friendListUrl = $value;
258  }
259 
260  public function setRecoveryChangePasswordUrl(array $value)
261  {
262  $this->_recoveryChangePasswordUrl = $value;
263  }
264 
265  /**
266  * @param array $value
267  */
268  public function setNoPermissionsUrl(array $value)
269  {
270  $this->_noPermissionsUrl = $value;
271  }
272 
273  public function getRecoveryChangePasswordUrl($locale = '')
274  {
275  return $this->_getUrl('_recoveryChangePasswordUrl', $locale);
276  }
277 
278  public function getLoginUrl($locale = '')
279  {
280  return $this->_getUrl('_loginUrl', $locale, '/user/login');
281  }
282 
283  public function getFriendListUrl($locale = '')
284  {
285  return $this->_getUrl('_friendListUrl', $locale);
286  }
287 
288  public function getRegistrationUrl($locale = '')
289  {
290  return $this->_getUrl('_registrationUrl', $locale);
291  }
292 
293  public function getRecoveryUrl($locale = '')
294  {
295  return $this->_getUrl('_recoveryUrl', $locale);
296  }
297 
298  public function getReturnUrl($locale = '')
299  {
300  return $this->_getUrl('_returnUrl', $locale);
301  }
302 
303  public function getReturnLogoutUrl($locale = '')
304  {
305  return $this->_getUrl('_returnLogoutUrl', $locale);
306  }
307 
308  public function getNoPermissionsUrl($locale = '')
309  {
310  return $this->_getUrl('_noPermissionsUrl', $locale);
311  }
312 
313  public function getHomePageUrl($locale = '')
314  {
315  return $this->_getUrl('_homePageUrl', $locale);
316  }
317 
318  /**
319  * Method for getting right url depends of locale
320  *
321  * @param string $urlField name of private property
322  * @param string $locale requested locale
323  * @param string $default default value if urls are not set
324  *
325  * @return mixed
326  */
327  private function _getUrl($urlField, $locale, $default = '')
328  {
329  if (empty($locale)) {
330  $locale = Yii::app()->language;
331  }
332  if (isset($this->{$urlField}[$locale])) {
333  return $this->{$urlField}[$locale];
334  } else {
335  $tmp = array_values($this->$urlField);
336  return count($this->$urlField) > 0 ? array_shift($tmp) : $default;
337  }
338  }
339 
340  /**
341  * @var bool define if username should be unique during registration
342  */
343  public $usernameUnique = true;
344 
345  /**
346  * @var bool define if username will be shown in register process
347  */
348  public $usernameRequired = true;
349 
350  /**
351  * @var boolean
352  */
353  //public $cacheEnable = false;
354 
355  public $tableUsers = '{{users}}';
356  public $tableProfiles = '{{profiles}}';
357  public $tableProfileFields = '{{profiles_fields}}';
358  public $tableAccounts = '{{accounts}}';
359 
360  static private $_user;
361  static private $_admin;
362  static private $_admins;
363 
364  /**
365  * @var array
366  * @desc Behaviors for models
367  */
368  public $componentBehaviors = array();
369 
370  public function init()
371  {
372  // this method is called when the module is being created
373  // you may place code here to customize the module or the application
374 
375  // import the module-level models and components
376  $this->setImport(array(
377  'user.models.*',
378  'user.components.*',
379  'user.widgets.*',
380  'user.helpers.*',
381  'user.services.*',
382  ));
383  Yii::app()->user->loginUrl = $this->getLoginUrl();
384  }
385 
386  public function getBehaviorsFor($componentName)
387  {
388  if (isset($this->componentBehaviors[$componentName])) {
389  return $this->componentBehaviors[$componentName];
390  } else {
391  return array();
392  }
393  }
394 
395  public function beforeControllerAction($controller, $action)
396  {
397  if (parent::beforeControllerAction($controller, $action)) {
398  // this method is called before any module controller action is performed
399  // you may place customized code here
400  return true;
401  } else
402  return false;
403  }
404 
405  /**
406  * @param $str
407  * @param $params
408  * @param $dic
409  * @return string
410  */
411  public static function t($str = '', $params = array(), $dic = 'user')
412  {
413  return Yii::t("UserModule." . $dic, $str, $params);
414  }
415 
416  /**
417  * @return hash string.
418  */
419  public static function encrypting($string = "")
420  {
421  $hash = Yii::app()->getModule('user')->hash;
422  if ($hash == "md5")
423  return md5($string);
424  if ($hash == "sha1")
425  return sha1($string);
426  else
427  return hash($hash, $string);
428  }
429 
430  /**
431  * @param $place
432  * @return boolean
433  */
434  public static function doCaptcha($place = '')
435  {
436  if (!extension_loaded('gd'))
437  return false;
438  if (in_array($place, Yii::app()->getModule('user')->captcha))
439  return Yii::app()->getModule('user')->captcha[$place];
440  return false;
441  }
442 
443  /**
444  * Return admin status.
445  * @return boolean
446  */
447  public static function isAdmin()
448  {
449  if (Yii::app()->user->isGuest)
450  return false;
451  else {
452  return Yii::app()->user->checkAccess('Admin');
453  }
454  }
455 
456  /**
457  * Return safe user data.
458  * @param user id not required
459  * @return user object or false
460  */
461  public static function user($id = 0)
462  {
463  if ($id)
464  return User::model()->active()->findbyPk($id);
465  else {
466  if (Yii::app()->user->isGuest) {
467  return false;
468  } else {
469  if (!self::$_user)
470  self::$_user = User::model()->active()->findbyPk(Yii::app()->user->id);
471  return self::$_user;
472  }
473  }
474  }
475 
476  public function getAssetsUrl()
477  {
478  if ($this->_assetsUrl === null) {
479  $assetsPath = Yii::getPathOfAlias('user.views.asset');
480  $this->_assetsUrl = Yii::app()->getAssetManager()->publish($assetsPath, true);
481  }
482 
483  return $this->_assetsUrl;
484  }
485 
486 }