using Oqtane.Shared; using Oqtane.Models; using System.Net.Http; using System.Threading.Tasks; using Oqtane.Documentation; using System.Net; using System.ComponentModel.DataAnnotations; namespace Oqtane.Services { [PrivateApi("Don't show in the documentation, as everything should use the Interface")] public class UserService : ServiceBase, IUserService { public UserService(HttpClient http, SiteState siteState) : base(http, siteState) { } private string Apiurl => CreateApiUrl("User"); public async Task GetUserAsync(int userId, int siteId) { return await GetJsonAsync($"{Apiurl}/{userId}?siteid={siteId}"); } public async Task GetUserAsync(string username, int siteId) { return await GetUserAsync(username, "", siteId); } public async Task GetUserAsync(string username, string email, int siteId) { return await GetJsonAsync($"{Apiurl}/name/{(!string.IsNullOrEmpty(username) ? username : "-")}/{(!string.IsNullOrEmpty(email) ? email : "-")}/?siteid={siteId}"); } public async Task AddUserAsync(User user) { return await PostJsonAsync(Apiurl, user); } public async Task UpdateUserAsync(User user) { return await PutJsonAsync($"{Apiurl}/{user.UserId}", user); } public async Task DeleteUserAsync(int userId, int siteId) { await DeleteAsync($"{Apiurl}/{userId}?siteid={siteId}"); } public async Task LoginUserAsync(User user, bool setCookie, bool isPersistent) { return await PostJsonAsync($"{Apiurl}/login?setcookie={setCookie}&persistent={isPersistent}", user); } public async Task LogoutUserAsync(User user) { // best practices recommend post is preferrable to get for logout await PostJsonAsync($"{Apiurl}/logout", user); } public async Task VerifyEmailAsync(User user, string token) { return await PostJsonAsync($"{Apiurl}/verify?token={token}", user); } public async Task ForgotPasswordAsync(User user) { await PostJsonAsync($"{Apiurl}/forgot", user); } public async Task ResetPasswordAsync(User user, string token) { return await PostJsonAsync($"{Apiurl}/reset?token={token}", user); } public async Task VerifyTwoFactorAsync(User user, string token) { return await PostJsonAsync($"{Apiurl}/twofactor?token={token}", user); } public async Task ValidatePasswordAsync(string password) { return await GetJsonAsync($"{Apiurl}/validate/{WebUtility.UrlEncode(password)}"); } public async Task GetTokenAsync() { return await GetStringAsync($"{Apiurl}/token"); } public async Task GetPersonalAccessTokenAsync() { return await GetStringAsync($"{Apiurl}/personalaccesstoken"); } public async Task LinkUserAsync(User user, string token, string type, string key, string name) { return await PostJsonAsync($"{Apiurl}/link?token={token}&type={type}&key={key}&name={name}", user); } public async Task GetPasswordRequirementsAsync(int siteId) { return await GetStringAsync($"{Apiurl}/passwordrequirements/{siteId}"); } } }