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 Securityの実装を理解していないためです. 特定のロールを持つユーザーのリストを取得するには:

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

あなたの問題はおそらく、 User.findAllByRole() がRoleではなく を引数として期待していることStringです。

http://grails.org/doc/2.2.x/guide/single.html#findersの「Querying Associations」サブセクションに関連する例があります(以下を参照)。

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

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