2013-05-17 44 views
0

我正在研究使用Grails並使用GORM訪問MongoDB。DomainClass.where查詢,授權和過濾

說我有一個名爲BlogPost的域名類,但是一個特定的用戶只能看到某些博客文章。

現在,在一個控制器動作中,我可以輕鬆地做一些類似BlogPost.where的事情,但是這將返回所有博客文章。

我需要一個集中式機制來過濾控制器執行查詢時返回哪些域對象。

使用的插件是http://grails.org/plugin/mongodb

回答

0

您可以使用GORM事件「的onLoad」在域類和有應用濾鏡。例如:

Class BlogPost{ 
    String content  
    String viewableByRole //it might be another domain for roles/string/enum as per your requirement 
    .... 
    def onLoad() { 
     //apply your condition here 
} 
} 

在這裏看到:GORM Events

或者你也可以在服務類或域類中定義的方法來應用過濾器。例如,

... 
    def getAllPost() { 
     def currentRole = take_role_from_security_plugin_like_shiro_or_spring_security_session etc 
     return BlogPost.findAllByViewableByRole(currentRole)    

} 
... 

其實你的問題包含一個很大的範圍。如果問題更具體些,你可以得到更好的建議。