如何在 Grails 中过滤列表以仅显示“属于”的内容?特定用户?
我想答案是非常基本的,但我对 Grails 还很陌生,并且很难在其他地方找到我的问题的答案。
基本上,我有两个域类:用户和事件。事件与用户具有“belongsTo”关系,并且用户与事件具有“hasMany”关系。该应用程序要求用户登录。登录后,他们可以查看已创建事件的列表,但该列表包含所有已创建事件;即使是其他用户创建的。如何过滤列表以仅显示登录用户创建的内容?我不希望一个用户能够访问和更改另一用户创建的事件。
I'd imagine the answer is pretty basic, but I'm quite new to Grails and struggling to find the answer to my question anywhere else.
Basically, I have two domain classes, Users and Events. The events has a 'belongsTo' relationship with the User and the User has a 'hasMany' relationship with the Events. The application requires a user to log in. Once logged in they can view a list of created events, however the list contains all of the created events; even those created by other users. How can I filter the list to only show what the logged in user has created? I don't want one user to be able to access and alter events created by another.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
伊森的答案是正确的,而且很容易做到。但是,如果您有很多不同的地方需要过滤,那么您就有可能忘记进行过滤。
您可以使用 hibernate 过滤器插件自动过滤所有数据。请参阅 http://trac.serveall.net/web/ibidem 了解我过滤数据的示例基于当前用户的公司。
Ethan's answer is correct, and easy to do. But if you have a lot of different places you need then filtering, then you're a bit a risk of forgetting to do the filter.
You can use the hibernate filter plugin to automatically filter all the data. See http://trac.serveall.net/web/ibidem for an example where I filter data based on the current user's company.
如果您的控制器具有登录用户的 ID,您应该能够执行
此操作并将其传递到您的视图:
redirect(controller: 'event', action: 'list', userEvents:events)
If your controller has the logged in user's id, you should be able to do
and pass that on to your view:
redirect(controller: 'event', action: 'list', userEvents:events)