Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AutoConfig.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using Codice.Client.Common;
  2. using Codice.CM.Common;
  3. using PlasticGui;
  4. using Unity.PlasticSCM.Editor.Configuration.CloudEdition.Welcome;
  5. using Unity.PlasticSCM.Editor.WebApi;
  6. namespace Unity.PlasticSCM.Editor.Configuration
  7. {
  8. internal static class AutoConfig
  9. {
  10. internal static TokenExchangeResponse PlasticCredentials(
  11. string unityAccessToken,
  12. string serverName,
  13. string projectPath)
  14. {
  15. SetupUnityEditionToken.CreateCloudEditionTokenIfNeeded();
  16. bool isClientConfigConfigured = ClientConfig.IsConfigured();
  17. if (!isClientConfigConfigured)
  18. {
  19. ConfigureClientConf.FromUnityAccessToken(
  20. unityAccessToken, serverName, projectPath);
  21. }
  22. TokenExchangeResponse tokenExchangeResponse = WebRestApiClient.
  23. PlasticScm.TokenExchange(unityAccessToken);
  24. if (tokenExchangeResponse.Error != null)
  25. return tokenExchangeResponse;
  26. CloudEditionWelcomeWindow.JoinCloudServer(
  27. serverName,
  28. tokenExchangeResponse.User,
  29. tokenExchangeResponse.AccessToken);
  30. if (!isClientConfigConfigured)
  31. return tokenExchangeResponse;
  32. ConfigureProfile.ForServerIfNeeded(
  33. serverName,
  34. tokenExchangeResponse.User);
  35. return tokenExchangeResponse;
  36. }
  37. static class ConfigureClientConf
  38. {
  39. internal static void FromUnityAccessToken(
  40. string unityAccessToken,
  41. string serverName,
  42. string projectPath)
  43. {
  44. CredentialsResponse response = WebRestApiClient.
  45. PlasticScm.GetCredentials(unityAccessToken);
  46. if (response.Error != null)
  47. {
  48. UnityEngine.Debug.LogErrorFormat(
  49. PlasticLocalization.GetString(
  50. PlasticLocalization.Name.ErrorGettingCredentialsCloudProject),
  51. response.Error.Message,
  52. response.Error.ErrorCode);
  53. return;
  54. }
  55. ClientConfigData configData = BuildClientConfigData(
  56. serverName, projectPath, response);
  57. ClientConfig.Get().Save(configData);
  58. }
  59. static ClientConfigData BuildClientConfigData(
  60. string serverName,
  61. string projectPath,
  62. CredentialsResponse response)
  63. {
  64. SEIDWorkingMode workingMode = GetWorkingMode(response.Type);
  65. ClientConfigData configData = new ClientConfigData();
  66. configData.WorkspaceServer = serverName;
  67. configData.CurrentWorkspace = projectPath;
  68. configData.WorkingMode = workingMode.ToString();
  69. configData.SecurityConfig = UserInfo.GetSecurityConfigStr(
  70. workingMode,
  71. response.Email,
  72. GetPassword(response.Token, response.Type));
  73. configData.LastRunningEdition = InstalledEdition.Get();
  74. return configData;
  75. }
  76. static string GetPassword(
  77. string token,
  78. CredentialsResponse.TokenType tokenType)
  79. {
  80. if (tokenType == CredentialsResponse.TokenType.Bearer)
  81. return BEARER_PREFIX + token;
  82. return token;
  83. }
  84. static SEIDWorkingMode GetWorkingMode(CredentialsResponse.TokenType tokenType)
  85. {
  86. if (tokenType == CredentialsResponse.TokenType.Bearer)
  87. return SEIDWorkingMode.SSOWorkingMode;
  88. return SEIDWorkingMode.LDAPWorkingMode;
  89. }
  90. const string BEARER_PREFIX = "Bearer ";
  91. }
  92. static class ConfigureProfile
  93. {
  94. internal static void ForServerIfNeeded(string serverName, string user)
  95. {
  96. ProfileManager profileManager = CmConnection.Get().GetProfileManager();
  97. ServerProfile serverProfile = profileManager.GetProfileForServer(serverName);
  98. if (serverProfile != null)
  99. return;
  100. serverProfile = ProfileManager.CreateProfile(
  101. serverName,
  102. SEIDWorkingMode.SSOWorkingMode,
  103. user);
  104. profileManager.SaveProfile(serverProfile);
  105. }
  106. }
  107. }
  108. }