/// <summary> /// Deletes the given role from the system. /// </summary> /// <param name="roleID">ID of role to delete</param> /// <returns>true if succeeded, false otherwise</returns> public static bool DeleteRole(int roleID) { RoleEntity toDelete = SecurityGuiHelper.GetRole(roleID); if (toDelete == null) { // not found return(false); } Transaction trans = new Transaction(IsolationLevel.ReadCommitted, "DeleteRole"); try { // remove the role - forum - action right entities ForumRoleForumActionRightCollection forumRoleActionRights = new ForumRoleForumActionRightCollection(); trans.Add(forumRoleActionRights); forumRoleActionRights.DeleteMulti(ForumRoleForumActionRightFields.RoleID == roleID); // Remove role-audit action entities RoleAuditActionCollection roleAuditActions = new RoleAuditActionCollection(); trans.Add(roleAuditActions); roleAuditActions.DeleteMulti(RoleAuditActionFields.RoleID == roleID); // remove Role - systemright entities RoleSystemActionRightCollection roleSystemRights = new RoleSystemActionRightCollection(); trans.Add(roleSystemRights); roleSystemRights.DeleteMulti(RoleSystemActionRightFields.RoleID == roleID); // remove Role - user entities RoleUserCollection roleUsers = new RoleUserCollection(); trans.Add(roleUsers); roleUsers.DeleteMulti(RoleUserFields.RoleID == roleID); // delete the actual role trans.Add(toDelete); toDelete.Delete(); trans.Commit(); return(true); } catch { // error occured, rollback trans.Rollback(); throw; } finally { trans.Dispose(); } }
/// <summary> /// Modifies the given role: it resets the system action rights for the given role to the given set of action rights and it modifies /// the role description for the given role. If the user specified a role description that is already available, false will be returned to signal /// that the save failed. /// </summary> /// <param name="actionRightIDs">The action rights.</param> /// <param name="roleID">The role ID.</param> /// <param name="roleDescription">The role description.</param> /// <returns>true if succeeded, false otherwise</returns> public static bool ModifyRole(List<int> actionRightIDs, int roleID, string roleDescription) { // read the existing role entity from the database. RoleEntity roleToModify = new RoleEntity(roleID); if(roleToModify.IsNew) { // not found return false; } // check if the description is different. If so, we've to check if the new roledescription is already present. If so, we'll // abort the save if(roleToModify.RoleDescription != roleDescription) { if(CheckIfRoleDescriptionIsPresent(roleDescription)) { // new description, is already present, fail return false; } } // all set. We're going to delete all Role - SystemAction Rights combinations first, as we're going to re-insert them later on. // We'll use a transaction to be able to roll back all our changes if something fails. Transaction trans = new Transaction(IsolationLevel.ReadCommitted, "ModifyRole"); try { RoleSystemActionRightCollection roleActionRights = new RoleSystemActionRightCollection(); // add this collection to the transaction so all actions executed through this collection will be inside the transaction trans.Add(roleActionRights); // delete all role-systemactionright combinations directly from the database, by issuing a direct delete on the database, using a filter // on roleid roleActionRights.DeleteMulti(RoleSystemActionRightFields.RoleID == roleID); // add new role-systemactionright entities which we'll save to the database after that foreach(int actionRightID in actionRightIDs) { RoleSystemActionRightEntity toAdd = new RoleSystemActionRightEntity(); toAdd.ActionRightID = actionRightID; toAdd.RoleID = roleID; roleActionRights.Add(toAdd); } // save the new entities to the database roleActionRights.SaveMulti(); // we'll now save the role and the role description, if it's changed. Otherwise the save action will be a no-op. // add it to the transaction trans.Add(roleToModify); roleToModify.RoleDescription = roleDescription; roleToModify.Save(); // all done, commit the transaction trans.Commit(); return true; } catch { // failed, roll back transaction. trans.Rollback(); throw; } finally { trans.Dispose(); } }
/// <summary> /// Modifies the given role: it resets the system action rights for the given role to the given set of action rights and it modifies /// the role description for the given role. If the user specified a role description that is already available, false will be returned to signal /// that the save failed. /// </summary> /// <param name="actionRightIDs">The action rights.</param> /// <param name="roleID">The role ID.</param> /// <param name="roleDescription">The role description.</param> /// <returns>true if succeeded, false otherwise</returns> public static bool ModifyRole(List <int> actionRightIDs, int roleID, string roleDescription) { // read the existing role entity from the database. RoleEntity roleToModify = new RoleEntity(roleID); if (roleToModify.IsNew) { // not found return(false); } // check if the description is different. If so, we've to check if the new roledescription is already present. If so, we'll // abort the save if (roleToModify.RoleDescription != roleDescription) { if (CheckIfRoleDescriptionIsPresent(roleDescription)) { // new description, is already present, fail return(false); } } // all set. We're going to delete all Role - SystemAction Rights combinations first, as we're going to re-insert them later on. // We'll use a transaction to be able to roll back all our changes if something fails. Transaction trans = new Transaction(IsolationLevel.ReadCommitted, "ModifyRole"); try { RoleSystemActionRightCollection roleActionRights = new RoleSystemActionRightCollection(); // add this collection to the transaction so all actions executed through this collection will be inside the transaction trans.Add(roleActionRights); // delete all role-systemactionright combinations directly from the database, by issuing a direct delete on the database, using a filter // on roleid roleActionRights.DeleteMulti(RoleSystemActionRightFields.RoleID == roleID); // add new role-systemactionright entities which we'll save to the database after that foreach (int actionRightID in actionRightIDs) { RoleSystemActionRightEntity toAdd = new RoleSystemActionRightEntity(); toAdd.ActionRightID = actionRightID; toAdd.RoleID = roleID; roleActionRights.Add(toAdd); } // save the new entities to the database roleActionRights.SaveMulti(); // we'll now save the role and the role description, if it's changed. Otherwise the save action will be a no-op. // add it to the transaction trans.Add(roleToModify); roleToModify.RoleDescription = roleDescription; roleToModify.Save(); // all done, commit the transaction trans.Commit(); return(true); } catch { // failed, roll back transaction. trans.Rollback(); throw; } finally { trans.Dispose(); } }
/// <summary> /// Deletes the given role from the system. /// </summary> /// <param name="roleID">ID of role to delete</param> /// <returns>true if succeeded, false otherwise</returns> public static bool DeleteRole(int roleID) { RoleEntity toDelete = SecurityGuiHelper.GetRole(roleID); if(toDelete == null) { // not found return false; } Transaction trans = new Transaction(IsolationLevel.ReadCommitted, "DeleteRole"); try { // remove the role - forum - action right entities ForumRoleForumActionRightCollection forumRoleActionRights = new ForumRoleForumActionRightCollection(); trans.Add(forumRoleActionRights); forumRoleActionRights.DeleteMulti(ForumRoleForumActionRightFields.RoleID == roleID); // Remove role-audit action entities RoleAuditActionCollection roleAuditActions = new RoleAuditActionCollection(); trans.Add(roleAuditActions); roleAuditActions.DeleteMulti(RoleAuditActionFields.RoleID == roleID); // remove Role - systemright entities RoleSystemActionRightCollection roleSystemRights = new RoleSystemActionRightCollection(); trans.Add(roleSystemRights); roleSystemRights.DeleteMulti(RoleSystemActionRightFields.RoleID == roleID); // remove Role - user entities RoleUserCollection roleUsers = new RoleUserCollection(); trans.Add(roleUsers); roleUsers.DeleteMulti(RoleUserFields.RoleID == roleID); // delete the actual role trans.Add(toDelete); toDelete.Delete(); trans.Commit(); return true; } catch { // error occured, rollback trans.Rollback(); throw; } finally { trans.Dispose(); } }