Reduce amount of raw sql

This commit is contained in:
Bond_009 2019-04-03 18:15:04 +02:00 committed by Bond_009
parent db2765aae5
commit d961278b3d
2 changed files with 38 additions and 35 deletions

View File

@ -75,10 +75,8 @@ namespace Emby.Server.Implementations.Data
private void RemoveEmptyPasswordHashes(ManagedConnection connection) private void RemoveEmptyPasswordHashes(ManagedConnection connection)
{ {
foreach (var row in connection.Query("select id,guid,data from LocalUsersv2")) foreach (var user in RetrieveAllUsers(connection))
{ {
var user = GetUser(row);
// If the user password is the sha1 hash of the empty string, remove it // If the user password is the sha1 hash of the empty string, remove it
if (!string.Equals(user.Password, "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709", StringComparison.Ordinal) if (!string.Equals(user.Password, "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709", StringComparison.Ordinal)
&& !string.Equals(user.Password, "$SHA1$DA39A3EE5E6B4B0D3255BFEF95601890AFD80709", StringComparison.Ordinal)) && !string.Equals(user.Password, "$SHA1$DA39A3EE5E6B4B0D3255BFEF95601890AFD80709", StringComparison.Ordinal))
@ -198,17 +196,22 @@ namespace Emby.Server.Implementations.Data
/// <returns>IEnumerable{User}.</returns> /// <returns>IEnumerable{User}.</returns>
public List<User> RetrieveAllUsers() public List<User> RetrieveAllUsers()
{ {
var list = new List<User>();
using (var connection = GetConnection(true)) using (var connection = GetConnection(true))
{ {
foreach (var row in connection.Query("select id,guid,data from LocalUsersv2")) return new List<User>(RetrieveAllUsers(connection));
{
list.Add(GetUser(row));
}
} }
}
return list; /// <summary>
/// Retrieve all users from the database
/// </summary>
/// <returns>IEnumerable{User}.</returns>
private IEnumerable<User> RetrieveAllUsers(ManagedConnection connection)
{
foreach (var row in connection.Query("select id,guid,data from LocalUsersv2"))
{
yield return GetUser(row);
}
} }
/// <summary> /// <summary>

View File

@ -222,9 +222,8 @@ namespace Emby.Server.Implementations.Library
public void Initialize() public void Initialize()
{ {
_users = LoadUsers(); var users = LoadUsers();
_users = users.ToArray();
var users = Users.ToList();
// If there are no local users with admin rights, make them all admins // If there are no local users with admin rights, make them all admins
if (!users.Any(i => i.Policy.IsAdministrator)) if (!users.Any(i => i.Policy.IsAdministrator))
@ -555,35 +554,36 @@ namespace Emby.Server.Implementations.Library
/// Loads the users from the repository /// Loads the users from the repository
/// </summary> /// </summary>
/// <returns>IEnumerable{User}.</returns> /// <returns>IEnumerable{User}.</returns>
private User[] LoadUsers() private List<User> LoadUsers()
{ {
var users = UserRepository.RetrieveAllUsers(); var users = UserRepository.RetrieveAllUsers();
// There always has to be at least one user. // There always has to be at least one user.
if (users.Count == 0) if (users.Count != 0)
{ {
var defaultName = Environment.UserName; return users;
if (string.IsNullOrWhiteSpace(defaultName))
{
defaultName = "MyJellyfinUser";
}
var name = MakeValidUsername(defaultName);
var user = InstantiateNewUser(name);
user.DateLastSaved = DateTime.UtcNow;
UserRepository.CreateUser(user);
users.Add(user);
user.Policy.IsAdministrator = true;
user.Policy.EnableContentDeletion = true;
user.Policy.EnableRemoteControlOfOtherUsers = true;
UpdateUserPolicy(user, user.Policy, false);
} }
return users.ToArray(); var defaultName = Environment.UserName;
if (string.IsNullOrWhiteSpace(defaultName))
{
defaultName = "MyJellyfinUser";
}
var name = MakeValidUsername(defaultName);
var user = InstantiateNewUser(name);
user.DateLastSaved = DateTime.UtcNow;
UserRepository.CreateUser(user);
user.Policy.IsAdministrator = true;
user.Policy.EnableContentDeletion = true;
user.Policy.EnableRemoteControlOfOtherUsers = true;
UpdateUserPolicy(user, user.Policy, false);
return new List<User> { user };
} }
public UserDto GetUserDto(User user, string remoteEndPoint = null) public UserDto GetUserDto(User user, string remoteEndPoint = null)