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

Last change on this file since 91 was 91, checked in by gav, 15 years ago

More detailing on Task and Entry. Added another authorisation ROLE_Manager and view to go with it. Work on PersonController? delete action.

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