This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Installer.php
305 lines (212 loc) · 9.43 KB
/
Installer.php
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
/**
* The Installer Helper extends Record to provide an easy interface for adding
* initial tables and creating permissions.
*
* This helper provides the following capabilities:
* - table creation and removal with verification
* - permission creation, removal, renaming, assignment and revokation
* - role creation, removal and renaming
* - driver identification
* - error generation specific to installations
*
* Note: This file must be placed in /wolf/helpers and can be called like
* any helper. All methods are static and should be called directly.
*
* @package Helpers
* @subpackage Installer
*
* @author Shannon Brooks <shannon@brooksworks.com>
* @copyright Shannon Brooks, 2011
* @license http://www.gnu.org/licenses/gpl.html GPLv3 license
*/
class Installer extends Record {
public static $LAST_ERROR = null;
// createTable
// usage: Installer::createTable(<table name>,<create statement>);
// returns: true on success
// false on failure, sets self::$LAST_ERROR on failure
final public static function createTable( $table, $query ) {
if ( self::$__CONN__->query( "SELECT COUNT(*) FROM ".$table ) !== false ) return self::__ERROR( __('Table exists.') );
self::$__CONN__->exec($query);
if ( self::$__CONN__->query( "SELECT COUNT(*) FROM ".$table ) === false ) return self::__ERROR( __('Table not created!') );
return true;
}
// updateTable
// usage: Installer::updateTable(<table name>,<update statement>);
// returns: true
// false if table doesn't exist, sets self::$LAST_ERROR on failure
final public static function updateTable( $table, $query ) {
if ( self::$__CONN__->query( "SELECT COUNT(*) FROM ".$table ) === false ) return self::__ERROR( __('Table does not exist!') );
self::$__CONN__->exec($query);
return true;
}
// removeTable
// usage: Installer::removeTable(<table name>);
// returns: true on success
// false on failure, sets self::$LAST_ERROR on failure
final public static function removeTable( $table ) {
self::$__CONN__->exec( 'DROP TABLE IF EXISTS '.$table );
if ( self::$__CONN__->query( "SELECT COUNT(*) FROM ".$table ) !== false ) return self::__ERROR( __('Table not removed!') );
return true;
}
// createPermissions
// usage: Installer::createPermissions(<comma separated list of permissions>);
// returns: true on success, skips existing permissions
// false on first failure, sets self::$LAST_ERROR on failure
final public static function createPermissions( $permissions ) {
foreach ( explode(',', $permissions) as $permission ) {
$permission = trim($permission);
if ( ! Permission::findByName($permission) ) {
$p = new Permission( array('name'=>$permission) );
if ( ! $p->save() ) return self::__ERROR( __('Could not create Permission') . ': ' . $permission );
}
}
return true;
}
// removePermissions
// usage: Installer::removePermissions(<comma separated list of permissions>);
// returns: true on success, skips non-existent permissions
// false on first failure, sets self::$LAST_ERROR on failure
final public static function removePermissions( $permissions ) {
foreach ( explode(',', $permissions) as $permission ) {
$permission = trim($permission);
if ( $p = Permission::findByName($permission) ) {
RolePermission::deleteWhere( 'RolePermission', 'permission_id='.$p->id );
if ( RolePermission::countFrom( 'RolePermission', 'permission_id='.$p->id ) > 0 ) return self::__ERROR( __('Could not remove Role->Permission link') . ': ' . $permission );
if ( ! $p->delete() ) return self::__ERROR( __('Could not remove Permission') . ': ' . $permission );
}
}
return true;
}
// renamePermission
// usage: Installer::renamePermission(<permission name>,<new name>);
// returns: true on success
// false on failure, sets self::$LAST_ERROR on failure
final public static function renamePermission( $permission, $name ) {
$permission = trim($permission);
$name = trim($name);
if ( ! $p = Permission::findByName($permission) ) return self::__ERROR( __('Permission does not exist!') );
$p->name = $name;
$p->save();
if ( ! Permission::findByName($name) ) return self::__ERROR( __('Could not rename Permission!') );
return true;
}
// createRoles
// usage: Installer::createRoles(<comma separated list of roles>);
// returns: true on success, skips existing roles
// false on first failue, sets self::$LAST_ERROR on failure
final public static function createRoles( $roles ) {
foreach ( explode(',',$roles) as $role ) {
$role = trim($role);
if ( ! Role::findByName($role) ) {
$r = new Role(array('name'=>$role) );
if ( ! $r->save() ) return self::__ERROR( __('Could not add Role') . ': ' . $role );
}
}
return true;
}
// removeRoles
// usage: Installer::removeRoles(<comma separated list of roles>);
// returns: true on success, skips non-existant roles
// false on first failue, sets self::$LAST_ERROR on failure
final public static function removeRoles( $roles ) {
foreach ( explode(',',$roles) as $role ) {
$role = trim($role);
if ( $r = Role::findByName($role) ) {
UserRole::deleteWhere('UserRole','role_id='.$r->id);
if ( UserRole::countFrom('UserRole','role_id='.$r->id) > 0 ) return self::__ERROR( __('Could not remove User->Role link') . ': ' . $role );
RolePermission::deleteWhere('RolePermission','role_id='.$r->id);
if ( RolePermission::countFrom('RolePermission','role_id='.$r->id) > 0 ) return self::__ERROR( __('Could not remove Role->Permission link') . ': ' . $role );
if ( ! $r->delete() ) return self::__ERROR( __('Could not remove Role') . ': ' . $role );
}
}
return true;
}
// renameRole
// usage: Installer::renameRole(<role name>,<new name>);
// returns: true on success
// false on failure, sets self::$LAST_ERROR on failure
final public static function renameRole( $role, $name ) {
$role = trim($role);
$name = trim($name);
if ( ! $r = Role::findByName($role) ) return self::__ERROR( __('Role does not exist!') );
$r->name = $name;
$r->save();
if ( ! Role::findByName($name) ) return self::__ERROR( __('Could not rename Role!') );
return true;
}
// assignPermissions
// usage: Installer::assignPermissions(<role name>,<comma separated list of permissions>);
// returns: true on success
// false on first failure, sets self::$LAST_ERROR on failure
final public static function assignPermissions( $role, $permissions ) {
$role = trim($role);
if ( ! $r = Role::findByName($role) ) return self::__ERROR( __('Role does not exist!') );
foreach (explode(',', $permissions) as $permission) {
$permission = trim($permission);
if ( ! $r->hasPermission($permission) ) {
if ( ! $p = Permission::findByName($permission) ) return self::__ERROR( __('Permission does not exist!') );
$rp = new RolePermission(array('role_id'=>$r->id,'permission_id'=>$p->id) );
if ( ! $rp->save() ) return self::__ERROR( __('Could not assign Permission to Role!') );
}
}
return true;
}
// revokePermissions
// usage: Installer::revokePermissions(<role name>,<comma separated list of permissions>);
// returns: true on success
// false on first failure, sets self::$LAST_ERROR on failure
final public static function revokePermissions( $role, $permissions ) {
$role = trim($role);
if ( ! $r = Role::findByName($role) ) return self::__ERROR( __('Role does not exist!') );
foreach (explode(',', $permissions) as $permission) {
$permission = trim($permission);
if ( $r->hasPermission($permission) ) {
if ( ! $p = Permission::findByName($permission) ) return self::__ERROR( __('Permission does not exist!') );
RolePermission::deleteWhere('RolePermission','role_id='.$r->id.', permission_id='.$p->id);
if ( RolePermission::countFrom('RolePermission','role_id='.$r->id.', permission_id='.$p->id) > 0 ) return self::__ERROR( __('Could not remove Role->Permission link!') );
}
}
return true;
}
// getDriver
// description:
// returns SQL driver in use
// usage: Installer::getDriver();
// returns: driver as string
final public static function getDriver() {
return strtolower(self::$__CONN__->getAttribute(self::ATTR_DRIVER_NAME) );
}
// failInstall
// description:
// When plugin is not installed successfully you can call this function to set an
// error message and remove the plugin from the installed table. If failing without
// calling this function plugin will show as installed even when it failed to install
// usage: Installer::failInstall(<plugin name>[,<message>]);
final public static function failInstall($plugin,$message=false) {
if ( $message === false ) $message = self::$LAST_ERROR;
unset(Plugin::$plugins[$plugin]);
Plugin::save();
Flash::set('error',$message);
exit;
}
// failUninstall
// description:
// When plugin is not uninstalled successfully you can call this function to set an
// error message.
// usage: Installer::faileUninstall(<plugin name>[,<message>]);
final public static function failUninstall($plugin,$message=false) {
if ( $message === false ) $message = self::$LAST_ERROR;
Flash::set('error',$message);
redirect(get_url('setting') );
}
// __ERROR
// description:
// Internal function to set self::$LAST_ERROR and return false
final private static function __ERROR($message) {
self::$LAST_ERROR = $message;
return false;
}
}
// EOF