public DynPermFullData SetDynPermFullData(ulong serverId, string input, out string error) { try { DynamicPerms perms = JsonConvert.DeserializeObject <DynamicPerms>(input); if (perms == null) { error = "Deserialization of input resulted in null params."; return(null); } DynPermFullData fullPermData = new DynPermFullData(perms); Server server = _client.GetServer(serverId); // verify the data (role && user ids) if (server == null) { error = $"Server id {serverId} not found."; return(null); } foreach (var pair in fullPermData.Perms.RolePerms) { ulong invalidChannelId; if (IsInvalidChannelsInBlock(pair.Value, server, out invalidChannelId)) { error = $"Channel id {invalidChannelId} not found."; return(null); } } foreach (var pair in fullPermData.Perms.UserPerms) { if (server.GetUser(pair.Key) == null) { error = $"User id {pair.Key} not found."; return(null); } } // data looks fine, add the dynrole to the dict. _perms.AddOrUpdate(serverId, fullPermData, (k, v) => fullPermData); error = null; return(fullPermData); } catch (Exception ex) { error = $"Failed deserializing input. Make sure your input is valid JSON.\r\nDebug: ```{ex.GetType().Name}: {ex.Message}:{ex.TargetSite}```"; } return(null); }
public DynPermFullData SetDynPermFullData(ulong serverId, string input, out string error) { try { DynamicPerms perms = JsonConvert.DeserializeObject<DynamicPerms>(input); if (perms == null) { error = "Deserialization of input resulted in null params."; return null; } DynPermFullData fullPermData = new DynPermFullData(perms); Server server = _client.GetServer(serverId); // verify the data (role && user ids) if (server == null) { error = $"Server id {serverId} not found."; return null; } foreach (var pair in fullPermData.Perms.RolePerms) { ulong invalidChannelId; if (IsInvalidChannelsInBlock(pair.Value, server, out invalidChannelId)) { error = $"Channel id {invalidChannelId} not found."; return null; } } foreach (var pair in fullPermData.Perms.UserPerms) { if (server.GetUser(pair.Key) == null) { error = $"User id {pair.Key} not found."; return null; } } // data looks fine, add the dynrole to the dict. _perms.AddOrUpdate(serverId, fullPermData, (k, v) => fullPermData); error = null; return fullPermData; } catch (Exception ex) { error = $"Failed deserializing input. Make sure your input is valid JSON.\r\nDebug: ```{ex.GetType().Name}: {ex.Message}:{ex.TargetSite}```"; } return null; }
public bool CanRun(Command command, User user, Channel channel, out string error) { error = null; if (channel.IsPrivate) { return(DefaultPermissionLevel <= DefaultPermChecker.GetPermissionLevel(user, channel)); } DynPermFullData data = DynPerms.GetPerms(channel.Server.Id); // apply default perms. bool retval = DefaultPermissionLevel <= DefaultPermChecker.GetPermissionLevel(user, channel); // if we do not have dynamic perms in place for the user's server, return the default perms. if (data == null || (!data.Perms.RolePerms.Any() && !data.Perms.UserPerms.Any())) { return(retval); } /* * Firsly do role checks. * Lower entries override higher entries. * To do that we have to iterate over the dict instead of using roles the user has as keys. */ foreach (var pair in data.Perms.RolePerms) { if (user.HasRole(pair.Key)) { retval = EvaluatePerms(pair.Value, command, retval, channel, ref error); } } // users override roles, do them next. DynamicPermissionBlock permBlock; if (data.Perms.UserPerms.TryGetValue(user.Id, out permBlock)) { retval = EvaluatePerms(permBlock, command, retval, channel, ref error); } return(retval); }
public void Install(ModuleManager manager) { Nullcheck(Config.PastebinPassword, Config.PastebinUsername, Config.PastebinApiKey); _client = manager.Client; _dynPerms = _client.GetService <DynamicPermissionService>(); _pastebin = _client.GetService <PastebinService>(); manager.CreateCommands("dynperm", group => { group.MinPermissions((int)PermissionLevel.ServerAdmin); group.AddCheck((cmd, usr, chnl) => !chnl.IsPrivate); group.CreateCommand("set") .Description( "Sets the dynamic permissions for this server.**Pastebin links are supported.**Use the dynperm help command for more info.") .Parameter("perms", ParameterType.Unparsed) .Do(async e => { string input = e.GetArg("perms"); string error; if (input.StartsWith(PastebinIdentifier)) { string rawUrl = input.Insert(PastebinIdentifier.Length, RawPath); input = await Utils.AsyncDownloadRaw(rawUrl); } DynPermFullData perms = _dynPerms.SetDynPermFullData(e.Server.Id, input, out error); if (!string.IsNullOrEmpty(error)) { await e.Channel.SendMessage($"Failed parsing Dynamic Permissions. {error}"); return; } await e.Channel.SendMessage($"Parsed Dynamic Permissions:\r\n```" + $"- Role Rules: {perms.Perms.RolePerms.Count}\r\n" + $"- User Rules: {perms.Perms.UserPerms.Count}```"); }); // commands which can only be executed if the caller server has dynperms. group.CreateGroup("", existsGroup => { existsGroup.AddCheck((cmd, usr, chnl) => _dynPerms.GetPerms(chnl.Server.Id) != null); existsGroup.CreateCommand("show") .Description("Shows the Dynamic Permissions for this server.") .Do(async e => { DynPermFullData data = _dynPerms.GetPerms(e.Server.Id); if (string.IsNullOrEmpty(data.PastebinUrl) || data.IsDirty) { if (!_pastebin.IsLoggedIn) { await _pastebin.Login(Config.PastebinUsername, Config.PastebinPassword); } data.PastebinUrl = await _pastebin.Paste(new PastebinService.PasteBinEntry { Expiration = PastebinService.PasteBinExpiration.Never, Format = "json", Private = true, Text = JsonConvert.SerializeObject(data.Perms), Title = $"{e.Server.Name}@{DateTime.Now}" }); } await e.Channel.SendMessage($"Paste: {data.PastebinUrl}"); }); existsGroup.CreateCommand("clear") .Description( "Clears the Dynamic Permissions. This cannot be undone. Pass yes as an argument for this to work.") .Parameter("areyousure") .Do(async e => { string input = e.GetArg("areyousure").ToLowerInvariant(); if (input == "yes" || input == "y") { _dynPerms.DestroyServerPerms(e.Server.Id); await e.Channel.SendMessage("Dynamic Permissions have been wiped."); } }); }); group.CreateCommand("help") .Description("help") .Do( async e => await e.Channel.SendMessage("https://github.com/SSStormy/Stormbot/blob/master/docs/dynperm.md")); }); }