source: trunk/grails-app/controllers/ContactDetailedController.groovy @ 913

Last change on this file since 913 was 628, checked in by gav, 14 years ago

Full authorisation review.
Add manager role to BaseController.
Remove inventory manager role from CostCode controller as per ticket #77.
Remove inventory manager role from InventoryGroup controller CUD actions.
Add all manager roles to Address and Contact controllers.
Add production and task manager roles to ProductionReference controller.

File size: 4.6 KB
Line 
1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2
3@Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager',
4                    'ROLE_InventoryManager', 'ROLE_AssetManager', 'ROLE_ProductionManager'])
5class ContactDetailedController extends BaseController {
6
7    def contactService
8
9    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager',
10                        'ROLE_InventoryManager', 'ROLE_AssetManager', 'ROLE_ProductionManager', 'ROLE_AppUser'])
11    def index = { redirect(action:list,params:params) }
12
13    // the delete, save and update actions only accept POST requests
14    static allowedMethods = [delete:'POST', save:'POST', update:'POST']
15
16    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager',
17                        'ROLE_InventoryManager', 'ROLE_AssetManager', 'ROLE_ProductionManager', 'ROLE_AppUser'])
18    def list = {
19        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
20        [ contactInstanceList: Contact.list( params ), contactInstanceTotal: Contact.count() ]
21    }
22
23    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager',
24                        'ROLE_InventoryManager', 'ROLE_AssetManager', 'ROLE_ProductionManager', 'ROLE_AppUser'])
25    def show = {
26        def contactInstance = Contact.get( params.id )
27
28        if(!contactInstance) {
29            flash.message = "Contact not found with id ${params.id}"
30            redirect(action:list)
31        }
32        else { return [ contactInstance : contactInstance ] }
33    }
34
35    def delete = {
36        def result = contactService.delete(params)
37
38        if(!result.error) {
39            if(result.ownerInstance) {
40                def c = getControllerName(result.ownerInstance)
41                flash.message = g.message(code: "default.delete.success", args: ["Contact", ''])
42                redirect(controller:c, action:'edit', id: result.ownerInstance.id)
43                return
44            }
45            flash.message = g.message(code: "default.delete.success", args: ["Contact", params.id])
46            redirect(action:list)
47            return
48        }
49
50        flash.message = g.message(code: result.error.code, args: result.error.args)
51
52        if(result.error.code == "default.not.found") {
53            redirect(action:list)
54            return
55        }
56
57        redirect(action:show, id: params.id)
58    }
59
60    def edit = {
61        def contactInstance = Contact.get( params.id )
62
63        if(!contactInstance) {
64            flash.message = "Contact not found with id ${params.id}"
65            redirect(action:list)
66        }
67        else {
68            return [ contactInstance : contactInstance ]
69        }
70    }
71
72    def update = {
73        def contactInstance = Contact.get( params.id )
74        if(contactInstance) {
75            if(params.version) {
76                def version = params.version.toLong()
77                if(contactInstance.version > version) {
78
79                    contactInstance.errors.rejectValue("version", "default.optimistic.locking.failure")
80                    render(view:'edit',model:[contactInstance:contactInstance])
81                    return
82                }
83            }
84            contactInstance.properties = params
85            if(!contactInstance.hasErrors() && contactInstance.save(flush: true)) {
86                flash.message = "Contact ${params.id} updated"
87                redirect(action:show,id:contactInstance.id)
88            }
89            else {
90                render(view:'edit',model:[contactInstance:contactInstance])
91            }
92        }
93        else {
94            flash.message = "Contact not found with id ${params.id}"
95            redirect(action:list)
96        }
97    }
98
99    def create = {
100        def result = contactService.create(params)
101
102        if(!result.error)
103            return [contactInstance: result.contactInstance]
104
105        flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
106        redirect(action: list)
107    }
108
109    def save = {
110        def result = contactService.save(params)
111
112        if(!result.error) {
113            def c = getControllerName(result.ownerInstance)
114            flash.message = g.message(code: "default.create.success", args: ["Contact", ''])
115            redirect(controller:c, action:'edit', id: result.ownerInstance.id)
116            return
117        }
118
119        render(view:'create', model:[contactInstance: result.contactInstance])
120    }
121
122    private getControllerName(ownerInstance) {
123        if(ownerInstance.class.name == 'Person')
124            return "${ownerInstance.class.name[0].toLowerCase() + ownerInstance.class.name[1..-1]}"
125        else
126            return "${ownerInstance.class.name[0].toLowerCase() + ownerInstance.class.name[1..-1]}"+"Detailed"
127    }
128
129}
Note: See TracBrowser for help on using the repository browser.