2019-01-13 12:54:44 -07:00
|
|
|
using System;
|
2016-11-18 11:28:45 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using MediaBrowser.Controller;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Persistence;
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2019-01-13 12:20:16 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2016-11-18 11:28:45 -07:00
|
|
|
using SQLitePCL.pretty;
|
|
|
|
|
|
|
|
namespace Emby.Server.Implementations.Data
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class SQLiteUserRepository
|
|
|
|
/// </summary>
|
|
|
|
public class SqliteUserRepository : BaseSqliteRepository, IUserRepository
|
|
|
|
{
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
|
|
|
2019-01-17 15:55:05 -07:00
|
|
|
public SqliteUserRepository(
|
|
|
|
ILoggerFactory loggerFactory,
|
|
|
|
IServerApplicationPaths appPaths,
|
|
|
|
IJsonSerializer jsonSerializer)
|
|
|
|
: base(loggerFactory.CreateLogger(nameof(SqliteUserRepository)))
|
2016-11-18 11:28:45 -07:00
|
|
|
{
|
|
|
|
_jsonSerializer = jsonSerializer;
|
|
|
|
|
|
|
|
DbFilePath = Path.Combine(appPaths.DataPath, "users.db");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the name of the repository
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The name.</value>
|
2019-01-06 13:50:43 -07:00
|
|
|
public string Name => "SQLite";
|
2016-11-18 11:28:45 -07:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Opens the connection to the database
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
public void Initialize()
|
|
|
|
{
|
|
|
|
using (var connection = CreateConnection())
|
|
|
|
{
|
2016-11-29 12:12:37 -07:00
|
|
|
RunDefaultInitialization(connection);
|
2016-11-20 20:52:58 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
var localUsersTableExists = TableExists(connection, "LocalUsersv2");
|
2016-11-18 11:28:45 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
connection.RunQueries(new[] {
|
|
|
|
"create table if not exists LocalUsersv2 (Id INTEGER PRIMARY KEY, guid GUID NOT NULL, data BLOB NOT NULL)",
|
|
|
|
"drop index if exists idx_users"
|
|
|
|
});
|
2016-11-18 11:28:45 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
if (!localUsersTableExists && TableExists(connection, "Users"))
|
|
|
|
{
|
|
|
|
TryMigrateToLocalUsersTable(connection);
|
|
|
|
}
|
2019-02-13 01:33:00 -07:00
|
|
|
RemoveEmptyPasswordHashes();
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
}
|
2016-11-18 11:28:45 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
private void TryMigrateToLocalUsersTable(ManagedConnection connection)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
connection.RunQueries(new[]
|
|
|
|
{
|
|
|
|
"INSERT INTO LocalUsersv2 (guid, data) SELECT guid,data from users"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
Logger.LogError(ex, "Error migrating users database");
|
2016-11-18 11:28:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 01:33:00 -07:00
|
|
|
private void RemoveEmptyPasswordHashes()
|
|
|
|
{
|
|
|
|
foreach (var user in RetrieveAllUsers())
|
|
|
|
{
|
|
|
|
// If the user password is the sha1 hash of the empty string, remove it
|
|
|
|
if (!string.Equals(user.Password, "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709") || !string.Equals(user.Password, "$SHA1$DA39A3EE5E6B4B0D3255BFEF95601890AFD80709"))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Password = null;
|
|
|
|
var serialized = _jsonSerializer.SerializeToBytes(user);
|
|
|
|
|
|
|
|
using (WriteLock.Write())
|
|
|
|
using (var connection = CreateConnection())
|
|
|
|
{
|
|
|
|
connection.RunInTransaction(db =>
|
|
|
|
{
|
|
|
|
using (var statement = db.PrepareStatement("update LocalUsersv2 set data=@data where Id=@InternalId"))
|
|
|
|
{
|
|
|
|
statement.TryBind("@InternalId", user.InternalId);
|
|
|
|
statement.TryBind("@data", serialized);
|
|
|
|
statement.MoveNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
}, TransactionMode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-18 11:28:45 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Save a user in the repo
|
|
|
|
/// </summary>
|
2018-09-12 10:26:21 -07:00
|
|
|
public void CreateUser(User user)
|
2016-11-18 11:28:45 -07:00
|
|
|
{
|
|
|
|
if (user == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(user));
|
2016-11-18 11:28:45 -07:00
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
var serialized = _jsonSerializer.SerializeToBytes(user);
|
|
|
|
|
|
|
|
using (WriteLock.Write())
|
|
|
|
{
|
|
|
|
using (var connection = CreateConnection())
|
|
|
|
{
|
|
|
|
connection.RunInTransaction(db =>
|
|
|
|
{
|
|
|
|
using (var statement = db.PrepareStatement("insert into LocalUsersv2 (guid, data) values (@guid, @data)"))
|
|
|
|
{
|
|
|
|
statement.TryBind("@guid", user.Id.ToGuidBlob());
|
|
|
|
statement.TryBind("@data", serialized);
|
|
|
|
|
|
|
|
statement.MoveNext();
|
|
|
|
}
|
2016-11-18 11:28:45 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
var createdUser = GetUser(user.Id, false);
|
2016-11-18 11:28:45 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
if (createdUser == null)
|
|
|
|
{
|
|
|
|
throw new ApplicationException("created user should never be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
user.InternalId = createdUser.InternalId;
|
|
|
|
|
|
|
|
}, TransactionMode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdateUser(User user)
|
|
|
|
{
|
|
|
|
if (user == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(user));
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var serialized = _jsonSerializer.SerializeToBytes(user);
|
2016-11-18 11:28:45 -07:00
|
|
|
|
2016-12-10 22:12:00 -07:00
|
|
|
using (WriteLock.Write())
|
2016-11-18 11:28:45 -07:00
|
|
|
{
|
2016-12-10 22:12:00 -07:00
|
|
|
using (var connection = CreateConnection())
|
2016-11-18 11:28:45 -07:00
|
|
|
{
|
|
|
|
connection.RunInTransaction(db =>
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
using (var statement = db.PrepareStatement("update LocalUsersv2 set data=@data where Id=@InternalId"))
|
2016-11-19 22:59:36 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
statement.TryBind("@InternalId", user.InternalId);
|
2016-11-20 00:10:07 -07:00
|
|
|
statement.TryBind("@data", serialized);
|
2016-11-19 22:59:36 -07:00
|
|
|
statement.MoveNext();
|
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2016-11-28 12:26:48 -07:00
|
|
|
}, TransactionMode);
|
2016-11-18 11:28:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
private User GetUser(Guid guid, bool openLock)
|
|
|
|
{
|
|
|
|
using (openLock ? WriteLock.Read() : null)
|
|
|
|
{
|
|
|
|
using (var connection = CreateConnection(true))
|
|
|
|
{
|
|
|
|
using (var statement = connection.PrepareStatement("select id,guid,data from LocalUsersv2 where guid=@guid"))
|
|
|
|
{
|
|
|
|
statement.TryBind("@guid", guid);
|
|
|
|
|
|
|
|
foreach (var row in statement.ExecuteQuery())
|
|
|
|
{
|
|
|
|
return GetUser(row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private User GetUser(IReadOnlyList<IResultSetValue> row)
|
|
|
|
{
|
|
|
|
var id = row[0].ToInt64();
|
|
|
|
var guid = row[1].ReadGuidFromBlob();
|
|
|
|
|
|
|
|
using (var stream = new MemoryStream(row[2].ToBlob()))
|
|
|
|
{
|
|
|
|
stream.Position = 0;
|
|
|
|
var user = _jsonSerializer.DeserializeFromStream<User>(stream);
|
|
|
|
user.InternalId = id;
|
|
|
|
user.Id = guid;
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 11:28:45 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Retrieve all users from the database
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>IEnumerable{User}.</returns>
|
2018-09-12 10:26:21 -07:00
|
|
|
public List<User> RetrieveAllUsers()
|
2016-11-18 11:28:45 -07:00
|
|
|
{
|
|
|
|
var list = new List<User>();
|
|
|
|
|
2016-12-10 22:12:00 -07:00
|
|
|
using (WriteLock.Read())
|
2016-11-18 11:28:45 -07:00
|
|
|
{
|
2016-12-10 22:12:00 -07:00
|
|
|
using (var connection = CreateConnection(true))
|
2016-11-18 11:28:45 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
foreach (var row in connection.Query("select id,guid,data from LocalUsersv2"))
|
2016-11-18 11:28:45 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
list.Add(GetUser(row));
|
2016-11-18 11:28:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Deletes the user.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="user">The user.</param>
|
|
|
|
/// <returns>Task.</returns>
|
2019-01-13 13:37:13 -07:00
|
|
|
/// <exception cref="ArgumentNullException">user</exception>
|
2018-09-12 10:26:21 -07:00
|
|
|
public void DeleteUser(User user)
|
2016-11-18 11:28:45 -07:00
|
|
|
{
|
|
|
|
if (user == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(user));
|
2016-11-18 11:28:45 -07:00
|
|
|
}
|
|
|
|
|
2016-12-10 22:12:00 -07:00
|
|
|
using (WriteLock.Write())
|
2016-11-18 11:28:45 -07:00
|
|
|
{
|
2016-12-10 22:12:00 -07:00
|
|
|
using (var connection = CreateConnection())
|
2016-11-18 11:28:45 -07:00
|
|
|
{
|
|
|
|
connection.RunInTransaction(db =>
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
using (var statement = db.PrepareStatement("delete from LocalUsersv2 where Id=@id"))
|
2016-11-19 22:59:36 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
statement.TryBind("@id", user.InternalId);
|
2016-11-19 22:59:36 -07:00
|
|
|
statement.MoveNext();
|
|
|
|
}
|
2016-11-28 12:26:48 -07:00
|
|
|
}, TransactionMode);
|
2016-11-18 11:28:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 06:18:25 -07:00
|
|
|
}
|