Ignore:
Timestamp:
Oct 9, 2009, 10:11:43 PM (15 years ago)
Author:
gav
Message:

Add params.message to Person create and edit views.
Code format to 4 spaces, no tabs.

File:
1 edited

Legend:

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

    r149 r150  
    77    def filterService
    88
    9         // the delete, save and update actions only accept POST requests
    10         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']
    1111
    12         def index = {
    13                 redirect action: list, params: params
    14         }
     12    def index = {
     13        redirect action: list, params: params
     14    }
    1515
    1616    def list = {
     
    2727    }
    2828
    29         def show = {
     29    def show = {
    3030
    3131        // In the case of an actionSubmit button, rewrite action name from 'index'.
     
    3333        { params.action='show' }
    3434
    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         }
     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    }
    5050
    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 = {
    5656
    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."
    6363                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) }
    6868                person.isActive = false
    6969                person.save(flush: true)
     
    7878                    redirect(action:show,id:params.id)
    7979                }
    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    }
    8686
    87         def edit = {
     87    def edit = {
    8888
    8989        // In the case of an actionSubmit button, rewrite action name from 'index'.
     
    9191        { params.action='edit' }
    9292
    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         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    }
    102102
    103         /**
    104         * Person update action.
    105         */
    106         def update = {
     103    /**
     104        * Person update action.
     105        */
     106    def update = {
    107107
    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                 }
     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        }
    114114
    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."
    119119            render view: 'edit', model: buildPersonModel(person)
    120                         return
    121                 }
     120            return
     121        }
    122122
    123                 person.properties = params
     123        person.properties = params
    124124
    125125        if(params.pass == "") {
     
    142142        }
    143143
    144         }
     144    }
    145145
    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    }
    150150
    151         /**
    152         * Person save action.
    153         */
    154         def save = {
     151    /**
     152        * Person save action.
     153        */
     154    def save = {
    155155
    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         }
     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    }
    167167
    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    }
    175175
    176         private Map buildPersonModel(person) {
     176    private Map buildPersonModel(person) {
    177177
    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                 }
     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        }
    190190
    191                 return [person: person, roleMap: roleMap]
    192         }
     191        return [person: person, roleMap: roleMap]
     192    }
    193193}
Note: See TracChangeset for help on using the changeset viewer.