开发者

Creating a blocked user intercepter in Grails

I am working on a social site where users can block other users. Throughout the site --dozens of places, user information is displayed. For example, user comments, reply forms, online user list.. etc etc.. The problem is that given the high number of places user info is displayed, it's b开发者_如何转开发ecoming very difficult to check each time if that user is blocked. For example:

<g:each var="comment" in="${comments}">
  <g:if test="!${loggedInUser.blockedUsers.find { it == comment.user}">
     show comment
  </g:if>
</g:each>

Does Grails provide any functionality that would facilitate creating some kind of filter or intercepter where I could simply exclude blocked users when iterating lists, etc? If not, what would you suggest I do?


A good rule of thumb is to only load what you need, so your service method should filter out blocked users at the database level. Then your controllers will be simpler and views will only have to display what they are given.

But if you prefer interceptors, take a look here:

http://www.grails.org/Controllers+-+Interceptors


It could probably be done like this:

<g:each var="comment" in="${comments.findAll { !(it in loggedInUsers.blockedUsers) }}">
    show comment
</g:each>

But I think it would be more efficient to filter your comments in the controller instead. You could probably do a Grails Criteria on your comments.


Maybe the Hibernate Filter plugin can help you?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜