public RoleResult Save(UserRole userRole, BORoleRightXCollection roleRights) { try { if (userRole == null) { return RoleResult.Failed; } else { if (userRole.RoleId == SQLPersistent.NULL_INT64) { // Create Role if (UserRole.GetByName(userRole.RoleName) != null) return RoleResult.NameAlreadyExists; BORole role = new BORole(); role.Name = userRole.RoleName; role.RoleType = new BORoleType(userRole.RoleTypeId); role.Description = userRole.Description; role.Save(); UserRole newUserRole = UserRole.GetByName(userRole.RoleName); if (newUserRole == null) { return RoleResult.Failed; } userRole = newUserRole; } UserRole.UserRoleResult userRoleResult = userRole.Save(roleRights); switch (userRoleResult) { case UserRole.UserRoleResult.Success: return RoleResult.Success; case UserRole.UserRoleResult.NameAlreadyExists: return RoleResult.NameAlreadyExists; case UserRole.UserRoleResult.FailedToUpdateRights: return RoleResult.FailedToUpdateRights; default: return RoleResult.Failed; } } } catch (Exception ex) { logger.ErrorException("Failed to update Role.", ex); return RoleResult.Failed; } }
public void MergeRoleRights(BORoleRightXCollection roleRights) { //Save Existing. foreach (BORoleRightX newRoleRight in roleRights) { bool saved = false; foreach (BORoleRightX roleRight in RoleRights) { if (roleRight.Right.ID == newRoleRight.Right.ID && !saved) { roleRight.CanCreate = newRoleRight.CanCreate; roleRight.CanDelete = newRoleRight.CanDelete; roleRight.CanExecute = newRoleRight.CanExecute; roleRight.CanRead = newRoleRight.CanRead; roleRight.CanUpdate = newRoleRight.CanUpdate; roleRight.Save(); saved = true; break; } } if (!saved) { BORoleRightX roleRight = new BORoleRightX(); roleRight.Role = newRoleRight.Role; roleRight.Right = newRoleRight.Right; roleRight.CanCreate = newRoleRight.CanCreate; roleRight.CanDelete = newRoleRight.CanDelete; roleRight.CanExecute = newRoleRight.CanExecute; roleRight.CanRead = newRoleRight.CanRead; roleRight.CanUpdate = newRoleRight.CanUpdate; roleRight.Save(); } } //Remove not existing foreach (BORoleRightX roleRight in RoleRights) { bool exists = false; foreach (BORoleRightX newRoleRight in roleRights) { if (roleRight.Right.ID == newRoleRight.Right.ID) { exists = true; break; } } if (!exists) roleRight.Delete(); } }
public UserRoleResult Save(BORoleRightXCollection roleRights) { //Does this role already Exist UserRole roleByName = GetByName(this.RoleName); //Does the role match the one that is being modified if (roleByName != null && roleByName.RoleId != SQLPersistent.NULL_INT64 && roleByName.RoleId != this.RoleId) { //Role exists, do not modify. It is a different role. return UserRoleResult.NameAlreadyExists; } else if (roleByName == null || roleByName != null && roleByName.RoleId != SQLPersistent.NULL_INT64 && roleByName.RoleId == this.RoleId) { //Same Role, update it if (!BORoleRightX.UpdateRole(this.RoleId, this.RoleName, this.Description)) return UserRoleResult.Fail; //Update Rights for the role try { if (roleRights != null) this.MergeRoleRights(roleRights); return UserRoleResult.Success; } catch (Exception ex) { logger.ErrorException("Failed to update Role Rights", ex); return UserRoleResult.FailedToUpdateRights; } } else return UserRoleResult.Fail; }
public static BORoleRightXCollection GetByRole(string roleId) { BORoleRightXCollection collection = new BORoleRightXCollection(); SqlConnection con = new SqlConnection(BOBase.GetConnectionString()); con.Open(); try { SqlCommand cmd = new SqlCommand("P_RoleRightX_GetByRole", con); cmd.CommandType = CommandType.StoredProcedure; SQLPersistent.SetVarCharParameter(cmd.Parameters, "@RORI_ROLGUID", 50, roleId, false); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SingleResult); try { while (rdr.Read()) { collection.Add(new BORoleRightX(rdr)); } } catch (Exception ex) { logger.ErrorException("Failed to fetch record", ex); return null; } finally { rdr.Close(); } } finally { con.Close(); } return collection; }