Changeset 150 for trunk/grails-app/controllers
- Timestamp:
- Oct 9, 2009, 10:11:43 PM (16 years ago)
- Location:
- trunk/grails-app/controllers
- Files:
-
- 3 edited
-
AppCoreController.groovy (modified) (1 diff)
-
AuthorityController.groovy (modified) (1 diff)
-
PersonController.groovy (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/AppCoreController.groovy
r149 r150 55 55 def personInstance = Person.get(authenticateService.userDomain().id) 56 56 return [ personInstance : personInstance ] 57 } 57 } 58 58 59 59 if (request.method == 'POST') { -
trunk/grails-app/controllers/AuthorityController.groovy
r116 r150 3 3 class AuthorityController extends BaseAppAdminController { 4 4 5 // the delete, save and update actions only accept POST requests6 static Map allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']5 // the delete, save and update actions only accept POST requests 6 static Map allowedMethods = [delete: 'POST', save: 'POST', update: 'POST'] 7 7 8 def authenticateService8 def authenticateService 9 9 10 def index = {11 redirect action: list, params: params12 }10 def index = { 11 redirect action: list, params: params 12 } 13 13 14 /**15 * Display the list authority page.16 */17 def list = {18 if (!params.max) {19 params.max = 1020 }21 [authorityList: Authority.list(params)]22 }14 /** 15 * Display the list authority page. 16 */ 17 def list = { 18 if (!params.max) { 19 params.max = 10 20 } 21 [authorityList: Authority.list(params)] 22 } 23 23 24 /**25 * Display the show authority page.26 */27 def show = {28 def authority = Authority.get(params.id)29 if (!authority) {30 flash.message = "Authority not found with id $params.id"31 redirect action: list32 return33 }24 /** 25 * Display the show authority page. 26 */ 27 def show = { 28 def authority = Authority.get(params.id) 29 if (!authority) { 30 flash.message = "Authority not found with id $params.id" 31 redirect action: list 32 return 33 } 34 34 35 [authority: authority]36 }35 [authority: authority] 36 } 37 37 38 /**39 * Delete an authority.40 */41 def delete = {42 def authority = Authority.get(params.id)43 if (!authority) {44 flash.message = "Authority not found with id $params.id"45 redirect action: list46 return47 }38 /** 39 * Delete an authority. 40 */ 41 def delete = { 42 def authority = Authority.get(params.id) 43 if (!authority) { 44 flash.message = "Authority not found with id $params.id" 45 redirect action: list 46 return 47 } 48 48 49 authenticateService.deleteRole(authority)49 authenticateService.deleteRole(authority) 50 50 51 flash.message = "Authority $params.id deleted."52 redirect action: list53 }51 flash.message = "Authority $params.id deleted." 52 redirect action: list 53 } 54 54 55 /**56 * Display the edit authority page.57 */58 def edit = {59 def authority = Authority.get(params.id)60 if (!authority) {61 flash.message = "Authority not found with id $params.id"62 redirect action: list63 return64 }55 /** 56 * Display the edit authority page. 57 */ 58 def edit = { 59 def authority = Authority.get(params.id) 60 if (!authority) { 61 flash.message = "Authority not found with id $params.id" 62 redirect action: list 63 return 64 } 65 65 66 [authority: authority]67 }66 [authority: authority] 67 } 68 68 69 /**70 * Authority update action.71 */72 def update = {69 /** 70 * Authority update action. 71 */ 72 def update = { 73 73 74 def authority = Authority.get(params.id)75 if (!authority) {76 flash.message = "Authority not found with id $params.id"77 redirect action: edit, id: params.id78 return79 }74 def authority = Authority.get(params.id) 75 if (!authority) { 76 flash.message = "Authority not found with id $params.id" 77 redirect action: edit, id: params.id 78 return 79 } 80 80 81 long version = params.version.toLong()82 if (authority.version > version) {83 authority.errors.rejectValue 'version', 'authority.optimistic.locking.failure',84 'Another user has updated this Authority while you were editing.'85 render view: 'edit', model: [authority: authority]86 return87 }81 long version = params.version.toLong() 82 if (authority.version > version) { 83 authority.errors.rejectValue 'version', 'authority.optimistic.locking.failure', 84 'Another user has updated this Authority while you were editing.' 85 render view: 'edit', model: [authority: authority] 86 return 87 } 88 88 89 if (authenticateService.updateRole(authority, params)) {90 authenticateService.clearCachedRequestmaps()91 redirect action: show, id: authority.id92 }93 else {94 render view: 'edit', model: [authority: authority]95 }96 }89 if (authenticateService.updateRole(authority, params)) { 90 authenticateService.clearCachedRequestmaps() 91 redirect action: show, id: authority.id 92 } 93 else { 94 render view: 'edit', model: [authority: authority] 95 } 96 } 97 97 98 /**99 * Display the create new authority page.100 */101 def create = {102 [authority: new Authority()]103 }98 /** 99 * Display the create new authority page. 100 */ 101 def create = { 102 [authority: new Authority()] 103 } 104 104 105 /**106 * Save a new authority.107 */108 def save = {105 /** 106 * Save a new authority. 107 */ 108 def save = { 109 109 110 def authority = new Authority()111 authority.properties = params112 if (authority.save()) {113 redirect action: show, id: authority.id114 }115 else {116 render view: 'create', model: [authority: authority]117 }118 }110 def authority = new Authority() 111 authority.properties = params 112 if (authority.save()) { 113 redirect action: show, id: authority.id 114 } 115 else { 116 render view: 'create', model: [authority: authority] 117 } 118 } 119 119 } -
trunk/grails-app/controllers/PersonController.groovy
r149 r150 7 7 def filterService 8 8 9 // the delete, save and update actions only accept POST requests10 static Map allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']9 // the delete, save and update actions only accept POST requests 10 static Map allowedMethods = [delete: 'POST', save: 'POST', update: 'POST'] 11 11 12 def index = {13 redirect action: list, params: params14 }12 def index = { 13 redirect action: list, params: params 14 } 15 15 16 16 def list = { … … 27 27 } 28 28 29 def show = {29 def show = { 30 30 31 31 // In the case of an actionSubmit button, rewrite action name from 'index'. … … 33 33 { params.action='show' } 34 34 35 def person = Person.get(params.id)36 if (!person) {37 flash.message = "Person not found with id $params.id"38 redirect action: list39 return40 }41 List roleNames = []42 for (role in person.authorities) {43 roleNames << role.authority44 }45 roleNames.sort { n1, n2 ->46 n1 <=> n247 }48 [person: person, roleNames: roleNames]49 }35 def person = Person.get(params.id) 36 if (!person) { 37 flash.message = "Person not found with id $params.id" 38 redirect action: list 39 return 40 } 41 List roleNames = [] 42 for (role in person.authorities) { 43 roleNames << role.authority 44 } 45 roleNames.sort { n1, n2 -> 46 n1 <=> n2 47 } 48 [person: person, roleNames: roleNames] 49 } 50 50 51 /**52 * Person delete action. Before removing an existing person,53 * they should be removed from those authorities which they are involved.54 */55 def delete = {51 /** 52 * Person delete action. Before removing an existing person, 53 * they should be removed from those authorities which they are involved. 54 */ 55 def delete = { 56 56 57 def person = Person.get(params.id)58 if (person) {59 def authPrincipal = authenticateService.principal()60 // Avoid self-delete.61 if (!(authPrincipal instanceof String) && authPrincipal.username == person.loginName) {62 flash.message = "You cannot delete yourself, please login as another manager and try again."57 def person = Person.get(params.id) 58 if (person) { 59 def authPrincipal = authenticateService.principal() 60 // Avoid self-delete. 61 if (!(authPrincipal instanceof String) && authPrincipal.username == person.loginName) { 62 flash.message = "You cannot delete yourself, please login as another manager and try again." 63 63 redirect(action:show,id:params.id) 64 }65 else {66 //first, delete this person from Persons_Authorities table.67 Authority.findAll().each { it.removeFromPersons(person) }64 } 65 else { 66 //first, delete this person from Persons_Authorities table. 67 Authority.findAll().each { it.removeFromPersons(person) } 68 68 person.isActive = false 69 69 person.save(flush: true) … … 78 78 redirect(action:show,id:params.id) 79 79 } 80 }81 }82 else {83 flash.message = "Person not found with id $params.id"84 }85 }80 } 81 } 82 else { 83 flash.message = "Person not found with id $params.id" 84 } 85 } 86 86 87 def edit = {87 def edit = { 88 88 89 89 // In the case of an actionSubmit button, rewrite action name from 'index'. … … 91 91 { params.action='edit' } 92 92 93 def person = Person.get(params.id)94 if (!person) {95 flash.message = "Person not found with id $params.id"96 redirect action: list97 return98 }99 flash.message = "To allow login at least the 'ROLE_AppUser' authority must be given."100 return buildPersonModel(person)101 }93 def person = Person.get(params.id) 94 if (!person) { 95 flash.message = "Person not found with id $params.id" 96 redirect action: list 97 return 98 } 99 params.message = "To allow login at least the 'ROLE_AppUser' authority must be given." 100 return buildPersonModel(person) 101 } 102 102 103 /**104 * Person update action.105 */106 def update = {103 /** 104 * Person update action. 105 */ 106 def update = { 107 107 108 def person = Person.get(params.id)109 if (!person) {110 flash.message = "Person not found with id $params.id"111 redirect action: edit, id: params.id112 return113 }108 def person = Person.get(params.id) 109 if (!person) { 110 flash.message = "Person not found with id $params.id" 111 redirect action: edit, id: params.id 112 return 113 } 114 114 115 long version = params.version.toLong()116 if (person.version > version) {117 person.errors.rejectValue 'version', "person.optimistic.locking.failure",118 "Another user has updated this Person while you were editing."115 long version = params.version.toLong() 116 if (person.version > version) { 117 person.errors.rejectValue 'version', "person.optimistic.locking.failure", 118 "Another user has updated this Person while you were editing." 119 119 render view: 'edit', model: buildPersonModel(person) 120 return121 }120 return 121 } 122 122 123 person.properties = params123 person.properties = params 124 124 125 125 if(params.pass == "") { … … 142 142 } 143 143 144 }144 } 145 145 146 def create = {147 flash.message = "To allow login at least the 'ROLE_AppUser' authority must be given."148 [person: new Person(params), authorityList: Authority.list()]149 }146 def create = { 147 params.message = "To allow login at least the 'ROLE_AppUser' authority must be given." 148 [person: new Person(params), authorityList: Authority.list()] 149 } 150 150 151 /**152 * Person save action.153 */154 def save = {151 /** 152 * Person save action. 153 */ 154 def save = { 155 155 156 def person = new Person()157 person.properties = params158 person.password = authenticateService.encodePassword(params.pass)159 if (person.save()) {160 addRoles(person)161 redirect action: show, id: person.id162 }163 else {164 render view: 'create', model: [authorityList: Authority.list(), person: person]165 }166 }156 def person = new Person() 157 person.properties = params 158 person.password = authenticateService.encodePassword(params.pass) 159 if (person.save()) { 160 addRoles(person) 161 redirect action: show, id: person.id 162 } 163 else { 164 render view: 'create', model: [authorityList: Authority.list(), person: person] 165 } 166 } 167 167 168 private void addRoles(person) {169 for (String key in params.keySet()) {170 if (key.contains('ROLE') && 'on' == params.get(key)) {171 Authority.findByAuthority(key).addToPersons(person)172 }173 }174 }168 private void addRoles(person) { 169 for (String key in params.keySet()) { 170 if (key.contains('ROLE') && 'on' == params.get(key)) { 171 Authority.findByAuthority(key).addToPersons(person) 172 } 173 } 174 } 175 175 176 private Map buildPersonModel(person) {176 private Map buildPersonModel(person) { 177 177 178 List roles = Authority.list()179 roles.sort { r1, r2 ->180 r1.authority <=> r2.authority181 }182 Set userRoleNames = []183 for (role in person.authorities) {184 userRoleNames << role.authority185 }186 LinkedHashMap<Authority, Boolean> roleMap = [:]187 for (role in roles) {188 roleMap[(role)] = userRoleNames.contains(role.authority)189 }178 List roles = Authority.list() 179 roles.sort { r1, r2 -> 180 r1.authority <=> r2.authority 181 } 182 Set userRoleNames = [] 183 for (role in person.authorities) { 184 userRoleNames << role.authority 185 } 186 LinkedHashMap<Authority, Boolean> roleMap = [:] 187 for (role in roles) { 188 roleMap[(role)] = userRoleNames.contains(role.authority) 189 } 190 190 191 return [person: person, roleMap: roleMap]192 }191 return [person: person, roleMap: roleMap] 192 } 193 193 }
Note: See TracChangeset
for help on using the changeset viewer.
