1 <?php
2 3 4 5 6 7 8
9 class RInstaller extends CApplicationComponent
10 {
11 const ERROR_NONE=0;
12 const ERROR_QUERY_FAILED=1;
13
14 15 16
17 public $defaultRoles;
18 19 20
21 public $superuserName;
22 23 24
25 public $authenticatedName;
26 27 28
29 public $guestName;
30 31 32
33 private $_authManager;
34 35 36
37 private $_installed;
38
39 40 41
42 public $db;
43
44 45 46 47 48
49 public function init()
50 {
51 parent::init();
52
53
54
55 $authManager = Yii::app()->getAuthManager();
56 if( ($authManager instanceof RDbAuthManager)===false )
57 throw new CException(Rights::t('install', 'Application authorization manager must extend the RDbAuthManager class.'));
58
59
60
61 $user = Yii::app()->getUser();
62 if( ($user instanceof RWebUser)===false )
63 throw new CException(Rights::t('install', 'Application web user must extend the RWebUser class.'));
64
65 $this->_authManager = $authManager;
66 $this->db = $this->_authManager->db;
67 }
68
69 70 71 72 73
74 public function run()
75 {
76
77 $itemTable = $this->_authManager->itemTable;
78 $itemChildTable = $this->_authManager->itemChildTable;
79 $assignmentTable = $this->_authManager->assignmentTable;
80 $rightsTable = $this->_authManager->rightsTable;
81
82
83 $schema = file_get_contents(dirname(__FILE__).'/../data/schema.sql');
84
85
86 $schema = strtr($schema, array(
87 'AuthItem'=>$itemTable,
88 'AuthItemChild'=>$itemChildTable,
89 'AuthAssignment'=>$assignmentTable,
90 'Rights'=>$rightsTable,
91 ));
92
93
94 $schema = preg_split("/;\s*/", trim($schema, ';'));
95
96
97 $txn = $this->db->beginTransaction();
98
99 try
100 {
101
102 foreach( $schema as $sql )
103 {
104 $command = $this->db->createCommand($sql);
105 $command->execute();
106 }
107
108
109 $roles = $this->getUniqueRoles();
110 foreach( $roles as $roleName )
111 {
112 $sql = "INSERT INTO {$itemTable} (name, type, data)
113 VALUES (:name, :type, :data)";
114 $command = $this->db->createCommand($sql);
115 $command->bindValue(':name', $roleName);
116 $command->bindValue(':type', CAuthItem::TYPE_ROLE);
117 $command->bindValue(':data', 'N;');
118 $command->execute();
119 }
120
121
122 $sql = "INSERT INTO {$assignmentTable} (itemname, userid, data)
123 VALUES (:itemname, :userid, :data)";
124 $command = $this->db->createCommand($sql);
125 $command->bindValue(':itemname', $this->superuserName);
126 $command->bindValue(':userid', Yii::app()->getUser()->id);
127 $command->bindValue(':data', 'N;');
128 $command->execute();
129
130
131 $txn->commit();
132 return self::ERROR_NONE;
133 }
134 catch( CDbException $e )
135 {
136
137 $txn->rollback();
138
139 return self::ERROR_QUERY_FAILED;
140 }
141 }
142
143 144 145 146
147 private function getUniqueRoles()
148 {
149 $roles = CMap::mergeArray($this->defaultRoles, array(
150 $this->superuserName,
151 $this->authenticatedName,
152 $this->guestName,
153 ));
154 return array_unique($roles);
155 }
156
157 158 159
160 public function getInstalled()
161 {
162 if( $this->_installed!==null )
163 {
164 return $this->_installed;
165 }
166 else
167 {
168 $schema = array(
169 "SELECT COUNT(*) FROM {$this->_authManager->itemTable}",
170 "SELECT COUNT(*) FROM {$this->_authManager->itemChildTable}",
171 "SELECT COUNT(*) FROM {$this->_authManager->assignmentTable}",
172 "SELECT COUNT(*) FROM {$this->_authManager->rightsTable}",
173 );
174
175 try
176 {
177 foreach( $schema as $sql )
178 {
179 $command = $this->db->createCommand($sql);
180 $command->queryScalar();
181 }
182
183 $installed = true;
184 }
185 catch( CDbException $e )
186 {
187 $installed = false;
188 }
189
190 return $this->_installed = $installed;
191 }
192 }
193 }
194