LoginController.php
2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Controls authorization moments or request to identify who is logged in currently
*
*/
class LoginController extends WRestController
{
/**
* Authorize identity
*/
private $_identity;
/**
* Use model with this controller
*/
protected $_modelName = "manager";
/**
* Default action
*/
public $defaultAction = "Identity";
/**
* Link List action to the identity
*/
public function actionList() {
$this->actionIdentity();
} // end actionList()
/**
* Link Get action to the identity
*/
public function actionGet() {
$this->actionIdentity();
} // end actionGet()
/**
* Default actions
*/
public function actionIdentity()
{
if(Yii::app()->user->isGuest) {
$this->sendResponse(401, array(
"success" => false
));
}
else {
$this->sendResponse(200, array(
"success" => true,
"results" => array(
"login" => Yii::app()->user->getLogin(),
"name" => Yii::app()->user->getFullName()
)
));
}
} // end actionIdentity()
/**
* Authorization method
*/
public function actionAuthorize()
{
if(Yii::app()->user->isGuest) {
$login = $this->getRequest()->getParam("login");
$pass = $this->getRequest()->getParam("pass");
$this->_identity = new UserIdentity($login, $pass);
if(!$this->_identity->authenticate()) {
$this->sendResponse(401, array(
"success" => false,
"details" => 'Wrong authentication data'
));
}
else {
Yii::app()->user->login($this->_identity);
$this->sendResponse(200, array(
"success" => true,
"results" => array(
"login" => Yii::app()->user->getLogin(),
"name" => Yii::app()->user->getFullName()
)
));
}
}
}
/**
* Logs out the current user and redirect to home page.
*/
public function actionLogout()
{
Yii::app()->user->logout();
if(!Yii::app()->request->isAjaxRequest) {
$this->redirect(Yii::app()->homeUrl);
}
else {
$this->sendResponse(200, array("url" => Yii::app()->homeUrl));
}
}
}