From fd8dd46cc7a6939c8d09a7c98372cff02c802b68 Mon Sep 17 00:00:00 2001 From: Joe Fields Date: Tue, 23 Jun 2020 18:18:47 -0600 Subject: [PATCH] add update user function (PATCH) --- .gitignore | 11 ++ BimProjectSetupCLI/Application.cs | 12 +- BimProjectSetupCommon/AppOptions.cs | 6 + .../Workflows/ProjectUser.cs | 104 ++++++++++++++++++ ForgeBimApi/ForgeBimApi.csproj | 1 + .../ForgeBimApiWrappers/BimProjectApi.cs | 31 ++++++ ForgeBimApi/ForgeBimApiWrappers/ForgeApi.cs | 1 + .../Serialization/ProjectUserPatchResponse.cs | 20 ++++ 8 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 ForgeBimApi/Serialization/ProjectUserPatchResponse.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a58c06f --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +################################################################################ +# This .gitignore file was automatically created by Microsoft(R) Visual Studio. +################################################################################ + +/.vs +/BimProjectSetupCLI/obj +/BimProjectSetupCommon/obj +/bin +/ForgeBimApi/obj +/packages +/BimProjectSetupCLI/BimProjectSetupCLI.csproj.user diff --git a/BimProjectSetupCLI/Application.cs b/BimProjectSetupCLI/Application.cs index 1ac5db4..7210010 100755 --- a/BimProjectSetupCLI/Application.cs +++ b/BimProjectSetupCLI/Application.cs @@ -78,12 +78,19 @@ public void Process() } if (options.ProjectUserFilePath != null) { - projectUserProcess.AddProjectUsersFromCsvProcess(); + if (options.UpdateProjectUsers) + { + projectUserProcess.UpdateProjectUsersFromCsvProcess(); + } + else + { + projectUserProcess.AddProjectUsersFromCsvProcess(); + } } } internal static void PrintHelp() { - Console.WriteLine("Usage: Autodesk.BimProjectSetup [-p] [-x] [-u] [-c] [-s] [-a] [-b] [-t] [-z] [-e] [-d] [-r] [-h] [--CF] [--EU]"); + Console.WriteLine("Usage: Autodesk.BimProjectSetup [-p] [-x] [-u] [-c] [-s] [-a] [-b] [-t] [-z] [-e] [-d] [-r] [-h] [--CF] [--EU] [--UP]"); Console.WriteLine(" -p Path to CSV input file for project creation"); Console.WriteLine(" -x Path to CSV input file for service activation"); Console.WriteLine(" -u Path to CSV input file with project user information"); @@ -100,6 +107,7 @@ internal static void PrintHelp() // Switches Console.WriteLine(" --CF Copy folders"); Console.WriteLine(" --EU Use the EU region account"); + Console.WriteLine(" --UP Update Project User Access, Companies, or Roles"); Console.WriteLine("At least one path to an input file must be provided with the -p or -x options"); } internal static void PrintHeader() diff --git a/BimProjectSetupCommon/AppOptions.cs b/BimProjectSetupCommon/AppOptions.cs index e18f9dc..149d449 100644 --- a/BimProjectSetupCommon/AppOptions.cs +++ b/BimProjectSetupCommon/AppOptions.cs @@ -72,6 +72,7 @@ public string FormatPattern public bool TrialRun { get; private set; } public string HqAdmin { get; private set; } public bool CopyFolders { get; private set; } + public bool UpdateProjectUsers { get; private set; } public string AccountRegion { get @@ -102,6 +103,7 @@ internal AppOptions() Encoding = GetEncoding("UTF-8"); TrialRun = false; CopyFolders = false; + UpdateProjectUsers = false; AccountRegion = "US"; } @@ -172,6 +174,10 @@ public static AppOptions Parse(string[] args) { options.CopyFolders = true; } + else if (arg.Equals("--UP", StringComparison.InvariantCultureIgnoreCase)) + { + options.UpdateProjectUsers = true; + } else if (arg.Equals("--AR", StringComparison.InvariantCultureIgnoreCase)) { options.AdminRole = args[++i]; diff --git a/BimProjectSetupCommon/Workflows/ProjectUser.cs b/BimProjectSetupCommon/Workflows/ProjectUser.cs index eab76ab..2f08bc0 100755 --- a/BimProjectSetupCommon/Workflows/ProjectUser.cs +++ b/BimProjectSetupCommon/Workflows/ProjectUser.cs @@ -98,6 +98,28 @@ public void AddProjectUsersProcess(List _projectUsers) } } + public void UpdateProjectUsersFromCsvProcess() + { + try + { + DataController._projcetUserTable = CsvReader.ReadDataFromCSV(DataController._projcetUserTable, DataController._options.ProjectUserFilePath); + + if (false == _options.TrialRun) + { + List _projectUsers = GetUsers(DataController._projcetUserTable); + PatchUser(_projectUsers); + } + else + { + Log.Info("-Trial run (-r option is true) - no further processing"); + } + } + catch (Exception ex) + { + HandleError(ex); + } + } + private void AddUsers(List _projectUsers) { if (_projectUsers == null || _projectUsers.Count < 1) @@ -142,6 +164,49 @@ private void AddUsers(List _projectUsers) } } } + + private void PatchUser(List _projectUsers) + { + if (_projectUsers == null || _projectUsers.Count < 1) + { + return; + } + + HqUser hqAdmin = new HqUser(); + if (!CheckAdminEmail(_options.HqAdmin, out hqAdmin)) + { + return; + } + + Log.Info($""); + Log.Info($"Start updating users in projects"); + var projectNames = _projectUsers.Select(u => u.project_name).Distinct(); + foreach (string name in projectNames) + { + Log.Info($"- update users to project {name}"); + var users = _projectUsers.Where(u => u.project_name.Equals(name)); + if (users == null || users.Any() == false) + { + Log.Warn($"- no valid users found for project {name} - skipping this project and continue with next"); + continue; + } + var project = DataController.AllProjects.FirstOrDefault(p => p.name != null && p.name.Equals(name)); + foreach (ProjectUser projectUser in users) + { + HqUser hqUser = new HqUser(); + if (CheckUserId(projectUser.email, out hqUser)) + { + Log.Info($"- updating user {projectUser.email}"); + IRestResponse response = _projectsApi.PatchUser(project.id, hqAdmin.uid, hqUser.id, projectUser); + ProjectUserPatchResponseHandler(response); + } + else + { + Log.Error($"User {projectUser.email} not in account. Add them to a project first."); + } + } + } + } private static void AddServices(ProjectUser user) { // normal user @@ -320,6 +385,19 @@ private bool CheckAdminEmail(string adminEmail, out HqUser HqAdmin) return true; } + private bool CheckUserId(string email, out HqUser HqUser) + { + HqUser = DataController.AccountUsers.FirstOrDefault(u => + u.email != null && u.email.Equals(email, StringComparison.InvariantCultureIgnoreCase)); + if (HqUser == null) + { + Log.Error($"Error initializing account user {email}"); + return false; + } + + return true; + } + #region Response Handler internal static void ProjectUsersResponseHandler(List responses) { @@ -357,6 +435,32 @@ internal static void ProjectUserResponseHandler(IRestResponse response) LogResponse(response); } } + internal static void ProjectUserPatchResponseHandler(IRestResponse response) + { + if (response.StatusCode == System.Net.HttpStatusCode.OK) + { + ProjectUserPatchResponse r = JsonConvert.DeserializeObject(response.Content); + if (r != null) + { + if (r.error == null) + { + Log.Info($"Successfully updated user: {r.email}"); + } + else + { + Log.Error($"Error when updating project user"); + foreach (var e in r.error) + { + Log.Error($"code:{e.code} message:{e.message}"); + } + } + } + } + else + { + LogResponse(response); + } + } internal static void HandleError(Exception e) { Log.Error(e.Message); diff --git a/ForgeBimApi/ForgeBimApi.csproj b/ForgeBimApi/ForgeBimApi.csproj index 01e4016..18a5274 100644 --- a/ForgeBimApi/ForgeBimApi.csproj +++ b/ForgeBimApi/ForgeBimApi.csproj @@ -107,6 +107,7 @@ + diff --git a/ForgeBimApi/ForgeBimApiWrappers/BimProjectApi.cs b/ForgeBimApi/ForgeBimApiWrappers/BimProjectApi.cs index fb40474..b07d70e 100755 --- a/ForgeBimApi/ForgeBimApiWrappers/BimProjectApi.cs +++ b/ForgeBimApi/ForgeBimApiWrappers/BimProjectApi.cs @@ -120,6 +120,37 @@ public IRestResponse PostUsersImport(string projectId, string userId, List /// Update projects properties and services assigned to the project diff --git a/ForgeBimApi/ForgeBimApiWrappers/ForgeApi.cs b/ForgeBimApi/ForgeBimApiWrappers/ForgeApi.cs index 1766e1a..da0abbc 100644 --- a/ForgeBimApi/ForgeBimApiWrappers/ForgeApi.cs +++ b/ForgeBimApi/ForgeBimApiWrappers/ForgeApi.cs @@ -113,6 +113,7 @@ private void setUrls(string accountRegion) Urls["projects_projectId"] = "hq/v1/" + regionBasedUrl + "accounts/{AccountId}/projects/{ProjectId}"; Urls["projects_projectId_users"] = "hq/v1/" + regionBasedUrl + "accounts/{AccountId}/projects/{ProjectId}/users"; Urls["projects_projectId_users_import"] = "hq/v2/" + regionBasedUrl + "accounts/{AccountId}/projects/{ProjectId}/users/import"; + Urls["projects_projectId_user_patch"] = "hq/v2/" + regionBasedUrl + "accounts/{AccountId}/projects/{ProjectId}/users/{UserId}"; Urls["projects_projectId_industryRoles"] = "hq/v2/" + regionBasedUrl + "accounts/{AccountId}/projects/{ProjectId}/industry_roles"; Urls["companies"] = "hq/v1/" + regionBasedUrl + "accounts/{AccountId}/companies"; Urls["companies_import"] = "hq/v1/" + regionBasedUrl + "accounts/{AccountId}/companies/import"; diff --git a/ForgeBimApi/Serialization/ProjectUserPatchResponse.cs b/ForgeBimApi/Serialization/ProjectUserPatchResponse.cs new file mode 100644 index 0000000..985ea9d --- /dev/null +++ b/ForgeBimApi/Serialization/ProjectUserPatchResponse.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Autodesk.Forge.BIM360.Serialization +{ + public class ProjectUserPatchResponse + { + public string email; + public string company_id; + public string[] industry_roles; + public Services services; + public string user_id; + public string project_id; + public string account_id; + public ResponseContent[] error; + } +}