【问题标题】:grails 3; Seeing your own data with Spring Security圣杯3;使用 Spring Security 查看自己的数据
【发布时间】:2018-02-13 08:53:12
【问题描述】:

Grails:3.3.0 春季安全:3.2.0.M1

我对此进行了一些研究,我发现来自 (Seeing only your own data in Grails) 帖子的答案可能是我正在寻找的答案,但不知何故它不起作用。

这就是我捕获登录用户并尝试过滤掉并让登录用户查看他自己的数据的方式。 (这是我的任务控制器) 顺便问一下[tasks:tasks]有什么用

def index(Integer max) {

    def authenticated = getAuthenticatedUser().username
    def tasks = User.findAllByUsername(authenticated)
    [tasks: tasks]
    params.max = Math.min(max ?: 10, 100)
    respond Task.list(params), model:[tasks: Task.count()]
}

这是我的任务域

class Task {

    transient springSecurityService
    
    String task
    Project project
    Pic picName
   
    static hasMany = [subTask:Subtask]
    static belongsTo =[Project,Pic,User]
    }
    

请给我一些建议或让我知道我在哪里犯了错误! 提前致谢! 最好的问候,嘻嘻

【问题讨论】:

    标签: spring grails grails-domain-class grails-controller grails3


    【解决方案1】:

    我认为您的要求与 Spring Security 无关。

    关于“顺便说一下 [tasks:tasks] 的用途是什么”——看起来你在代码中有两个返回点,所以你需要修复它——在 groovy 中,如果你可以省略“返回”在最后一行 - 所以我假设这一行是包含任务列表的模型的返回 - 但代码在它之后继续......

    1. 如果任何任务属于用户,那么您应该使用:

      User user = getAuthenticatedUser() // method for getting the curren user
      params.max = Math.min(max ?: 10, 100) // any query params you want to add
      def tasks = Task.findAllByUser(user, params) //get the user Tasks using the query params
      

    然后返回数据 + 任何其他数据,如计数等。

    1. 你可以考虑不要使用多个belongsTo,它会让你的模型过于复杂而不需要:

      static belongsTo =[Project,Pic,User]
      

      如果任务属于用户,您可以保留每个任务的用户 ID 或用户名,然后通过此属性查询 - 例如:

      class Task {
      
      transient springSecurityService 
      
      String username  // not unique
      String task
      Project project
      Pic picName
      
      static hasMany = [subTask:Subtask]
      static belongsTo =[Project,Pic]
      }
      
      def username = getAuthenticatedUser().username // method for getting the current username.
      params.max = Math.min(max ?: 10, 100) // any query params you want to add.
      def tasks = Task.findAllByUsername(username, params) get the user Tasks using the query params.
      
    2. 顺便说一句,将服务保留在域模型中并不是一个好习惯 - 通过将服务注入到您的控制器/服务中来使用该服务

      transient springSecurityService
      

    【讨论】:

    • 感谢您的回复!其实我的问题只是我没有在我的 GSP 上说出来。非常感谢您的建议
    【解决方案2】:

    我通过在 gsp 调用“任务”来完成。它对我有用

     def     authenticated = getAuthenticatedUser().username
            
            def     tasks = Task.findAllByLogginUser(authenticated)
            
            params.max = Math.min(max ?: 10, 100)
            respond Task.list(params), model:[tasks:tasks] // [tasks:tasks] is to passing tasks into my domain

    然后我就从我的域类 ${tasks} 中调用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-13
      相关资源
      最近更新 更多