#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> <ItemGroup>
<Compile Include="AssemblyHelper.cs" /> <Compile Include="AssemblyHelper.cs" />
<Compile Include="ByteConverterHelper.cs" /> <Compile Include="ByteConverterHelper.cs" />
<Compile Include="DateTimeHelper.cs" />
<Compile Include="Exceptions\ApplicationSettingsException.cs" /> <Compile Include="Exceptions\ApplicationSettingsException.cs" />
<Compile Include="HtmlRemover.cs" /> <Compile Include="HtmlRemover.cs" />
<Compile Include="ICacheProvider.cs" /> <Compile Include="ICacheProvider.cs" />

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

@ -28,12 +28,14 @@
using Nancy; using Nancy;
using Nancy.Extensions; using Nancy.Extensions;
using PlexRequests.UI.Models; using PlexRequests.UI.Models;
using System;
namespace PlexRequests.UI.Modules namespace PlexRequests.UI.Modules
{ {
public class BaseModule : NancyModule public class BaseModule : NancyModule
{ {
private string _username; private string _username;
private int _dateTimeOffset = -1;
protected string Username 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() public BaseModule()
{ {
Before += (ctx) => CheckAuth(); Before += (ctx) => CheckAuth();

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

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

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

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

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

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

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

Loading…
Cancel
Save