more work on permissions

This commit is contained in:
Master Kwoth 2016-02-11 13:22:24 +01:00
parent 0a44ae9922
commit e0fb86a43e
3 changed files with 170 additions and 23 deletions

View File

@ -1,18 +0,0 @@
using Discord.Commands.Permissions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
namespace NadekoBot.Classes.PermissionCheckers {
class NSFWPermissionChecker : PermissionChecker<NSFWPermissionChecker> {
public override bool CanRun(Command command, User user, Channel channel, out string error) {
error = string.Empty;
Console.WriteLine(command.Category);
return false;
}
}
}

View File

@ -0,0 +1,23 @@
using Discord.Commands.Permissions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
namespace NadekoBot.Classes.Permissions {
class PermissionChecker : IPermissionChecker {
public static readonly PermissionChecker _instance = new PermissionChecker();
public static PermissionChecker Instance => _instance;
static PermissionChecker() { }
public PermissionChecker() { }
public bool CanRun(Command command, User user, Channel channel, out string error) {
error = string.Empty;
return false;
}
}
}

View File

@ -25,6 +25,132 @@ namespace NadekoBot.Classes.Permissions {
WriteServerToJson(kvp.Key); WriteServerToJson(kvp.Key);
} }
} }
public static void SetServerModulePermission(Server server, string moduleName, bool value) {
if (!_permissionsDict.ContainsKey(server)) {
_permissionsDict.TryAdd(server, new ServerPermissions(server.Id.ToString()));
}
var modules = _permissionsDict[server].Permissions.modules;
if (modules.ContainsKey(moduleName))
modules[moduleName] = value;
else
modules.Add(moduleName, value);
WriteServerToJson(server);
}
public static void SetServerCommandPermission(Server server, string commandName, bool value) {
if (!_permissionsDict.ContainsKey(server)) {
_permissionsDict.TryAdd(server, new ServerPermissions(server.Id.ToString()));
}
var commands = _permissionsDict[server].Permissions.commands;
if (commands.ContainsKey(commandName))
commands[commandName] = value;
else
commands.Add(commandName, value);
WriteServerToJson(server);
}
public static void SetChannelModulePermission(Channel channel, string moduleName, bool value) {
var server = channel.Server;
if (!_permissionsDict.ContainsKey(server)) {
_permissionsDict.TryAdd(server, new ServerPermissions(server.Id.ToString()));
}
if(!_permissionsDict[server].ChannelPermissions.ContainsKey(channel.Id.ToString()))
_permissionsDict[server].ChannelPermissions.Add(channel.Id.ToString(), new Permissions());
var modules = _permissionsDict[server].ChannelPermissions[channel.Id.ToString()].modules;
if (modules.ContainsKey(moduleName))
modules[moduleName] = value;
else
modules.Add(moduleName, value);
WriteServerToJson(server);
}
public static void SetChannelCommandPermission(Channel channel, string commandName, bool value) {
var server = channel.Server;
if (!_permissionsDict.ContainsKey(server)) {
_permissionsDict.TryAdd(server, new ServerPermissions(server.Id.ToString()));
}
if (!_permissionsDict[server].ChannelPermissions.ContainsKey(channel.Id.ToString()))
_permissionsDict[server].ChannelPermissions.Add(channel.Id.ToString(), new Permissions());
var commands = _permissionsDict[server].ChannelPermissions[channel.Id.ToString()].commands;
if (commands.ContainsKey(commandName))
commands[commandName] = value;
else
commands.Add(commandName, value);
WriteServerToJson(server);
}
public static void SetRoleModulePermission(Role role, string roleName, bool value) {
var server = role.Server;
if (!_permissionsDict.ContainsKey(server)) {
_permissionsDict.TryAdd(server, new ServerPermissions(server.Id.ToString()));
}
if (!_permissionsDict[server].RolePermissions.ContainsKey(role.Id.ToString()))
_permissionsDict[server].RolePermissions.Add(role.Id.ToString(), new Permissions());
var modules = _permissionsDict[server].RolePermissions[role.Id.ToString()].modules;
if (modules.ContainsKey(roleName))
modules[roleName] = value;
else
modules.Add(roleName, value);
WriteServerToJson(server);
}
public static void SetRoleCommandPermission(Role role, string roleName, bool value) {
var server = role.Server;
if (!_permissionsDict.ContainsKey(server)) {
_permissionsDict.TryAdd(server, new ServerPermissions(server.Id.ToString()));
}
if (!_permissionsDict[server].RolePermissions.ContainsKey(role.Id.ToString()))
_permissionsDict[server].RolePermissions.Add(role.Id.ToString(), new Permissions());
var commands = _permissionsDict[server].RolePermissions[role.Id.ToString()].commands;
if (commands.ContainsKey(roleName))
commands[roleName] = value;
else
commands.Add(roleName, value);
WriteServerToJson(server);
}
public static void SetUserModulePermission(User user, string userName, bool value) {
var server = user.Server;
if (!_permissionsDict.ContainsKey(server)) {
_permissionsDict.TryAdd(server, new ServerPermissions(server.Id.ToString()));
}
if (!_permissionsDict[server].UserPermissions.ContainsKey(user.Id.ToString()))
_permissionsDict[server].UserPermissions.Add(user.Id.ToString(), new Permissions());
var modules = _permissionsDict[server].UserPermissions[user.Id.ToString()].modules;
if (modules.ContainsKey(userName))
modules[userName] = value;
else
modules.Add(userName, value);
WriteServerToJson(server);
}
public static void SetUserCommandPermission(User user, string userName, bool value) {
var server = user.Server;
if (!_permissionsDict.ContainsKey(server)) {
_permissionsDict.TryAdd(server, new ServerPermissions(server.Id.ToString()));
}
if (!_permissionsDict[server].UserPermissions.ContainsKey(user.Id.ToString()))
_permissionsDict[server].UserPermissions.Add(user.Id.ToString(), new Permissions());
var commands = _permissionsDict[server].UserPermissions[user.Id.ToString()].commands;
if (commands.ContainsKey(userName))
commands[userName] = value;
else
commands.Add(userName, value);
WriteServerToJson(server);
}
} }
/// <summary> /// <summary>
/// Holds a permission list /// Holds a permission list
@ -38,6 +164,11 @@ namespace NadekoBot.Classes.Permissions {
/// Command name with allowed/disallowed /// Command name with allowed/disallowed
/// </summary> /// </summary>
public Dictionary<string, bool> commands { get; set; } public Dictionary<string, bool> commands { get; set; }
public Permissions() {
modules = new Dictionary<string, bool>();
commands = new Dictionary<string, bool>();
}
} }
public class PermissionsContainer { public class PermissionsContainer {
@ -50,7 +181,10 @@ namespace NadekoBot.Classes.Permissions {
/// </summary> /// </summary>
public Permissions Permissions { get; set; } public Permissions Permissions { get; set; }
public PermissionsContainer() { } public PermissionsContainer(string id) {
Id = id;
Permissions = new Permissions();
}
} }
public class ServerPermissions : PermissionsContainer { public class ServerPermissions : PermissionsContainer {
@ -63,8 +197,16 @@ namespace NadekoBot.Classes.Permissions {
/// </summary> /// </summary>
public bool Verbose { get; set; } public bool Verbose { get; set; }
public List<PermissionsContainer> UserPermissions { get; set; } public Dictionary<string, Permissions> UserPermissions { get; set; }
public List<PermissionsContainer> ChannelPermissions { get; set; } public Dictionary<string, Permissions> ChannelPermissions { get; set; }
public List<PermissionsContainer> RolePermissions { get; set; } public Dictionary<string, Permissions> RolePermissions { get; set; }
public ServerPermissions(string id) : base(id) {
PermissionsControllerRoleName = "PermissionsKing";
Verbose = true;
UserPermissions = new Dictionary<string, Permissions>();
ChannelPermissions = new Dictionary<string, Permissions>();
RolePermissions = new Dictionary<string, Permissions>();
}
} }
} }