Completed
Push — master ( aec9f9...84751c )
by Sam
02:36 queued 01:13
created

SiteController::filters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Displays exceptions and handles authentication
5
 * 
6
 * @author Sam Stenvall <[email protected]>
7
 * @copyright Copyright &copy; Sam Stenvall 2013-
8
 * @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
9
 */
10
class SiteController extends Controller
11
{
12
13
	/**
14
	 * Returns the filters for this controller. We don't want any filters to 
15
	 * apply in this context so we return an empty array.
16
	 * @return array
17
	 */
18
	public function filters()
19
	{
20
		return array();
21
	}
22
23
	/**
24
	 * This is the action to handle external exceptions.
25
	 */
26
	public function actionError()
27
	{
28
		if (($error = Yii::app()->errorHandler->error))
29
		{
30
			if (Yii::app()->request->isAjaxRequest)
31
				echo $error['message'];
32
			else
33
			{
34
				// Change layout if the user is not logged in, otherwise he 
35
				// will "see" the real application
36
				if (Yii::app()->user->isGuest)
37
					$this->layout = 'login';
38
39
				$this->render('error', $error);
40
			}
41
		}
42
	}
43
44
	/**
45
	 * Displays the login page and logs in the user if correct credentials are 
46
	 * entered
47
	 */
48
	public function actionLogin()
49
	{
50
		$this->layout = 'login';
51
52
		$model = new LoginForm();
53
54
		if (isset($_POST['LoginForm']))
55
		{
56
			$model->attributes = $_POST['LoginForm'];
57
			$address = $_SERVER['REMOTE_ADDR'];
58
59
			if ($model->validate() && $model->login())
60
			{
61
				$this->log('"%s" logged in from %s', $model->username, $address);
62
63
				/* @var User $user */
64
				$user = User::model()->findByPk(Yii::app()->user->id);
65
				$this->redirect($user->getStartPageRoute());
66
			}
67
			else
68
			{
69
				// Log invalid login attempts
70
				if (!empty($model->username) && !empty($model->password))
71
				{
72
					$this->log('Invalid login attempt for user "%s" from %s', 
73
							$model->username, $address);
74
				}
75
			}
76
		}
77
78
		$this->render('login', array('model'=>$model));
79
	}
80
81
	/**
82
	 * Logs out the current user and redirect to the login page (since all 
83
	 * other pages require authentication)
84
	 */
85
	public function actionLogout()
86
	{
87
		// Don't attempt to log out guests, it confuses the logs
88
		if (!Yii::app()->user->isGuest)
89
		{
90
			$this->log('"%s" logged out', Yii::app()->user->name);
91
			Yii::app()->user->logout();
92
		}
93
94
		$this->redirect(array('site/login'));
95
	}
96
	
97
	/**
98
	 * Flushes the API call cache and redirects to the home URL
99
	 */
100
	public function actionFlushCache()
101
	{
102
		Yii::app()->apiCallCache->flush();
103
		Yii::app()->user->setFlash('success', Yii::t('Misc', 'The cache has been flushed successfully'));
104
		$this->redirectToPrevious(Yii::app()->homeUrl);
105
	}
106
107
}
108