1

当调用 list() 操作时,我正在尝试向所有具有管理员角色的用户发送电子邮件。在我的列表方法中,我输入了以下代码:

 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)

但是 Grails 不识别 findAllByRole() 方法。我究竟做错了什么?当调用控制器中的某些操作时,是否有任何其他方式可以从服务发送消息。

这也是我的服务代码:

  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."


        }
    }
}

这是错误:

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

这是我的用户类

包 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)
    }
}
4

2 回答 2

0

您遇到的问题是因为您不了解默认的 spring 安全实现。要获取具有给定角色的用户列表:

UserRole.findAllByRole(Role.findByAuthority("ROLE_ADMIN"))*.user
于 2013-05-22T23:49:30.867 回答
-1

您的问题可能是 User.findAllByRole() 期望 aRole作为参数,而不是 a String

在http://grails.org/doc/2.2.x/guide/single.html#finders的“查询关联”小节中有一个相关示例(如下所示)

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

def books = author ? Book.findAllByAuthor(author) : []
于 2013-05-22T23:09:00.443 回答