public void AssignRole(RoleUser roleUser, Role role) { roleUser.Roles.Add(role); }
public void AssignPrivilegeToUser(RoleUser roleUser, Privilege privilege) { roleUser.Privileges.Add(privilege); }
private void InitUnitPermissions(Unit unit, User admin) { var privilegeRepository = new PrivilegeRepository(); var privileges = privilegeRepository.GetAllPrivileges(); var roleRepository = new RoleRepository(); var roles = roleRepository.GetAllRoleByUnitId(unit.Id); var role = roles.FirstOrDefault(r => r.Code == "SuperAdmin"); if (role == null) { role = new Role(); role.Name = "超级管理员"; role.Code = "SuperAdmin"; role.UnitId = unit.Id; privileges.ToList().ForEach(privilege => role.Privileges.Add(privilege)); roleRepository.Save(role); } var roleUserRepository = new RoleUserRepository(); var roleUser = roleUserRepository.GetRoleUserByUserId(admin.Id); if (roleUser == null) { roleUser = new RoleUser(); roleUser.UserId = admin.Id; roleUser.Roles.Add(role); roleUserRepository.Save(roleUser); } }