Beispiel #1
0
        private static void WriteServerToJson(ServerPermissions serverPerms)
        {
            string pathToFile = $"data/permissions/{serverPerms.Id}.json";

            File.WriteAllText(pathToFile,
                              Newtonsoft.Json.JsonConvert.SerializeObject(serverPerms, Newtonsoft.Json.Formatting.Indented));
        }
Beispiel #2
0
 public static void SetServerCommandPermission(Server server, string commandName, bool value) {
     ServerPermissions serverPerms = _permissionsDict.GetOrAdd(server.Id,
         serverPerms = new ServerPermissions(server.Id, server.Name));
     var commands = serverPerms.Permissions.commands;
     if (commands.ContainsKey(commandName))
         commands[commandName] = value;
     else
         commands.Add(commandName, value);
     Task.Run(() => WriteServerToJson(server.Id));
 }
Beispiel #3
0
 public static void SetServerModulePermission(Server server, string moduleName, bool value) {
     ServerPermissions serverPerms = _permissionsDict.GetOrAdd(server.Id,
         serverPerms = new ServerPermissions(server.Id, server.Name));
     var modules = serverPerms.Permissions.modules;
     if (modules.ContainsKey(moduleName))
         modules[moduleName] = value;
     else
         modules.Add(moduleName, value);
     Task.Run(() => WriteServerToJson(server.Id));
 }
Beispiel #4
0
        public static void SetUserCommandPermission(User user, string commandName, bool value) {
            var server = user.Server;
            ServerPermissions serverPerms = _permissionsDict.GetOrAdd(server.Id,
                serverPerms = new ServerPermissions(server.Id, server.Name));
            if (!serverPerms.UserPermissions.ContainsKey(user.Id))
                serverPerms.UserPermissions.Add(user.Id, new Permissions(user.Name));

            var commands = serverPerms.UserPermissions[user.Id].commands;

            if (commands.ContainsKey(commandName))
                commands[commandName] = value;
            else
                commands.Add(commandName, value);
            Task.Run(() => WriteServerToJson(server.Id));
        }
Beispiel #5
0
        public static void SetUserModulePermission(User user, string moduleName, bool value) {
            var server = user.Server;
            ServerPermissions serverPerms = _permissionsDict.GetOrAdd(server.Id,
                serverPerms = new ServerPermissions(server.Id, server.Name));

            if (!serverPerms.UserPermissions.ContainsKey(user.Id))
                serverPerms.UserPermissions.Add(user.Id, new Permissions(user.Name));

            var modules = serverPerms.UserPermissions[user.Id].modules;

            if (modules.ContainsKey(moduleName))
                modules[moduleName] = value;
            else
                modules.Add(moduleName, value);
            Task.Run(() => WriteServerToJson(server.Id));
        }
Beispiel #6
0
        private static bool IsChannelOrServerFiltering(Channel channel, out ServerPermissions serverPerms)
        {
            if (!PermissionsHandler.PermissionsDict.TryGetValue(channel.Server.Id, out serverPerms))
            {
                return(false);
            }

            if (serverPerms.Permissions.FilterInvites)
            {
                return(true);
            }

            Permissions perms;

            return(serverPerms.ChannelPermissions.TryGetValue(channel.Id, out perms) && perms.FilterInvites);
        }
Beispiel #7
0
        public static void SetChannelModulePermission(Channel channel, string moduleName, bool value) {
            var server = channel.Server;

            ServerPermissions serverPerms = _permissionsDict.GetOrAdd(server.Id,
                serverPerms = new ServerPermissions(server.Id, server.Name));

            if (!serverPerms.ChannelPermissions.ContainsKey(channel.Id))
                serverPerms.ChannelPermissions.Add(channel.Id, new Permissions(channel.Name));

            var modules = serverPerms.ChannelPermissions[channel.Id].modules;

            if (modules.ContainsKey(moduleName))
                modules[moduleName] = value;
            else
                modules.Add(moduleName, value);
            Task.Run(() => WriteServerToJson(server.Id));
        }
Beispiel #8
0
        internal static PermissionBanType GetPermissionBanType(Command command, User user, Channel channel)
        {
            var server = user.Server;
            ServerPermissions serverPerms;

            if (!PermissionsDict.TryGetValue(server.Id, out serverPerms))
            {
                serverPerms = new ServerPermissions(server.Id, server.Name);
                PermissionsDict.TryAdd(server.Id, serverPerms);
            }
            bool        val;
            Permissions perm;

            //server
            if (serverPerms.Permissions.Modules.TryGetValue(command.Category, out val) && val == false)
            {
                return(PermissionBanType.ServerBanModule);
            }
            if (serverPerms.Permissions.Commands.TryGetValue(command.Text, out val) && val == false)
            {
                return(PermissionBanType.ServerBanCommand);
            }
            //channel
            if (serverPerms.ChannelPermissions.TryGetValue(channel.Id, out perm) &&
                perm.Modules.TryGetValue(command.Category, out val) && val == false)
            {
                return(PermissionBanType.ChannelBanModule);
            }
            if (serverPerms.ChannelPermissions.TryGetValue(channel.Id, out perm) &&
                perm.Commands.TryGetValue(command.Text, out val) && val == false)
            {
                return(PermissionBanType.ChannelBanCommand);
            }

            //ROLE PART - TWO CASES
            // FIRST CASE:
            // IF EVERY ROLE USER HAS IS BANNED FROM THE MODULE,
            // THAT MEANS USER CANNOT RUN THIS COMMAND
            // IF AT LEAST ONE ROLE EXIST THAT IS NOT BANNED,
            // USER CAN RUN THE COMMAND
            var foundNotBannedRole = false;

            foreach (var role in user.Roles)
            {
                //if every role is banned from using the module -> rolebanmodule
                if (serverPerms.RolePermissions.TryGetValue(role.Id, out perm) &&
                    perm.Modules.TryGetValue(command.Category, out val) && val == false)
                {
                    continue;
                }
                foundNotBannedRole = true;
                break;
            }
            if (!foundNotBannedRole)
            {
                return(PermissionBanType.RoleBanModule);
            }

            // SECOND CASE:
            // IF EVERY ROLE USER HAS IS BANNED FROM THE COMMAND,
            // THAT MEANS USER CANNOT RUN THAT COMMAND
            // IF AT LEAST ONE ROLE EXISTS THAT IS NOT BANNED,
            // USER CAN RUN THE COMMAND
            foundNotBannedRole = false;
            foreach (var role in user.Roles)
            {
                //if every role is banned from using the module -> rolebanmodule
                if (serverPerms.RolePermissions.TryGetValue(role.Id, out perm) &&
                    perm.Commands.TryGetValue(command.Text, out val) && val == false)
                {
                    continue;
                }
                else
                {
                    foundNotBannedRole = true;
                    break;
                }
            }
            if (!foundNotBannedRole)
            {
                return(PermissionBanType.RoleBanCommand);
            }

            //user
            if (serverPerms.UserPermissions.TryGetValue(user.Id, out perm) &&
                perm.Modules.TryGetValue(command.Category, out val) && val == false)
            {
                return(PermissionBanType.UserBanModule);
            }
            if (serverPerms.UserPermissions.TryGetValue(user.Id, out perm) &&
                perm.Commands.TryGetValue(command.Text, out val) && val == false)
            {
                return(PermissionBanType.UserBanCommand);
            }

            return(PermissionBanType.None);
        }
Beispiel #9
0
 internal static void SetVerbosity(Server server, bool val) {
     ServerPermissions serverPerms = _permissionsDict.GetOrAdd(server.Id,
         serverPerms = new ServerPermissions(server.Id, server.Name));
     serverPerms.Verbose = val;
     Task.Run(() => WriteServerToJson(server.Id));
 }
Beispiel #10
0
 internal static void SetPermissionsRole(Server server, string roleName) {
     ServerPermissions serverPerms = _permissionsDict.GetOrAdd(server.Id,
         serverPerms = new ServerPermissions(server.Id, server.Name));
     serverPerms.PermissionsControllerRole = roleName;
     Task.Run(() => WriteServerToJson(server.Id));
 }
Beispiel #11
0
 public static string GetServerPermissionsRoleName(Server server) {
     ServerPermissions serverPerms = _permissionsDict.GetOrAdd(server.Id,
         serverPerms = new ServerPermissions(server.Id, server.Name));
     return serverPerms.PermissionsControllerRole;
 }