Ejemplo n.º 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));
        }
Ejemplo n.º 2
0
        internal static PermissionBanType GetPermissionBanType(Command command, User user, Channel channel)
        {
            var server = user.Server;
            ServerPermissions serverPerms = PermissionsDict.GetOrAdd(server.Id, id => new ServerPermissions(id, server.Name));
            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);
        }