private void GuardTarget(IPermissionEntity target) { if (!SupportsTarget(target)) { throw new NotSupportedException(target.GetType().FullName + " is not supported!"); } }
public Task <bool> AddDeniedPermissionAsync(IPermissionEntity target, string permission) { GuardPermission(ref permission); GuardTarget(target); return(AddPermissionAsync(target, "!" + permission)); }
public async Task <PermissionResult> CheckHasAllPermissionsAsync(IPermissionEntity target, params string[] permissions) { GuardLoaded(); GuardPermissions(permissions); GuardTarget(target); PermissionResult result = PermissionResult.Grant; foreach (string permission in permissions) { PermissionResult tmp = await CheckPermissionAsync(target, permission); if (tmp == PermissionResult.Deny) { return(PermissionResult.Deny); } if (tmp == PermissionResult.Default) { result = PermissionResult.Default; } } return(result); }
public async Task <IEnumerable <string> > GetDeniedPermissionsAsync(IPermissionEntity target, bool inherit = true) { PermissionSection section = target is IPermissionGroup ? (PermissionSection) await GetConfigSectionAsync <GroupPermissionSection>(target, false) : await GetConfigSectionAsync <PlayerPermissionSection>(target, false); List <string> permissions; if (section != null) { permissions = section.Permissions .Where(c => c.StartsWith("!")) .Select(c => c.Substring(1).ToLower()) .ToList(); } else { permissions = new List <string>(); } if (inherit) { foreach (var parent in await GetGroupsAsync(target)) { permissions .AddRange(await GetDeniedPermissionsAsync(parent, true)); } } return(permissions.Distinct()); }
public async Task <PermissionResult> CheckHasAnyPermissionAsync(IPermissionEntity target, params string[] permissions) { GuardLoaded(); GuardPermissions(permissions); GuardTarget(target); foreach (string permission in permissions) { Console.WriteLine("Checking: " + permission); PermissionResult result = await CheckPermissionAsync(target, permission); if (result == PermissionResult.Deny) { Console.WriteLine("Denied: " + permission); return(PermissionResult.Deny); } if (result == PermissionResult.Grant) { Console.WriteLine("Granted: " + permission); return(PermissionResult.Grant); } } Console.WriteLine("Default: " + permissions); return(PermissionResult.Default); }
public IEnumerable <string> GetGrantedPermissions(IPermissionEntity target, bool inherit = true) { PermissionSection section = target is IPermissionGroup ? (PermissionSection)GetConfigSection <GroupPermissionSection>(target, false) : GetConfigSection <PlayerPermissionSection>(target, false); List <string> permissions; if (section != null) { permissions = section?.Permissions .Where(c => !c.StartsWith("!")) .Select(c => c.ToLower()) .ToList(); } else { permissions = new List <string>(); } if (inherit) { foreach (var parent in GetGroups(target)) { permissions .AddRange(GetGrantedPermissions(parent, true)); } } return(permissions.Distinct()); }
public bool RemoveDeniedPermission(IPermissionEntity target, string permission) { GuardPermission(ref permission); GuardTarget(target); return(RemovePermission(target, "!" + permission)); }
public async Task <bool> RemoveDeniedPermissionAsync(IPermissionEntity target, string permission) { GuardPermission(ref permission); GuardTarget(target); return(await RemovePermissionAsync(target, "!" + permission)); }
public async Task <PermissionResult> CheckPermissionAsync(IPermissionEntity target, string permission) { GuardLoaded(); GuardPermission(ref permission); GuardTarget(target); permission = permission.Replace("!!", ""); //remove double negations if (!permission.StartsWith("!") && await CheckPermissionAsync(target, "!" + permission) == PermissionResult.Grant) { return(PermissionResult.Deny); } IEnumerable <string> permissionTree = BuildPermissionTree(permission); PermissionSection section = target is IPermissionGroup ? (PermissionSection) await GetConfigSectionAsync <GroupPermissionSection>(target, false) : await GetConfigSectionAsync <PlayerPermissionSection>(target, false); string[] permissions = section?.Permissions ?? new string[0]; foreach (string permissionNode in permissionTree) { foreach (string c in permissions) { if (c.Contains("*") && !c.StartsWith("!") && permission.StartsWith("!")) { continue; } if (c.Trim().Equals(permissionNode, StringComparison.OrdinalIgnoreCase)) { return(PermissionResult.Grant); } } } // check parent group permissions / player group permissions IEnumerable <IPermissionGroup> groups = await GetGroupsAsync(target); foreach (IPermissionGroup group in groups) { PermissionResult result = await CheckPermissionAsync(group, permission); if (result == PermissionResult.Grant) { return(PermissionResult.Grant); } if (result == PermissionResult.Deny) { return(PermissionResult.Deny); } } return(PermissionResult.Default); }
public static string GetDisplayName(this IPermissionEntity target) { if (target is IUser user) { return(user.DisplayName); } return(target.Id); }
public bool SupportsTarget(IPermissionEntity target) { if (target is UnturnedUser user) { return(user.Player.IsAdmin); } return(false); }
public async Task <IPermissionGroup> GetPrimaryGroupAsync(IPermissionEntity permissionEntity) { GuardLoaded(); if (!(permissionEntity is IUser)) { throw new NotSupportedException(); } return((await GetGroupsAsync(permissionEntity)).OrderByDescending(c => c.Priority).FirstOrDefault()); }
private bool DeleteConfigSection(IPermissionEntity target) { IConfigurationElement config = target is IPermissionGroup ? GroupsConfig["Groups"] : PlayersConfig[((IUser)target).UserType]; List <PermissionSection> values = config.Get <PermissionSection[]>().ToList(); int i = values.RemoveAll(c => c.Id.Equals(target.Id, StringComparison.OrdinalIgnoreCase)); config.Set(values); return(i > 0); }
protected override async Task UpdatePermissionAsync(IUser user, IPermissionProvider permissions, IPermissionEntity target, string permissionToUpdate) { if (await permissions.AddPermissionAsync(target, permissionToUpdate)) { await user.SendMessageAsync($"Successfully added \"{permissionToUpdate}\" to \"{target.ToString()}\"!", Color.DarkGreen); } else { await user.SendMessageAsync($"Failed to add \"{permissionToUpdate}\" to \"{target.ToString()}\"!", Color.Red); } }
protected override void UpdatePermission(IUser user, IPermissionProvider permissions, IPermissionEntity target, string permissionToUpdate) { if (permissions.AddPermission(target, permissionToUpdate)) { user.SendMessage($"Successfully added \"{permissionToUpdate}\" to \"{target.ToString()}\"!", Color.DarkGreen); } else { user.SendMessage($"Failed to add \"{permissionToUpdate}\" to \"{target.ToString()}\"!", Color.Red); } }
public override bool SupportsTarget(IPermissionEntity target) { if (target is UnturnedUser user) { return(user.UnturnedPlayer.isAdmin); } if (target is UnturnedPlayer player) { return(player.IsAdmin); } return(false); }
protected override async Task UpdatePermissionAsync(IUser user, IPermissionProvider permissions, IPermissionEntity target, string permissionToUpdate) { if (await permissions.RemovePermissionAsync(target, permissionToUpdate)) { await user.SendMessageAsync($"Successfully removed \"{permissionToUpdate}\" from \"{target.GetDisplayName()}\"!", Color.DarkGreen); } else { await user.SendMessageAsync($"Failed to remove \"{permissionToUpdate}\" from \"{target.GetDisplayName()}\"!", Color.Red); } }
public async Task <IPermissionGroup> GetPrimaryGroupAsync(IPermissionEntity user) { IPermissionGroup group; foreach (IPermissionProvider service in ProxiedServices.Where(c => c.SupportsTarget(user))) { if ((group = await service.GetPrimaryGroupAsync(user)) != null) { return(group); } } return(null); }
public bool AddPermission(IPermissionEntity target, string permission) { GuardPermission(ref permission); GuardTarget(target); PermissionSection section = target is IPermissionGroup ? (PermissionSection)GetConfigSection <GroupPermissionSection>(target, true) : GetConfigSection <PlayerPermissionSection>(target, true); List <string> permissions = section.Permissions.ToList(); permissions.Add(permission); section.Permissions = permissions.ToArray(); section.Save(); return(true); }
public async Task <bool> AddPermissionAsync(IPermissionEntity target, string permission) { GuardPermission(ref permission); GuardTarget(target); PermissionSection section = (target is IPermissionGroup ? (PermissionSection)await GetConfigSectionAsync <GroupPermissionSection>(target, true) : await GetConfigSectionAsync <PlayerPermissionSection>(target, true)); List <string> permissions = section.Permissions.ToList(); permissions.Add(permission); section.Permissions = permissions.ToArray(); await section.SaveAsync(); return(true); }
public PermissionResult CheckHasAnyPermission(IPermissionEntity target, params string[] permissions) { GuardTarget(target); foreach (IPermissionProvider provider in ProxiedServices.Where(c => c.SupportsTarget(target))) { PermissionResult result = provider.CheckHasAnyPermission(target, permissions); if (result == PermissionResult.Default) { continue; } return(result); } return(PermissionResult.Default); }
public async Task <PermissionResult> CheckPermissionAsync(IPermissionEntity target, string permission) { GuardTarget(target); foreach (IPermissionProvider provider in ProxiedServices.Where(c => c.SupportsTarget(target))) { PermissionResult result = await provider.CheckPermissionAsync(target, permission); if (result == PermissionResult.Default) { continue; } return(result); } return(PermissionResult.Default); }
public IEnumerable <IPermissionGroup> GetGroups(IPermissionEntity target) { GuardLoaded(); GuardTarget(target); PermissionSection section = target is IPermissionGroup ? (PermissionSection)GetConfigSection <GroupPermissionSection>(target, false) : GetConfigSection <PlayerPermissionSection>(target, false); if (section == null) { return(GetGroups().Where(c => c.AutoAssign)); } return(section.GetGroups() .Select(GetGroup) .Where(c => c != null)); }
public async Task <IEnumerable <IPermissionGroup> > GetGroupsAsync(IPermissionEntity target) { GuardLoaded(); GuardTarget(target); PermissionSection section = (target is IPermissionGroup ? (PermissionSection)await GetConfigSectionAsync <GroupPermissionSection>(target, false) : await GetConfigSectionAsync <PlayerPermissionSection>(target, false)); if (section == null) { return((await GetGroupsAsync()).Where(c => c.AutoAssign)); } return(section.GetGroups() .Select(e => GetGroupAsync(e).GetAwaiter().GetResult()) .Where(c => c != null)); }
public bool RemovePermission(IPermissionEntity target, string permission) { GuardPermission(ref permission); PermissionSection section = target is IPermissionGroup ? (PermissionSection)GetConfigSection <GroupPermissionSection>(target, false) : GetConfigSection <PlayerPermissionSection>(target, false); if (section == null) { return(false); } List <string> permissions = section.Permissions.ToList(); int i = permissions.RemoveAll(c => c.Trim().Equals(permission, StringComparison.OrdinalIgnoreCase)); section.Permissions = permissions.ToArray(); section.Save(); return(i > 0); }
public bool AddGroup(IPermissionEntity target, IPermissionGroup group) { GuardLoaded(); GuardTarget(target); GuardTarget(group); PermissionSection section = target is IPermissionGroup ? (PermissionSection)GetConfigSection <GroupPermissionSection>(target, true) : GetConfigSection <PlayerPermissionSection>(target, true); List <string> groups = section.GetGroups().ToList(); if (!groups.Any(c => c.Equals(group.Id, StringComparison.OrdinalIgnoreCase))) { groups.Add(group.Id); } section.SetGroups(groups.ToArray()); section.Save(); return(true); }
public bool RemoveGroup(IPermissionEntity target, IPermissionGroup group) { GuardLoaded(); GuardTarget(target); GuardTarget(group); PermissionSection section = target is IPermissionGroup ? (PermissionSection)GetConfigSection <GroupPermissionSection>(target, false) : GetConfigSection <PlayerPermissionSection>(target, false); if (section == null) { return(false); } List <string> groups = section.GetGroups().ToList(); int i = groups.RemoveAll(c => c.Equals(group.Id, StringComparison.OrdinalIgnoreCase)); section.SetGroups(groups.ToArray()); section.Save(); return(i > 0); }
public override bool SupportsTarget(IPermissionEntity target) => target is T;
protected abstract Task UpdatePermissionAsync(IUser user, IPermissionProvider permissions, IPermissionEntity target, string permissionToUpdate);
public Task <bool> RemovePermissionAsync(IPermissionEntity target, string permission) => throw new NotSupportedException("Removing permissions from proxy is not supported.");