Announcement Announcement Module
No announcement yet.
How to limit domain results with criteria Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to limit domain results with criteria

    I am new to grails and I have a question about limiting a query result: domain User:

    class User {    
    String  login
    String  password
    String  fname
    String  lname
    String role = "user"    
    static constraints = {
    login       (size:5..15, blank:false, unique:true,matches:"[a-zA-Z0-9]+")
    password    (size:5..15, blank:false)
    fname       (blank:false)
    lname       (blank:false)
    role(inList:["user", "admin"])
    String toString(){
      fname & " " & lname
    static mapping = {
        cache true
        columns {
            fname       type:'text'
            lname       type: 'text'
            password    type:'text'
            login       type:'text'
    my GSP page snippet that displays the results:

    <g:each in="${userInstanceList}" status="i" var="userInstance">
     <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
     <td><g:link action="show" id="${}">
      ${fieldValue(bean: userInstance, field:   "id")}    </g:link></td>
     <td>${fieldValue(bean: userInstance, field: "login")}</td>
     <td>${fieldValue(bean: userInstance, field: "fname")}</td>
     <td>${fieldValue(bean: userInstance, field: "lname")}</td>
     <td>${fieldValue(bean: userInstance, field: "role")}</td>
    I call the controller with this code, in separate gsp view:
    <g:link class="users" controller="user" params="[sort:'fname',order:'desc']" action="" >Manager Users</g:link>
    My question is, how do I call the domain and display the results according to the following criteria: first, if the Role is admin, display everything. If the role is not admin, only display the results of certain login value (ie, just show the results where the login = the current user)

    Thanks for your help! jason

  • #2
    You have two options. You can either do the check inside the controller (or a service that the controller delegates to) such that
    only contains the entries that the user is allowed to view.

    The other option is to perform the filtering in the view, but in this case I don't think that makes sense.

    Hope that helps,