commit
3d21a6e204
|
@ -5,7 +5,6 @@ using System.Net.Http.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Oqtane.Models;
|
using Oqtane.Models;
|
||||||
using Oqtane.Modules.HtmlText.Models;
|
|
||||||
|
|
||||||
namespace Oqtane.Services
|
namespace Oqtane.Services
|
||||||
{
|
{
|
||||||
|
@ -18,51 +17,40 @@ namespace Oqtane.Services
|
||||||
_http = client;
|
_http = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async Task<T> PutJsonAsync<T>(string uri, T value)
|
|
||||||
{
|
|
||||||
var response = await _http.PutAsJsonAsync(uri, value);
|
|
||||||
var result = await response.Content.ReadFromJsonAsync<T>();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected async Task PutAsync(string uri)
|
|
||||||
{
|
|
||||||
await _http.PutAsync(uri, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected async Task PostAsync(string uri)
|
|
||||||
{
|
|
||||||
await _http.PostAsync(uri, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected async Task GetAsync(string uri)
|
protected async Task GetAsync(string uri)
|
||||||
{
|
{
|
||||||
await _http.GetAsync(uri);
|
var response = await _http.GetAsync(uri);
|
||||||
|
CheckResponse(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async Task<string> GetStringAsync(string uri)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return await _http.GetStringAsync(uri);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
//TODO replace with logging
|
||||||
|
Console.WriteLine(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async Task<byte[]> GetByteArrayAsync(string uri)
|
protected async Task<byte[]> GetByteArrayAsync(string uri)
|
||||||
{
|
{
|
||||||
return await _http.GetByteArrayAsync(uri);
|
try
|
||||||
}
|
{
|
||||||
|
return await _http.GetByteArrayAsync(uri);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e);
|
||||||
|
}
|
||||||
|
|
||||||
protected async Task<R> PostJsonAsync<T, R>(string uri, T value)
|
return default;
|
||||||
{
|
|
||||||
var response = await _http.PostAsJsonAsync(uri, value);
|
|
||||||
if (!ValidateJsonContent(response.Content)) return default;
|
|
||||||
|
|
||||||
var result = await response.Content.ReadFromJsonAsync<R>();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool ValidateJsonContent(HttpContent content)
|
|
||||||
{
|
|
||||||
var mediaType = content?.Headers.ContentType?.MediaType;
|
|
||||||
return mediaType != null && mediaType.Equals("application/json", StringComparison.OrdinalIgnoreCase);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected async Task<T> PostJsonAsync<T>(string uri, T value)
|
|
||||||
{
|
|
||||||
return await PostJsonAsync<T, T>(uri, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async Task<T> GetJsonAsync<T>(string uri)
|
protected async Task<T> GetJsonAsync<T>(string uri)
|
||||||
|
@ -76,25 +64,75 @@ namespace Oqtane.Services
|
||||||
return default;
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected async Task PutAsync(string uri)
|
||||||
|
{
|
||||||
|
var response = await _http.PutAsync(uri, null);
|
||||||
|
CheckResponse(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async Task<T> PutJsonAsync<T>(string uri, T value)
|
||||||
|
{
|
||||||
|
return await PutJsonAsync<T, T>(uri, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async Task<TResult> PutJsonAsync<TValue, TResult>(string uri, TValue value)
|
||||||
|
{
|
||||||
|
var response = await _http.PutAsJsonAsync(uri, value);
|
||||||
|
if (CheckResponse(response) && ValidateJsonContent(response.Content))
|
||||||
|
{
|
||||||
|
var result = await response.Content.ReadFromJsonAsync<TResult>();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async Task PostAsync(string uri)
|
||||||
|
{
|
||||||
|
var response = await _http.PostAsync(uri, null);
|
||||||
|
CheckResponse(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async Task<T> PostJsonAsync<T>(string uri, T value)
|
||||||
|
{
|
||||||
|
return await PostJsonAsync<T, T>(uri, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async Task<TResult> PostJsonAsync<TValue, TResult>(string uri, TValue value)
|
||||||
|
{
|
||||||
|
var response = await _http.PostAsJsonAsync(uri, value);
|
||||||
|
if (CheckResponse(response) && ValidateJsonContent(response.Content))
|
||||||
|
{
|
||||||
|
var result = await response.Content.ReadFromJsonAsync<TResult>();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async Task DeleteAsync(string uri)
|
||||||
|
{
|
||||||
|
var response = await _http.DeleteAsync(uri);
|
||||||
|
CheckResponse(response);
|
||||||
|
}
|
||||||
|
|
||||||
private bool CheckResponse(HttpResponseMessage response)
|
private bool CheckResponse(HttpResponseMessage response)
|
||||||
{
|
{
|
||||||
if (response.IsSuccessStatusCode) return true;
|
if (response.IsSuccessStatusCode) return true;
|
||||||
if (response.StatusCode != HttpStatusCode.NoContent && response.StatusCode != HttpStatusCode.NotFound)
|
if (response.StatusCode != HttpStatusCode.NoContent && response.StatusCode != HttpStatusCode.NotFound)
|
||||||
{
|
{
|
||||||
//TODO: Log error here
|
//TODO: Log errors here
|
||||||
|
Console.WriteLine($"Response status: {response.StatusCode} {response.ReasonPhrase}");
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async Task DeleteAsync(string uri)
|
private static bool ValidateJsonContent(HttpContent content)
|
||||||
{
|
{
|
||||||
await _http.DeleteAsync(uri);
|
var mediaType = content?.Headers.ContentType?.MediaType;
|
||||||
}
|
return mediaType != null && mediaType.Equals("application/json", StringComparison.OrdinalIgnoreCase);
|
||||||
|
//TODO Missing content JSON validation
|
||||||
protected async Task<string> GetStringAsync(string uri)
|
|
||||||
{
|
|
||||||
return await _http.GetStringAsync(uri);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string CreateApiUrl(Alias alias, string absoluteUri, string serviceName)
|
public static string CreateApiUrl(Alias alias, string absoluteUri, string serviceName)
|
||||||
|
|
|
@ -8,14 +8,12 @@ namespace Oqtane.Services
|
||||||
{
|
{
|
||||||
public class UserService : ServiceBase, IUserService
|
public class UserService : ServiceBase, IUserService
|
||||||
{
|
{
|
||||||
|
|
||||||
private readonly SiteState _siteState;
|
private readonly SiteState _siteState;
|
||||||
private readonly NavigationManager _navigationManager;
|
private readonly NavigationManager _navigationManager;
|
||||||
private readonly ISiteService _siteService;
|
private readonly ISiteService _siteService;
|
||||||
|
|
||||||
public UserService(HttpClient http, SiteState siteState, NavigationManager navigationManager, ISiteService siteService) : base(http)
|
public UserService(HttpClient http, SiteState siteState, NavigationManager navigationManager, ISiteService siteService) : base(http)
|
||||||
{
|
{
|
||||||
|
|
||||||
_siteState = siteState;
|
_siteState = siteState;
|
||||||
_navigationManager = navigationManager;
|
_navigationManager = navigationManager;
|
||||||
_siteService = siteService;
|
_siteService = siteService;
|
||||||
|
@ -38,39 +36,29 @@ namespace Oqtane.Services
|
||||||
|
|
||||||
public async Task<User> AddUserAsync(User user)
|
public async Task<User> AddUserAsync(User user)
|
||||||
{
|
{
|
||||||
Site site = await _siteService.GetSiteAsync(_siteState.Alias.SiteId, _siteState.Alias);
|
// On initial site creation alias is null and we always want to create host user
|
||||||
|
if (user.Username != Constants.HostUser && _siteState.Alias != null)
|
||||||
if (!site.AllowRegistration)
|
|
||||||
{
|
{
|
||||||
return null;
|
Site site = await _siteService.GetSiteAsync(_siteState.Alias.SiteId, _siteState.Alias);
|
||||||
|
if (!site.AllowRegistration)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
return await PostJsonAsync<User>(Apiurl, user);
|
||||||
{
|
|
||||||
return await PostJsonAsync<User>(Apiurl, user);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<User> AddUserAsync(User user, Alias alias)
|
public async Task<User> AddUserAsync(User user, Alias alias)
|
||||||
{
|
{
|
||||||
try
|
return await PostJsonAsync<User>(CreateCrossTenantUrl(Apiurl, alias), user);
|
||||||
{
|
|
||||||
return await PostJsonAsync<User>(CreateCrossTenantUrl(Apiurl, alias), user);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<User> UpdateUserAsync(User user)
|
public async Task<User> UpdateUserAsync(User user)
|
||||||
{
|
{
|
||||||
return await PutJsonAsync<User>($"{Apiurl}/{user.UserId.ToString()}", user);
|
return await PutJsonAsync<User>($"{Apiurl}/{user.UserId.ToString()}", user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DeleteUserAsync(int userId)
|
public async Task DeleteUserAsync(int userId)
|
||||||
{
|
{
|
||||||
await DeleteAsync($"{Apiurl}/{userId.ToString()}");
|
await DeleteAsync($"{Apiurl}/{userId.ToString()}");
|
||||||
|
@ -101,6 +89,5 @@ namespace Oqtane.Services
|
||||||
{
|
{
|
||||||
return await PostJsonAsync<User>($"{Apiurl}/reset?token={token}", user);
|
return await PostJsonAsync<User>($"{Apiurl}/reset?token={token}", user);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user