diff --git a/PlexRequests.Core/Setup.cs b/PlexRequests.Core/Setup.cs index 8bdf2faa6..740bc3df1 100644 --- a/PlexRequests.Core/Setup.cs +++ b/PlexRequests.Core/Setup.cs @@ -61,6 +61,10 @@ namespace PlexRequests.Core { MigrateToVersion1700(); } + if (version > 1800 && version <= 1899) + { + MigrateToVersion1800(); + } } return Db.DbConnection().ConnectionString; @@ -173,5 +177,32 @@ namespace PlexRequests.Core TableCreation.DropTable(Db.DbConnection(), "User"); TableCreation.DropTable(Db.DbConnection(), "Log"); } + + /// + /// Migrates to version 1.8. + /// This includes updating the admin account to have all roles. + /// + private void MigrateToVersion1800() + { + try + { + var userMapper = new UserMapper(new UserRepository(Db)); + var users = userMapper.GetUsers(); + + foreach (var u in users) + { + var claims = new[] { UserClaims.User, UserClaims.Admin, UserClaims.PowerUser }; + u.Claims = ByteConverterHelper.ReturnBytes(claims); + + userMapper.EditUser(u); + } + } + catch (Exception e) + { + Log.Error(e); + throw; + } + + } } } diff --git a/PlexRequests.Core/UserMapper.cs b/PlexRequests.Core/UserMapper.cs index eb66cbea3..909c3a34c 100644 --- a/PlexRequests.Core/UserMapper.cs +++ b/PlexRequests.Core/UserMapper.cs @@ -81,6 +81,16 @@ namespace PlexRequests.Core return null; } + public UsersModel EditUser(UsersModel user) + { + var existingUser = Repo.Get(user.UserGuid); + + user.Id = existingUser.Id; + user.UserGuid = existingUser.UserGuid; + Repo.Update(user); + return user; + } + public bool DoUsersExist() { var users = Repo.GetAll(); @@ -88,7 +98,7 @@ namespace PlexRequests.Core return users.Any(); } - public Guid? CreateUser(string username, string password, string[] claims = default(string[])) + private Guid? CreateUser(string username, string password, string[] claims = default(string[])) { var salt = PasswordHasher.GenerateSalt(); @@ -108,6 +118,21 @@ namespace PlexRequests.Core return new Guid(userRecord.UserGuid); } + public Guid? CreateAdmin(string username, string password) + { + return CreateUser(username, password, new[] { UserClaims.User, UserClaims.PowerUser, UserClaims.Admin }); + } + + public Guid? CreatePowerUser(string username, string password) + { + return CreateUser(username, password, new[] { UserClaims.User, UserClaims.PowerUser }); + } + + public Guid? CreateRegularUser(string username, string password) + { + return CreateUser(username, password, new[] { UserClaims.User }); + } + public bool UpdatePassword(string username, string oldPassword, string newPassword) { var users = Repo.GetAll(); @@ -134,15 +159,25 @@ namespace PlexRequests.Core { return Repo.GetAll(); } + + public UsersModel GetUser(Guid userId) + { + var user = Repo.Get(userId.ToString()); + return user; + } } public interface ICustomUserMapper { IEnumerable GetUsers(); - Guid? CreateUser(string username, string password, string[] claims = default(string[])); + UsersModel GetUser(Guid userId); + UsersModel EditUser(UsersModel user); bool DoUsersExist(); Guid? ValidateUser(string username, string password); bool UpdatePassword(string username, string oldPassword, string newPassword); + Guid? CreateAdmin(string username, string password); + Guid? CreatePowerUser(string username, string password); + Guid? CreateRegularUser(string username, string password); } } diff --git a/PlexRequests.UI/Modules/LoginModule.cs b/PlexRequests.UI/Modules/LoginModule.cs index 326b7ec13..8e6a9d39c 100644 --- a/PlexRequests.UI/Modules/LoginModule.cs +++ b/PlexRequests.UI/Modules/LoginModule.cs @@ -35,6 +35,7 @@ using Nancy.Security; using PlexRequests.Core; using PlexRequests.Core.SettingModels; +using PlexRequests.Helpers; using PlexRequests.UI.Models; namespace PlexRequests.UI.Modules @@ -103,7 +104,7 @@ namespace PlexRequests.UI.Modules { return Context.GetRedirect(!string.IsNullOrEmpty(BaseUrl) ? $"~/{BaseUrl}/register?error=true" : "~/register?error=true"); } - var userId = UserMapper.CreateUser(username, Request.Form.Password, new[] { "Admin" }); + var userId = UserMapper.CreateAdmin(username, Request.Form.Password); Session[SessionKeys.UsernameKey] = username; return this.LoginAndRedirect((Guid)userId); }; diff --git a/PlexRequests.UI/Modules/UserManagementModule.cs b/PlexRequests.UI/Modules/UserManagementModule.cs index c93977cd7..0d6dee073 100644 --- a/PlexRequests.UI/Modules/UserManagementModule.cs +++ b/PlexRequests.UI/Modules/UserManagementModule.cs @@ -46,24 +46,24 @@ namespace PlexRequests.UI.Modules return Response.AsJson(users); } - private Response CreateUser(string username, string password, string claims) - { - if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password)) - { - return Response.AsJson(new JsonResponseModel - { - Result = true, - Message = "Please enter in a valid Username and Password" - }); - } - var user = UserMapper.CreateUser(username, password, new string[] {claims}); - if (user.HasValue) - { - return Response.AsJson(new JsonResponseModel {Result = true}); - } + //private Response CreateUser(string username, string password, string claims) + //{ + // if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password)) + // { + // return Response.AsJson(new JsonResponseModel + // { + // Result = true, + // Message = "Please enter in a valid Username and Password" + // }); + // } + // var user = UserMapper.CreateUser(username, password, new string[] {claims}); + // if (user.HasValue) + // { + // return Response.AsJson(new JsonResponseModel {Result = true}); + // } - return Response.AsJson(new JsonResponseModel {Result = false, Message = "Could not save user"}); - } + // return Response.AsJson(new JsonResponseModel {Result = false, Message = "Could not save user"}); + //} } }