Ignore:
Timestamp:
Jan 24, 2010, 10:21:39 PM (14 years ago)
Author:
gav
Message:

Add custom checkBoxList for personGroups to Person.
Wrap save and update in transactions, tweak role add/remove logic and use a limited role list for non-admin users.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/PersonController.groovy

    r250 r294  
    105105
    106106    /**
    107         * Person update action.
    108         */
     107    * Person update action.
     108    */
    109109    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()
    148150
    149151    def create = {
    150152        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         * Person save action.
    156         */
     153        [person: new Person(params), authorityList: getLimitedAuthorityList()]
     154    }
     155
     156    /**
     157    * Person save action.
     158    */
    157159    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"))
    174180                Authority.findByAuthority(key).addToPersons(person)
    175             }
     181            else if(key.startsWith("_ROLE"))
     182                Authority.findByAuthority(key.substring(1)).removeFromPersons(person)
    176183        }
    177184    }
     
    179186    private Map buildPersonModel(person) {
    180187
    181         List roles = Authority.list()
     188        List roles = getLimitedAuthorityList()
    182189        roles.sort { r1, r2 ->
    183190            r1.authority <=> r2.authority
     
    194201        return [person: person, roleMap: roleMap]
    195202    }
     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    }
    196216}
Note: See TracChangeset for help on using the changeset viewer.