From 7d89bc4ad9035a37b1506a635d9fb3fe3df4cde0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 30 Jan 2022 21:07:50 -0600 Subject: add user/group mapping to UserService --- src/slskd/Users/IUserService.cs | 7 +++++++ src/slskd/Users/UserService.cs | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 4 deletions(-) (limited to 'src') 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 @@ -25,6 +25,13 @@ namespace slskd.Users /// public interface IUserService { + /// + /// Gets the name of the group for the specified . + /// + /// The username of the peer. + /// The group for the specified username. + string GetGroup(string username); + /// /// Retrieves peer . /// 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/. // +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; /// @@ -30,26 +33,55 @@ namespace slskd.Users public class UserService : IUserService { private const int CacheTTLSeconds = 300; + private static readonly string DefaultGroup = "default"; /// /// Initializes a new instance of the class. /// /// /// The database context to use. + /// /// The logger. public UserService( ISoulseekClient soulseekClient, IDbContextFactory contextFactory, - ILogger log) + IOptionsMonitor optionsMonitor) { Client = soulseekClient; ContextFactory = contextFactory; - Log = log; + + OptionsMonitor = optionsMonitor; + OptionsMonitor.OnChange(Configure); + + Configure(OptionsMonitor.CurrentValue); } private ISoulseekClient Client { get; } private IDbContextFactory ContextFactory { get; } - private ILogger Log { get; set; } + private IOptionsMonitor OptionsMonitor { get; } + private ILogger Log { get; set; } = Serilog.Log.ForContext(); + private Dictionary Map { get; set; } = new Dictionary(); + + private void Configure(Options options) + { + var map = new Dictionary(); + + 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); + } /// /// Retrieves peer . -- cgit v1.2.3