source: trunk/grails-app/controllers/PersonController.groovy @ 250

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

Small fix for some Person list view bugs.

File size: 6.2 KB
Line 
1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2
3@Secured(['ROLE_Manager','ROLE_AppAdmin'])
4class PersonController extends BaseAppAdminController {
5
6    def authenticateService
7    def filterService
8
9    // the delete, save and update actions only accept POST requests
10    static Map allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']
11
12    def index = {
13        redirect action: list, params: params
14    }
15
16    def list = {
17        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100 )
18
19        if(!params.filter) {
20            return [personList: Person.list(params),
21                            personTotal: Person.count(),
22                            filterParams: params]
23        }
24
25        // filterPane:
26        return[ personList: filterService.filter( params, Person ),
27            personTotal: filterService.count( params, Person ),
28            filterParams: com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params),
29            params:params ]
30    }
31
32    def show = {
33
34        // In the case of an actionSubmit button, rewrite action name from 'index'.
35        if(params._action_Show)
36        { params.action='show' }
37
38        def person = Person.get(params.id)
39        if (!person) {
40            flash.message = "Person not found with id $params.id"
41            redirect action: list
42            return
43        }
44        List roleNames = []
45        for (role in person.authorities) {
46            roleNames << role.authority
47        }
48        roleNames.sort { n1, n2 ->
49            n1 <=> n2
50        }
51        [person: person, roleNames: roleNames]
52    }
53
54    /**
55    * Person delete action. Before removing an existing person,
56    * they should be removed from those authorities which they are involved.
57    */
58    def delete = {
59
60        def person = Person.get(params.id)
61        if (person) {
62            def authPrincipal = authenticateService.principal()
63            // Avoid self-delete.
64            if (!(authPrincipal instanceof String) && authPrincipal.username == person.loginName) {
65                flash.message = "You cannot delete yourself, please login as another manager and try again."
66                redirect(action:show,id:params.id)
67            }
68            else {
69                //first, delete this person from Persons_Authorities table.
70                Authority.findAll().each { it.removeFromPersons(person) }
71                person.isActive = false
72                person.save(flush: true)
73
74                try {
75                    person.delete(flush: true)
76                    flash.message = "Person $params.id deleted."
77                    redirect(action:list)
78                }
79                catch(org.springframework.dao.DataIntegrityViolationException e) {
80                    flash.message = "Could not delete '$person.loginName' due to database constraints, but all authorities have been removed."
81                    redirect(action:show,id:params.id)
82                }
83            }
84        }
85        else {
86            flash.message = "Person not found with id $params.id"
87        }
88    }
89
90    def edit = {
91
92        // In the case of an actionSubmit button, rewrite action name from 'index'.
93        if(params._action_Edit)
94        { params.action='edit' }
95
96        def person = Person.get(params.id)
97        if (!person) {
98            flash.message = "Person not found with id $params.id"
99            redirect action: list
100            return
101        }
102        params.message = "To allow login at least the 'ROLE_AppUser' authority must be given."
103        return buildPersonModel(person)
104    }
105
106    /**
107        * Person update action.
108        */
109    def update = {
110
111        def person = Person.get(params.id)
112        if (!person) {
113            flash.message = "Person not found with id $params.id"
114            redirect action: edit, id: params.id
115            return
116        }
117
118        long version = params.version.toLong()
119        if (person.version > version) {
120            person.errors.rejectValue 'version', "person.optimistic.locking.failure",
121                "Another user has updated this Person while you were editing."
122            render view: 'edit', model: buildPersonModel(person)
123            return
124        }
125
126        person.properties = params
127
128        if(params.pass == "") {
129            person.pass = "InsertNothingToClearValidation"
130        }
131        else {
132            if (person.validate()) {
133                person.password = authenticateService.encodePassword(params.pass)
134            }
135        }
136
137        if (!person.hasErrors() && person.save(flush: true)) {
138            Authority.findAll().each { it.removeFromPersons(person) }
139            addRoles(person)
140            flash.message = "Person '$params.id - $params.loginName' updated."
141            redirect action: show, id: person.id
142        }
143        else {
144            render view: 'edit', model: buildPersonModel(person)
145        }
146
147    }
148
149    def create = {
150        params.message = "To allow login at least the 'ROLE_AppUser' authority must be given."
151        [person: new Person(params), authorityList: Authority.list()]
152    }
153
154    /**
155        * Person save action.
156        */
157    def save = {
158
159        def person = new Person()
160        person.properties = params
161        person.password = authenticateService.encodePassword(params.pass)
162        if (person.save(flush: true)) {
163            addRoles(person)
164            redirect action: show, id: person.id
165        }
166        else {
167            render view: 'create', model: [authorityList: Authority.list(), person: person]
168        }
169    }
170
171    private void addRoles(person) {
172        for (String key in params.keySet()) {
173            if (key.contains('ROLE') && 'on' == params.get(key)) {
174                Authority.findByAuthority(key).addToPersons(person)
175            }
176        }
177    }
178
179    private Map buildPersonModel(person) {
180
181        List roles = Authority.list()
182        roles.sort { r1, r2 ->
183            r1.authority <=> r2.authority
184        }
185        Set userRoleNames = []
186        for (role in person.authorities) {
187            userRoleNames << role.authority
188        }
189        LinkedHashMap<Authority, Boolean> roleMap = [:]
190        for (role in roles) {
191            roleMap[(role)] = userRoleNames.contains(role.authority)
192        }
193
194        return [person: person, roleMap: roleMap]
195    }
196}
Note: See TracBrowser for help on using the repository browser.