Jan
Jan

Reputation: 3401

How to filter user by spring security role in domain

Is that possible? If not then how do i filter in my gsp a specific role for this code?

<div class="fieldcontain ${hasErrors(bean: tableInstance, field: 'user', 'error')} ">
<label for="user">
    <g:message code="table.user.label" default="User" />        
</label>
<g:select id="user" name="user.id" from="${rms.User.list()}" optionKey="id" value="${tableInstance?.user?.id}" class="many-to-one" noSelection="['null': '']"/>

The snippet above is for the basic create and edit functions

Upvotes: 0

Views: 517

Answers (1)

emilan
emilan

Reputation: 13075

Use sec:access tag, provided by spring-security plugin.

<sec:access expression="hasRole('ROLE')">
      <div class="fieldcontain ${hasErrors(bean: tableInstance, field: 'user', 'error')} ">
       <label for="user">
          <g:message code="table.user.label" default="User" />        
       </label>
       <g:select id="user" name="user.id" from="${rms.User.list()}" optionKey="id" value="${tableInstance?.user?.id}" class="many-to-one" noSelection="['null': '']"/>
</sec:access>

or you can use sec:ifAnyGranted tag

<sec:ifAnyGranted roles="ROLE1, ROLE2, ...">
    ....
</sec:ifAnyGranted>

EDIT

def users = UserRole.findAllByRole(Role.get('ROLE'))*.user.unique()

Upvotes: 2

Related Questions