-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Update.cfc
412 lines (359 loc) · 12 KB
/
Update.cfc
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/**
********************************************************************************
* ContentBox - A Modular Content Platform
* Copyright 2012 by Luis Majano and Ortus Solutions, Corp
* www.ortussolutions.com
* ---
* Updater for 3.0.0 Beta Final
*
* DB Structure Changes Comment Below
*
* ---
* Start Commit Hash: 762aede3a97eb00519e7f171ac4c3c6d6924daca
* End Commit Hash: ff79cc3b8a1e7e37bbe5170e91d510c9e46e7e73
*/
component implements="contentbox.models.updates.IUpdate"{
// DI
property name="settingService" inject="id:settingService@cb";
property name="permissionService" inject="permissionService@cb";
property name="authorService" inject="id:authorService@cb";
property name="roleService" inject="roleService@cb";
property name="securityRuleService" inject="securityRuleService@cb";
property name="pageService" inject="pageService@cb";
property name="contentService" inject="contentService@cb";
property name="log" inject="logbox:logger:{this}";
property name="wirebox" inject="wirebox";
property name="coldbox" inject="coldbox";
property name="securityService" inject="id:securityService@cb";
/**
* Constructor
*/
function init(){
return this;
}
/**
* On DI Complete
*/
function onDIComplete(){
// setup update variables.
variables.version = "3.0.0";
variables.currentVersion = replace( variables.coldbox.getSetting( "modules" ).contentbox.version, ".", "", "all" );
variables.thisPath = getDirectoryFromPath( getMetadata( this ).path );
variables.contentBoxPath = coldbox.getSetting( "modules" )[ "contentbox" ].path;
}
/**
* pre installation
*/
function preInstallation(){
try{
log.info("About to begin #version# patching");
// Verify if less than 2.1.0 with message
if( !isValidInstall() ){ return; }
/****************************** RENAME LAYOUTS TO THEMES ******************************/
if( !directoryExists( contentBoxPath & "/themes" ) && directoryExists( contentBoxPath & "/layouts" ) ){
directoryRename( contentBoxPath & "/layouts" , contentBoxPath & "/themes" );
}
/****************************** RENAME MODULES ******************************/
if( !directoryExists( contentBoxPath & "/modules_user" ) && directoryExists( contentBoxPath & "/modules" ) ){
directoryRename( contentBoxPath & "/modules" , contentBoxPath & "/modules_user" );
}
/****************************** UPDATE SECURITY RULES ******************************/
var aRules = securityRuleService.getAll();
for( var oRule in aRules ){
if( findNoCase( "LAYOUT_ADMIN", oRule.getPermissions() ) ){
oRule.setPermissions( replaceNoCase( oRule.getPermissions(), "LAYOUT_ADMIN", "THEME_ADMIN", "all" ) );
oRule.setSecureList( replaceNoCase( oRule.getSecureList(), "layouts", "themes", "all" ) );
}
securityRuleService.save( entity=oRule, transactional=false );
}
/****************************** UPDATE SETTINGS + PERMISSIONS ******************************/
// Update new settings
updateSettings();
// Update Permissions
updatePermissions();
// Update Roles with new permissions
updateAdmin();
updateEditor();
log.info("Finalized #version# patching");
}
catch(Any e){
ORMClearSession();
log.error("Error doing #version# patch preInstallation. #e.message# #e.detail# #e.stacktrace#", e);
rethrow;
}
}
/**
* post installation
*/
function postInstallation(){
try{
// Verify if less than 2.1.0 with message
if( !isValidInstall() ){ return; }
// Make changes on disk take effect
ORMREload();
}
catch(Any e){
ORMClearSession();
log.error("Error doing #version# patch postInstallation. #e.message# #e.detail#", e);
rethrow;
}
}
/************************************** PRIVATE *********************************************/
private function isValidInstall(){
// Verify if less than 1.6.0 with message
if( replace( currentVersion, ".", "", "all" ) LT 210 ){
log.info( "Cannot patch this installation until you upgrade to 2.1.0 first. You can find all of our patches here available for download: http://www.ortussolutions.com/products/contentbox. Then apply this patch." );
return false;
}
return true;
}
private function updateAdmin(){
var oRole = roleService.findWhere( { role = "Administrator" } );
// Create new Permissions
var perms = [
"EDITORS_FEATURED_IMAGE"
];
// iterate and add
for( var thisPermTitle in perms ){
var local.thisPerm = permissionService.findWhere( { permission=thisPermTitle } );
if( structKeyExists( local, "thisPerm" ) and !oRole.hasPermission( local.thisPerm ) ){
oRole.addPermission( local.thisPerm );
log.info( "Added #thisPermTitle# permission to admin role" );
} else {
log.info( "Skipped #thisPermTitle# permission to admin role" );
}
}
// save role
roleService.save( entity=oRole );
return oRole;
}
private function updateEditor(){
var oRole = roleService.findWhere({role="Editor"});
// Setup Permissions
var perms = [
"EDITORS_FEATURED_IMAGE"
];
// iterate and add
for( var thisPermTitle in perms ){
var local.thisPerm = permissionService.findWhere( {permission=thisPermTitle} );
if( structKeyExists( local, "thisPerm" ) and !oRole.hasPermission( local.thisPerm ) ){
oRole.addPermission( local.thisPerm );
log.info("Added #thisPermTitle# permission to editor role");
} else {
log.info("Skipped #thisPermTitle# permission to editor role");
}
}
// save role
roleService.save( entity=oRole );
return oRole;
}
private function updatePermissions(){
// Update Old Permissions to New name and description
var perm = permissionService.findWhere( { permission="LAYOUT_ADMIN" } );
// Case where LAYOUT_ADMIN exists
if( !isNull( perm ) ){
perm.setPermission( "THEME_ADMIN" );
perm.setDescription( "Ability to manage themes, default is view only" );
permissionService.save( entity=perm, transactional=false );
log.info( "LAYOUT_ADMIN permission found, renamed to THEME_ADMIN");
} else {
// Create it as a new permission only if THEME_ADMIN does not exist
var perm = permissionService.findWhere( { permission="THEME_ADMIN" } );
if( isNull( perm ) ){
addPermission( "THEME_ADMIN", "Ability to manage themes, default is view only" );
} else {
log.info( "THEME_ADMIN permission found, skipping changes");
}
}
// Create new Permissions
var perms = {
"EDITORS_FEATURED_IMAGE" = "Ability to view the featured image panel"
};
for( var key in perms ){
addPermission( key, perms[ key ] );
}
}
private function updateSettings(){
// Update Theme to layout
var oldLayoutSetting = settingService.findWhere( { name="cb_site_layout" } );
if( !isNull( oldLayoutSetting ) ){
oldLayoutSetting.setName( "cb_site_theme" );
settingService.save( entity=oldLayoutSetting, transactional=false );
}
// Update Search setting
var oSearchSetting = settingService.findWhere( { name="cb_search_adapter" } );
oSearchSetting.setValue( "contentbox.models.search.DBSearch" );
settingService.save( entity=oSearchSetting, transactional=false );
// Add new settings
addSetting( "cb_site_settings_cache", "Template" );
}
/************************************** DB MIGRATION OPERATIONS *********************************************/
private function addPermission( permission, description ){
var props = { permission=arguments.permission, description=arguments.description };
// only add if not found
if( isNull( permissionService.findWhere( { permission=props.permission } ) ) ){
permissionService.save( entity=permissionService.new( properties=props ), transactional=false );
log.info( "Added #arguments.permission# permission" );
} else {
log.info( "Skipped #arguments.permission# permission addition as it was already in system" );
}
}
private function addSetting( name, value ){
var setting = settingService.findWhere( { name = arguments.name } );
if( isNull( setting ) ){
setting = settingService.new();
setting.setValue( trim( arguments.value ) );
setting.setName( arguments.name );
settingService.save( entity=setting, transactional=false );
log.info( "Added #arguments.name# setting" );
} else {
log.info( "Skipped #arguments.name# setting, already there" );
}
return this;
}
// Add a new column: type=[varchar, boolean, text]
private function addColumn(required table, required column, required type, required limit, boolean nullable=false, defaultValue){
if( !columnExists( arguments.table, arguments.column ) ){
// Nullable string
var sNullable = ( arguments.nullable ? "NULL" : "NOT NULL" );
// Type String
var sType = evaluate( "variables.get#arguments.type#DBType()" );
// Default String
var sDefault = "";
if( structKeyExists( arguments, "defaultValue") ){
// if null as default
if( arguments.defaultValue eq "null" ){
sDefault = " DEFAULT NULL";
}
// if boolean default value type
else if( arguments.type eq "boolean" ){
sDefault = " DEFAULT #( arguments.defaultValue ? 1 : 0 )#";
}
else{
sDefault = " DEFAULT '#ReplaceNoCase( arguments.defaultValue, "'", "''" )#'";
}
}
// Build SQL
var q = new Query(datasource=getDatasource());
q.setSQL( "ALTER TABLE #arguments.table# ADD #arguments.column# #arguments.type#(#arguments.limit#) #sNullable##sDefault#;" );
q.execute();
log.info("Added column: #arguments.column# to table: #arguments.table#");
}
else{
log.info("Skipping adding column: #arguments.column# to table: #arguments.table# as it already existed");
}
}
// Verify if a column exists
private boolean function columnExists(required table, required column){
var colFound = false;
var cols = getTableColumns( arguments.table );
for( var x=1; x lte cols.recordcount; x++ ){
if( cols[ "column_name" ][ x ] eq arguments.column){
colFound = true;
}
}
return colFound;
}
// Get a DB specific varchar type
private function getVarcharDBType(){
var dbType = getDatabaseType();
switch( dbType ){
case "PostgreSQL" : {
return "varchar";
}
case "MySQL" : {
return "varchar";
}
case "Microsoft SQL Server" : {
return "varchar";
}
case "Oracle" :{
return "varchar2";
}
default : {
return "varchar";
}
}
}
// Get a DB specific datetime type
private function getDateTimeDBType(){
var dbType = getDatabaseType();
switch( dbType ){
case "PostgreSQL" : {
return "cf_sql_timestamp";
}
case "MySQL" : {
return "cf_sql_timestamp";
}
case "Microsoft SQL Server" : {
return "cf_sql_date";
}
case "Oracle" :{
return "cf_sql_timestamp";
}
default : {
return "cf_sql_timestamp";
}
}
}
// Get a DB specific long text type
private function getTextDBType(){
var dbType = getDatabaseType();
switch( dbType ){
case "PostgreSQL" : {
return "text";
}
case "MySQL" : {
return "longtext";
}
case "Microsoft SQL Server" : {
return "ntext";
}
case "Oracle" :{
return "clob";
}
default : {
return "text";
}
}
}
// Get a DB specific boolean column
private function getBooleanDBType(){
var dbType = getDatabaseType();
switch( dbType ){
case "PostgreSQL" : {
return "boolean";
}
case "MySQL" : {
return "bit";
}
case "Microsoft SQL Server" : {
return "tinyint";
}
case "Oracle" :{
return "number";
}
default : {
return "bit";
}
}
}
// get Columns
private function getTableColumns(required table){
if( structkeyexists( server, "railo") ){
return new RailoDBInfo().getTableColumns(datasource=getDatasource(), table=arguments.table);
}
return new dbinfo(datasource=getDatasource(), table=arguments.table).columns();
}
// Get the database type
private function getDatabaseType(){
if( structkeyexists( server, "railo") ){
return new RailoDBInfo().getDatabaseType(datasource=getDatasource()).database_productName;
}
return new dbinfo(datasource=getDatasource()).version().database_productName;
}
// Get the default datasource
private function getDatasource(){
return new coldbox.system.orm.hibernate.util.ORMUtilFactory().getORMUtil().getDefaultDatasource();
}
}