Work on permissions, added tests for permission linked list

This commit is contained in:
Kwoth
2016-09-27 15:26:37 +02:00
parent 597b73d38c
commit 210ca07cde
29 changed files with 13146 additions and 36 deletions

View File

@@ -1,5 +1,6 @@
using Discord;
using Discord.Commands;
using NadekoBot.Services.Database;
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Generic;
@@ -51,8 +52,8 @@ namespace NadekoBot.Modules.Permissions
if (!((perm.SecondaryTarget == SecondaryPermissionType.Command &&
perm.SecondaryTargetName == command.Text.ToLowerInvariant()) ||
((perm.SecondaryTarget == SecondaryPermissionType.Module || perm.SecondaryTarget == SecondaryPermissionType.AllCommands) &&
perm.SecondaryTargetName == command.Module.Name.ToLowerInvariant()) ||
perm.SecondaryTarget == SecondaryPermissionType.AllModules ||
perm.SecondaryTargetName == command.Module.Name.ToLowerInvariant()) ||
perm.SecondaryTarget == SecondaryPermissionType.AllModules ||
(perm.SecondaryTarget == SecondaryPermissionType.AllCommands && perm.SecondaryTargetName == command.Module.Name.ToLowerInvariant())))
return null;
@@ -126,5 +127,109 @@ namespace NadekoBot.Modules.Permissions
return NadekoBot.ModulePrefixes[typeof(Permissions).Name] + com;
}
public static void Add(this Permission perm, Permission toAdd)
{
var last = perm;
while (last.Next != null)
{
last = last.Next;
}
toAdd.Previous = last;
last.Next = toAdd;
toAdd.Next = null;
}
public static void Insert(this Permission perm, int index, Permission toAdd)
{
if (index < 0)
throw new IndexOutOfRangeException();
if (index == 0)
{
perm.Previous = toAdd;
toAdd.Next = perm;
return;
}
var atIndex = perm;
var i = 0;
while (i != index)
{
atIndex = atIndex.Next;
i++;
if (atIndex == null)
throw new IndexOutOfRangeException();
}
var previous = atIndex.Previous;
//connect right side
atIndex.Previous = toAdd;
toAdd.Next = atIndex;
//connect left side
toAdd.Previous = previous;
previous.Next = toAdd;
}
public static Permission RemoveAt(this Permission perm, int index)
{
if (index < 0)
throw new IndexOutOfRangeException();
if (index == 0)
{
perm.Next.Previous = null;
perm.Next = null;
return perm;
}
var toRemove = perm;
var i = 0;
while (i != index)
{
toRemove = toRemove.Next;
i++;
if (toRemove == null)
throw new IndexOutOfRangeException();
}
toRemove.Previous.Next = toRemove.Next;
toRemove.Next.Previous = toRemove.Previous;
return toRemove;
}
public static Permission GetAt(this Permission perm, int index)
{
if (index < 0)
throw new IndexOutOfRangeException();
var temp = perm;
while (index > 0) { temp = temp?.Next; index--; }
if (temp == null)
throw new IndexOutOfRangeException();
return temp;
}
public static int Count(this Permission perm)
{
var i = 1;
var temp = perm;
while ((temp = temp.Next) != null) { i++; }
return i;
}
public static IEnumerable<Permission> AsEnumerable(this Permission perm)
{
do yield return perm;
while ((perm = perm.Next) != null);
}
public static Permission GetRoot(this Permission perm)
{
Permission toReturn;
do toReturn = perm;
while ((perm = perm.Previous) != null);
return toReturn;
}
}
}

View File

@@ -30,7 +30,7 @@ namespace NadekoBot.Modules.Permissions
string toSend = "";
using (var uow = DbHandler.UnitOfWork())
{
var perms = uow.GuildConfigs.For(channel.Guild.Id).Permissions.AsEnumerable().Reverse();
var perms = uow.GuildConfigs.For(channel.Guild.Id).RootPermission.AsEnumerable().Reverse();
var i = 1;
toSend = String.Join("\n", perms.Select(p => $"`{(i++)}.` {p.GetCommand()}"));
@@ -52,10 +52,8 @@ namespace NadekoBot.Modules.Permissions
Permission p;
using (var uow = DbHandler.UnitOfWork())
{
var perms = uow.GuildConfigs.For(channel.Guild.Id).Permissions.AsEnumerable().ToList();
p = perms[perms.Count - index];
perms.RemoveAt(perms.Count - index);
uow.GuildConfigs.For(channel.Guild.Id).Permissions = perms;
var perms = uow.GuildConfigs.For(channel.Guild.Id).RootPermission;
p = perms.RemoveAt(perms.Count() - index);
await uow.CompleteAsync().ConfigureAwait(false);
}
await channel.SendMessageAsync($"`Removed permission \"{p.GetCommand()}\" from position #{index}.`").ConfigureAwait(false);
@@ -78,13 +76,13 @@ namespace NadekoBot.Modules.Permissions
Permission toInsert;
using (var uow = DbHandler.UnitOfWork())
{
var perms = uow.GuildConfigs.For(channel.Guild.Id).Permissions.AsEnumerable().ToList();
toInsert = perms[perms.Count - from];
perms.RemoveAt(perms.Count - from);
uow.GuildConfigs.For(channel.Guild.Id).Permissions = perms;
var perms = uow.GuildConfigs.For(channel.Guild.Id).RootPermission;
var count = perms.Count();
toInsert = perms.RemoveAt(count - from);
if (from < to)
to -= 1;
perms.Insert(perms.Count - to, toInsert);
perms.Insert(count - to, toInsert);
uow.GuildConfigs.For(channel.Guild.Id).RootPermission = perms;
await uow.CompleteAsync().ConfigureAwait(false);
}
await channel.SendMessageAsync($"`Moved permission \"{toInsert.GetCommand()}\" from #{from} to #{to}.`").ConfigureAwait(false);
@@ -105,7 +103,7 @@ namespace NadekoBot.Modules.Permissions
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(channel.Guild.Id).Permissions.Add(new Permission
uow.GuildConfigs.For(channel.Guild.Id).RootPermission.Add(new Permission
{
PrimaryTarget = PrimaryPermissionType.User,
PrimaryTargetId = user.Id,
@@ -126,7 +124,7 @@ namespace NadekoBot.Modules.Permissions
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(channel.Guild.Id).Permissions.Add(new Permission
uow.GuildConfigs.For(channel.Guild.Id).RootPermission.Add(new Permission
{
PrimaryTarget = PrimaryPermissionType.User,
PrimaryTargetId = user.Id,
@@ -147,7 +145,7 @@ namespace NadekoBot.Modules.Permissions
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(channel.Guild.Id).Permissions.Add(new Permission
uow.GuildConfigs.For(channel.Guild.Id).RootPermission.Add(new Permission
{
PrimaryTarget = PrimaryPermissionType.Role,
PrimaryTargetId = role.Id,
@@ -168,7 +166,7 @@ namespace NadekoBot.Modules.Permissions
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(channel.Guild.Id).Permissions.Add(new Permission
uow.GuildConfigs.For(channel.Guild.Id).RootPermission.Add(new Permission
{
PrimaryTarget = PrimaryPermissionType.Role,
PrimaryTargetId = role.Id,
@@ -189,7 +187,7 @@ namespace NadekoBot.Modules.Permissions
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(channel.Guild.Id).Permissions.Add(new Permission
uow.GuildConfigs.For(channel.Guild.Id).RootPermission.Add(new Permission
{
PrimaryTarget = PrimaryPermissionType.Channel,
PrimaryTargetId = chnl.Id,
@@ -210,7 +208,7 @@ namespace NadekoBot.Modules.Permissions
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(channel.Guild.Id).Permissions.Add(new Permission
uow.GuildConfigs.For(channel.Guild.Id).RootPermission.Add(new Permission
{
PrimaryTarget = PrimaryPermissionType.Channel,
PrimaryTargetId = chnl.Id,
@@ -231,7 +229,7 @@ namespace NadekoBot.Modules.Permissions
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(channel.Guild.Id).Permissions.Add(new Permission
uow.GuildConfigs.For(channel.Guild.Id).RootPermission.Add(new Permission
{
PrimaryTarget = PrimaryPermissionType.Channel,
PrimaryTargetId = chnl.Id,
@@ -252,7 +250,7 @@ namespace NadekoBot.Modules.Permissions
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(channel.Guild.Id).Permissions.Add(new Permission
uow.GuildConfigs.For(channel.Guild.Id).RootPermission.Add(new Permission
{
PrimaryTarget = PrimaryPermissionType.Role,
PrimaryTargetId = role.Id,
@@ -273,7 +271,7 @@ namespace NadekoBot.Modules.Permissions
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(channel.Guild.Id).Permissions.Add(new Permission
uow.GuildConfigs.For(channel.Guild.Id).RootPermission.Add(new Permission
{
PrimaryTarget = PrimaryPermissionType.User,
PrimaryTargetId = user.Id,
@@ -294,7 +292,7 @@ namespace NadekoBot.Modules.Permissions
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(channel.Guild.Id).Permissions.Add(new Permission
uow.GuildConfigs.For(channel.Guild.Id).RootPermission.Add(new Permission
{
PrimaryTarget = PrimaryPermissionType.Channel,
PrimaryTargetId = chnl.Id,
@@ -315,7 +313,7 @@ namespace NadekoBot.Modules.Permissions
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(channel.Guild.Id).Permissions.Add(new Permission
uow.GuildConfigs.For(channel.Guild.Id).RootPermission.Add(new Permission
{
PrimaryTarget = PrimaryPermissionType.Role,
PrimaryTargetId = role.Id,
@@ -336,7 +334,7 @@ namespace NadekoBot.Modules.Permissions
using (var uow = DbHandler.UnitOfWork())
{
uow.GuildConfigs.For(channel.Guild.Id).Permissions.Add(new Permission
uow.GuildConfigs.For(channel.Guild.Id).RootPermission.Add(new Permission
{
PrimaryTarget = PrimaryPermissionType.User,
PrimaryTargetId = user.Id,