#122 store utc time in the databse + obtain timezone offset of the client upon login + offset times returned to client based on session offset

pull/130/head
Drewster727 9 years ago
parent ec99dafa29
commit 3dbef199aa

@ -0,0 +1,14 @@
using System;
namespace PlexRequests.Helpers
{
public static class DateTimeHelper
{
public static DateTimeOffset OffsetUTCDateTime(DateTime utcDateTime, int minuteOffset)
{
TimeSpan ts = TimeSpan.FromMinutes(-minuteOffset);
return new DateTimeOffset(utcDateTime).ToOffset(ts);
}
}
}

@ -52,6 +52,7 @@
<ItemGroup>
<Compile Include="AssemblyHelper.cs" />
<Compile Include="ByteConverterHelper.cs" />
<Compile Include="DateTimeHelper.cs" />
<Compile Include="Exceptions\ApplicationSettingsException.cs" />
<Compile Include="HtmlRemover.cs" />
<Compile Include="ICacheProvider.cs" />

@ -29,5 +29,6 @@ namespace PlexRequests.UI.Models
public class SessionKeys
{
public const string UsernameKey = "Username";
public const string ClientDateTimeOffsetKey = "ClientDateTimeOffset";
}
}

@ -28,12 +28,14 @@
using Nancy;
using Nancy.Extensions;
using PlexRequests.UI.Models;
using System;
namespace PlexRequests.UI.Modules
{
public class BaseModule : NancyModule
{
private string _username;
private int _dateTimeOffset = -1;
protected string Username
{
@ -47,6 +49,19 @@ namespace PlexRequests.UI.Modules
}
}
protected int DateTimeOffset
{
get
{
if (_dateTimeOffset == -1)
{
_dateTimeOffset = Session[SessionKeys.ClientDateTimeOffsetKey] != null ?
(int)Session[SessionKeys.ClientDateTimeOffsetKey] : (new DateTimeOffset().Offset).Minutes;
}
return _dateTimeOffset;
}
}
public BaseModule()
{
Before += (ctx) => CheckAuth();

@ -60,6 +60,7 @@ namespace PlexRequests.UI.Modules
{
var username = (string)Request.Form.Username;
var password = (string)Request.Form.Password;
var dtOffset = (int)Request.Form.DateTimeOffset;
var userId = UserMapper.ValidateUser(username, password);
@ -72,7 +73,7 @@ namespace PlexRequests.UI.Modules
{
expiry = DateTime.Now.AddDays(7);
}
Session[SessionKeys.UsernameKey] = username;
Session[SessionKeys.ClientDateTimeOffsetKey] = dtOffset;
return this.LoginAndRedirect(userId.Value, expiry);
};

@ -40,12 +40,12 @@ using PlexRequests.Services.Interfaces;
using PlexRequests.Services.Notification;
using PlexRequests.Store;
using PlexRequests.UI.Models;
using PlexRequests.Helpers;
namespace PlexRequests.UI.Modules
{
public class RequestsModule : BaseModule
{
public RequestsModule(IRequestService service, ISettingsService<PlexRequestSettings> prSettings, ISettingsService<PlexSettings> plex, INotificationService notify) : base("requests")
{
Service = service;
@ -98,8 +98,8 @@ namespace PlexRequests.UI.Modules
PosterPath = movie.PosterPath,
ReleaseDate = movie.ReleaseDate.Humanize(),
ReleaseDateTicks = movie.ReleaseDate.Ticks,
RequestedDate = movie.RequestedDate.Humanize(),
RequestedDateTicks = movie.RequestedDate.Ticks,
RequestedDate = DateTimeHelper.OffsetUTCDateTime(movie.RequestedDate, DateTimeOffset).Humanize(),
RequestedDateTicks = DateTimeHelper.OffsetUTCDateTime(movie.RequestedDate, DateTimeOffset).Ticks,
Approved = movie.Available || movie.Approved,
Title = movie.Title,
Overview = movie.Overview,
@ -137,8 +137,8 @@ namespace PlexRequests.UI.Modules
PosterPath = tv.PosterPath,
ReleaseDate = tv.ReleaseDate.Humanize(),
ReleaseDateTicks = tv.ReleaseDate.Ticks,
RequestedDate = tv.RequestedDate.Humanize(),
RequestedDateTicks = tv.RequestedDate.Ticks,
RequestedDate = DateTimeHelper.OffsetUTCDateTime(tv.RequestedDate, DateTimeOffset).Humanize(),
RequestedDateTicks = DateTimeHelper.OffsetUTCDateTime(tv.RequestedDate, DateTimeOffset).Ticks,
Approved = tv.Available || tv.Approved,
Title = tv.Title,
Overview = tv.Overview,

@ -220,7 +220,7 @@ namespace PlexRequests.UI.Modules
Title = movieInfo.Title,
ReleaseDate = movieInfo.ReleaseDate ?? DateTime.MinValue,
Status = movieInfo.Status,
RequestedDate = DateTime.Now,
RequestedDate = DateTime.UtcNow,
Approved = false,
RequestedUsers = new List<string>() { Username },
Issues = IssueState.None,
@ -356,7 +356,7 @@ namespace PlexRequests.UI.Modules
Title = showInfo.name,
ReleaseDate = firstAir,
Status = showInfo.status,
RequestedDate = DateTime.Now,
RequestedDate = DateTime.UtcNow,
Approved = false,
RequestedUsers = new List<string>() { Username },
Issues = IssueState.None,

@ -68,6 +68,7 @@ namespace PlexRequests.UI.Modules
private Response LoginUser()
{
var dateTimeOffset = Request.Form.DateTimeOffset;
var username = Request.Form.username.Value;
Log.Debug("Username \"{0}\" attempting to login",username);
if (string.IsNullOrWhiteSpace(username))
@ -138,6 +139,12 @@ namespace PlexRequests.UI.Modules
Session[SessionKeys.UsernameKey] = (string)username;
}
Session[SessionKeys.ClientDateTimeOffsetKey] = (int)dateTimeOffset;
int z = 240;
return Response.AsJson(authenticated
? new JsonResponseModel { Result = true }
: new JsonResponseModel { Result = false, Message = "Incorrect User or Password"});

@ -6,6 +6,7 @@
Remember Me <input name="RememberMe" type="checkbox" value="True"/>
<br/><br/>
<input class="btn btn-success-outline" type="submit" value="Login"/>
<input type="hidden" id="DateTimeOffset" name="DateTimeOffset" />
</form>
@if (!Model.AdminExists)
{
@ -19,3 +20,9 @@
</div>
}
<script>
$(function () {
var dtOffset = new Date().getTimezoneOffset();
$('#DateTimeOffset').val(dtOffset);
});
</script>

@ -1,7 +1,7 @@
<form method="POST">
Username <input class="form-control" type="text" name="Username" />
Username <input class="form-control form-control-custom" type="text" name="Username" />
<br />
Password <input class="form-control" name="Password" type="password" />
Password <input class="form-control form-control-custom" name="Password" type="password" />
<br />
<br />
<input class="btn btn-success-outline" type="submit" value="Create User" />

@ -38,10 +38,14 @@
$('#loginBtn').click(function (e) {
e.preventDefault();
var $form = $("#loginForm");
var formData = $form.serialize();
var dtOffset = new Date().getTimezoneOffset();
formData += ('&DateTimeOffset=' + dtOffset)
$.ajax({
type: $form.prop("method"),
url: $form.prop("action"),
data: $form.serialize(),
data: formData,
dataType: "json",
success: function (response) {
console.log(response);

Loading…
Cancel
Save