【问题标题】:How to assign role to a new user in bootstrap with grails spring security?如何使用 grails spring security 在引导程序中为新用户分配角色?
【发布时间】:2011-09-28 02:52:24
【问题描述】:

在我的 grails 应用程序中,我使用的是 Spring Security 插件。我有注册表单,注册用户将存储在 db 表中。

在引导程序中,我创建了新角色, def PhysicianRole = new Role(authority: 'ROLE_PHYSICIAN').save(flush: true)

然后我有另一种形式,管理员在表格中启用或禁用用户。

现在我想为所有启用的用户分配医师角色。所以我在引导程序中做了如下操作, def医师=Physician.findAllWhere(enabled:true) 有了这个我得到数组列表,

医师-------------->[com.HospitalManagement.User : 1, com.HospitalManagement.User : 2, com.HospitalManagement.User : 4, com.HospitalManagement.User : 5、com.HospitalManagement.User:6、com.HospitalManagement.User:7]

现在我应该如何为这个数组列表中的所有对象分配医师角色?

【问题讨论】:

  • 请仔细阅读提问时应遵循的语法以提高可读性。

标签: grails spring-security role


【解决方案1】:
def physicianRole = Role.findByAuthority('ROLE_PHYSICIAN')

for (physician in Physician.findAllWhere(enabled:true)) {
   if (!PhysicianRole.findByPhysicianAndRole(physician, physicianRole)) {
      PhysicianRole.create physician, physicianRole 
   }
}

【讨论】:

  • 这样我得到了错误 ERROR context.GrailsContextLoader - 执行引导时出错:无法在空对象上调用方法 findByPhysicianAndRole()
  • 您缺少对 PhysicianRole 类的导入。
猜你喜欢
  • 2011-11-27
  • 1970-01-01
  • 2017-09-08
  • 1970-01-01
  • 2016-02-27
  • 2012-11-28
  • 2021-12-19
  • 2016-10-03
  • 2013-12-08
相关资源
最近更新 更多