【问题标题】:DomainClass.where queries, authorization, and filteringDomainClass.where 查询、授权和过滤
【发布时间】:2013-05-12 08:31:51
【问题描述】:

我正在研究使用 Grails 并使用 GORM 访问 MongoDB。

假设我有一个名为 BlogPost 的域类,但特定用户只能查看某些博客帖子。

现在,在控制器操作中,我可以轻松地执行 BlogPost.where {} 之类的操作,但这会返回所有博客文章。

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

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

【问题讨论】:

    标签: mongodb grails grails-orm


    【解决方案1】:

    您可以在域类中使用 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)               
    
    }
    ...
    

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

    【讨论】:

      猜你喜欢
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 2014-06-25
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多