aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Data/SqliteUserRepository.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Emby.Server.Implementations/Data/SqliteUserRepository.cs')
-rw-r--r--Emby.Server.Implementations/Data/SqliteUserRepository.cs160
1 files changed, 67 insertions, 93 deletions
diff --git a/Emby.Server.Implementations/Data/SqliteUserRepository.cs b/Emby.Server.Implementations/Data/SqliteUserRepository.cs
index 5957b2903..11629b389 100644
--- a/Emby.Server.Implementations/Data/SqliteUserRepository.cs
+++ b/Emby.Server.Implementations/Data/SqliteUserRepository.cs
@@ -18,10 +18,10 @@ namespace Emby.Server.Implementations.Data
private readonly IJsonSerializer _jsonSerializer;
public SqliteUserRepository(
- ILoggerFactory loggerFactory,
+ ILogger<SqliteUserRepository> logger,
IServerApplicationPaths appPaths,
IJsonSerializer jsonSerializer)
- : base(loggerFactory.CreateLogger(nameof(SqliteUserRepository)))
+ : base(logger)
{
_jsonSerializer = jsonSerializer;
@@ -35,15 +35,12 @@ namespace Emby.Server.Implementations.Data
public string Name => "SQLite";
/// <summary>
- /// Opens the connection to the database
+ /// Opens the connection to the database.
/// </summary>
- /// <returns>Task.</returns>
public void Initialize()
{
- using (var connection = CreateConnection())
+ using (var connection = GetConnection())
{
- RunDefaultInitialization(connection);
-
var localUsersTableExists = TableExists(connection, "LocalUsersv2");
connection.RunQueries(new[] {
@@ -56,7 +53,7 @@ namespace Emby.Server.Implementations.Data
TryMigrateToLocalUsersTable(connection);
}
- RemoveEmptyPasswordHashes();
+ RemoveEmptyPasswordHashes(connection);
}
}
@@ -75,9 +72,9 @@ namespace Emby.Server.Implementations.Data
}
}
- private void RemoveEmptyPasswordHashes()
+ private void RemoveEmptyPasswordHashes(ManagedConnection connection)
{
- foreach (var user in RetrieveAllUsers())
+ foreach (var user in RetrieveAllUsers(connection))
{
// If the user password is the sha1 hash of the empty string, remove it
if (!string.Equals(user.Password, "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709", StringComparison.Ordinal)
@@ -89,22 +86,16 @@ namespace Emby.Server.Implementations.Data
user.Password = null;
var serialized = _jsonSerializer.SerializeToBytes(user);
- using (WriteLock.Write())
- using (var connection = CreateConnection())
+ connection.RunInTransaction(db =>
{
- connection.RunInTransaction(db =>
+ using (var statement = db.PrepareStatement("update LocalUsersv2 set data=@data where Id=@InternalId"))
{
- 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);
- }
+ statement.TryBind("@InternalId", user.InternalId);
+ statement.TryBind("@data", serialized);
+ statement.MoveNext();
+ }
+ }, TransactionMode);
}
-
}
/// <summary>
@@ -119,31 +110,28 @@ namespace Emby.Server.Implementations.Data
var serialized = _jsonSerializer.SerializeToBytes(user);
- using (WriteLock.Write())
+ using (var connection = GetConnection())
{
- using (var connection = CreateConnection())
+ connection.RunInTransaction(db =>
{
- connection.RunInTransaction(db =>
+ using (var statement = db.PrepareStatement("insert into LocalUsersv2 (guid, data) values (@guid, @data)"))
{
- using (var statement = db.PrepareStatement("insert into LocalUsersv2 (guid, data) values (@guid, @data)"))
- {
- statement.TryBind("@guid", user.Id.ToGuidBlob());
- statement.TryBind("@data", serialized);
+ statement.TryBind("@guid", user.Id.ToGuidBlob());
+ statement.TryBind("@data", serialized);
- statement.MoveNext();
- }
+ statement.MoveNext();
+ }
- var createdUser = GetUser(user.Id, false);
+ var createdUser = GetUser(user.Id, connection);
- if (createdUser == null)
- {
- throw new ApplicationException("created user should never be null");
- }
+ if (createdUser == null)
+ {
+ throw new ApplicationException("created user should never be null");
+ }
- user.InternalId = createdUser.InternalId;
+ user.InternalId = createdUser.InternalId;
- }, TransactionMode);
- }
+ }, TransactionMode);
}
}
@@ -156,39 +144,30 @@ namespace Emby.Server.Implementations.Data
var serialized = _jsonSerializer.SerializeToBytes(user);
- using (WriteLock.Write())
+ using (var connection = GetConnection())
{
- using (var connection = CreateConnection())
+ connection.RunInTransaction(db =>
{
- connection.RunInTransaction(db =>
+ using (var statement = db.PrepareStatement("update LocalUsersv2 set data=@data where Id=@InternalId"))
{
- 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);
- }
+ statement.TryBind("@InternalId", user.InternalId);
+ statement.TryBind("@data", serialized);
+ statement.MoveNext();
+ }
+
+ }, TransactionMode);
}
}
- private User GetUser(Guid guid, bool openLock)
+ private User GetUser(Guid guid, ManagedConnection connection)
{
- using (openLock ? WriteLock.Read() : null)
+ using (var statement = connection.PrepareStatement("select id,guid,data from LocalUsersv2 where guid=@guid"))
{
- using (var connection = CreateConnection(true))
- {
- using (var statement = connection.PrepareStatement("select id,guid,data from LocalUsersv2 where guid=@guid"))
- {
- statement.TryBind("@guid", guid);
+ statement.TryBind("@guid", guid);
- foreach (var row in statement.ExecuteQuery())
- {
- return GetUser(row);
- }
- }
+ foreach (var row in statement.ExecuteQuery())
+ {
+ return GetUser(row);
}
}
@@ -200,14 +179,10 @@ namespace Emby.Server.Implementations.Data
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;
- }
+ var user = _jsonSerializer.DeserializeFromString<User>(row.GetString(2));
+ user.InternalId = id;
+ user.Id = guid;
+ return user;
}
/// <summary>
@@ -216,20 +191,22 @@ namespace Emby.Server.Implementations.Data
/// <returns>IEnumerable{User}.</returns>
public List<User> RetrieveAllUsers()
{
- var list = new List<User>();
-
- using (WriteLock.Read())
+ using (var connection = GetConnection(true))
{
- using (var connection = CreateConnection(true))
- {
- foreach (var row in connection.Query("select id,guid,data from LocalUsersv2"))
- {
- list.Add(GetUser(row));
- }
- }
+ return new List<User>(RetrieveAllUsers(connection));
}
+ }
- 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>
@@ -245,19 +222,16 @@ namespace Emby.Server.Implementations.Data
throw new ArgumentNullException(nameof(user));
}
- using (WriteLock.Write())
+ using (var connection = GetConnection())
{
- using (var connection = CreateConnection())
+ connection.RunInTransaction(db =>
{
- connection.RunInTransaction(db =>
+ using (var statement = db.PrepareStatement("delete from LocalUsersv2 where Id=@id"))
{
- using (var statement = db.PrepareStatement("delete from LocalUsersv2 where Id=@id"))
- {
- statement.TryBind("@id", user.InternalId);
- statement.MoveNext();
- }
- }, TransactionMode);
- }
+ statement.TryBind("@id", user.InternalId);
+ statement.MoveNext();
+ }
+ }, TransactionMode);
}
}
}