Browse Source

Merge pull request #8880 from abpframework/LoginInfoCommand

Cli: Created LoginInfoCommand
pull/8893/head
Halil İbrahim Kalkan 5 years ago
committed by GitHub
parent
commit
e853d62052
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs
  2. 43
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Auth/AuthService.cs
  3. 16
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Auth/LoginInfo.cs
  4. 63
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/LoginInfoCommand..cs

1
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs

@ -36,6 +36,7 @@ namespace Volo.Abp.Cli
options.Commands["add-module"] = typeof(AddModuleCommand);
options.Commands["list-modules"] = typeof(ListModulesCommand);
options.Commands["login"] = typeof(LoginCommand);
options.Commands["login-info"] = typeof(LoginInfoCommand);
options.Commands["logout"] = typeof(LogoutCommand);
options.Commands[GenerateProxyCommand.Name] = typeof(GenerateProxyCommand);
options.Commands[RemoveProxyCommand.Name] = typeof(RemoveProxyCommand);

43
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Auth/AuthService.cs

@ -9,8 +9,10 @@ using IdentityModel;
using Microsoft.Extensions.Logging;
using Volo.Abp.Cli.Commands;
using Volo.Abp.Cli.Http;
using Volo.Abp.Cli.ProjectBuilding;
using Volo.Abp.DependencyInjection;
using Volo.Abp.IdentityModel;
using Volo.Abp.Json;
using Volo.Abp.Threading;
namespace Volo.Abp.Cli.Auth
@ -18,21 +20,54 @@ namespace Volo.Abp.Cli.Auth
public class AuthService : ITransientDependency
{
protected IIdentityModelAuthenticationService AuthenticationService { get; }
protected ILogger<NewCommand> Logger { get; }
protected ILogger<AuthService> Logger { get; }
protected CliHttpClientFactory CliHttpClientFactory { get; }
public RemoteServiceExceptionHandler RemoteServiceExceptionHandler { get; }
public IJsonSerializer JsonSerializer { get; }
public ICancellationTokenProvider CancellationTokenProvider { get; }
public AuthService(
IIdentityModelAuthenticationService authenticationService,
ILogger<NewCommand> logger,
ILogger<AuthService> logger,
ICancellationTokenProvider cancellationTokenProvider,
CliHttpClientFactory cliHttpClientFactory
CliHttpClientFactory cliHttpClientFactory,
RemoteServiceExceptionHandler remoteServiceExceptionHandler,
IJsonSerializer jsonSerializer
)
{
AuthenticationService = authenticationService;
Logger = logger;
CancellationTokenProvider = cancellationTokenProvider;
CliHttpClientFactory = cliHttpClientFactory;
RemoteServiceExceptionHandler = remoteServiceExceptionHandler;
JsonSerializer = jsonSerializer;
}
public async Task<LoginInfo> GetLoginInfoAsync()
{
if (!IsLoggedIn())
{
return null;
}
var url = $"{CliUrls.WwwAbpIo}api/license/login-info";
var client = CliHttpClientFactory.CreateClient();
using (var response = await client.GetHttpResponseMessageWithRetryAsync(url, CancellationTokenProvider.Token, Logger))
{
if (!response.IsSuccessStatusCode)
{
Logger.LogError("Remote server returns '{response.StatusCode}'");
return null;
}
await RemoteServiceExceptionHandler.EnsureSuccessfulHttpResponseAsync(response);
var responseContent = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<LoginInfo>(responseContent);
}
}
public async Task LoginAsync(string userName, string password, string organizationName = null)
@ -108,4 +143,4 @@ namespace Volo.Abp.Cli.Auth
return File.Exists(CliPaths.AccessToken);
}
}
}
}

16
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Auth/LoginInfo.cs

@ -0,0 +1,16 @@

namespace Volo.Abp.Cli.Auth
{
public class LoginInfo
{
public string Name { get; set; }
public string Surname { get; set; }
public string Username { get; set; }
public string EmailAddress { get; set; }
public string Organization { get; set; }
}
}

63
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/LoginInfoCommand..cs

@ -0,0 +1,63 @@
using System;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Cli.Args;
using Volo.Abp.Cli.Auth;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Cli.Commands
{
public class LoginInfoCommand : IConsoleCommand, ITransientDependency
{
public ILogger<LoginInfoCommand> Logger { get; set; }
protected AuthService AuthService { get; }
public LoginInfoCommand(AuthService authService)
{
AuthService = authService;
Logger = NullLogger<LoginInfoCommand>.Instance;
}
public async Task ExecuteAsync(CommandLineArgs commandLineArgs)
{
var loginInfo = await AuthService.GetLoginInfoAsync();
if (loginInfo == null)
{
Logger.LogError("Unable to get login info.");
return;
}
var sb = new StringBuilder();
sb.AppendLine("");
sb.AppendLine($"Login info:");
sb.AppendLine($"Name: {loginInfo.Name}");
sb.AppendLine($"Surname: {loginInfo.Surname}");
sb.AppendLine($"Username: {loginInfo.Username}");
sb.AppendLine($"Email Address: {loginInfo.EmailAddress}");
sb.AppendLine($"Organization: {loginInfo.Organization}");
Logger.LogInformation(sb.ToString());
}
public string GetUsageInfo()
{
var sb = new StringBuilder();
sb.AppendLine("");
sb.AppendLine("Usage:");
sb.AppendLine(" abp login-info");
sb.AppendLine("");
sb.AppendLine("See the documentation for more info: https://docs.abp.io/en/abp/latest/CLI");
return sb.ToString();
}
public string GetShortDescription()
{
return "Show your login info.";
}
}
}
Loading…
Cancel
Save