-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
LdapProfileImageSyncTask.cs
164 lines (142 loc) · 6.83 KB
/
LdapProfileImageSyncTask.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.LDAP_Auth.Helpers;
using MediaBrowser.Common;
using MediaBrowser.Controller.Authentication;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
using Novell.Directory.Ldap;
namespace Jellyfin.Plugin.LDAP_Auth
{
/// <summary>
/// Ldap Authentication Provider Plugin.
/// </summary>
public class LdapProfileImageSyncTask : IScheduledTask
{
private readonly ILocalizationManager _localization;
private readonly IApplicationHost _applicationHost;
private readonly ILogger<LdapProfileImageSyncTask> _logger;
private readonly IUserManager _userManager;
private readonly IProviderManager _providerManager;
private readonly IServerConfigurationManager _serverConfigurationManager;
/// <summary>
/// Initializes a new instance of the <see cref="LdapProfileImageSyncTask"/> class.
/// </summary>
/// <param name="applicationHost">Instance of the <see cref="IApplicationHost"/> interface.</param>
/// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
/// <param name="providerManager">Instance of the <see cref="IProviderManager"/> interface.</param>
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
/// <param name="logger">Instance of the <see cref="ILogger{LDAPImageSyncScheduledTask}"/> interface.</param>
/// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
public LdapProfileImageSyncTask(
IApplicationHost applicationHost,
IUserManager userManager,
IProviderManager providerManager,
IServerConfigurationManager serverConfigurationManager,
ILogger<LdapProfileImageSyncTask> logger,
ILocalizationManager localization)
{
_logger = logger;
_localization = localization;
_applicationHost = applicationHost;
_userManager = userManager;
_providerManager = providerManager;
_serverConfigurationManager = serverConfigurationManager;
}
private bool EnableProfileImageSync => LdapPlugin.Instance.Configuration.EnableLdapProfileImageSync;
private string ProfileImageAttr => LdapPlugin.Instance.Configuration.LdapProfileImageAttribute;
/// <inheritdoc/>
public string Name => "LDAP - Synchronize profile images";
/// <inheritdoc/>
public string Key => "LdapProfileImageSync";
/// <inheritdoc/>
public string Description => "Synchronizes user profile images from LDAP.";
/// <inheritdoc/>
public string Category => _localization.GetLocalizedString("TasksApplicationCategory");
/// <inheritdoc/>
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
if (!EnableProfileImageSync)
{
_logger.LogDebug("Synchronizing profile images is deactivated");
return;
}
var ldapAuthProvider = _applicationHost.GetExports<LdapAuthenticationProviderPlugin>(false).First();
var updatePluginConfig = false;
foreach (var configUser in LdapPlugin.Instance.Configuration.GetAllLdapUsers())
{
var user = _userManager.GetUserById(configUser.LinkedJellyfinUserId);
LdapEntry ldapUser;
try
{
ldapUser = ldapAuthProvider.LocateLdapUser(configUser.LdapUid);
}
catch (AuthenticationException)
{
_logger.LogWarning("User '{configUser}' is not found in LDAP. Cannot synchronize profile image.", configUser.LdapUid);
continue;
}
var ldapProfileImage = ldapAuthProvider.GetAttribute(ldapUser, ProfileImageAttr)?.ByteValue;
if (ldapProfileImage is not null)
{
// Found a profile image in LDAP data. Check if image changed since last synchronization and update if so
var ldapProfileImageHash = Convert.ToBase64String(MD5.HashData(ldapProfileImage));
if (user.ProfileImage is null ||
!string.Equals(ldapProfileImageHash, configUser.ProfileImageHash, StringComparison.Ordinal))
{
_logger.LogDebug("Updating profile image for user {Username}", configUser.LdapUid);
if (user.ProfileImage is not null)
{
await _userManager.ClearProfileImageAsync(user).ConfigureAwait(false);
}
await ProfileImageUpdater.SetProfileImage(user, ldapProfileImage, _serverConfigurationManager, _providerManager).ConfigureAwait(false);
configUser.ProfileImageHash = ldapProfileImageHash;
updatePluginConfig = true;
await _userManager.UpdateUserAsync(user).ConfigureAwait(false);
}
}
else if (user.ProfileImage is not null)
{
// Did not find a profile image in LDAP data but user still has a profile image set. Reset it.
_logger.LogDebug("Removing profile image for user {Username}", configUser.LdapUid);
try
{
File.Delete(user.ProfileImage.Path);
}
catch (IOException e)
{
_logger.LogError(e, "Error deleting user profile image during LDAP user profile image update");
}
configUser.ProfileImageHash = string.Empty;
updatePluginConfig = true;
await _userManager.ClearProfileImageAsync(user).ConfigureAwait(false);
}
}
if (updatePluginConfig)
{
LdapPlugin.Instance.SaveConfiguration();
}
}
/// <inheritdoc/>
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
{
return new[]
{
new TaskTriggerInfo
{
Type = TaskTriggerInfo.TriggerInterval,
IntervalTicks = TimeSpan.FromHours(24).Ticks
}
};
}
}
}