2019-02-20 09:17:30 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2019-09-25 15:43:20 +00:00
|
|
|
using System.Text.Json;
|
|
|
|
using MediaBrowser.Common.Json;
|
2019-02-20 09:17:30 +00:00
|
|
|
using MediaBrowser.Controller;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Persistence;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using SQLitePCL.pretty;
|
|
|
|
|
|
|
|
namespace Emby.Server.Implementations.Data
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class SQLiteUserRepository
|
|
|
|
/// </summary>
|
|
|
|
public class SqliteUserRepository : BaseSqliteRepository, IUserRepository
|
|
|
|
{
|
2019-09-25 15:43:20 +00:00
|
|
|
private readonly JsonSerializerOptions _jsonOptions;
|
2019-02-20 09:17:30 +00:00
|
|
|
|
|
|
|
public SqliteUserRepository(
|
2019-08-14 18:35:36 +00:00
|
|
|
ILogger<SqliteUserRepository> logger,
|
2019-09-25 15:43:20 +00:00
|
|
|
IServerApplicationPaths appPaths)
|
2019-08-14 18:35:36 +00:00
|
|
|
: base(logger)
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
2019-09-25 15:43:20 +00:00
|
|
|
_jsonOptions = JsonDefaults.GetOptions();;
|
2019-02-20 09:17:30 +00:00
|
|
|
|
|
|
|
DbFilePath = Path.Combine(appPaths.DataPath, "users.db");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the name of the repository
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The name.</value>
|
|
|
|
public string Name => "SQLite";
|
|
|
|
|
|
|
|
/// <summary>
|
2019-08-19 15:03:21 +00:00
|
|
|
/// Opens the connection to the database.
|
2019-02-20 09:17:30 +00:00
|
|
|
/// </summary>
|
|
|
|
public void Initialize()
|
|
|
|
{
|
2019-02-26 17:30:13 +00:00
|
|
|
using (var connection = GetConnection())
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
|
|
|
var localUsersTableExists = TableExists(connection, "LocalUsersv2");
|
|
|
|
|
|
|
|
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"
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!localUsersTableExists && TableExists(connection, "Users"))
|
|
|
|
{
|
|
|
|
TryMigrateToLocalUsersTable(connection);
|
2019-02-20 08:00:26 +00:00
|
|
|
}
|
2019-02-20 09:17:30 +00:00
|
|
|
|
2019-04-03 12:22:17 +00:00
|
|
|
RemoveEmptyPasswordHashes(connection);
|
2019-02-20 09:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 17:30:13 +00:00
|
|
|
private void TryMigrateToLocalUsersTable(ManagedConnection connection)
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
connection.RunQueries(new[]
|
|
|
|
{
|
|
|
|
"INSERT INTO LocalUsersv2 (guid, data) SELECT guid,data from users"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Logger.LogError(ex, "Error migrating users database");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 12:22:17 +00:00
|
|
|
private void RemoveEmptyPasswordHashes(ManagedConnection connection)
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
2019-04-03 16:15:04 +00:00
|
|
|
foreach (var user in RetrieveAllUsers(connection))
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
|
|
|
// If the user password is the sha1 hash of the empty string, remove it
|
|
|
|
if (!string.Equals(user.Password, "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709", StringComparison.Ordinal)
|
2019-04-20 15:42:11 +00:00
|
|
|
&& !string.Equals(user.Password, "$SHA1$DA39A3EE5E6B4B0D3255BFEF95601890AFD80709", StringComparison.Ordinal))
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Password = null;
|
2019-09-25 15:43:20 +00:00
|
|
|
var serialized = JsonSerializer.SerializeToUtf8Bytes(user, _jsonOptions);
|
2019-02-20 09:17:30 +00:00
|
|
|
|
2019-04-03 12:22:17 +00:00
|
|
|
connection.RunInTransaction(db =>
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
2019-04-03 12:22:17 +00:00
|
|
|
using (var statement = db.PrepareStatement("update LocalUsersv2 set data=@data where Id=@InternalId"))
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
2019-04-03 12:22:17 +00:00
|
|
|
statement.TryBind("@InternalId", user.InternalId);
|
|
|
|
statement.TryBind("@data", serialized);
|
|
|
|
statement.MoveNext();
|
|
|
|
}
|
|
|
|
}, TransactionMode);
|
2019-02-20 09:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Save a user in the repo
|
|
|
|
/// </summary>
|
|
|
|
public void CreateUser(User user)
|
|
|
|
{
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(user));
|
|
|
|
}
|
|
|
|
|
2019-09-25 15:43:20 +00:00
|
|
|
var serialized = JsonSerializer.SerializeToUtf8Bytes(user, _jsonOptions);
|
2019-02-20 09:17:30 +00:00
|
|
|
|
2019-02-26 17:30:13 +00:00
|
|
|
using (var connection = GetConnection())
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
2019-02-20 13:26:49 +00:00
|
|
|
connection.RunInTransaction(db =>
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
2019-02-20 13:26:49 +00:00
|
|
|
using (var statement = db.PrepareStatement("insert into LocalUsersv2 (guid, data) values (@guid, @data)"))
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
2019-02-20 13:26:49 +00:00
|
|
|
statement.TryBind("@guid", user.Id.ToGuidBlob());
|
|
|
|
statement.TryBind("@data", serialized);
|
2019-02-20 09:17:30 +00:00
|
|
|
|
2019-02-20 13:26:49 +00:00
|
|
|
statement.MoveNext();
|
|
|
|
}
|
2019-02-20 09:17:30 +00:00
|
|
|
|
2019-02-20 13:26:49 +00:00
|
|
|
var createdUser = GetUser(user.Id, connection);
|
2019-02-20 09:17:30 +00:00
|
|
|
|
2019-02-20 13:26:49 +00:00
|
|
|
if (createdUser == null)
|
|
|
|
{
|
|
|
|
throw new ApplicationException("created user should never be null");
|
|
|
|
}
|
2019-02-20 09:17:30 +00:00
|
|
|
|
2019-02-20 13:26:49 +00:00
|
|
|
user.InternalId = createdUser.InternalId;
|
2019-02-20 09:17:30 +00:00
|
|
|
|
2019-02-20 13:26:49 +00:00
|
|
|
}, TransactionMode);
|
2019-02-20 09:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdateUser(User user)
|
|
|
|
{
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(user));
|
|
|
|
}
|
|
|
|
|
2019-09-25 15:43:20 +00:00
|
|
|
var serialized = JsonSerializer.SerializeToUtf8Bytes(user, _jsonOptions);
|
2019-02-20 09:17:30 +00:00
|
|
|
|
2019-02-26 17:30:13 +00:00
|
|
|
using (var connection = GetConnection())
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
2019-02-20 13:26:49 +00:00
|
|
|
connection.RunInTransaction(db =>
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
2019-02-20 13:26:49 +00:00
|
|
|
using (var statement = db.PrepareStatement("update LocalUsersv2 set data=@data where Id=@InternalId"))
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
2019-02-20 13:26:49 +00:00
|
|
|
statement.TryBind("@InternalId", user.InternalId);
|
|
|
|
statement.TryBind("@data", serialized);
|
|
|
|
statement.MoveNext();
|
|
|
|
}
|
2019-02-20 09:17:30 +00:00
|
|
|
|
2019-02-20 13:26:49 +00:00
|
|
|
}, TransactionMode);
|
2019-02-20 09:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 17:30:13 +00:00
|
|
|
private User GetUser(Guid guid, ManagedConnection connection)
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
2019-02-20 13:26:49 +00:00
|
|
|
using (var statement = connection.PrepareStatement("select id,guid,data from LocalUsersv2 where guid=@guid"))
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
2019-02-20 13:26:49 +00:00
|
|
|
statement.TryBind("@guid", guid);
|
2019-02-20 09:17:30 +00:00
|
|
|
|
2019-02-20 13:26:49 +00:00
|
|
|
foreach (var row in statement.ExecuteQuery())
|
|
|
|
{
|
|
|
|
return GetUser(row);
|
2019-02-20 09:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private User GetUser(IReadOnlyList<IResultSetValue> row)
|
|
|
|
{
|
|
|
|
var id = row[0].ToInt64();
|
|
|
|
var guid = row[1].ReadGuidFromBlob();
|
|
|
|
|
2019-09-25 15:43:20 +00:00
|
|
|
var user = JsonSerializer.Deserialize<User>(row[2].ToBlob(), _jsonOptions);
|
2019-08-19 15:03:21 +00:00
|
|
|
user.InternalId = id;
|
|
|
|
user.Id = guid;
|
|
|
|
return user;
|
2019-02-20 09:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Retrieve all users from the database
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>IEnumerable{User}.</returns>
|
|
|
|
public List<User> RetrieveAllUsers()
|
|
|
|
{
|
2019-02-26 17:30:13 +00:00
|
|
|
using (var connection = GetConnection(true))
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
2019-04-03 16:15:04 +00:00
|
|
|
return new List<User>(RetrieveAllUsers(connection));
|
2019-02-20 09:17:30 +00:00
|
|
|
}
|
2019-04-03 16:15:04 +00:00
|
|
|
}
|
2019-02-20 09:17:30 +00:00
|
|
|
|
2019-04-03 16:15:04 +00:00
|
|
|
/// <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);
|
|
|
|
}
|
2019-02-20 09:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Deletes the user.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="user">The user.</param>
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
/// <exception cref="ArgumentNullException">user</exception>
|
|
|
|
public void DeleteUser(User user)
|
|
|
|
{
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(user));
|
|
|
|
}
|
|
|
|
|
2019-02-26 17:30:13 +00:00
|
|
|
using (var connection = GetConnection())
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
2019-02-20 13:26:49 +00:00
|
|
|
connection.RunInTransaction(db =>
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
2019-02-20 13:26:49 +00:00
|
|
|
using (var statement = db.PrepareStatement("delete from LocalUsersv2 where Id=@id"))
|
2019-02-20 09:17:30 +00:00
|
|
|
{
|
2019-02-20 13:26:49 +00:00
|
|
|
statement.TryBind("@id", user.InternalId);
|
|
|
|
statement.MoveNext();
|
|
|
|
}
|
|
|
|
}, TransactionMode);
|
2019-02-20 09:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|