0

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

遵循示例域类对象

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

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

用户名 = test1

密码=test1

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

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

编辑 2:如何获取当前登录用户的角色?我已经尝试使用以下代码如何使用 spring 安全插件获取当前用户角色?但我无法执行 getAuthorities() 因为它告诉我它不存在

我已经在以下讨论grails 异常中解决了 EDIT2:标签 [paginate] is missing required attribute [total] 我需要解决 EDIT1 谢谢

4

1 回答 1

0

If I understand you right you need to define belongsTo. This will create mapping in database from Patient to User.

Edit: to get current logged in user use

class SomeController {
  def authenticateService

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

To map to user change logic in controller or use events to create mapping

Edit: edit create action:

class PatientController {
   def authenticateService
   ...
   def create() { 
      def patientInstance = new Patient(params)
      patientInstance.user = authenticateService.principal()
   ... 
      [patientInstance: patientInstance] 
   }
   ...
}
于 2013-08-27T08:38:25.157 回答