Changeset 294


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.

Location:
trunk/grails-app
Files:
5 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}
  • trunk/grails-app/domain/Person.groovy

    r166 r294  
    5454    //Overriding the default toString method
    5555    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  
    143143        def authInstance
    144144
     145        // Authority #1
    145146        authInstance = new Authority(description:"Application Admin, not required for daily use! Grants full admin access to the application.",
    146147                                        authority:"ROLE_AppAdmin")
     
    152153        def authInstance
    153154
     155        // Authority #2
    154156        authInstance = new Authority(description:"Business manager, grants full management access.",
    155157                                        authority:"ROLE_Manager")
    156158        saveAndTest(authInstance)
    157159
    158         // #3
     160        // Authority #3
    159161        authInstance = new Authority(description:"Application User, all application users need this base role to allow login.",
    160162                                        authority:"ROLE_AppUser")
  • trunk/grails-app/views/person/create.gsp

    r168 r294  
    111111                        </td>
    112112                        <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--']"/>
    117113                            <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"/>
    118118                        </td>
    119119                    </tr>
  • trunk/grails-app/views/person/edit.gsp

    r239 r294  
    118118                        </td>
    119119                        <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--']"/>
    124120                            <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"/>
    125125                        </td>
    126126                    </tr>
Note: See TracChangeset for help on using the changeset viewer.