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

View file

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