如何使用grails spring security为bootstrap中的新用户分配角色?

时间:2011-07-04 12:18:45

标签: grails spring-security role

在我的grails应用程序中,我正在使用spring security plugin ..我有注册表单,注册用户将存储在db表中。

在bootstrap中我创建了新角色,   def PhysicianRole =新角色(权限:'ROLE_PHYSICIAN')。save(flush:true)

然后我有另一个表单,管理员正在启用或禁用表中的用户。

现在我想将医生角色分配给所有已启用的用户。所以我在bootstrap中执行了以下操作,   def physicians = 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]

现在我应该如何将医生角色分配给此数组列表中的所有对象?

1 个答案:

答案 0 :(得分:1)

def physicianRole = Role.findByAuthority('ROLE_PHYSICIAN')

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