Browse Source

Show a warning on logging in via CLI for multiple organizations

resolves https://github.com/abpframework/abp/issues/6420
pull/6439/head
Yunus Emre Kalkan 6 years ago
parent
commit
1a9b276c51
  1. 49
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/LoginCommand.cs

49
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/LoginCommand.cs

@ -2,11 +2,15 @@
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using System; using System;
using System.Text; using System.Text;
using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Cli.Args; using Volo.Abp.Cli.Args;
using Volo.Abp.Cli.Auth; using Volo.Abp.Cli.Auth;
using Volo.Abp.Cli.Http;
using Volo.Abp.Cli.ProjectBuilding;
using Volo.Abp.Cli.Utils; using Volo.Abp.Cli.Utils;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Threading;
namespace Volo.Abp.Cli.Commands namespace Volo.Abp.Cli.Commands
{ {
@ -15,10 +19,16 @@ namespace Volo.Abp.Cli.Commands
public ILogger<LoginCommand> Logger { get; set; } public ILogger<LoginCommand> Logger { get; set; }
protected AuthService AuthService { get; } protected AuthService AuthService { get; }
public ICancellationTokenProvider CancellationTokenProvider { get; }
public IRemoteServiceExceptionHandler RemoteServiceExceptionHandler { get; }
public LoginCommand(AuthService authService) public LoginCommand(AuthService authService,
ICancellationTokenProvider cancellationTokenProvider,
IRemoteServiceExceptionHandler remoteServiceExceptionHandler)
{ {
AuthService = authService; AuthService = authService;
CancellationTokenProvider = cancellationTokenProvider;
RemoteServiceExceptionHandler = remoteServiceExceptionHandler;
Logger = NullLogger<LoginCommand>.Instance; Logger = NullLogger<LoginCommand>.Instance;
} }
@ -33,6 +43,14 @@ namespace Volo.Abp.Cli.Commands
); );
} }
var organization = commandLineArgs.Options.GetOrNull(Options.Organization.Short, Options.Organization.Long);
if (string.IsNullOrWhiteSpace(organization) && await CheckMultipleOrganizationsAsync(commandLineArgs.Target))
{
Logger.LogError($"You have multiple organizations, please specify your organization with `--organization` parameter.");
return;
}
var password = commandLineArgs.Options.GetOrNull(Options.Password.Short, Options.Password.Long); var password = commandLineArgs.Options.GetOrNull(Options.Password.Short, Options.Password.Long);
if (password == null) if (password == null)
{ {
@ -51,12 +69,37 @@ namespace Volo.Abp.Cli.Commands
await AuthService.LoginAsync( await AuthService.LoginAsync(
commandLineArgs.Target, commandLineArgs.Target,
password, password,
commandLineArgs.Options.GetOrNull(Options.Organization.Short, Options.Organization.Long) organization
); );
Logger.LogInformation($"Successfully logged in as '{commandLineArgs.Target}'"); Logger.LogInformation($"Successfully logged in as '{commandLineArgs.Target}'");
} }
private async Task<bool> CheckMultipleOrganizationsAsync(string username)
{
var url = $"{CliUrls.WwwAbpIo}api/license/check-multiple-organizations?username={username}";
using (var client = new CliHttpClient())
{
var response = await client.GetHttpResponseMessageWithRetryAsync(
url,
CancellationTokenProvider.Token,
Logger);
if (!response.IsSuccessStatusCode)
{
throw new Exception($"ERROR: Remote server returns '{response.StatusCode}'");
}
await RemoteServiceExceptionHandler.EnsureSuccessfulHttpResponseAsync(response);
var responseContent = await response.Content.ReadAsStringAsync();
var apiKeyResult = JsonSerializer.Deserialize<bool>(responseContent);
return apiKeyResult;
}
}
public string GetUsageInfo() public string GetUsageInfo()
{ {
var sb = new StringBuilder(); var sb = new StringBuilder();
@ -96,4 +139,4 @@ namespace Volo.Abp.Cli.Commands
} }
} }
} }
} }

Loading…
Cancel
Save