【问题标题】:grails and spring security acl: show only some instances of a domain classgrails 和 spring security acl:仅显示域类的一些实例
【发布时间】:2013-08-29 22:05:47
【问题描述】:

我在我的 Grails 项目中使用 Spring Security ACL 来管理对我的应用程序的访问。我可以创建管理员和用户以对应用程序具有不同的权限。 现在,我希望特定用户只能看到域类对象的某些实例。那就是:

按照示例域类对象

class Patient
{
   String name;
   String surname;
   ...
}

假设有 3 个创建的 Patient 对象。 我想要那个,如果我用

登录

用户名 = test1

密码=test1

我只能看到属于该用户的患者。 我认为,当我创建一个新患者时,需要存储该患者属于当前登录的用户。 我该怎么做?

编辑: 另一个问题是,如果我将 id 部分中的 URL 更改为显示,我可以看到所有创建的 Patient。我想要这样,如果我手动更改 URL,我会看到访问错误。有可能吗?

编辑 2: 如何获取当前登录用户的角色?我已经尝试使用以下代码 How to get current user role with spring security plugin? 但我无法执行 getAuthorities() 因为它告诉我它不存在

我在下面的讨论中解决了 EDIT2 grails exception: Tag [paginate] is missing required attribute [total] 我需要解决 EDIT1 谢谢

【问题讨论】:

    标签: grails spring-security acl


    【解决方案1】:

    如果我理解正确,您需要定义 belongsTo。这将在数​​据库中创建从患者到用户的映射。

    编辑:获取当前登录用户使用

    class SomeController {
      def authenticateService
    
      def list = { 
         def user = authenticateService.principal() 
         def username = user?.getUsername()
         .....
         .....
      } 
    }
    

    映射到控制器中的用户更改逻辑或使用events创建映射

    编辑:编辑创建动作:

    class PatientController {
       def authenticateService
       ...
       def create() { 
          def patientInstance = new Patient(params)
          patientInstance.user = authenticateService.principal()
       ... 
          [patientInstance: patientInstance] 
       }
       ...
    }
    

    【讨论】:

    • 我已将 belongsTo 添加到域类中,但是当我创建新的 Patient 时出现以下错误:Property [secUser]of class [Patient] cannot be null。这意味着我认为不会自动设置用户...所以我需要将保存方法更改为控制器...但是如何获取当前记录的用户 ID?
    • 如果我想改变控制器的逻辑,我该怎么做?我有以下内容: def create() { [patientInstance: new Patient(params)] } 问题是,当我想查看患者列表时,我只想让属于当前用户的患者登录,所以我不仅需要更改创建方法,还需要将其他方法更改到控制器上
    • 在您的列表方法中,您获得了用户的用户名,但我需要将用户的 ID 存储到我的 Patient 对象中......那么我怎样才能获得 ID?
    • 由于前一行代码中的定义,我无法在 create() 中执行 patientInstance.user = authenticateService.principal()。然后,authenticateService 为空。它需要哪个作业?
    猜你喜欢
    • 2013-07-04
    • 2014-11-21
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2015-11-12
    • 2010-12-27
    • 2012-11-07
    相关资源
    最近更新 更多