aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2022-01-30 21:07:50 -0600
committerJP Dillingham <jpdillingham@gmail.com>2022-01-30 21:07:50 -0600
commit7d89bc4ad9035a37b1506a635d9fb3fe3df4cde0 (patch)
tree63527b2f0ae7fc693f3fd56d24e426ac904b1e9e
parentc586e614c7894c8791541f1526d38a989160eaa0 (diff)
downloadslskd-7d89bc4ad9035a37b1506a635d9fb3fe3df4cde0.tar.gz
add user/group mapping to UserService
-rw-r--r--src/slskd/Users/IUserService.cs7
-rw-r--r--src/slskd/Users/UserService.cs40
2 files changed, 43 insertions, 4 deletions
diff --git a/src/slskd/Users/IUserService.cs b/src/slskd/Users/IUserService.cs
index 469acef6..ad368c65 100644
--- a/src/slskd/Users/IUserService.cs
+++ b/src/slskd/Users/IUserService.cs
@@ -26,6 +26,13 @@ namespace slskd.Users
public interface IUserService
{
/// <summary>
+ /// Gets the name of the group for the specified <paramref name="username"/>.
+ /// </summary>
+ /// <param name="username">The username of the peer.</param>
+ /// <returns>The group for the specified username.</returns>
+ string GetGroup(string username);
+
+ /// <summary>
/// Retrieves peer <see cref="Info"/>.
/// </summary>
/// <param name="username">The username of the peer.</param>
diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs
index f52f19f1..1f037d3d 100644
--- a/src/slskd/Users/UserService.cs
+++ b/src/slskd/Users/UserService.cs
@@ -15,13 +15,16 @@
// along with this program. If not, see https://www.gnu.org/licenses/.
// </copyright>
+using Microsoft.Extensions.Options;
+
namespace slskd.Users
{
using System;
+ using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Logging;
+ using Serilog;
using Soulseek;
/// <summary>
@@ -30,26 +33,55 @@ namespace slskd.Users
public class UserService : IUserService
{
private const int CacheTTLSeconds = 300;
+ private static readonly string DefaultGroup = "default";
/// <summary>
/// Initializes a new instance of the <see cref="UserService"/> class.
/// </summary>
/// <param name="soulseekClient"></param>
/// <param name="contextFactory">The database context to use.</param>
+ /// <param name="optionsMonitor"></param>
/// <param name="log">The logger.</param>
public UserService(
ISoulseekClient soulseekClient,
IDbContextFactory<UserDbContext> contextFactory,
- ILogger<UserService> log)
+ IOptionsMonitor<Options> optionsMonitor)
{
Client = soulseekClient;
ContextFactory = contextFactory;
- Log = log;
+
+ OptionsMonitor = optionsMonitor;
+ OptionsMonitor.OnChange(Configure);
+
+ Configure(OptionsMonitor.CurrentValue);
}
private ISoulseekClient Client { get; }
private IDbContextFactory<UserDbContext> ContextFactory { get; }
- private ILogger<UserService> Log { get; set; }
+ private IOptionsMonitor<Options> OptionsMonitor { get; }
+ private ILogger Log { get; set; } = Serilog.Log.ForContext<UserService>();
+ private Dictionary<string, string> Map { get; set; } = new Dictionary<string, string>();
+
+ private void Configure(Options options)
+ {
+ var map = new Dictionary<string, string>();
+
+ foreach (var group in options.Groups.UserDefined)
+ {
+ foreach (var user in group.Value.Members)
+ {
+ map.Add(user, group.Key);
+ }
+ }
+
+ Map = map;
+ Log.Debug("Updated user/group map with {Count} entries", Map.Count);
+ }
+
+ public string GetGroup(string username)
+ {
+ return Map.GetValueOrDefault(username, DefaultGroup);
+ }
/// <summary>
/// Retrieves peer <see cref="Info"/>.