1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
2 | |
---|
3 | @Secured(['ROLE_Manager']) |
---|
4 | class PersonController extends BaseAppAdminController { |
---|
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 | |
---|
56 | try { |
---|
57 | person.delete() |
---|
58 | flash.message = "Person $params.id deleted." |
---|
59 | redirect(action:list) |
---|
60 | } |
---|
61 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
---|
62 | flash.message = "Could not delete '$person.loginName' due to database constraints, but all authorities have been removed." |
---|
63 | redirect(action:show,id:params.id) |
---|
64 | } |
---|
65 | } |
---|
66 | } |
---|
67 | else { |
---|
68 | flash.message = "Person not found with id $params.id" |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | def edit = { |
---|
73 | |
---|
74 | def person = Person.get(params.id) |
---|
75 | if (!person) { |
---|
76 | flash.message = "Person not found with id $params.id" |
---|
77 | redirect action: list |
---|
78 | return |
---|
79 | } |
---|
80 | |
---|
81 | return buildPersonModel(person) |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * Person update action. |
---|
86 | */ |
---|
87 | def update = { |
---|
88 | |
---|
89 | def person = Person.get(params.id) |
---|
90 | if (!person) { |
---|
91 | flash.message = "Person not found with id $params.id" |
---|
92 | redirect action: edit, id: params.id |
---|
93 | return |
---|
94 | } |
---|
95 | |
---|
96 | long version = params.version.toLong() |
---|
97 | if (person.version > version) { |
---|
98 | person.errors.rejectValue 'version', "person.optimistic.locking.failure", |
---|
99 | "Another user has updated this Person while you were editing." |
---|
100 | render view: 'edit', model: buildPersonModel(person) |
---|
101 | return |
---|
102 | } |
---|
103 | |
---|
104 | person.properties = params |
---|
105 | |
---|
106 | if(params.pass == "") { |
---|
107 | person.pass = "InsertNothingToClearValidation" |
---|
108 | } |
---|
109 | else { |
---|
110 | if (person.validate()) { |
---|
111 | person.password = authenticateService.encodePassword(params.pass) |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | if (!person.hasErrors() && person.save()) { |
---|
116 | Authority.findAll().each { it.removeFromPersons(person) } |
---|
117 | addRoles(person) |
---|
118 | flash.message = "Person '$params.id - $params.loginName' updated." |
---|
119 | redirect action: show, id: person.id |
---|
120 | } |
---|
121 | else { |
---|
122 | render view: 'edit', model: buildPersonModel(person) |
---|
123 | } |
---|
124 | |
---|
125 | } |
---|
126 | |
---|
127 | def create = { |
---|
128 | [person: new Person(params), authorityList: Authority.list()] |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * Person save action. |
---|
133 | */ |
---|
134 | def save = { |
---|
135 | |
---|
136 | def person = new Person() |
---|
137 | person.properties = params |
---|
138 | person.password = authenticateService.encodePassword(params.pass) |
---|
139 | if (person.save()) { |
---|
140 | addRoles(person) |
---|
141 | redirect action: show, id: person.id |
---|
142 | } |
---|
143 | else { |
---|
144 | render view: 'create', model: [authorityList: Authority.list(), person: person] |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | private void addRoles(person) { |
---|
149 | for (String key in params.keySet()) { |
---|
150 | if (key.contains('ROLE') && 'on' == params.get(key)) { |
---|
151 | Authority.findByAuthority(key).addToPersons(person) |
---|
152 | } |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | private Map buildPersonModel(person) { |
---|
157 | |
---|
158 | List roles = Authority.list() |
---|
159 | roles.sort { r1, r2 -> |
---|
160 | r1.authority <=> r2.authority |
---|
161 | } |
---|
162 | Set userRoleNames = [] |
---|
163 | for (role in person.authorities) { |
---|
164 | userRoleNames << role.authority |
---|
165 | } |
---|
166 | LinkedHashMap<Authority, Boolean> roleMap = [:] |
---|
167 | for (role in roles) { |
---|
168 | roleMap[(role)] = userRoleNames.contains(role.authority) |
---|
169 | } |
---|
170 | |
---|
171 | return [person: person, roleMap: roleMap] |
---|
172 | } |
---|
173 | } |
---|