- Timestamp:
- Jan 24, 2010, 10:21:39 PM (15 years ago)
- Location:
- trunk/grails-app
- Files:
-
- 5 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 } -
trunk/grails-app/domain/Person.groovy
r166 r294 54 54 //Overriding the default toString method 55 55 String toString() {"${this.firstName} ${this.lastName}"} 56 } 56 57 // This additional setter is used to convert the checkBoxList string 58 // of ids selected to the corresponding domain objects. 59 public void setPersonGroupsFromCheckBoxList(ids) { 60 def idList = [] 61 ids.each() { 62 if(it.isInteger()) 63 idList << it.toInteger() 64 } 65 this.personGroups = idList.collect { PersonGroup.get( it ) } 66 } 67 68 } // end class -
trunk/grails-app/services/CreateDataService.groovy
r291 r294 143 143 def authInstance 144 144 145 // Authority #1 145 146 authInstance = new Authority(description:"Application Admin, not required for daily use! Grants full admin access to the application.", 146 147 authority:"ROLE_AppAdmin") … … 152 153 def authInstance 153 154 155 // Authority #2 154 156 authInstance = new Authority(description:"Business manager, grants full management access.", 155 157 authority:"ROLE_Manager") 156 158 saveAndTest(authInstance) 157 159 158 // #3160 // Authority #3 159 161 authInstance = new Authority(description:"Application User, all application users need this base role to allow login.", 160 162 authority:"ROLE_AppUser") -
trunk/grails-app/views/person/create.gsp
r168 r294 111 111 </td> 112 112 <td valign="top" class="value ${hasErrors(bean:person,field:'personGroups','errors')}"> 113 <g:select id="personGroups" name="personGroups"114 from="${PersonGroup.list()}"115 optionKey="id" size="5" multiple="yes"116 value="${person?.personGroups?.id}" noSelection="['':'--None--']"/>117 113 <g:helpBalloon class="helpballoon" code="person.personGroups" /> 114 <custom:checkBoxList name="personGroups" 115 from="${PersonGroup.list().sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }}" 116 value="${person?.personGroups?.collect{it.id}}" 117 optionKey="id"/> 118 118 </td> 119 119 </tr> -
trunk/grails-app/views/person/edit.gsp
r239 r294 118 118 </td> 119 119 <td valign="top" class="value ${hasErrors(bean:person,field:'personGroups','errors')}"> 120 <g:select id="personGroups" name="personGroups"121 from="${PersonGroup.list()}"122 optionKey="id" size="10" multiple="yes"123 value="${person?.personGroups.id}" noSelection="['':'--None--']"/>124 120 <g:helpBalloon class="helpballoon" code="person.personGroups" /> 121 <custom:checkBoxList name="personGroups" 122 from="${PersonGroup.list().sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }}" 123 value="${person?.personGroups?.collect{it.id}}" 124 optionKey="id"/> 125 125 </td> 126 126 </tr>
Note: See TracChangeset
for help on using the changeset viewer.