代码之家  ›  专栏  ›  技术社区  ›  Trainee

圣杯3;使用Spring Security查看您自己的数据

  •  0
  • Trainee  · 技术社区  · 7 年前

    Grails:3.3.0 春季安全:3.2.0。M1

    我对此做了一些研究,并从中找到了答案( Seeing only your own data in Grails )post可能是我正在寻找的答案,但不知怎么的,它不起作用。

    这就是我如何捕获登录用户并尝试过滤掉,让登录用户查看自己的数据。 [任务:任务]

    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]
        }
        

    请给我一些建议,或者让我知道我在哪里犯了错误! 提前感谢!

    2 回复  |  直到 7 年前
        1
  •  0
  •   Meni Lubetkin    7 年前

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

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

    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. 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
      
        2
  •  0
  •   Trainee    7 年前

    我通过在普惠制上喊出“任务”来做到这一点。它对我有用

     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