Fixed permissions duplication bug WOOHOO

This commit is contained in:
Kwoth 2017-03-18 02:18:31 +01:00
parent 7f7c53af34
commit 60dda075a5
5 changed files with 46 additions and 14 deletions

View File

@ -59,7 +59,7 @@ namespace NadekoBot.Modules.Administration
{
using (var uow = DbHandler.UnitOfWork())
{
var config = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.Permissions));
var config = uow.GuildConfigs.GcWithPermissionsv2For(Context.Guild.Id);
config.Permissions = Permissionv2.GetDefaultPermlist;
await uow.CompleteAsync();
UpdateCache(config);

View File

@ -87,7 +87,7 @@ namespace NadekoBot.Modules.Permissions
if (i % 3 == 0)
log.Info("Migrating Permissions #" + i + " - GuildId: " + oc.Key);
i++;
var gc = uow.GuildConfigs.For(oc.Key, set => set.Include(x => x.Permissions));
var gc = uow.GuildConfigs.GcWithPermissionsv2For(oc.Key);
var oldPerms = oc.Value.RootPermission.AsEnumerable().Reverse().ToList();
uow._context.Set<Permission>().RemoveRange(oldPerms);
@ -126,7 +126,7 @@ namespace NadekoBot.Modules.Permissions
{
using (var uow = DbHandler.UnitOfWork())
{
var config = uow.GuildConfigs.For(guildId, set => set.Include(x => x.Permissions));
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)
@ -161,7 +161,7 @@ namespace NadekoBot.Modules.Permissions
{
using (var uow = DbHandler.UnitOfWork())
{
var config = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.Permissions));
var config = uow.GuildConfigs.GcWithPermissionsv2For(Context.Guild.Id);
config.VerbosePermissions = action.Value;
await uow.CompleteAsync().ConfigureAwait(false);
UpdateCache(config);
@ -185,7 +185,7 @@ namespace NadekoBot.Modules.Permissions
using (var uow = DbHandler.UnitOfWork())
{
var config = uow.GuildConfigs.For(Context.Guild.Id, set => set);
var config = uow.GuildConfigs.GcWithPermissionsv2For(Context.Guild.Id);
if (role == null)
{
await ReplyConfirmLocalized("permrole", Format.Bold(config.PermissionRole)).ConfigureAwait(false);
@ -247,7 +247,7 @@ namespace NadekoBot.Modules.Permissions
Permissionv2 p;
using (var uow = DbHandler.UnitOfWork())
{
var config = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.Permissions));
var config = uow.GuildConfigs.GcWithPermissionsv2For(Context.Guild.Id);
var permsCol = new PermissionsCollection<Permissionv2>(config.Permissions);
p = permsCol[index];
permsCol.RemoveAt(index);
@ -278,7 +278,7 @@ namespace NadekoBot.Modules.Permissions
Permissionv2 fromPerm;
using (var uow = DbHandler.UnitOfWork())
{
var config = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.Permissions));
var config = uow.GuildConfigs.GcWithPermissionsv2For(Context.Guild.Id);
var permsCol = new PermissionsCollection<Permissionv2>(config.Permissions);
var fromFound = from < permsCol.Count;

View File

@ -423,7 +423,7 @@ namespace NadekoBot.Services
{
using (var uow = DbHandler.UnitOfWork())
{
var config = uow.GuildConfigs.For(context.Guild.Id, set => set.Include(x => x.Permissions));
var config = uow.GuildConfigs.GcWithPermissionsv2For(context.Guild.Id);
Permissions.UpdateCache(config);
}
Permissions.Cache.TryGetValue(context.Guild.Id, out pc);

View File

@ -15,5 +15,6 @@ namespace NadekoBot.Services.Database.Repositories
IEnumerable<FollowedStream> GetAllFollowedStreams();
void SetCleverbotEnabled(ulong id, bool cleverbotEnabled);
IEnumerable<GuildConfig> Permissionsv2ForAll();
GuildConfig GcWithPermissionsv2For(ulong guildId);
}
}

View File

@ -67,19 +67,25 @@ namespace NadekoBot.Services.Database.Repositories.Impl
}));
_context.SaveChanges();
}
else if (config.Permissions == null)
{
config.Permissions = Permissionv2.GetDefaultPermlist;
_context.SaveChanges();
}
return config;
}
public GuildConfig LogSettingsFor(ulong guildId)
{
return _set.Include(gc => gc.LogSetting)
var config = _set.Include(gc => gc.LogSetting)
.ThenInclude(gc => gc.IgnoredChannels)
.FirstOrDefault();
if (config == null)
{
_set.Add((config = new GuildConfig
{
GuildId = guildId,
Permissions = Permissionv2.GetDefaultPermlist
}));
_context.SaveChanges();
}
return config;
}
public IEnumerable<GuildConfig> OldPermissionsForAll()
@ -108,6 +114,31 @@ namespace NadekoBot.Services.Database.Repositories.Impl
return query.ToList();
}
public GuildConfig GcWithPermissionsv2For(ulong guildId)
{
var config = _set
.Where(gc => gc.GuildId == guildId)
.Include(gc => gc.Permissions)
.FirstOrDefault();
if (config == null) // if there is no guildconfig, create new one
{
_set.Add((config = new GuildConfig
{
GuildId = guildId,
Permissions = Permissionv2.GetDefaultPermlist
}));
_context.SaveChanges();
}
else if (config.Permissions == null || !config.Permissions.Any()) // if no perms, add default ones
{
config.Permissions = Permissionv2.GetDefaultPermlist;
_context.SaveChanges();
}
return config;
}
public IEnumerable<FollowedStream> GetAllFollowedStreams() =>
_set.Include(gc => gc.FollowedStreams)
.SelectMany(gc => gc.FollowedStreams)