source: trunk/grails-app/controllers/dbnp/studycapturing/TemplateEditorController.groovy @ 377

Last change on this file since 377 was 377, checked in by duh, 14 years ago
  • working on the templateEditor
  • Property svn:keywords set to Date Author Rev
File size: 3.3 KB
Line 
1/**
2 * TemplateEditorController Controler
3 *
4 * Webflow driven template editor
5 *
6 * @author  Jeroen Wesbeek
7 * @since       20100415
8 * @package     studycapturing
9 *
10 * Revision information:
11 * $Rev: 377 $
12 * $Author: duh $
13 * $Date: 2010-04-23 14:16:44 +0000 (vr, 23 apr 2010) $
14 */
15package dbnp.studycapturing
16import dbnp.data.*
17import dbnp.studycapturing.*
18import cr.co.arquetipos.crypto.Blowfish
19
20class TemplateEditorController {
21        /**
22         * index closure
23         */
24    def index = {
25                // got a entity get parameter?
26                def entity = null
27                if (params.entity) {
28                        // decode entity get parameter
29                        if (grailsApplication.config.crypto) {
30                                // generate a Blowfish encrypted and Base64 encoded string.
31                                entity = Blowfish.decryptBase64(
32                                        params.entity,
33                                        grailsApplication.config.crypto.shared.secret
34                                )
35                        } else {
36                                // base64 only; this is INSECURE! Even though it is not
37                                // very likely, it is possible to exploit this and have
38                                // Grails dynamically instantiate whatever class you like.
39                                // If that constructor does something harmfull this could
40                                // be dangerous. Hence, use encryption (above) instead...
41                                entity = new String(params.entity.toString().decodeBase64())
42                        }
43                }
44
45                // go with the flow!
46        redirect(action: 'pages', params:["entity":entity])
47    }
48
49        /**
50         * Webflow
51         */
52        def pagesFlow = {
53                // start the flow
54                start {
55                        action {
56                                // define initial flow variables
57                                flow.entity = null
58                                flow.templates = []
59
60                                // define success variable
61                                def errors = true
62
63                                // got an entity parameter?
64                                if (params.entity && params.entity instanceof String) {
65                                        // yes, try to dynamicall load the entity
66                                        try {
67                                                // dynamically load the entity
68                                                def entity = Class.forName(params.entity, true, this.getClass().getClassLoader())
69
70                                                // succes, is entity an instance of TemplateEntity?
71                                                if (entity.superclass =~ /TemplateEntity$/) {
72                                                        errors = false
73
74                                                        // yes, assign entity to the flow
75                                                        flow.entity = entity
76
77                                                        // fetch all templates to this entity
78                                                        flow.templates = Template.findAllByEntity(entity)
79
80                                                        // find all template fields for this particular entity
81                                                        // for now, all
82                                                        // TODO: limit for this entity only
83                                                        flow.allTemplateFields = TemplateField.findAll().sort{ it.name }
84                                                }
85                                        } catch (Exception e) { }
86                                }
87
88                                // success?
89                                if (errors) {
90                                        error()
91                                } else {
92                                        success()
93                                }
94                        }
95                        on("success").to "templates"
96                        on("error").to "errorInvalidEntity"
97                }
98
99                // could not dynamically load entity, possible hack
100                // or invalid entity specified in template field
101                errorInvalidEntity {
102                        render(view: "errorInvalidEntity")
103                }
104
105                // main template editor page
106                templates {
107                        render(view: "templates")
108                        onRender {
109                                // template parameter given?
110                                if (params.template) {
111                                        // yes, find template by name
112                                        flow.template = Template.findByName(params.template)
113                                        flow.templateFields = flow.allTemplateFields
114
115                                        flow.template.fields.each() {
116                                                println it
117                                                flow.templateFields.remove(it)
118                                               
119                                        }
120                                        println "count: "+flow.template.fields.size()
121
122                                        println "---"
123                                        flow.allTemplateFields.each() {
124                                                println it
125                                        }
126                                        println "count: "+flow.allTemplateFields.size()
127                                        println "---"
128
129                                        println flow.allTemplateFields.class
130                                        println flow.template.fields.class
131                                        println "---"
132                                }
133                        }
134                        on("next").to "start"
135                }
136        }
137}
Note: See TracBrowser for help on using the repository browser.