diff --git a/PlexRequests.Core/JsonRequestService.cs b/PlexRequests.Core/JsonRequestService.cs index 533bc1c56..1504faed6 100644 --- a/PlexRequests.Core/JsonRequestService.cs +++ b/PlexRequests.Core/JsonRequestService.cs @@ -30,6 +30,7 @@ using System.Text; using Newtonsoft.Json; +using PlexRequests.Helpers; using PlexRequests.Store; using PlexRequests.Store.Models; using PlexRequests.Store.Repository; @@ -45,13 +46,13 @@ namespace PlexRequests.Core private IRequestRepository Repo { get; } public long AddRequest(RequestedModel model) { - var entity = new RequestBlobs { Type = model.Type, Content = ReturnBytes(model), ProviderId = model.ProviderId }; + var entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId }; var id = Repo.Insert(entity); // TODO Keep an eye on this, since we are now doing 2 DB update for 1 single request, inserting and then updating model.Id = (int)id; - entity = new RequestBlobs { Type = model.Type, Content = ReturnBytes(model), ProviderId = model.ProviderId, Id = (int)id }; + entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId, Id = (int)id }; var result = Repo.Update(entity); return result ? id : -1; @@ -71,15 +72,14 @@ namespace PlexRequests.Core public bool UpdateRequest(RequestedModel model) { - var entity = new RequestBlobs { Type = model.Type, Content = ReturnBytes(model), ProviderId = model.ProviderId, Id = model.Id }; + var entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId, Id = model.Id }; return Repo.Update(entity); } public RequestedModel Get(int id) { var blob = Repo.Get(id); - var json = Encoding.UTF8.GetString(blob.Content); - var model = JsonConvert.DeserializeObject(json); + var model = ByteConverterHelper.ReturnObject(blob.Content); return model; } @@ -93,16 +93,8 @@ namespace PlexRequests.Core public bool BatchUpdate(List model) { - var entities = model.Select(m => new RequestBlobs { Type = m.Type, Content = ReturnBytes(m), ProviderId = m.ProviderId, Id = m.Id }).ToList(); + var entities = model.Select(m => new RequestBlobs { Type = m.Type, Content = ByteConverterHelper.ReturnBytes(m), ProviderId = m.ProviderId, Id = m.Id }).ToList(); return Repo.UpdateAll(entities); } - - public byte[] ReturnBytes(object obj) - { - var json = JsonConvert.SerializeObject(obj); - var bytes = Encoding.UTF8.GetBytes(json); - - return bytes; - } } } \ No newline at end of file diff --git a/PlexRequests.Core/Models/UserProperties.cs b/PlexRequests.Core/Models/UserProperties.cs new file mode 100644 index 000000000..8cd210d57 --- /dev/null +++ b/PlexRequests.Core/Models/UserProperties.cs @@ -0,0 +1,35 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: UserProperties.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +namespace PlexRequests.Core.Models +{ + public class UserProperties + { + public string EmailAddress { get; set; } + public bool NotifyOnRelease { get; set; } + public bool NotifyOnApprove { get; set; } + } +} \ No newline at end of file diff --git a/PlexRequests.Core/PlexRequests.Core.csproj b/PlexRequests.Core/PlexRequests.Core.csproj index 05bef9e03..a1e0538f7 100644 --- a/PlexRequests.Core/PlexRequests.Core.csproj +++ b/PlexRequests.Core/PlexRequests.Core.csproj @@ -72,6 +72,7 @@ + diff --git a/PlexRequests.Core/UserMapper.cs b/PlexRequests.Core/UserMapper.cs index 6bacbdca9..211145069 100644 --- a/PlexRequests.Core/UserMapper.cs +++ b/PlexRequests.Core/UserMapper.cs @@ -25,6 +25,7 @@ // ************************************************************************/ #endregion using System; +using System.Collections.Generic; using System.Linq; using System.Security; @@ -32,6 +33,7 @@ using Nancy; using Nancy.Authentication.Forms; using Nancy.Security; +using PlexRequests.Core.Models; using PlexRequests.Helpers; using PlexRequests.Store; @@ -46,7 +48,7 @@ namespace PlexRequests.Core private static ISqliteConfiguration Db { get; set; } public IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context) { - var repo = new UserRepository(Db); + var repo = new UserRepository(Db); var user = repo.Get(identifier.ToString()); @@ -58,6 +60,7 @@ namespace PlexRequests.Core return new UserIdentity { UserName = user.UserName, + Claims = ByteConverterHelper.ReturnObject(user.Claims) }; } @@ -84,7 +87,7 @@ namespace PlexRequests.Core { var repo = new UserRepository(Db); var users = repo.GetAll(); - + return users.Any(); } @@ -93,15 +96,23 @@ namespace PlexRequests.Core var repo = new UserRepository(Db); var salt = PasswordHasher.GenerateSalt(); - var userModel = new UsersModel { UserName = username, UserGuid = Guid.NewGuid().ToString(), Salt = salt, Hash = PasswordHasher.ComputeHash(password, salt), Claims = claims}; + var userModel = new UsersModel + { + UserName = username, + UserGuid = Guid.NewGuid().ToString(), + Salt = salt, + Hash = PasswordHasher.ComputeHash(password, salt), + Claims = JsonRequestService.ReturnBytes(claims), + UserProperties = JsonRequestService.ReturnBytes(new UserProperties()) + }; repo.Insert(userModel); var userRecord = repo.Get(userModel.UserGuid); - + return new Guid(userRecord.UserGuid); } - public static bool UpdateUser(string username, string oldPassword, string newPassword) + public static bool UpdatePassword(string username, string oldPassword, string newPassword) { var repo = new UserRepository(Db); var users = repo.GetAll(); @@ -123,5 +134,11 @@ namespace PlexRequests.Core return repo.Update(userToChange); } + + public static IEnumerable GetUsers() + { + var repo = new UserRepository(Db); + return repo.GetAll(); + } } } diff --git a/PlexRequests.Helpers/ByteConverterHelper.cs b/PlexRequests.Helpers/ByteConverterHelper.cs new file mode 100644 index 000000000..87d569592 --- /dev/null +++ b/PlexRequests.Helpers/ByteConverterHelper.cs @@ -0,0 +1,54 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: ByteConverterHelper.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using System.Text; + +using Newtonsoft.Json; + +namespace PlexRequests.Helpers +{ + public class ByteConverterHelper + { + public static byte[] ReturnBytes(object obj) + { + var json = JsonConvert.SerializeObject(obj); + var bytes = Encoding.UTF8.GetBytes(json); + + return bytes; + } + + public static T ReturnObject(byte[] bytes) + { + var json = Encoding.UTF8.GetString(bytes); + var model = JsonConvert.DeserializeObject(json); + return model; + } + public static string ReturnFromBytes(byte[] bytes) + { + return Encoding.UTF8.GetString(bytes); + } + } +} \ No newline at end of file diff --git a/PlexRequests.Helpers/PlexRequests.Helpers.csproj b/PlexRequests.Helpers/PlexRequests.Helpers.csproj index 6af45a9b5..ec3c70a8d 100644 --- a/PlexRequests.Helpers/PlexRequests.Helpers.csproj +++ b/PlexRequests.Helpers/PlexRequests.Helpers.csproj @@ -51,6 +51,7 @@ + diff --git a/PlexRequests.Store/SqlTables.sql b/PlexRequests.Store/SqlTables.sql index 657c2ea60..f23b3c5fc 100644 --- a/PlexRequests.Store/SqlTables.sql +++ b/PlexRequests.Store/SqlTables.sql @@ -7,7 +7,8 @@ CREATE TABLE IF NOT EXISTS Users UserName varchar(50) NOT NULL, Salt BLOB NOT NULL, Hash BLOB NOT NULL, - Claims BLOB NOT NULL + Claims BLOB NOT NULL, + UserProperties BLOB ); diff --git a/PlexRequests.Store/UsersModel.cs b/PlexRequests.Store/UsersModel.cs index cf5eec764..8a3753c6c 100644 --- a/PlexRequests.Store/UsersModel.cs +++ b/PlexRequests.Store/UsersModel.cs @@ -33,6 +33,7 @@ namespace PlexRequests.Store { public byte[] Hash { get; set; } public byte[] Salt { get; set; } - public string[] Claims { get; set; } + public byte[] Claims { get; set; } + public byte[] UserProperties { get; set; } } } diff --git a/PlexRequests.UI/Modules/LoginModule.cs b/PlexRequests.UI/Modules/LoginModule.cs index 36f0619c4..5436be973 100644 --- a/PlexRequests.UI/Modules/LoginModule.cs +++ b/PlexRequests.UI/Modules/LoginModule.cs @@ -49,7 +49,7 @@ namespace PlexRequests.UI.Modules model.Errored = Request.Query.error.HasValue; var adminCreated = UserMapper.DoUsersExist(); model.AdminExists = adminCreated; - return View["Login/Index", model]; + return View["Index", model]; } }; @@ -82,7 +82,7 @@ namespace PlexRequests.UI.Modules dynamic model = new ExpandoObject(); model.Errored = Request.Query.error.HasValue; - return View["Login/Register", model]; + return View["Register", model]; } }; @@ -109,7 +109,7 @@ namespace PlexRequests.UI.Modules return View["ChangePassword"]; } - private Negotiator ChangePasswordPost() + private Response ChangePasswordPost() { var username = Context.CurrentUser.UserName; var oldPass = Request.Form.OldPassword; @@ -117,11 +117,16 @@ namespace PlexRequests.UI.Modules var newPasswordAgain = Request.Form.NewPasswordAgain; if (!newPassword.Equals(newPasswordAgain)) { + return Response.AsJson(new JsonResponseModel { Message = "The passwords do not match", Result = false }); + } + var result = UserMapper.UpdatePassword(username, oldPass, newPassword); + if (result) + { + return Response.AsJson(new JsonResponseModel { Message = "Password has been changed!", Result = true }); } - var result = UserMapper.UpdateUser(username, oldPass, newPassword); - return View["ChangePassword"]; + return Response.AsJson(new JsonResponseModel { Message = "Could not update the password in the database", Result = false }); } } } \ No newline at end of file diff --git a/PlexRequests.UI/Views/Login/ChangePassword.cshtml b/PlexRequests.UI/Views/Login/ChangePassword.cshtml index f87dfa76e..73099f67b 100644 --- a/PlexRequests.UI/Views/Login/ChangePassword.cshtml +++ b/PlexRequests.UI/Views/Login/ChangePassword.cshtml @@ -1,9 +1,37 @@ -
+
Old Password New Password New Password again

- +
+ + \ No newline at end of file diff --git a/PlexRequests.UI/Views/Shared/_Layout.cshtml b/PlexRequests.UI/Views/Shared/_Layout.cshtml index b96e89b20..440f12a10 100644 --- a/PlexRequests.UI/Views/Shared/_Layout.cshtml +++ b/PlexRequests.UI/Views/Shared/_Layout.cshtml @@ -72,9 +72,9 @@ }