Add validation when adding calendar items

This commit is contained in:
Sijmen 2021-07-04 12:28:21 +02:00
parent 64fb38e1ad
commit 6c696c7821
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48
2 changed files with 27 additions and 22 deletions

View file

@ -28,10 +28,11 @@ namespace BlazorApp.Data
public string Password { get; set; } public string Password { get; set; }
public string Name { get; set; } [Required] public string Name { get; set; }
private string AuthorizationHeader => private string AuthorizationHeader => Username == null || Password == null
$"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Username}:{Password}"))}"; ? null
: $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Username}:{Password}"))}";
private static DateTime _parseDateTime(string s) private static DateTime _parseDateTime(string s)
{ {
@ -49,14 +50,14 @@ namespace BlazorApp.Data
.Replace("\n ", "") .Replace("\n ", "")
.Replace("\n\t", "") .Replace("\n\t", "")
.Replace("\r", ""); .Replace("\r", "");
foreach (var line in body.Split("\n")) foreach (var line in body.Split("\n"))
{ {
var trimmed = line.Trim(); var trimmed = line.Trim();
var split = trimmed.Split(':', 2); var split = trimmed.Split(':', 2);
if (split.Length != 2) if (split.Length != 2)
continue; continue;
var left = split[0].Trim().Split(';'); var left = split[0].Trim().Split(';');
var name = left[0]; var name = left[0];
@ -119,10 +120,12 @@ namespace BlazorApp.Data
public async Task<List<Event>> GetEvents() public async Task<List<Event>> GetEvents()
{ {
var client = new HttpClient(); var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", AuthorizationHeader); if (AuthorizationHeader != null)
client.DefaultRequestHeaders.Add("Authorization", AuthorizationHeader);
var body = await client.GetStringAsync(Url); var body = await client.GetStringAsync(Url);
var events = ParseICal(body); var events = ParseICal(body);
var now = DateTime.Now; var now = DateTime.Now;
events = events.FindAll(@event => @event.DtStart > now || @event.DtEnd > now); events = events.FindAll(@event => @event.DtStart > now || @event.DtEnd > now);
@ -132,7 +135,7 @@ namespace BlazorApp.Data
return -1; return -1;
if (y.DtStart == null) if (y.DtStart == null)
return 1; return 1;
return DateTime.Compare(x.DtStart.Value, y.DtStart.Value); return DateTime.Compare(x.DtStart.Value, y.DtStart.Value);
}); });
@ -145,11 +148,12 @@ namespace BlazorApp.Data
public async Task<Event> GetEventByUid(string uid) public async Task<Event> GetEventByUid(string uid)
{ {
var client = new HttpClient(); var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", AuthorizationHeader); if (AuthorizationHeader != null)
client.DefaultRequestHeaders.Add("Authorization", AuthorizationHeader);
var request = new HttpRequestMessage(new HttpMethod("REPORT"), Url); var request = new HttpRequestMessage(new HttpMethod("REPORT"), Url);
request.Headers.Add("Depth", "1"); request.Headers.Add("Depth", "1");
// TODO Use XML generator to safely add UID - this is sensitive to scary injection stuff // TODO Use XML generator to safely add UID - this is sensitive to scary injection stuff
request.Content = new StringContent(@$"<?xml version=""1.0"" encoding=""utf-8"" ?> request.Content = new StringContent(@$"<?xml version=""1.0"" encoding=""utf-8"" ?>
<C:calendar-query xmlns:C=""urn:ietf:params:xml:ns:caldav""> <C:calendar-query xmlns:C=""urn:ietf:params:xml:ns:caldav"">

View file

@ -18,16 +18,18 @@ else
{ {
<li> <li>
<a href="/calendars/@calendar.Id">@calendar.Name</a> <a href="/calendars/@calendar.Id">@calendar.Name</a>
(<a href="#" @onclick="() => DeleteCalendar(calendar)"> (
<a href="#" @onclick="() => DeleteCalendar(calendar)">
@if (_confirmDeleteId == calendar.Id) @if (_confirmDeleteId == calendar.Id)
{ {
<span>Confirm deletion?</span> @:Confirm deletion?
} }
else else
{ {
<span class="oi oi-x"></span> <span class="oi oi-x"></span>
} }
</a>) </a>
)
</li> </li>
} }
</ul> </ul>
@ -37,33 +39,33 @@ else
<h2>Add New Calendar</h2> <h2>Add New Calendar</h2>
<EditForm Model="@_calendarModel" OnValidSubmit="@HandleValidSubmit"> <EditForm Model="@_calendarModel" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator/>
<ValidationSummary/>
<div class="form-group"> <div class="form-group">
<label for="url">URL</label> <label for="url">URL</label>
<InputText id="url" class="form-control" placeholder="Enter URL" @bind-Value="_calendarModel.Url" /> <InputText id="url" class="form-control" placeholder="Enter URL" @bind-Value="_calendarModel.Url"/>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="username">Username</label> <label for="username">Username</label>
<InputText <InputText
id="username" class="form-control" placeholder="Enter username" id="username" class="form-control" placeholder="Enter username"
@bind-Value="_calendarModel.Username" @bind-Value="_calendarModel.Username"/>
/>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="password">Password</label> <label for="password">Password</label>
<InputText <InputText
type="password" id="password" class="form-control" placeholder="Enter password" type="password" id="password" class="form-control" placeholder="Enter password"
@bind-Value="_calendarModel.Password" @bind-Value="_calendarModel.Password"/>
/>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="name">Name</label> <label for="name">Name</label>
<InputText <InputText
type="text" id="name" class="form-control" placeholder="Enter name" type="text" id="name" class="form-control" placeholder="Enter name"
@bind-Value="_calendarModel.Name" @bind-Value="_calendarModel.Name"/>
/>
</div> </div>
<button type="submit" class="btn btn-primary">Submit</button> <button type="submit" class="btn btn-primary">Submit</button>
@ -95,7 +97,6 @@ else
{ {
await CalendarService.CreateCalendar(_calendarModel); await CalendarService.CreateCalendar(_calendarModel);
_calendarModel = null; _calendarModel = null;
_calendars = await CalendarService.GetCalendars(); _calendars = await CalendarService.GetCalendars();
} }