Changeset 294 for trunk/grails-app/controllers
- Timestamp:
- Jan 24, 2010, 10:21:39 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/PersonController.groovy
r250 r294 105 105 106 106 /** 107 108 107 * Person update action. 108 */ 109 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 } 110 Person.withTransaction { status -> 111 112 def person = Person.get(params.id) 113 if (!person) { 114 flash.message = "Person not found with id $params.id" 115 redirect action: edit, id: params.id 116 return 117 } 118 119 long version = params.version.toLong() 120 if (person.version > version) { 121 person.errors.rejectValue 'version', "person.optimistic.locking.failure", 122 "Another user has updated this Person while you were editing." 123 render view: 'edit', model: buildPersonModel(person) 124 return 125 } 126 127 person.properties = params 128 person.setPersonGroupsFromCheckBoxList(params.personGroups) 129 130 if(params.pass == "") { 131 person.pass = "InsertNothingToClearValidation" 132 } 133 else { 134 if (person.validate()) { 135 person.password = authenticateService.encodePassword(params.pass) 136 } 137 } 138 139 if (!person.hasErrors() && person.save(flush: true)) { 140 addRemoveRoles(person) 141 flash.message = "Person '$params.id - $params.loginName' updated." 142 redirect action: show, id: person.id 143 } 144 else { 145 render view: 'edit', model: buildPersonModel(person) 146 } 147 148 } //end withTransaction 149 } // update() 148 150 149 151 def create = { 150 152 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 156 153 [person: new Person(params), authorityList: getLimitedAuthorityList()] 154 } 155 156 /** 157 * Person save action. 158 */ 157 159 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)) { 160 Person.withTransaction { status -> 161 162 def person = new Person() 163 person.properties = params 164 person.password = authenticateService.encodePassword(params.pass) 165 person.setPersonGroupsFromCheckBoxList(params.personGroups) 166 if (person.save(flush: true)) { 167 addRemoveRoles(person) 168 redirect action: show, id: person.id 169 } 170 else { 171 render view: 'create', model: [person: person, authorityList: getLimitedAuthorityList()] 172 } 173 174 } //end withTransaction 175 } 176 177 private void addRemoveRoles(person) { 178 for (key in params.keySet()) { 179 if(key.startsWith("ROLE")) 174 180 Authority.findByAuthority(key).addToPersons(person) 175 } 181 else if(key.startsWith("_ROLE")) 182 Authority.findByAuthority(key.substring(1)).removeFromPersons(person) 176 183 } 177 184 } … … 179 186 private Map buildPersonModel(person) { 180 187 181 List roles = Authority.list()188 List roles = getLimitedAuthorityList() 182 189 roles.sort { r1, r2 -> 183 190 r1.authority <=> r2.authority … … 194 201 return [person: person, roleMap: roleMap] 195 202 } 203 204 /** 205 * Get the full authorityList if current user is an App Admin else leave that authority off the list. 206 */ 207 def getLimitedAuthorityList = { 208 def authorityList = [] 209 if(authenticateService.ifAnyGranted('ROLE_AppAdmin')) 210 authorityList = Authority.list().sort { p1, p2 -> p1.authority.compareToIgnoreCase(p2.authority) } 211 else 212 authorityList = Authority.withCriteria { gt("id", 1L) }.sort { p1, p2 -> p1.authority.compareToIgnoreCase(p2.authority) } 213 214 return authorityList 215 } 196 216 }
Note: See TracChangeset
for help on using the changeset viewer.