1 <?php
2 /**
3 * Rights installation controller class file.
4 *
5 * @author Christoffer Niska <cniska@live.com>
6 * @copyright Copyright © 2010 Christoffer Niska
7 * @since 0.9.8
8 */
9 class InstallController extends RController
10 {
11 /**
12 * @property RAuthorizer
13 */
14 private $_authorizer;
15 /**
16 * @property RInstaller
17 */
18 private $_installer;
19
20 /**
21 * Initializes the controller.
22 */
23 public function init()
24 {
25 if( $this->module->install!==true )
26 $this->redirect(Yii::app()->homeUrl);
27
28 $this->_authorizer = $this->module->getAuthorizer();
29 $this->_installer = $this->module->getInstaller();
30 $this->layout = $this->module->layout;
31 $this->defaultAction = 'run';
32
33 // Register the scripts.
34 $this->module->registerScripts();
35 }
36
37 /**
38 * @return array action filters
39 */
40 public function filters()
41 {
42 // Use access control when installed.
43 return $this->_installer->installed===true ? array('accessControl') : array();
44 }
45
46 /**
47 * Specifies the access control rules.
48 * This method is used by the 'accessControl' filter.
49 * @return array access control rules
50 */
51 public function accessRules()
52 {
53 return array(
54 array('allow', // Allow superusers to access Rights
55 'actions'=>array(
56 'confirm',
57 'run',
58 'error',
59 'ready',
60 ),
61 'users'=>$this->_authorizer->getSuperusers(),
62 ),
63 array('deny', // Deny all users
64 'users'=>array('*'),
65 ),
66 );
67 }
68
69 /**
70 * Displays the confirm overwrite page.
71 */
72 public function actionConfirm()
73 {
74 $this->render('confirm');
75 }
76
77 /**
78 * Installs the module.
79 * @throws CHttpException if the user is not logged in.
80 */
81 public function actionRun()
82 {
83 // Make sure the user is not a guest.
84 if( Yii::app()->user->isGuest===false )
85 {
86 // Make sure that the module is not already installed.
87 if( isset($_GET['confirm'])===true || $this->_installer->installed===false )
88 {
89 // Run the installer and check for an error.
90 if( $this->_installer->run()===RInstaller::ERROR_NONE )
91 {
92 // Mark the user to have superuser privileges.
93 Yii::app()->user->isSuperuser = true;
94 $this->redirect(array('install/ready'));
95 }
96
97 // Redirect to the error page.
98 $this->redirect(array('install/error'));
99 }
100 // Module is already installed.
101 else
102 {
103 $this->redirect(array('install/confirm'));
104 }
105 }
106 // User is guest, deny access.
107 else
108 {
109 $this->accessDenied(Rights::t('install', 'You must be logged in to install Rights.'));
110 }
111 }
112
113 /**
114 * Displays the install ready page.
115 */
116 public function actionReady()
117 {
118 $this->render('ready');
119 }
120
121 /**
122 * Displays the install ready page.
123 */
124 public function actionError()
125 {
126 $this->render('error');
127 }
128 }
129