PowerOffController::filters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
/**
4
 * Handles power off actions
5
 * 
6
 * @author Sam Stenvall <[email protected]>
7
 * @copyright Copyright &copy; Sam Stenvall 2015-
8
 * @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
9
 */
10
class PowerOffController extends AdminOnlyController
11
{
12
13
	/**
14
	 * @return array the filters
15
	 */
16
	public function filters()
17
	{
18
		return array_merge(parent::filters(), array(
19
			array('application.filters.CheckBackendConnectivityFilter + '.
20
				'shutdown, suspend, hibernate, reboot'),
21
		));
22
	}
23
	
24
	/**
25
	 * @return array the access control rules
26
	 */
27
	public function accessRules()
28
	{
29
		return array_merge(array(
30
			// Allow normal users to optionally power off the backend
31
			array('allow', 'actions'=>Yii::app()->powerOffManager->getAllowedActions())
32
		), parent::accessRules());
33
	}
34
	
35
	/**
36
	 * Shuts down the backend
37
	 */
38
	public function actionShutdown()
39
	{
40
		$this->powerOff(PowerOffManager::SHUTDOWN);
41
	}
42
43
	/**
44
	 * Suspends the backend
45
	 */
46
	public function actionSuspend()
47
	{
48
		$this->powerOff(PowerOffManager::SUSPEND);
49
	}
50
51
	/**
52
	 * Hibernates the backend
53
	 */
54
	public function actionHibernate()
55
	{
56
		$this->powerOff(PowerOffManager::HIBERNATE);
57
	}
58
59
	/**
60
	 * Reboots the backend
61
	 */
62
	public function actionReboot()
63
	{
64
		$this->powerOff(PowerOffManager::REBOOT);
65
	}
66
	
67
	/**
68
	 * Powers off the backend by shutting down, suspending, hibernating or
69
	 * rebooting the backend
70
	 * @param string $action the power off action to use, 
71
	 * could be either of the strings 'shutdown', 'suspend', 'hibernate' or 'reboot'
72
	 */
73
	private function powerOff($action)
74
	{
75
		if (Yii::app()->powerOffManager->hasBackendCapability($action))
76
		{
77
			Yii::app()->powerOffManager->powerOff($action);
78
79
			$messages = array(
80
				PowerOffManager::SHUTDOWN => Yii::t('Backend', 'The current backend is shutting down'),
81
				PowerOffManager::SUSPEND => Yii::t('Backend', 'The current backend is suspending'),
82
				PowerOffManager::HIBERNATE => Yii::t('Backend', 'The current backend is hibernating'),
83
				PowerOffManager::REBOOT => Yii::t('Backend', 'The current backend is rebooting'));
84
			
85
			$message = $messages[$action];
86
			
87
			Yii::app()->user->setFlash('success', $message);
88
		}
89
		else
90
		{
91
			$messages = array(
92
				PowerOffManager::SHUTDOWN => Yii::t('Backend', 'The current backend cannot be shut down'),
93
				PowerOffManager::SUSPEND => Yii::t('Backend', 'The current backend cannot be suspended'),
94
				PowerOffManager::HIBERNATE => Yii::t('Backend', 'The current backend cannot be hibernated'),
95
				PowerOffManager::REBOOT => Yii::t('Backend', 'The current backend cannot be rebooted'));
96
			
97
			$message = $messages[$action];
98
			
99
			Yii::app()->user->setFlash('error', $message);
100
		}
101
102
		$this->redirectToPrevious(Yii::app()->homeUrl);
103
	}
104
105
}
106