namespace Sandbox; /// /// A static class that handles user permissions. /// internal static class UserPermission { private static FileWatch Watcher { get; set; } /// /// Represents a user in the user permissions config. /// private class User { public HashSet Claims { get; init; } = new(); public ulong SteamId { get; init; } public string Name { get; init; } } private static List users = new(); /// /// Get whether or not the specified has this permission. /// public static bool Has( SteamId steamId, string permission ) { var user = users.FirstOrDefault( u => u.SteamId == steamId ); return user?.Claims.Contains( permission ) ?? false; } /// /// Save user permissions to disk. /// internal static void Save() { var fs = EngineFileSystem.Config; fs.WriteJson( "users.json", users ); } /// /// Load user permissions from disk. The file location should be "config/users.json". /// internal static void Load() { if ( Watcher is not null ) { Watcher.Dispose(); Watcher = null; } var fs = EngineFileSystem.Config; if ( !LoadFromFile() ) return; Watcher = fs.Watch( "/users.json" ); Watcher.OnChanges += _ => LoadFromFile(); } private static bool LoadFromFile() { var fs = EngineFileSystem.Config; // Nothing to do if the file does not exist. if ( !fs.FileExists( "users.json" ) ) return false; try { users = fs.ReadJson>( "users.json" ); } catch ( Exception e ) { Log.Error( e ); Log.Warning( "The users.json permissions file is incorrectly formatted!" ); } return true; } }