public bool AddPermission(IUser user, string permission) { GuardPermission(ref permission); GuardTarget(user); PlayerPermissionSection permsSection = GetConfigSection <PlayerPermissionSection>(user, true); List <string> groupPermissions = permsSection.Permissions.ToList(); groupPermissions.Add(permission); permsSection.Permissions = groupPermissions.ToArray(); permsSection.Save(); return(true); }
public async Task <T> GetConfigSectionAsync <T>(IPermissionActor target, bool createIfNotFound) where T : PermissionSection { GuardTarget(target); bool isPermissionGroup = target is IPermissionGroup; IConfiguration config = null; string path = null; if (isPermissionGroup) { config = GroupsConfig; path = "Groups"; } else { config = PlayersConfig; path = "Users"; } if (createIfNotFound && !config.ChildExists(path)) { config.CreateSection(path, SectionType.Array); await config.SaveAsync(); } else if (!createIfNotFound && !config.ChildExists(path)) { return(null); } IConfigurationElement configElement = config[path]; if (configElement.Type != SectionType.Array) { throw new Exception("Expected array type but got " + configElement.Type); } List <PermissionSection> values = configElement.Get <PermissionSection[]>().ToList(); if (!values.Any(c => c.Id.Equals(target.Id))) { if (!createIfNotFound) { return(null); } PermissionSection toCreate; if (target is IPermissionGroup) { toCreate = new GroupPermissionSection(target.Id, configElement); } else { toCreate = new PlayerPermissionSection(target.Id, configElement); } await toCreate.SaveAsync(); } T section = configElement.Get <T[]>().FirstOrDefault(c => c.Id == target.Id); section?.SetConfigElement(configElement); return(section); }