0

I'm looking into using Grails and accessing MongoDB using GORM.

Say I have a domain class named BlogPost, but a specific user is only allowed to see certain blogs posts.

Now, In a controller action I can easily do something like BlogPost.where {}, but that will return all blog posts.

我需要一种集中机制来过滤控制器执行查询时返回的域对象。

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

4

1 回答 1

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 活动

或者您可以在服务类或域类中定义方法来应用过滤器。例如,

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

}
...

其实你的问题涉及的范围很大。如果问题更具体一点,您可以获得更好的建议。

于 2013-05-17T09:58:19.123 回答