Set the admin to have all claims

pull/284/head
tidusjar 9 years ago
parent a679f1a6a1
commit 16bd4f5b71

@ -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");
}
/// <summary>
/// Migrates to version 1.8.
/// <para>This includes updating the admin account to have all roles.</para>
/// </summary>
private void MigrateToVersion1800()
{
try
{
var userMapper = new UserMapper(new UserRepository<UsersModel>(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;
}
}
}
}

@ -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<UsersModel> 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);
}
}

@ -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);
};

@ -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"});
//}
}
}

Loading…
Cancel
Save