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  Yii::app()->user->setState('UserLogin', array('attributes' => $loginForm->attributes, 'errors' => $loginForm->getErrors()));
58  }
59  } else {
60  throw new CHttpException(400, 'Wrong service');
61  }
62  } elseif (isset($_POST['ajax'])){
63  echo $loginForm->getAjaxErrors();
64  Yii::app()->end();
65  } else {
66  Yii::app()->user->setState('UserLogin', array('attributes' => $loginForm->attributes, 'errors' => $loginForm->getErrors()));
67  }
68  } else {
69  Yii::app()->user->setFlash(
70  $this->module->userLoginWidgetFlashError, UserModule::t('Please wait {s} seconds before next login.', array('{s}' => $this->timeRemains)));
71  }
72 
73  if (isset($_POST['return_url'])) {
74  $this->redirect($_POST['return_url'], false);
75  } else{
76  $this->render('/user/login', array('model' => $loginForm));
77  }
78  }
79 
80  public function actionTpa()
81  {
82  $loginForm = new UserLogin();
83  $loginForm->attributes = @$_GET['UserLogin'];
84  if (isset($_GET['ajax'])){
85  echo $loginForm->getAjaxErrors();
86  Yii::app()->end();
87  }
88  if ($loginForm->validate()) {
89  if ($loginForm->service != 'standard' && array_key_exists($loginForm->service, Yii::app()->eauth->services)) {
90  if(isset($_GET['ajax'])) {
91  throw new CHttpException(400, 'Wrong request');
92  }
93  $serviceIdentity = Yii::app()->eauth->getIdentity($loginForm->service);
94  $serviceIdentity->redirectUrl = Yii::app()->user->returnUrl;
95  $serviceIdentity->cancelUrl = $this->createAbsoluteUrl('user/login');
96  if ($serviceIdentity->authenticate()) {
97  $serviceIdentity->getAttributes();
98  $this->onTPAAuthorizationSuccess($serviceIdentity);
99  $userIdentity = new SUserIdentity($serviceIdentity);
100  if($userIdentity->authenticate()) {
101  Yii::app()->user->setState('UserLogin', null);
102  Yii::app()->user->login($userIdentity, $this->module->loggedInDuration * intval($loginForm->rememberMe));
103  } else {
104  $loginForm->applyErrors($userIdentity);
105  }
106  } else {
107  Yii::app()->user->setFlash('error', $serviceIdentity->getError());
108  }
109  } else {
110  throw new CHttpException(400, 'Wrong service');
111  }
112  } else {
113  Yii::app()->user->setState('UserLogin', array('attributes' => $loginForm->attributes, 'errors' => $loginForm->getErrors()));
114  }
115 
116  if (isset($_GET['return_url'])) {
117  $this->redirect($_GET['return_url']);
118  } else
119  $this->render('/user/login', array('model' => $loginForm));
120  }
121 
122 
123  private function lastViset()
124  {
125  $lastVisit = User::model()->notsafe()->findByPk(Yii::app()->user->id);
126  $lastVisit->lastvisit = time();
127  $lastVisit->save();
128  }
129 
130  public function onLogin($userIdentity)
131  {
132  $message = "User: {$userIdentity->username} logged in. Return URL: " . Yii::app()->user->returnUrl . ". \$_REQUEST['return_url']: " . @$_REQUEST['return_url'];
133  $notification = new Notification('Login', $message);
134  Yii::app()->notificationManager->notifyAbout($notification);
135  }
136 
137 
138  public function onTPAAuthorizationSuccess($serviceIdentity)
139  {
140  $message = "Account {$serviceIdentity->getEmail()} from TPA: {$serviceIdentity->serviceName} successfuly authorized\n";
141  $notification = new Notification('TPA', $message);
142  Yii::app()->notificationManager->notifyAbout($notification);
143  }
144 
145 }