1

I am trying to send email to all users with admin role when list() action has been called. Inside my list method I put the following code:

 def admins = User.findAllByRole("ROLE_ADMIN")
        //def approverEmails = User.findByRole("ROLE_APPROVER").collect { it.email }
        notifierService.sendApproverRequestEmail(admins)

        flash.message = message(code: 'default.created.message', args: [message(code: 'project.label', default: 'Project'), projectInstance.id])
        redirect(action: "show", id: projectInstance.id)

But Grails doesn't recognize findAllByRole() method. What am I doing wrong? Is there any other way to send message from service when certain actions in controller are called.

Here is also my service code:

  def sendApprovalRequestEmail( def users ) {
    users.each { -> user
        mailService.sendMail{
            to user.email
            from "padre@everyonecounts.com"
            subject "New project needs approval."
            body "Hi ${user.username}! " +
                    "New project has been requested and needs your approval."


        }
    }
}

Here is the error:

URI
/PaDRe/project/list
Class
org.codehaus.groovy.grails.exceptions.InvalidPropertyException
Message
No property found for name [role] for class [class com.everyonecounts.padre.User]
Around line 21 of grails-app\controllers\com\everyonecounts\padre\ProjectController.groovy
18:        params.max = Math.min(params.max ? params.int('max') : 10, 100)
19:        [projectInstanceList: Project.list(params), projectInstanceTotal: Project.count()]
20:
21:        def admins = User.findAllByRole("ROLE_ADMIN")
22:        //def approverEmails = User.findByRole("ROLE_APPROVER").collect { it.email }
23:        notifierService.sendApproverRequestEmail(admins)
24:
Trace
   Line | Method
->> 108 | methodMissing in org.grails.datastore.gorm.GormStaticApi
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    21 | list          in ProjectController.groovy
|   895 | runTask . . . in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run           in     ''
^   662 | run . . . . . in java.lang.Thread

Here is my User class

package com.everyonecounts.padre

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 size: 5..80, blank: false
    }

    static mapping = {
        password column: '`password`'
    }

    Set<Role> 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)
    }
}
  • Your question would benefit from having the error message included so we know what you are seeing. I've added an answer which I think is probably the problem you are experiencing, but if it isn't please edit your question include the stack trace. – David May 22 '13 at 23:11
  • Ok, so it's telling you that there is no 'role' property for `User`. Does that sound correct? Can you please edit your question so we can see the `User` class. – David May 22 '13 at 23:24
  • I just included my user class – Shaken Bissenkulov May 22 '13 at 23:28

2 Answers2

0

The problem you are having is because you don't understand the default spring security implementation. To get a list of users with a given role:

UserRole.findAllByRole(Role.findByAuthority("ROLE_ADMIN"))*.user
James Kleeh
  • 12,094
  • 5
  • 34
  • 61
-1

Your problem probably is that User.findAllByRole() is expecting a Role as the argument, not a String.

There is a relevant example in 'Querying Associations' subsection of http://grails.org/doc/2.2.x/guide/single.html#finders (shown below)

def author = Author.findByName("Stephen King")

def books = author ? Book.findAllByAuthor(author) : []
David
  • 1,940
  • 3
  • 17
  • 30
  • I changed this line def admins = User.findAllByRole("ROLE_ADMIN") to this def adminRole = User.findByName("ROLE_ADMIN") def admins = adminRole ? User.findAllByRole(adminRole) : [] but I still getting the same type of error. – Shaken Bissenkulov May 22 '13 at 23:23
  • It should be `def adminRole = Role.findByName("ROLE_ADMIN")` – David May 22 '13 at 23:27