How to limit domain results with criteria

jasonwucinski

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

Code:
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:

Code:
<g:each in="${userInstanceList}" status="i" var="userInstance">
 <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
 <td><g:link action="show" id="${userInstance.id}">
  ${fieldValue(bean: userInstance, field:   "id")}    </g:link></td>
 <td>${fieldValue(bean: userInstance, field: "login")}</td>
 <td>****</td>
 <td>${fieldValue(bean: userInstance, field: "fname")}</td>
 <td>${fieldValue(bean: userInstance, field: "lname")}</td>
 <td>${fieldValue(bean: userInstance, field: "role")}</td>
 </tr>
 </g:each>

I call the controller with this code, in separate gsp view:
Code:
<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
 

carlosmain

New member
If you are using a plugin like SpringSecurity you can check the role that the user has then display a different view for each type of user
 
Top