diff --git a/Marechai/Marechai.csproj b/Marechai/Marechai.csproj index af0a12ab..b221c686 100644 --- a/Marechai/Marechai.csproj +++ b/Marechai/Marechai.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 - 3.0.99.1226 + 3.0.99.1227 Canary Islands Computer Museum Copyright © 2003-2020 Natalia Portillo Canary Islands Computer Museum Website diff --git a/Marechai/Pages/Admin/Details/License.razor b/Marechai/Pages/Admin/Details/License.razor index f1c926e1..4e7d7195 100644 --- a/Marechai/Pages/Admin/Details/License.razor +++ b/Marechai/Pages/Admin/Details/License.razor @@ -31,8 +31,10 @@ } @page "/admin/licenses/details/{Id:int}" +@page "/admin/licenses/edit/{Id:int}" @inherits OwningComponentBase @inject IStringLocalizer L +@inject NavigationManager NavigationManager @attribute [Authorize(Roles = "UberAdmin, Admin")]

@L["License details"]


@@ -47,33 +49,84 @@
@L["Name"] - + + + + @L["Please enter a valid name."] + + + @L["SPDX identifier"] - + + + + @L["Please enter a valid SPDX identifier."] + + + @L["FSF approved"] - + @L["OSI approved"] - + - - @L["License text link"] - - - @if (_editable || _model.Text != null) + @if (_editing || _model.Link != null) + { + + @L["License text link"] + @if (_editing) + { + @L["Unknown or none (text link)"] + } + @if (!_editing || + !_unknownLink) + { + + + + @L["Please enter a license text link."] + + + + } + + } + @if (_editing || _model.Text != null) { @L["License text"] - + @if (_editing) + { + @L["Unknown (license text)"] + } + @if (!_editing || + !_unknownText) + { + + + + @L["Please enter a valid license text."] + + + + } }
- @L["Edit"] + @if (!_editing) + { + + } + else + { + + + } @L["Back to list"]
\ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/License.razor.cs b/Marechai/Pages/Admin/Details/License.razor.cs index 13a21854..ca28a6e5 100644 --- a/Marechai/Pages/Admin/Details/License.razor.cs +++ b/Marechai/Pages/Admin/Details/License.razor.cs @@ -1,13 +1,18 @@ +using System; using System.Threading.Tasks; +using Blazorise; +using Marechai.Shared; using Microsoft.AspNetCore.Components; namespace Marechai.Pages.Admin.Details { public partial class License { - bool _editable; + bool _editing; bool _loaded; Database.Models.License _model; + bool _unknownLink; + bool _unknownText; [Parameter] public int Id { get; set; } @@ -23,7 +28,74 @@ namespace Marechai.Pages.Admin.Details _model = await Service.GetAsync(Id); + _editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). + StartsWith("admin/licenses/edit/", StringComparison.InvariantCulture); + + if(_editing) + SetCheckboxes(); + StateHasChanged(); } + + void SetCheckboxes() + { + _unknownText = string.IsNullOrWhiteSpace(_model.Text); + _unknownLink = string.IsNullOrWhiteSpace(_model.Link); + } + + void OnEditClicked() + { + _editing = true; + SetCheckboxes(); + StateHasChanged(); + } + + async void OnCancelClicked() + { + _editing = false; + _model = await Service.GetAsync(Id); + SetCheckboxes(); + StateHasChanged(); + } + + async void OnSaveClicked() + { + if(_unknownText) + _model.Text = null; + else if(string.IsNullOrWhiteSpace(_model.Text)) + return; + + if(string.IsNullOrWhiteSpace(_model.Text) || + _model.Name?.Length > 255) + return; + + if(string.IsNullOrWhiteSpace(_model.SPDX) || + _model.SPDX?.Length > 255) + return; + + if(_unknownLink) + _model.Link = null; + else if(string.IsNullOrWhiteSpace(_model.Link) || + _model.Link?.Length > 512) + return; + + _editing = false; + await Service.UpdateAsync(_model); + _model = await Service.GetAsync(Id); + SetCheckboxes(); + StateHasChanged(); + } + + void ValidateName(ValidatorEventArgs e) => + Validators.ValidateString(e, L["License name cannot contain more than 255 characters."], 255); + + void ValidateSpdx(ValidatorEventArgs e) => + Validators.ValidateString(e, L["SPDX identifier cannot contain more than 255 characters."], 255); + + void ValidateLink(ValidatorEventArgs e) => + Validators.ValidateUrl(e, L["License text link must be smaller than 512 characters."], 512); + + void ValidateText(ValidatorEventArgs e) => + Validators.ValidateString(e, L["License text cannot contain more than 131072 characters."], 131072); } } \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/Person.razor.cs b/Marechai/Pages/Admin/Details/Person.razor.cs index 2b113ffd..ad02a140 100644 --- a/Marechai/Pages/Admin/Details/Person.razor.cs +++ b/Marechai/Pages/Admin/Details/Person.razor.cs @@ -1,20 +1,16 @@ using System; using System.Collections.Generic; -using System.Text.RegularExpressions; using System.Threading.Tasks; using Blazorise; using Marechai.Database.Models; using Marechai.Shared; using Marechai.ViewModels; using Microsoft.AspNetCore.Components; -using Match = System.Text.RegularExpressions.Match; namespace Marechai.Pages.Admin.Details { public partial class Person { - const string _webpageRegex = - @"^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$"; List _countries; bool _editing; bool _loaded; @@ -260,32 +256,8 @@ namespace Marechai.Pages.Admin.Details e.Status = ValidationStatus.Error; } - void ValidateWebpage(ValidatorEventArgs e) - { - if(!(e.Value is string webpage)) - { - e.Status = ValidationStatus.Error; - - return; - } - - if(webpage.Length < 1 || - webpage.Length > 255) - { - e.ErrorText = L["Webpage must be smaller than 255 characters."]; - e.Status = ValidationStatus.Error; - - return; - } - - var rx = new Regex(_webpageRegex); - Match m = rx.Match(webpage); - - if(m.Success) - return; - - e.Status = ValidationStatus.Error; - } + void ValidateWebpage(ValidatorEventArgs e) => + Validators.ValidateUrl(e, L["Webpage must be smaller than 255 characters."], 255); void ValidateTwitter(ValidatorEventArgs e) { diff --git a/Marechai/Pages/Admin/Licenses.razor b/Marechai/Pages/Admin/Licenses.razor index f16f69b5..f781a97b 100644 --- a/Marechai/Pages/Admin/Licenses.razor +++ b/Marechai/Pages/Admin/Licenses.razor @@ -93,9 +93,7 @@ @L["Details"] - - @L["Edit"] - + @L["Edit"] diff --git a/Marechai/Resources/Services/LicensesService.en.resx b/Marechai/Resources/Services/LicensesService.en.resx new file mode 100644 index 00000000..d2488fdb --- /dev/null +++ b/Marechai/Resources/Services/LicensesService.en.resx @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Unknown or none + Unknown or none, referring to a license text links + + + Unknown + Unknown, referring to the license text + + \ No newline at end of file diff --git a/Marechai/Resources/Services/LicensesService.es.resx b/Marechai/Resources/Services/LicensesService.es.resx index f03d4b0a..d46dcb45 100644 --- a/Marechai/Resources/Services/LicensesService.es.resx +++ b/Marechai/Resources/Services/LicensesService.es.resx @@ -202,4 +202,48 @@ Texto de la licencia License text + + Guardar + Save + + + Desconocido o ninguno + Unknown or none, referring to a license text links + + + Desconocido + Unknown, referring to the license text + + + Por favor introduce un nombre válido. + Please enter a valid name. + + + Por favor introduce un identificador SPDX válido. + Please enter a valid SPDX identifier. + + + Por favor introduce un enlace al texto de la licencia válido. + Please enter a license text link. + + + Por favor introduce un texto de licencia válido. + Please enter a valid license text. + + + El nombre de la licencia no puede contener más de 255 caracteres. + License name cannot contain more than 255 characters. + + + El identificador SPDX no puede contener más de 255 caracteres. + SPDX identifier cannot contain more than 255 characters. + + + El enlace al texto de la licencia debe contener menos de 512 caracteres. + License text link must be smaller than 512 characters. + + + El texto de la licencia no puede contener mas de 131072 caracteres. + License text cannot contain more than 131072 characters. + \ No newline at end of file diff --git a/Marechai/Services/LicensesService.cs b/Marechai/Services/LicensesService.cs index d52a82b9..24ff1e1d 100644 --- a/Marechai/Services/LicensesService.cs +++ b/Marechai/Services/LicensesService.cs @@ -19,7 +19,29 @@ namespace Marechai.Services OsiApproved = l.OsiApproved, SPDX = l.SPDX }).ToListAsync(); - public async Task GetAsync(int id) => await _context.Licenses.FindAsync(id); + public async Task GetAsync(int id) => + await _context.Licenses.Where(l => l.Id == id).Select(l => new License + { + FsfApproved = l.FsfApproved, Id = l.Id, Link = l.Link, Name = l.Name, + OsiApproved = l.OsiApproved, SPDX = l.SPDX, Text = l.Text + }).FirstOrDefaultAsync(); + + public async Task UpdateAsync(License viewModel) + { + License model = await _context.Licenses.FindAsync(viewModel.Id); + + if(model is null) + return; + + model.FsfApproved = viewModel.FsfApproved; + model.Link = viewModel.Link; + model.Name = viewModel.Name; + model.OsiApproved = viewModel.OsiApproved; + model.SPDX = viewModel.SPDX; + model.Text = viewModel.Text; + + await _context.SaveChangesAsync(); + } public async Task DeleteAsync(int id) { diff --git a/Marechai/Shared/Validators.cs b/Marechai/Shared/Validators.cs index 5be11bd4..36f8de76 100644 --- a/Marechai/Shared/Validators.cs +++ b/Marechai/Shared/Validators.cs @@ -1,10 +1,15 @@ using System; +using System.Text.RegularExpressions; using Blazorise; +using Match = System.Text.RegularExpressions.Match; namespace Marechai.Shared { public static class Validators { + const string _urlRegex = + @"^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$"; + public static void ValidateString(ValidatorEventArgs e, string message, int maxLength) { string item = e.Value as string; @@ -31,7 +36,8 @@ namespace Marechai.Shared public static void ValidateDouble(ValidatorEventArgs e, double minValue = 0, double maxValue = double.MaxValue) { if(!(e.Value is double item) || - item < minValue || item > maxValue) + item < minValue || + item > maxValue) e.Status = ValidationStatus.Error; else e.Status = ValidationStatus.Success; @@ -40,7 +46,8 @@ namespace Marechai.Shared public static void ValidateInteger(ValidatorEventArgs e, int minValue = 0, int maxValue = int.MaxValue) { if(!(e.Value is int item) || - item < minValue || item > maxValue) + item < minValue || + item > maxValue) e.Status = ValidationStatus.Error; else e.Status = ValidationStatus.Success; @@ -49,7 +56,8 @@ namespace Marechai.Shared public static void ValidateLong(ValidatorEventArgs e, long minValue = 0, long maxValue = long.MaxValue) { if(!(e.Value is long item) || - item < minValue || item > maxValue) + item < minValue || + item > maxValue) e.Status = ValidationStatus.Error; else e.Status = ValidationStatus.Success; @@ -58,10 +66,38 @@ namespace Marechai.Shared public static void ValidateFloat(ValidatorEventArgs e, float minValue = 0, float maxValue = float.MaxValue) { if(!(e.Value is float item) || - item < minValue || item > maxValue) + item < minValue || + item > maxValue) e.Status = ValidationStatus.Error; else e.Status = ValidationStatus.Success; } + + public static void ValidateUrl(ValidatorEventArgs e, string message, int maxLength) + { + if(!(e.Value is string url)) + { + e.Status = ValidationStatus.Error; + + return; + } + + if(url.Length < 1 || + url.Length > maxLength) + { + e.ErrorText = message; + e.Status = ValidationStatus.Error; + + return; + } + + var rx = new Regex(_urlRegex); + Match m = rx.Match(url); + + if(m.Success) + return; + + e.Status = ValidationStatus.Error; + } } } \ No newline at end of file