/// <summary> /// role不能为null,且role.Id必须大于0。 /// 若计算得出role.Id没有对应的数据, 抛异常 /// </summary> public int UpdateRole(Role role, List<long> powerIds) { CheckRoleNotNull(role); CheckRoleIdGreatThan0(role.Id); Role r = GetRole(new Role() { Id = role.Id, }); CheckRoleNotNull(r); foreach (var field in typeof(Role).GetProperties()) // 修改角色信息 { field.SetValue(r, field.GetValue(role)); } dbContext.Entry(r).State = EntityState.Modified; var oldrps = from RolePower in dbContext.RolePowers // 删除旧权限 where RolePower.RoleId == role.Id select RolePower; foreach (RolePower rp in oldrps) { dbContext.Entry(rp).State = EntityState.Deleted; } foreach (long powerId in powerIds) // 新增权限 { RolePower rp = new RolePower() { // Id RoleId = role.Id, PowerId = powerId, }; dbContext.RolePowers.Add(rp); dbContext.Entry(rp).State = EntityState.Added; } return dbContext.SaveChanges(); }
/// <summary> /// role不能为null, 且role.Id必须大于0 /// powerIds:给角色分配的权限(若不是全部存在, 抛异常) /// </summary> public void UpdateRole(Role role, List<long> powerIds) { CheckRoleNotNull(role); CheckRoleIdGreatThan0(role.Id); // CheckPowerIdsAllExist(powerIds); dal.UpdateRole(role, powerIds); }
/// <summary> /// role不能为null。优先匹配Id, 再匹配Name /// </summary> public Role GetRole(Role role) { CheckRoleNotNull(role); var r = from Role in dbContext.Roles where Role.Id == role.Id || Role.Name == role.Name select Role; return r.FirstOrDefault(); }
/// <summary> /// 新增角色时,分配权限(权限若不是全存在, 抛异常) /// </summary> public void AddRole(Role role, List<long> powerIds) { CheckRoleNotNull(role); if (role.Id != default(long)) { throw new Exception("不能为role赋值"); } CheckPowerIdsAllExist(powerIds); dal.AddRole(role, powerIds); }
/// <summary> /// role不能为Null, 只需对Id值赋值即可 /// </summary> public List<RolePower> GetRolePowersByRole(Role role) { if (role == null || role.Id <= default(long)) { throw new Exception("参数power非法"); } var roles = from RolePower in dbContext.RolePowers where RolePower.RoleId == role.Id select RolePower; return roles.ToList(); }
/// <summary> /// 新增角色时,分配权限 /// </summary> public int AddRole(Role role, List<long> powerIds) { CheckRoleNotNull(role); dbContext.Roles.Add(role); dbContext.Entry(role).State = EntityState.Added; // 新增角色 foreach (long powerId in powerIds) { RolePower rp = new RolePower() { // Id RoleId = role.Id, PowerId = powerId, }; dbContext.RolePowers.Add(rp); dbContext.Entry(rp).State = EntityState.Added; // 分配权限 } return dbContext.SaveChanges(); }
/// <summary> /// roleId值必须大于0 /// </summary> public bool DeleteRole(long roleId) { CheckRoleIdGreatThan0(roleId); Role role = new Role() { Id = roleId, }; int row = dal.DeleteRole(role); if (row != 1) { AddLog(role, "删除角色发生错误"); } return row == 1; }
private void AddLog(Role role, string description) { ILog logger = LogManager.GetLogger(typeof(RoleBLL)); logger.Error(role, new Exception(string.Format("\r\n\r\n{0}", description))); }
private void CheckRoleNotNull(Role role) { if (role == null) { throw new ArgumentNullException("参数role不能为null"); } }
/// <summary> /// role不能为null, 且role.Id必须大于0 /// </summary> public int DeleteRole(Role role) { CheckRoleNotNull(role); CheckRoleIdGreatThan0(role.Id); /*List<RolePower> rps = rolePowerDAL.GetRolePowersByRole(role); foreach (RolePower rp in rps) { dbContext.Entry(rp).State = EntityState.Deleted; }*/ dbContext.Entry(role).State = EntityState.Deleted; return dbContext.SaveChanges(); }