title: How to get All users which have a certain Role in grails
body: <p>I want to retrieve all users which have a specific Role like "ROLE_USER".</p>
  
  <p>Below are Domain Classes for User, Role and UserRole.</p>
  
  <p><strong>User.groovy</strong></p>
  
  <pre><code>class User {
  
      transient springSecurityService
  
      String username
      String password
          String email
      boolean enabled
      boolean accountExpired
      boolean accountLocked
      boolean passwordExpired
  
      static constraints = {
          username blank: false, unique: true
          password blank: false
      }
  
      static mapping = {
          password column: '`password`'
      }
  
      Set&lt;Role&gt; getAuthorities() {
          UserRole.findAllByUser(this).collect { it.role } as Set
      }
  
      def beforeInsert() {
          encodePassword()
      }
  
      def beforeUpdate() {
          if (isDirty('password')) {
              encodePassword()
          }
      }
  
      protected void encodePassword() {
          password = springSecurityService.encodePassword(password)
      }
  }
  </code></pre>
  
  <p><strong>Role.groovy</strong></p>
  
  <pre><code>class Role {
  
      String authority
  
      static mapping = {
          cache true
      }
  
      static constraints = {
          authority blank: false, unique: true
      }
  }
  </code></pre>
  
  <p><strong>UserRole.groovy</strong></p>
  
  <pre><code>class UserRole implements Serializable {
  
      User user
      Role role
  
      boolean equals(other) {
          if (!(other instanceof UserRole)) {
              return false
          }
  
          other.user?.id == user?.id &amp;&amp;
              other.role?.id == role?.id
      }
  
      int hashCode() {
          def builder = new HashCodeBuilder()
          if (user) builder.append(user.id)
          if (role) builder.append(role.id)
          builder.toHashCode()
      }
  
      static UserRole get(long userId, long roleId) {
          find 'from UserRole where user.id=:userId and role.id=:roleId',
              [userId: userId, roleId: roleId]
      }
  
      static UserRole create(User user, Role role, boolean flush = false) {
          new UserRole(user: user, role: role).save(flush: flush, insert: true)
      }
  
      static boolean remove(User user, Role role, boolean flush = false) {
          UserRole instance = UserRole.findByUserAndRole(user, role)
          if (!instance) {
              return false
          }
  
          instance.delete(flush: flush)
          true
      }
  
      static void removeAll(User user) {
          executeUpdate 'DELETE FROM UserRole WHERE user=:user', [user: user]
      }
  
      static void removeAll(Role role) {
          executeUpdate 'DELETE FROM UserRole WHERE role=:role', [role: role]
      }
  
      static mapping = {
          id composite: ['role', 'user']
          version false
      }
  }
  </code></pre>
  
  <p>These Domain Classes are generated by <strong>Spring Security</strong> plugin.<br>
  I have added only email field for User class.  </p>
  
  <p>Here is my <strong>UserController.groovy</strong></p>
  
  <pre><code>class UserController {
  
      def index = {
         }
  
  
      def list = {
  
          def role = Role.findByAuthority("ROLE_USER")
          println "role id "+role.id
  
          def users = User.findAll()         //Its giving me all Users regardless of Role
          println "total users "+users.size()
          for(user in users)
          {
              println "User "+user.username+" "+user.email
          }
          render (view: "listUsers", model:[users:users])
  
      }
  }
  </code></pre>
  
  <p>In the list action I used <code>User.findAll()</code> but its giving me all user with all roles.<br>
  I want user list only from a certain role..</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Code to Assign Roles to newly created user</p>
  
  <pre><code>def username = params.username
  def emailID = params.emailID
  def password = params.password
  
  def testUser = new User(username: username, enabled: true, password: password,email:emailID)
  testUser.save(flush: true)
  def userRole = new Role(authority: 'ROLE_USER').save(flush: true)
  UserRole.create testUser, userRole, true
  </code></pre>
  
  <p>Thanks..</p>