Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
LoginController.php
1 <?php
2 
4 {
5  public $defaultAction = 'login';
6 
7  /**
8  * Variable which is used by BadLoginAttemptsFilter to indicate that attepts are ended
9  *
10  * @public string
11  **/
12  public $attemptsEnded = false;
13 
14  /**
15  * Variable which is used by BadLoginAttemptsFilter to show remaiend for blocking login
16  *
17  * @public string
18  **/
19  public $timeRemains;
20 
21  public function filters(){
22  return array(
23  array(
24  'BadLoginAttemptsFilter + standard',
25  'attemptsCount' => $this->module->loginBadAttemps,
26  'blockTime' => $this->module->loginBlockTime,
27  )
28  );
29  }
30 
31  public function actionStandard()
32  {
33  $loginForm = new UserLogin();
34  if(!$this->attemptsEnded){
35  $loginForm = new UserLogin();
36  $loginForm->attributes = @$_POST['UserLogin'];
37  if ($loginForm->validate()) {
38  if ($loginForm->service == 'standard' && array_key_exists('standard', Yii::app()->eauth->services)) {
39  $userIdentity = new UserIdentity($loginForm->authField, $loginForm->password);
40  if($userIdentity->authenticate() && !isset($_POST['ajax'])) {
41  Yii::app()->user->setState('UserLogin', null);
42  $returnUrl = Yii::app()->user->getReturnUrl();
43  Yii::app()->user->login($userIdentity, $this->module->loggedInDuration * intval($loginForm->rememberMe));
44  if($returnUrl != '/'){
45  $this->redirect($returnUrl);
46  }
47  Yii::app()->user->setFlash(Yii::app()->getModule('user')->userLoginWidgetFlashSuccess, UserModule::t('You have been successfully logged in.'));
48  } else {
49  $loginForm->applyErrors($userIdentity);
50  }
51  if (isset($_REQUEST['ajax'])) {
52  // ajax validator
53  echo $loginForm->getAjaxErrors();
54  Yii::app()->end();
55  } else {
56  //if no ajax validation save model data to session
57  $loginForm->password = null;
58  Yii::app()->user->setState('UserLogin', array('attributes' => $loginForm->attributes, 'errors' => $loginForm->getErrors()));
59  }
60  } else {
61  throw new CHttpException(400, 'Wrong service');
62  }
63  } elseif (isset($_POST['ajax'])){
64  echo $loginForm->getAjaxErrors();
65  Yii::app()->end();
66  } else {
67  $loginForm->password = null;
68  Yii::app()->user->setState('UserLogin', array('attributes' => $loginForm->attributes, 'errors' => $loginForm->getErrors()));
69  }
70  } else {
71  Yii::app()->user->setFlash(
72  $this->module->userLoginWidgetFlashError, UserModule::t('Please wait {s} seconds before next login.', array('{s}' => $this->timeRemains)));
73  }
74 
75  if (isset($_POST['return_url'])) {
76  $this->redirect($_POST['return_url'], false);
77  } else{
78  $this->render('/user/login', array('model' => $loginForm));
79  }
80  }
81 
82  public function actionTpa()
83  {
84  $loginForm = new UserLogin();
85  $loginForm->attributes = @$_GET['UserLogin'];
86  if (isset($_GET['ajax'])){
87  echo $loginForm->getAjaxErrors();
88  Yii::app()->end();
89  }
90  if ($loginForm->validate()) {
91  if ($loginForm->service != 'standard' && array_key_exists($loginForm->service, Yii::app()->eauth->services)) {
92  if(isset($_GET['ajax'])) {
93  throw new CHttpException(400, 'Wrong request');
94  }
95  $serviceIdentity = Yii::app()->eauth->getIdentity($loginForm->service);
96  $serviceIdentity->redirectUrl = Yii::app()->user->returnUrl;
97  $serviceIdentity->cancelUrl = $this->createAbsoluteUrl('user/login');
98  if ($serviceIdentity->authenticate()) {
99  $serviceIdentity->getAttributes();
100  $this->onTPAAuthorizationSuccess($serviceIdentity);
101  $userIdentity = new SUserIdentity($serviceIdentity);
102  if($userIdentity->authenticate()) {
103  Yii::app()->user->setState('UserLogin', null);
104  Yii::app()->user->login($userIdentity, $this->module->loggedInDuration * intval($loginForm->rememberMe));
105  } else {
106  $loginForm->applyErrors($userIdentity);
107  }
108  } else {
109  Yii::app()->user->setFlash('error', $serviceIdentity->getError());
110  }
111  } else {
112  throw new CHttpException(400, 'Wrong service');
113  }
114  } else {
115  $loginForm->password = null;
116  Yii::app()->user->setState('UserLogin', array('attributes' => $loginForm->attributes, 'errors' => $loginForm->getErrors()));
117  }
118 
119  if (isset($_GET['return_url'])) {
120  $this->redirect($_GET['return_url']);
121  } else
122  $this->render('/user/login', array('model' => $loginForm));
123  }
124 
125 
126  private function lastViset()
127  {
128  $lastVisit = User::model()->notsafe()->findByPk(Yii::app()->user->id);
129  $lastVisit->lastvisit = time();
130  $lastVisit->save();
131  }
132 
133  public function onLogin($userIdentity)
134  {
135  $message = "User: {$userIdentity->username} logged in. Return URL: " . Yii::app()->user->returnUrl . ". \$_REQUEST['return_url']: " . @$_REQUEST['return_url'];
136  $notification = new Notification('Login', $message);
137  Yii::app()->notificationManager->notifyAbout($notification);
138  }
139 
140 
141  public function onTPAAuthorizationSuccess($serviceIdentity)
142  {
143  $message = "Account {$serviceIdentity->getEmail()} from TPA: {$serviceIdentity->serviceName} successfuly authorized\n";
144  $notification = new Notification('TPA', $message);
145  Yii::app()->notificationManager->notifyAbout($notification);
146  }
147 
148 }