Changeset 295


Ignore:
Timestamp:
Jan 25, 2010, 10:57:30 AM (14 years ago)
Author:
gav
Message:

Authority add/remove logic in r294 not behaving as expected, fixed.
Switch views back to using Authority desctiptions.

Location:
trunk/grails-app
Files:
4 edited

Legend:

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

    r294 r295  
    4242            return
    4343        }
    44         List roleNames = []
    45         for (role in person.authorities) {
    46             roleNames << role.authority
    47         }
    48         roleNames.sort { n1, n2 ->
    49             n1 <=> n2
    50         }
    51         [person: person, roleNames: roleNames]
     44        def authorityList = person.authorities.sort { p1, p2 -> p1.id <=> p2.id }
     45        [person: person, authorityList: authorityList]
    5246    }
    5347
     
    138132
    139133            if (!person.hasErrors() && person.save(flush: true)) {
    140                 addRemoveRoles(person)
     134                addRemoveAuthorities(person)
    141135                flash.message = "Person '$params.id - $params.loginName' updated."
    142136                redirect action: show, id: person.id
     
    165159            person.setPersonGroupsFromCheckBoxList(params.personGroups)
    166160            if (person.save(flush: true)) {
    167                 addRemoveRoles(person)
     161                addRemoveAuthorities(person)
    168162                redirect action: show, id: person.id
    169163            }
     
    175169    }
    176170
    177     private void addRemoveRoles(person) {
     171    /**
     172    * Add or remove authorities from person as indicated in params.
     173    */
     174    private void addRemoveAuthorities(person) {
     175        def authMap = [:]
     176
     177        // Build authMap from params.
    178178        for (key in params.keySet()) {
    179             if(key.startsWith("ROLE"))
    180                 Authority.findByAuthority(key).addToPersons(person)
    181             else if(key.startsWith("_ROLE"))
    182                 Authority.findByAuthority(key.substring(1)).removeFromPersons(person)
     179            if(key.startsWith("ROLE")) {
     180                authMap.(key.toString()) = "add"
     181            }
     182            else if(key.startsWith("_ROLE")) {
     183                if( !authMap.(key.substring(1)) ) authMap.(key.substring(1)) = "remove"
     184            }
     185        }
     186
     187        // Add or remove authorities.
     188        for(a in authMap) {
     189            if(a.value == "add")
     190                Authority.findByAuthority(a.key.toString()).addToPersons(person)
     191            else
     192                Authority.findByAuthority(a.key.toString()).removeFromPersons(person)
    183193        }
    184194    }
     
    187197
    188198        List roles = getLimitedAuthorityList()
    189         roles.sort { r1, r2 ->
    190             r1.authority <=> r2.authority
    191         }
    192199        Set userRoleNames = []
    193200        for (role in person.authorities) {
     
    205212    * Get the full authorityList if current user is an App Admin else leave that authority off the list.
    206213    */
    207     def getLimitedAuthorityList = {
     214    private List getLimitedAuthorityList() {
    208215        def authorityList = []
    209216        if(authenticateService.ifAnyGranted('ROLE_AppAdmin'))
    210             authorityList = Authority.list().sort { p1, p2 -> p1.authority.compareToIgnoreCase(p2.authority) }
     217            authorityList = Authority.list().sort { p1, p2 -> p1.id <=> p2.id }
    211218        else
    212             authorityList = Authority.withCriteria { gt("id", 1L) }.sort { p1, p2 -> p1.authority.compareToIgnoreCase(p2.authority) }
     219            authorityList = Authority.withCriteria { gt("id", 1L) }.sort { p1, p2 -> p1.id <=> p2.id }
    213220
    214221        return authorityList
    215222    }
    216 }
     223} // end class
  • trunk/grails-app/views/person/create.gsp

    r294 r295  
    130130                    <g:each in="${authorityList}">
    131131                    <tr>
    132                         <td valign="top" class="name" align="left">${it.authority.encodeAsHTML()}</td>
     132                        <td valign="top" class="name" align="left">${it.description.encodeAsHTML()}</td>
    133133                        <td align="left">
    134134                            <g:checkBox name="${it.authority}" value="${it.authority == 'ROLE_AppUser'}"/>
  • trunk/grails-app/views/person/edit.gsp

    r294 r295  
    137137                    <g:each var="entry" in="${roleMap}">
    138138                    <tr>
    139                         <td valign="top" class="name" align="left">${entry.key.authority.encodeAsHTML()}</td>
     139                        <td valign="top" class="name" align="left">${entry.key.description.encodeAsHTML()}</td>
    140140                        <td align="left"><g:checkBox name="${entry.key.authority}" value="${entry.value}"/></td>
    141141                    </tr>
  • trunk/grails-app/views/person/show.gsp

    r166 r295  
    8484                    <td valign="top" class="value">
    8585                        <ul>
    86                         <g:each in="${roleNames}" var='name'>
    87                             <li>${name}</li>
     86                        <g:each  var='a' in="${authorityList}">
     87                            <li>${a.description.encodeAsHTML()}</li>
    8888                        </g:each>
    8989                        </ul>
Note: See TracChangeset for help on using the changeset viewer.