Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
Adding support for Changing Content Ownership via XML instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
IvoSalmre-Tableau committed Dec 3, 2020
1 parent fe3db9a commit 3e748b8
Show file tree
Hide file tree
Showing 25 changed files with 1,186 additions and 94 deletions.
2 changes: 1 addition & 1 deletion src/ProvisionSite/ProvisionFromDirectoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


/// <summary>
/// Allows us to sort all users being provisioned into role
///
/// </summary>
internal partial class ProvisionFromDirectoryManager
{
Expand Down
8 changes: 7 additions & 1 deletion src/ProvisionSite/ProvisionSite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private void ShowLogs()
/// <param name="statusLogs"></param>
public void Execute()
{
WorkingListSiteUsers workingList_allKnownUsers = null;
//==================================================================================
//Get the data we need to run
//==================================================================================
Expand Down Expand Up @@ -97,13 +98,18 @@ public void Execute()
//=================================================================================
//Provision the users
//=================================================================================
var workingList_allKnownUsers = Execute_ProvisionUsers(siteSignIn);
workingList_allKnownUsers = Execute_ProvisionUsers(siteSignIn);
ShowLogs();

//=================================================================================
//Provision the groups
//=================================================================================
Execute_ProvisionGroups(siteSignIn, workingList_allKnownUsers);

//=================================================================================
//Content ownership changes
//=================================================================================
Execute_ProvisionContentOwnershipChanges(siteSignIn, workingList_allKnownUsers);
}


Expand Down
189 changes: 189 additions & 0 deletions src/ProvisionSite/ProvisionSite_contentOwnership.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

/// <summary>
/// Provision a site
/// </summary>
internal partial class ProvisionSite
{

/// <summary>
/// Follows the instructions for any content ownership changes we want to make
/// </summary>
/// <param name="siteSignIn"></param>
/// <param name="workingList_allKnownUsers"></param>
private void Execute_ProvisionContentOwnershipChanges(TableauServerSignIn siteSignIn, WorkingListSiteUsers workingList_allKnownUsers)
{

//===============================================================================================
//If there are no content ownership changes to perform, then just exit this step
//===============================================================================================
if ((_provisionInstructions.ContentOwnershipToProvision == null) ||
(_provisionInstructions.ContentOwnershipToProvision.Count == 0))
{
_statusLogs.AddStatus("Skipping content ownership mapping, because there are no instructions for this...");
return;
}


_statusLogs.AddStatusHeader("Provision content ownership changes");

//=================================================================================
//If we do not have it already, load the set of users for the site...we will need this to look up users
//=================================================================================
if (workingList_allKnownUsers == null)
{
var existingUsers = DownloadUsersList.CreateAndExecute(siteSignIn);
existingUsers.ExecuteRequest();

workingList_allKnownUsers = new WorkingListSiteUsers(existingUsers.Users);
}

//=================================================================================
//Unlike Workbooks, there is not a simple URL to download the list of Datasources
//for a single user. (There is a filter parameter that can be added to the REST
//API that queries data sources, but that takes a "user name" not a User ID).
//Here we can query for ALL the data sources pass that to all teh remappign request
//=================================================================================
var downloadDataSources = new DownloadDatasourcesList(siteSignIn);
downloadDataSources.ExecuteRequest();
var allSiteDataSources = downloadDataSources.Datasources;

//=================================================================================
//Get the list of flows in the iste
//=================================================================================
var downloadFlows = new DownloadFlowsList(siteSignIn);
downloadFlows.ExecuteRequest();
var allSiteFlows = downloadFlows.Flows;

//Go through each of the ownership change instructions...
foreach (var thisOwnershipChange in _provisionInstructions.ContentOwnershipToProvision)
{
Execute_ProvisionOwnership_SingleUserChange(
siteSignIn,
thisOwnershipChange, workingList_allKnownUsers,
allSiteDataSources,
allSiteFlows);
}

}


/// <summary>
/// Change the ownership from 1 user, to another user
/// </summary>
/// <param name="siteSignIn"></param>
/// <param name="thisOwnershipChange"></param>
/// <param name="workingList_allKnownUsers"></param>
private void Execute_ProvisionOwnership_SingleUserChange(
TableauServerSignIn siteSignIn,
ProvisioningChangeContentOwnership thisOwnershipChange,
WorkingListSiteUsers workingList_allKnownUsers,
ICollection<SiteDatasource> knownDataSources,
ICollection<SiteFlow> knownFlows)
{
try
{
Execute_ProvisionOwnership_SingleUserChange_inner(
siteSignIn,
thisOwnershipChange,
workingList_allKnownUsers,
knownDataSources,
knownFlows);
}
catch(Exception ex)
{
_statusLogs.AddError("Error attempting to change content ownership, from: "
+ thisOwnershipChange.OldOwnerName
+ ", to:" + thisOwnershipChange.NewOwnerName
+ ", error: " + ex.ToString());
}
}

/// <summary>
/// Process the ownership-mapping request for a single user
/// </summary>
/// <param name="siteSignIn"></param>
/// <param name="thisOwnershipChange"></param>
/// <param name="workingList_allKnownUsers"></param>
/// <param name="knownDataSources"></param>
private void Execute_ProvisionOwnership_SingleUserChange_inner(
TableauServerSignIn siteSignIn,
ProvisioningChangeContentOwnership thisOwnershipChange,
WorkingListSiteUsers workingList_allKnownUsers,
ICollection<SiteDatasource> knownDataSources,
ICollection<SiteFlow> knownFlows)
{
var userOldOwner = workingList_allKnownUsers.FindUserByName(thisOwnershipChange.OldOwnerName);
if(userOldOwner == null)
{
throw new Exception("201202-415: Could not find user: " + userOldOwner.Name);
}

var userNewOwner = workingList_allKnownUsers.FindUserByName(thisOwnershipChange.NewOwnerName);
if (userNewOwner == null)
{
throw new Exception("201202-416: Could not find user: " + userNewOwner.Name);
}


//------------------------------------------------------------------------------------
//Check the ownership of each of these types of content, and update from the
//old owner to the designated new owner
//------------------------------------------------------------------------------------
Execute_ProvisionOwnership_SingleUserChange_Workbooks(siteSignIn, userOldOwner, userNewOwner);
Execute_ProvisionOwnership_SingleUserChange_Datasources(siteSignIn, knownDataSources, userOldOwner, userNewOwner);
Execute_ProvisionOwnership_SingleUserChange_Flows(siteSignIn, userOldOwner, userNewOwner, knownFlows);
}



/// <summary>
/// Make a record of a user modification
/// </summary>
/// <param name="userName"></param>
/// <param name="userRole"></param>
/// <param name="userAuth"></param>
/// <param name="modification"></param>
/// <param name="notes"></param>
private void CSVRecord_ErrorUpdatingContentOwnership(string contentType, string contentName, string oldOwnerName, string newOwnerName, string errorNotes)
{
var colNames = new List<string>();
var colValues = new List<string>();

colNames.Add("area"); colValues.Add("error");
colNames.Add("content-name"); colValues.Add(contentName);
colNames.Add("user-name"); colValues.Add(newOwnerName);
colNames.Add("modification"); colValues.Add("ownership change");
colNames.Add("notes"); colValues.Add("Error updating ownership: " + errorNotes + ". Previous owner was: " + oldOwnerName);

_csvProvisionResults.AddKeyValuePairs(colNames.ToArray(), colValues.ToArray());
}


/// <summary>
/// Make a record of a user modification
/// </summary>
/// <param name="userName"></param>
/// <param name="userRole"></param>
/// <param name="userAuth"></param>
/// <param name="modification"></param>
/// <param name="notes"></param>
private void CSVRecord_ContentOwnershipModified(string contentType, string contentName, string oldOwnerName, string newOwnerName)
{
var colNames = new List<string>();
var colValues = new List <string>();

colNames.Add("area"); colValues.Add(contentType);
colNames.Add("content-name"); colValues.Add(contentName);
colNames.Add("user-name"); colValues.Add(newOwnerName);
colNames.Add("modification"); colValues.Add("ownership change");
colNames.Add("notes"); colValues.Add("previous owner was: " + oldOwnerName);

_csvProvisionResults.AddKeyValuePairs(colNames.ToArray(), colValues.ToArray());
}

}


85 changes: 85 additions & 0 deletions src/ProvisionSite/ProvisionSite_contentOwnership_datasources.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

/// <summary>
/// Provision a site
/// </summary>
internal partial class ProvisionSite
{

/// <summary>
/// For this type of content, change the ownership from one user to another
/// </summary>
/// <param name="siteSignIn"></param>
/// <param name="thisOwnershipChange"></param>
/// <param name="userOldOwner"></param>
/// <param name="userNewOwner"></param>
private void Execute_ProvisionOwnership_SingleUserChange_Datasources(TableauServerSignIn siteSignIn, ICollection<SiteDatasource> knownDataSources, SiteUser userOldOwner, SiteUser userNewOwner)
{
//If there are no known datasources in the site, there is nothing to do
if((knownDataSources == null) || (knownDataSources.Count == 0))
{
return;
}

//Change the ownership of each of these content items
foreach (var contentItem in knownDataSources)
{

if (string.Compare(contentItem.OwnerId, userOldOwner.Id, true) == 0)
{
Execute_ProvisionOwnership_SingleUserChange_SingleDatasource(siteSignIn, contentItem, userOldOwner, userNewOwner);
}
else
{
//The datasouce is not owned by the user, there is nothing to do...
}
}
}


/// <summary>
/// Change the ownership for a single Datasource
/// </summary>
/// <param name="contentItem"></param>
/// <param name="userOldOwner"></param>
/// <param name="userNewOwner"></param>
private void Execute_ProvisionOwnership_SingleUserChange_SingleDatasource(TableauServerSignIn siteSignIn, SiteDatasource contentItem, SiteUser userOldOwner, SiteUser userNewOwner)
{
try
{
Execute_ProvisionOwnership_SingleUserChange_SingleDatasource_inner(siteSignIn, contentItem, userOldOwner, userNewOwner);
}
catch (Exception ex)
{
_statusLogs.AddError("Error attempting to change content ownership, "
+ "datasource: " + contentItem.Name
+ "from: " + userOldOwner.Name
+ ", to:" + userNewOwner.Name
+ ", error: " + ex.ToString());

CSVRecord_ErrorUpdatingContentOwnership("datasource", contentItem.Name, userOldOwner.Name, userNewOwner.Name, ex.Message);
}
}

/// <summary>
/// Update the owner of a single datasource
/// </summary>
/// <param name="siteSignIn"></param>
/// <param name="contentItem"></param>
/// <param name="userOldOwner"></param>
/// <param name="userNewOwner"></param>
private void Execute_ProvisionOwnership_SingleUserChange_SingleDatasource_inner(TableauServerSignIn siteSignIn, SiteDatasource contentItem, SiteUser userOldOwner, SiteUser userNewOwner)
{

var updateContentOwner = new SendUpdateDatasourceOwner(siteSignIn, contentItem.Id, userNewOwner.Id);
updateContentOwner.ExecuteRequest();

CSVRecord_ContentOwnershipModified("datasource", contentItem.Name, userOldOwner.Name, userNewOwner.Name);

}

}


Loading

0 comments on commit 3e748b8

Please sign in to comment.