2017-07-17 19:42:36 +00:00
|
|
|
|
using System;
|
2017-05-27 23:51:22 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Diagnostics;
|
2017-05-25 02:24:43 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.WebSocket;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using NadekoBot.Common.ModuleBehaviors;
|
2017-05-30 04:54:59 +00:00
|
|
|
|
using NadekoBot.Extensions;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Modules.Permissions.Common;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services;
|
|
|
|
|
using NadekoBot.Core.Services.Database.Models;
|
|
|
|
|
using NadekoBot.Core.Services.Impl;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NLog;
|
2017-05-25 02:24:43 +00:00
|
|
|
|
|
2017-07-17 19:42:36 +00:00
|
|
|
|
namespace NadekoBot.Modules.Permissions.Services
|
2017-05-25 02:24:43 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
public class PermissionService : ILateBlocker, INService
|
2017-05-25 02:24:43 +00:00
|
|
|
|
{
|
2017-05-29 04:13:22 +00:00
|
|
|
|
private readonly DbService _db;
|
2017-06-01 03:12:00 +00:00
|
|
|
|
private readonly CommandHandler _cmd;
|
2017-06-25 02:58:48 +00:00
|
|
|
|
private readonly NadekoStrings _strings;
|
2017-05-27 23:51:22 +00:00
|
|
|
|
|
|
|
|
|
//guildid, root permission
|
|
|
|
|
public ConcurrentDictionary<ulong, PermissionCache> Cache { get; } =
|
|
|
|
|
new ConcurrentDictionary<ulong, PermissionCache>();
|
|
|
|
|
|
2017-07-15 03:04:16 +00:00
|
|
|
|
public PermissionService(DiscordSocketClient client, DbService db, CommandHandler cmd, NadekoStrings strings)
|
2017-05-27 23:51:22 +00:00
|
|
|
|
{
|
|
|
|
|
_db = db;
|
2017-06-01 03:12:00 +00:00
|
|
|
|
_cmd = cmd;
|
2017-06-25 02:58:48 +00:00
|
|
|
|
_strings = strings;
|
2017-05-27 23:51:22 +00:00
|
|
|
|
|
|
|
|
|
var sw = Stopwatch.StartNew();
|
2017-06-22 21:59:54 +00:00
|
|
|
|
if (client.ShardId == 0)
|
2017-07-15 03:04:16 +00:00
|
|
|
|
TryMigratePermissions();
|
2017-06-20 02:23:11 +00:00
|
|
|
|
|
2017-06-22 21:59:54 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2017-05-27 23:51:22 +00:00
|
|
|
|
{
|
2017-06-22 21:59:54 +00:00
|
|
|
|
foreach (var x in uow.GuildConfigs.Permissionsv2ForAll(client.Guilds.ToArray().Select(x => (long)x.Id).ToList()))
|
2017-05-27 23:51:22 +00:00
|
|
|
|
{
|
2017-06-22 21:59:54 +00:00
|
|
|
|
Cache.TryAdd(x.GuildId, new PermissionCache()
|
2017-05-27 23:51:22 +00:00
|
|
|
|
{
|
2017-06-22 21:59:54 +00:00
|
|
|
|
Verbose = x.VerbosePermissions,
|
|
|
|
|
PermRole = x.PermissionRole,
|
|
|
|
|
Permissions = new PermissionsCollection<Permissionv2>(x.Permissions)
|
|
|
|
|
});
|
2017-05-27 23:51:22 +00:00
|
|
|
|
}
|
2017-06-22 21:59:54 +00:00
|
|
|
|
}
|
2017-05-27 23:51:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PermissionCache GetCache(ulong guildId)
|
|
|
|
|
{
|
|
|
|
|
if (!Cache.TryGetValue(guildId, out var pc))
|
|
|
|
|
{
|
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
|
|
|
|
var config = uow.GuildConfigs.For(guildId,
|
|
|
|
|
set => set.Include(x => x.Permissions));
|
|
|
|
|
UpdateCache(config);
|
|
|
|
|
}
|
|
|
|
|
Cache.TryGetValue(guildId, out pc);
|
|
|
|
|
if (pc == null)
|
|
|
|
|
throw new Exception("Cache is null.");
|
|
|
|
|
}
|
|
|
|
|
return pc;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-15 03:04:16 +00:00
|
|
|
|
private void TryMigratePermissions()
|
2017-05-27 23:51:22 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
using (var uow = _db.UnitOfWork)
|
2017-05-27 23:51:22 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
var bc = uow.BotConfig.GetOrCreate();
|
|
|
|
|
var log = LogManager.GetCurrentClassLogger();
|
|
|
|
|
if (bc.PermissionVersion <= 1)
|
2017-05-27 23:51:22 +00:00
|
|
|
|
{
|
2017-05-29 04:13:22 +00:00
|
|
|
|
log.Info("Permission version is 1, upgrading to 2.");
|
|
|
|
|
var oldCache = new ConcurrentDictionary<ulong, OldPermissionCache>(uow.GuildConfigs
|
|
|
|
|
.OldPermissionsForAll()
|
|
|
|
|
.Where(x => x.RootPermission != null) // there is a check inside already, but just in case
|
|
|
|
|
.ToDictionary(k => k.GuildId,
|
|
|
|
|
v => new OldPermissionCache()
|
|
|
|
|
{
|
|
|
|
|
RootPermission = v.RootPermission,
|
|
|
|
|
Verbose = v.VerbosePermissions,
|
|
|
|
|
PermRole = v.PermissionRole
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
if (oldCache.Any())
|
2017-05-27 23:51:22 +00:00
|
|
|
|
{
|
2017-05-29 04:13:22 +00:00
|
|
|
|
log.Info("Old permissions found. Performing one-time migration to v2.");
|
|
|
|
|
var i = 0;
|
|
|
|
|
foreach (var oc in oldCache)
|
2017-05-27 23:51:22 +00:00
|
|
|
|
{
|
2017-05-29 04:13:22 +00:00
|
|
|
|
if (i % 3 == 0)
|
|
|
|
|
log.Info("Migrating Permissions #" + i + " - GuildId: " + oc.Key);
|
|
|
|
|
i++;
|
|
|
|
|
var gc = uow.GuildConfigs.GcWithPermissionsv2For(oc.Key);
|
2017-05-27 23:51:22 +00:00
|
|
|
|
|
2017-05-29 04:13:22 +00:00
|
|
|
|
var oldPerms = oc.Value.RootPermission.AsEnumerable().Reverse().ToList();
|
|
|
|
|
uow._context.Set<Permission>().RemoveRange(oldPerms);
|
|
|
|
|
gc.RootPermission = null;
|
|
|
|
|
if (oldPerms.Count > 2)
|
2017-05-27 23:51:22 +00:00
|
|
|
|
{
|
2017-05-29 04:13:22 +00:00
|
|
|
|
|
|
|
|
|
var newPerms = oldPerms.Take(oldPerms.Count - 1)
|
|
|
|
|
.Select(x => x.Tov2())
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var allowPerm = Permissionv2.AllowAllPerm;
|
|
|
|
|
var firstPerm = newPerms[0];
|
|
|
|
|
if (allowPerm.State != firstPerm.State ||
|
|
|
|
|
allowPerm.PrimaryTarget != firstPerm.PrimaryTarget ||
|
|
|
|
|
allowPerm.SecondaryTarget != firstPerm.SecondaryTarget ||
|
|
|
|
|
allowPerm.PrimaryTargetId != firstPerm.PrimaryTargetId ||
|
|
|
|
|
allowPerm.SecondaryTargetName != firstPerm.SecondaryTargetName)
|
|
|
|
|
newPerms.Insert(0, Permissionv2.AllowAllPerm);
|
|
|
|
|
Cache.TryAdd(oc.Key, new PermissionCache
|
|
|
|
|
{
|
|
|
|
|
Permissions = new PermissionsCollection<Permissionv2>(newPerms),
|
|
|
|
|
Verbose = gc.VerbosePermissions,
|
|
|
|
|
PermRole = gc.PermissionRole,
|
|
|
|
|
});
|
|
|
|
|
gc.Permissions = newPerms;
|
|
|
|
|
}
|
2017-05-27 23:51:22 +00:00
|
|
|
|
}
|
2017-05-29 04:13:22 +00:00
|
|
|
|
log.Info("Permission migration to v2 is done.");
|
2017-05-27 23:51:22 +00:00
|
|
|
|
}
|
2017-05-29 04:13:22 +00:00
|
|
|
|
|
2017-06-22 21:59:54 +00:00
|
|
|
|
bc.PermissionVersion = 2;
|
|
|
|
|
uow.Complete();
|
2017-05-27 23:51:22 +00:00
|
|
|
|
}
|
2017-07-15 03:04:16 +00:00
|
|
|
|
if (bc.PermissionVersion <= 2)
|
2017-06-01 03:12:00 +00:00
|
|
|
|
{
|
|
|
|
|
var oldPrefixes = new[] { ".", ";", "!!", "!m", "!", "+", "-", "$", ">" };
|
|
|
|
|
uow._context.Database.ExecuteSqlCommand(
|
2017-09-17 05:28:48 +00:00
|
|
|
|
@"UPDATE Permissionv2
|
2017-06-01 03:12:00 +00:00
|
|
|
|
SET secondaryTargetName=trim(substr(secondaryTargetName, 3))
|
|
|
|
|
WHERE secondaryTargetName LIKE '!!%' OR secondaryTargetName LIKE '!m%';
|
|
|
|
|
|
2017-09-17 05:28:48 +00:00
|
|
|
|
UPDATE Permissionv2
|
2017-06-01 03:12:00 +00:00
|
|
|
|
SET secondaryTargetName=substr(secondaryTargetName, 2)
|
|
|
|
|
WHERE secondaryTargetName LIKE '.%' OR
|
2017-07-15 03:04:16 +00:00
|
|
|
|
secondaryTargetName LIKE '~%' OR
|
|
|
|
|
secondaryTargetName LIKE ';%' OR
|
|
|
|
|
secondaryTargetName LIKE '>%' OR
|
|
|
|
|
secondaryTargetName LIKE '-%' OR
|
|
|
|
|
secondaryTargetName LIKE '!%';");
|
2017-06-22 21:59:54 +00:00
|
|
|
|
bc.PermissionVersion = 3;
|
|
|
|
|
uow.Complete();
|
2017-06-01 03:12:00 +00:00
|
|
|
|
}
|
2017-05-27 23:51:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task AddPermissions(ulong guildId, params Permissionv2[] perms)
|
|
|
|
|
{
|
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
|
|
|
|
var config = uow.GuildConfigs.GcWithPermissionsv2For(guildId);
|
|
|
|
|
//var orderedPerms = new PermissionsCollection<Permissionv2>(config.Permissions);
|
|
|
|
|
var max = config.Permissions.Max(x => x.Index); //have to set its index to be the highest
|
|
|
|
|
foreach (var perm in perms)
|
|
|
|
|
{
|
|
|
|
|
perm.Index = ++max;
|
|
|
|
|
config.Permissions.Add(perm);
|
|
|
|
|
}
|
|
|
|
|
await uow.CompleteAsync().ConfigureAwait(false);
|
|
|
|
|
UpdateCache(config);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateCache(GuildConfig config)
|
|
|
|
|
{
|
|
|
|
|
Cache.AddOrUpdate(config.GuildId, new PermissionCache()
|
|
|
|
|
{
|
|
|
|
|
Permissions = new PermissionsCollection<Permissionv2>(config.Permissions),
|
|
|
|
|
PermRole = config.PermissionRole,
|
|
|
|
|
Verbose = config.VerbosePermissions
|
|
|
|
|
}, (id, old) =>
|
|
|
|
|
{
|
|
|
|
|
old.Permissions = new PermissionsCollection<Permissionv2>(config.Permissions);
|
|
|
|
|
old.PermRole = config.PermissionRole;
|
|
|
|
|
old.Verbose = config.VerbosePermissions;
|
|
|
|
|
return old;
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-05-29 04:13:22 +00:00
|
|
|
|
|
2017-06-19 13:42:10 +00:00
|
|
|
|
public async Task<bool> TryBlockLate(DiscordSocketClient client, IUserMessage msg, IGuild guild, IMessageChannel channel, IUser user, string moduleName, string commandName)
|
2017-05-29 04:13:22 +00:00
|
|
|
|
{
|
|
|
|
|
await Task.Yield();
|
|
|
|
|
if (guild == null)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-05-29 23:53:16 +00:00
|
|
|
|
else
|
2017-05-29 04:13:22 +00:00
|
|
|
|
{
|
2017-05-29 23:53:16 +00:00
|
|
|
|
var resetCommand = commandName == "resetperms";
|
2017-05-29 04:13:22 +00:00
|
|
|
|
|
2017-05-29 23:53:16 +00:00
|
|
|
|
PermissionCache pc = GetCache(guild.Id);
|
|
|
|
|
if (!resetCommand && !pc.Permissions.CheckPermissions(msg, commandName, moduleName, out int index))
|
2017-05-29 04:13:22 +00:00
|
|
|
|
{
|
2017-05-30 04:54:59 +00:00
|
|
|
|
if (pc.Verbose)
|
2017-06-25 02:58:48 +00:00
|
|
|
|
try { await channel.SendErrorAsync(_strings.GetText("trigger", guild.Id, "Permissions".ToLowerInvariant(), index + 1, Format.Bold(pc.Permissions[index].GetCommand(_cmd.GetPrefix(guild), (SocketGuild)guild)))).ConfigureAwait(false); } catch { }
|
2017-05-29 04:13:22 +00:00
|
|
|
|
return true;
|
2017-05-29 23:53:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (moduleName == "Permissions")
|
|
|
|
|
{
|
|
|
|
|
var roles = (user as SocketGuildUser)?.Roles ?? ((IGuildUser)user).RoleIds.Select(x => guild.GetRole(x)).Where(x => x != null);
|
|
|
|
|
if (!roles.Any(r => r.Name.Trim().ToLowerInvariant() == pc.PermRole.Trim().ToLowerInvariant()) && user.Id != ((IGuildUser)user).Guild.OwnerId)
|
|
|
|
|
{
|
2017-05-30 04:54:59 +00:00
|
|
|
|
var returnMsg = $"You need the **{pc.PermRole}** role in order to use permission commands.";
|
|
|
|
|
if (pc.Verbose)
|
|
|
|
|
try { await channel.SendErrorAsync(returnMsg).ConfigureAwait(false); } catch { }
|
2017-05-29 23:53:16 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-05-29 04:13:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-05-25 02:24:43 +00:00
|
|
|
|
}
|
2017-07-15 03:04:16 +00:00
|
|
|
|
}
|