diff --git a/GanjooRazor/Pages/Search.cshtml b/GanjooRazor/Pages/Search.cshtml index 6b5a9f4d..5661c2ac 100644 --- a/GanjooRazor/Pages/Search.cshtml +++ b/GanjooRazor/Pages/Search.cshtml @@ -152,6 +152,8 @@ + + diff --git a/GanjooRazor/Pages/Search.cshtml.cs b/GanjooRazor/Pages/Search.cshtml.cs index 09148acb..18f41133 100644 --- a/GanjooRazor/Pages/Search.cshtml.cs +++ b/GanjooRazor/Pages/Search.cshtml.cs @@ -1,14 +1,23 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using DNTPersianUtils.Core; +using GanjooRazor.Utils; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Caching.Memory; +using Newtonsoft.Json; +using RMuseum.Models.Auth.Memory; using RMuseum.Models.Ganjoor.ViewModels; using RMuseum.Services; +using RSecurityBackend.Models.Auth.Memory; +using RSecurityBackend.Models.Auth.ViewModels; using RSecurityBackend.Models.Generic; namespace GanjooRazor.Pages @@ -27,16 +36,24 @@ namespace GanjooRazor.Pages /// private readonly IGanjoorService _ganjoorService; + /// + /// HttpClient instance + /// + private readonly HttpClient _httpClient; + + /// /// constructor /// /// /// - public SearchModel(IMemoryCache memoryCache, IGanjoorService ganjoorService) + /// + public SearchModel(IMemoryCache memoryCache, IGanjoorService ganjoorService, HttpClient httpClient) { _memoryCache = memoryCache; _ganjoorService = ganjoorService; + _httpClient = httpClient; } public List Poets { get; set; } @@ -48,6 +65,14 @@ namespace GanjooRazor.Pages public List Poems { get; set; } public string PagingToolsHtml { get; set; } + /// + /// is logged on + /// + public bool LoggedIn { get; set; } + + [BindProperty] + public LoginViewModel LoginViewModel { get; set; } + private async Task preparePoets(bool includeBio) { var cacheKey = $"/api/ganjoor/poets?includeBio={includeBio}"; @@ -81,8 +106,10 @@ namespace GanjooRazor.Pages Poet = poet; } - public async Task OnGet() + public async Task OnGetAsync() { + LoggedIn = !string.IsNullOrEmpty(Request.Cookies["Token"]); + Query = Request.Query["s"].ApplyCorrectYeKe().Trim(); PoetId = string.IsNullOrEmpty(Request.Query["author"]) ? 0 : int.Parse(Request.Query["author"]); CatId = string.IsNullOrEmpty(Request.Query["cat"]) ? 0 : int.Parse(Request.Query["cat"]); @@ -127,7 +154,7 @@ namespace GanjooRazor.Pages ); - Poems = new List(searchRes.Result.Items); + Poems = searchRes.Result.Items == null ? new List() : new List(searchRes.Result.Items); if (Poems != null) { // highlight searched word @@ -201,7 +228,7 @@ namespace GanjooRazor.Pages { string htmlText = "

"; - if (paginationMetadata.totalPages > 1) + if (paginationMetadata != null && paginationMetadata.totalPages > 1) { if (paginationMetadata.currentPage > 3) { @@ -232,6 +259,100 @@ namespace GanjooRazor.Pages htmlText += $"

{Environment.NewLine}"; return htmlText; } + + /// + /// Login + /// + /// + public async Task OnPostLoginAsync() + { + if (!ModelState.IsValid) + { + return BadRequest(); + } + + LoginViewModel.ClientAppName = "GanjooRazor"; + LoginViewModel.Language = "fa-IR"; + + var stringContent = new StringContent(JsonConvert.SerializeObject(LoginViewModel), Encoding.UTF8, "application/json"); + var loginUrl = $"{APIRoot.Url}/api/users/login"; + var response = await _httpClient.PostAsync(loginUrl, stringContent); + + if (response.StatusCode != HttpStatusCode.OK) + { + return Redirect($"/login?redirect={Request.Path}&error={await response.Content.ReadAsStringAsync()}"); + } + + LoggedOnUserModel loggedOnUser = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); + + var cookieOption = new CookieOptions() + { + Expires = DateTime.Now.AddDays(365), + }; + + Response.Cookies.Append("UserId", loggedOnUser.User.Id.ToString(), cookieOption); + Response.Cookies.Append("SessionId", loggedOnUser.SessionId.ToString(), cookieOption); + Response.Cookies.Append("Token", loggedOnUser.Token, cookieOption); + Response.Cookies.Append("Username", loggedOnUser.User.Username, cookieOption); + Response.Cookies.Append("Name", $"{loggedOnUser.User.FirstName} {loggedOnUser.User.SureName}", cookieOption); + Response.Cookies.Append("NickName", $"{loggedOnUser.User.NickName}", cookieOption); + + bool canEditContent = false; + var ganjoorEntity = loggedOnUser.SecurableItem.Where(s => s.ShortName == RMuseumSecurableItem.GanjoorEntityShortName).SingleOrDefault(); + if (ganjoorEntity != null) + { + var op = ganjoorEntity.Operations.Where(o => o.ShortName == SecurableItem.ModifyOperationShortName).SingleOrDefault(); + if (op != null) + { + canEditContent = op.Status; + } + } + + Response.Cookies.Append("CanEdit", canEditContent.ToString(), cookieOption); + + + return Redirect(Request.Path); + } + + /// + /// logout + /// + /// + public async Task OnPostLogoutAsync() + { + if (!ModelState.IsValid) + { + return BadRequest(); + } + + if (!string.IsNullOrEmpty(Request.Cookies["SessionId"]) && !string.IsNullOrEmpty(Request.Cookies["UserId"])) + { + using (HttpClient secureClient = new HttpClient()) + { + if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response)) + { + var logoutUrl = $"{APIRoot.Url}/api/users/delsession?userId={Request.Cookies["UserId"]}&sessionId={Request.Cookies["SessionId"]}"; + await secureClient.DeleteAsync(logoutUrl); + } + } + } + + + var cookieOption = new CookieOptions() + { + Expires = DateTime.Now.AddDays(-1) + }; + foreach (var cookieName in new string[] { "UserId", "SessionId", "Token", "Username", "Name", "NickName", "CanEdit" }) + { + if (Request.Cookies[cookieName] != null) + { + Response.Cookies.Append(cookieName, "", cookieOption); + } + } + + + return Redirect(Request.Path); + } } } diff --git a/GanjooRazor/Pages/_InlineLoginPartialForSearch.cshtml b/GanjooRazor/Pages/_InlineLoginPartialForSearch.cshtml new file mode 100644 index 00000000..289f66e5 --- /dev/null +++ b/GanjooRazor/Pages/_InlineLoginPartialForSearch.cshtml @@ -0,0 +1,106 @@ +@model GanjooRazor.Pages.SearchModel +@if (Model.LoggedIn) +{ +
+ + + + +
+} +else +{ +
+ + + + + +
+ + + +} + + diff --git a/GanjooRazor/Pages/_VaznPage.cshtml.cs b/GanjooRazor/Pages/_VaznPage.cshtml.cs index e5514b52..f77a0f59 100644 --- a/GanjooRazor/Pages/_VaznPage.cshtml.cs +++ b/GanjooRazor/Pages/_VaznPage.cshtml.cs @@ -29,7 +29,7 @@ namespace GanjooRazor.Pages } string metre = Request.Query["v"]; - int poetId = string.IsNullOrEmpty(Request.Query["a"]) ? 0 : int.Parse(Request.Query["a"]); + int? poetId = string.IsNullOrEmpty(Request.Query["a"]) ? null : int.Parse(Request.Query["a"]); var poemsRes = await _ganjoorService.GetSimilarPoems ( @@ -39,12 +39,12 @@ namespace GanjooRazor.Pages PageSize = 20 }, metre: metre, - rhyme: null, + rhyme: "", poetId: poetId ); GanjoorPage.Title = "شعرهای "; - if (poetId != 0) + if (poetId != null) { GanjoorPage.Title += $"{Poets.Where(p => p.Id == poetId).Single().Name} "; } @@ -56,7 +56,7 @@ namespace GanjooRazor.Pages string htmlText = ""; - if (poetId != 0) + if (poetId != null) { htmlText += $"
{Environment.NewLine}"; htmlText += $"

{Environment.NewLine}"; diff --git a/GanjoorService.sln b/GanjoorService.sln index 9d547aae..3bdacee2 100644 --- a/GanjoorService.sln +++ b/GanjoorService.sln @@ -7,7 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RSecurityBackend", "RSecuri EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RMuseum", "RMuseum\RMuseum.csproj", "{113EF3DE-E8A9-40EF-9DA0-8DFABB3CDD68}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GanjooRazor", "GanjooRazor\GanjooRazor.csproj", "{48F9CDFA-625F-4743-BF21-BD556F6F33E8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GanjooRazor", "GanjooRazor\GanjooRazor.csproj", "{48F9CDFA-625F-4743-BF21-BD556F6F33E8}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution