0

的Grails:3.3.0 春季安全:3.2.0.M1grails 3;使用Spring Security看到自己的数据

我已经做了这方面的一些研究,我发现,从(Seeing only your own data in Grails)的职位,答案可能是答案我正在寻找,但不知何故,它不起作用。

这就是我如何捕获登录用户并尝试过滤掉并让登录用户查看自己的数据。 (这是我的任务控制器) 通过什么用的[任务:任务]的方式

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

请给我一些建议或请让我知道我在哪里 犯错! 在此先感谢! 最好的问候,熙

回答

0

我已经在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

然后,我只是从我的领域类$叫出{}任务

0

我不认为你的要求是不相关的春安。

关于“顺便说明什么是[任务:任务]的用途” - 它看起来像你有两个返回点在代码中,所以你需要修复它 - 在常规你可以省略“返回”if你是在上线 - 所以我想这条线是模型的回归,包括任务列表 - 但是代码后继续吧...

  1. 如果任何任务属于用户,那么你shuld使用:

    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 
    

然后返回th Ë数据+像任何其他数据统计等

  • 可以考虑不使用多属于关联使其无需模型过于复杂:

    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. 
    
  • BTW保持服务领域模型是不是一个好的做法 - 用ser副通过在控制器/服务注入它

    transient springSecurityService 
    
  • +0

    感谢您的答复!其实我的问题只是我没有在我的GSP上提出。 非常感谢您的建议 – Trainee

    相关问题