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

Commit

Permalink
add update user function (PATCH)
Browse files Browse the repository at this point in the history
  • Loading branch information
joefields committed Jun 24, 2020
1 parent 9bca5c1 commit fd8dd46
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 2 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
12 changes: 10 additions & 2 deletions BimProjectSetupCLI/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions BimProjectSetupCommon/AppOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -102,6 +103,7 @@ internal AppOptions()
Encoding = GetEncoding("UTF-8");
TrialRun = false;
CopyFolders = false;
UpdateProjectUsers = false;
AccountRegion = "US";
}

Expand Down Expand Up @@ -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];
Expand Down
104 changes: 104 additions & 0 deletions BimProjectSetupCommon/Workflows/ProjectUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ public void AddProjectUsersProcess(List<ProjectUser> _projectUsers)
}
}

public void UpdateProjectUsersFromCsvProcess()
{
try
{
DataController._projcetUserTable = CsvReader.ReadDataFromCSV(DataController._projcetUserTable, DataController._options.ProjectUserFilePath);

if (false == _options.TrialRun)
{
List<ProjectUser> _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<ProjectUser> _projectUsers)
{
if (_projectUsers == null || _projectUsers.Count < 1)
Expand Down Expand Up @@ -142,6 +164,49 @@ private void AddUsers(List<ProjectUser> _projectUsers)
}
}
}

private void PatchUser(List<ProjectUser> _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
Expand Down Expand Up @@ -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<IRestResponse> responses)
{
Expand Down Expand Up @@ -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<ProjectUserPatchResponse>(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);
Expand Down
1 change: 1 addition & 0 deletions ForgeBimApi/ForgeBimApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
<Compile Include="Serialization\Project.cs" />
<Compile Include="Serialization\Projects.cs" />
<Compile Include="Serialization\ProjectUser.cs" />
<Compile Include="Serialization\ProjectUserPatchResponse.cs" />
<Compile Include="Serialization\ProjectUserResponse.cs" />
<Compile Include="Serialization\Refs.cs" />
<Compile Include="Serialization\Related.cs" />
Expand Down
31 changes: 31 additions & 0 deletions ForgeBimApi/ForgeBimApiWrappers/BimProjectApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,37 @@ public IRestResponse PostUsersImport(string projectId, string userId, List<Proje
return response;
}

public IRestResponse PatchUser(string projectId, string adminUserId, string userId, ProjectUser user, string accountId = null)
{
var request = new RestRequest(Method.PATCH);
request.Resource = Urls["projects_projectId_user_patch"];
if (accountId == null)
{
request.AddParameter("AccountId", options.ForgeBimAccountId, ParameterType.UrlSegment);
}
else
{
request.AddParameter("AccountId", accountId, ParameterType.UrlSegment);
}

request.AddParameter("ProjectId", projectId, ParameterType.UrlSegment);
request.AddParameter("UserId", userId, ParameterType.UrlSegment);

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.NullValueHandling = NullValueHandling.Ignore;
user.email = null;
string serviceString = JsonConvert.SerializeObject(user, settings);
request.AddParameter("application/json", serviceString, ParameterType.RequestBody);

request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", $"Bearer {Token}");
request.AddHeader("content-type", ContentType);
request.AddHeader("x-user-id", adminUserId);

IRestResponse response = ExecuteRequest(request);
return response;
}


/// <summary>
/// Update projects properties and services assigned to the project
Expand Down
1 change: 1 addition & 0 deletions ForgeBimApi/ForgeBimApiWrappers/ForgeApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
20 changes: 20 additions & 0 deletions ForgeBimApi/Serialization/ProjectUserPatchResponse.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit fd8dd46

Please sign in to comment.