source: branches/TaskRewrite/src/grails-app/controllers/PersonController.groovy @ 62

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

Add import security to required files.

File size: 3.7 KB
Line 
1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2
3@Secured(['ROLE_ADMIN'])
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) }
55                                person.delete()
56                                flash.message = "Person $params.id deleted."
57                        }
58                }
59                else {
60                        flash.message = "Person not found with id $params.id"
61                }
62
63                redirect action: list
64        }
65
66        def edit = {
67
68                def person = Person.get(params.id)
69                if (!person) {
70                        flash.message = "Person not found with id $params.id"
71                        redirect action: list
72                        return
73                }
74
75                return buildPersonModel(person)
76        }
77
78        /**
79         * Person update action.
80         */
81        def update = {
82
83                def person = Person.get(params.id)
84                if (!person) {
85                        flash.message = "Person not found with id $params.id"
86                        redirect action: edit, id: params.id
87                        return
88                }
89
90                long version = params.version.toLong()
91                if (person.version > version) {
92                        person.errors.rejectValue 'version', "person.optimistic.locking.failure",
93                                "Another user has updated this Person while you were editing."
94                                render view: 'edit', model: buildPersonModel(person)
95                        return
96                }
97
98                def oldPassword = person.password
99                person.properties = params
100                if (!params.password.equals(oldPassword)) {
101                        person.password = authenticateService.encodePassword(params.password)
102                }
103                if (person.save()) {
104                        Authority.findAll().each { it.removeFromPersons(person) }
105                        addRoles(person)
106                        redirect action: show, id: person.id
107                }
108                else {
109                        render view: 'edit', model: buildPersonModel(person)
110                }
111        }
112
113        def create = {
114                [person: new Person(params), authorityList: Authority.list()]
115        }
116
117        /**
118         * Person save action.
119         */
120        def save = {
121
122                def person = new Person()
123                person.properties = params
124                person.password = authenticateService.encodePassword(params.password)
125                if (person.save()) {
126                        addRoles(person)
127                        redirect action: show, id: person.id
128                }
129                else {
130                        render view: 'create', model: [authorityList: Authority.list(), person: person]
131                }
132        }
133
134        private void addRoles(person) {
135                for (String key in params.keySet()) {
136                        if (key.contains('ROLE') && 'on' == params.get(key)) {
137                                Authority.findByAuthority(key).addToPersons(person)
138                        }
139                }
140        }
141
142        private Map buildPersonModel(person) {
143
144                List roles = Authority.list()
145                roles.sort { r1, r2 ->
146                        r1.authority <=> r2.authority
147                }
148                Set userRoleNames = []
149                for (role in person.authorities) {
150                        userRoleNames << role.authority
151                }
152                LinkedHashMap<Authority, Boolean> roleMap = [:]
153                for (role in roles) {
154                        roleMap[(role)] = userRoleNames.contains(role.authority)
155                }
156
157                return [person: person, roleMap: roleMap]
158        }
159}
Note: See TracBrowser for help on using the repository browser.