public static async Task <RoleSearchResult> GetRoleGroup(JsonClasses.CharacterData characterData, ulong discordUserId, string refreshToken = null) { var result = new RoleSearchResult(); var discordGuild = APIHelper.DiscordAPI.GetGuild(); var u = discordGuild.GetUser(discordUserId); try { if (characterData == null) { await AuthWarningLog(discordUserId, "[RG] Aborted due to character data is null"); result.IsConnectionError = true; return(result); } if (u != null) { result.UpdatedRoles.Add(u.Roles.FirstOrDefault(x => x.Name == "@everyone")); } var groupsToCheck = new Dictionary <string, WebAuthGroup>(); var authData = await SQLHelper.GetAuthUserByCharacterId(characterData.character_id); #region Select groups to check if (!string.IsNullOrEmpty(authData?.GroupName)) { await AuthInfoLog(characterData, $"[RG] Has group name {authData.GroupName}", true); //check specified group for roles var(groupName, group) = GetGroupByName(authData.GroupName); if (group != null) { //process upgrade groups first if (group.UpgradeGroupNames.Any()) { await AuthInfoLog(characterData, $"[RG] Adding upgrade groups: {string.Join(',', group.UpgradeGroupNames)}", true); foreach (var item in group.UpgradeGroupNames) { var(key, value) = GetGroupByName(item); if (value != null) { groupsToCheck.Add(key, value); } } } //add root group groupsToCheck.Add(groupName, group); //add downgrade groups (checked last) if (authData.IsAuthed && group.DowngradeGroupNames.Any()) { await AuthInfoLog(characterData, $"[RG] Adding downgrade groups: {string.Join(',', group.DowngradeGroupNames)}", true); foreach (var item in group.DowngradeGroupNames) { var(key, value) = GetGroupByName(item); if (value != null) { groupsToCheck.Add(key, value); } } } } else { await AuthWarningLog(characterData, "[RG] Specified group not found!", true); } } else //no auth group specifies - fresh general auth { //check only GENERAL auth groups for roles //non-general group auth should have group name supplied foreach (var(key, value) in SettingsManager.Settings.WebAuthModule.AuthGroups.Where(a => !a.Value.ESICustomAuthRoles.Any() && !a.Value.BindToMainCharacter)) { groupsToCheck.Add(key, value); } await AuthInfoLog(characterData, $"[RG] No group were specified, selected for search: {string.Join(',', groupsToCheck.Keys)}!", true); } #endregion // string groupName = null; //refresh token var tq = string.IsNullOrEmpty(refreshToken) ? null : await APIHelper.ESIAPI.RefreshToken(refreshToken, SettingsManager.Settings.WebServerModule.CcpAppClientId, SettingsManager.Settings.WebServerModule.CcpAppSecret); var uToken = tq?.Result; if (tq != null) { if (tq.Data.IsFailed) { if (!tq.Data.IsNotValid) { result.IsConnectionError = true; await AuthWarningLog(characterData, $"[RG] {characterData.name} Connection error while fetching token!"); return(result); } } } await AuthInfoLog(characterData, $"[RG] PRE TOCHECK: {string.Join(',', groupsToCheck.Keys)} CHARID: {characterData.character_id} DID: {authData.DiscordId} AUTH: {authData.AuthState} GRP: {authData.GroupName}", true); var foundGroup = await GetAuthGroupByCharacter(groupsToCheck, characterData); if (foundGroup != null) { await AuthInfoLog(characterData, $"[RG] Group found: {foundGroup.GroupName} Roles: {string.Join(',', foundGroup.RoleEntities.SelectMany(a=> a.DiscordRoles))} Titles: {string.Join(',', foundGroup.RoleEntities.SelectMany(a=> a.Titles))}!", true); //bad token if (tq != null && tq.Data.IsNotValid) { if (foundGroup.Group.RemoveAuthIfTokenIsInvalid) { await AuthWarningLog(characterData, $"[RG] User {characterData.name} token is no more valid. Authentication will be declined."); return(result); } if (foundGroup.Group.RemoveTokenIfTokenIsInvalid) { await AuthWarningLog(characterData, $"[RG] User {characterData.name} token is no more valid. Token will be omitted from auth data."); result.TokenMustBeDeleted = true; // return result; } } await UpdateResultRolesWithTitles(discordGuild, foundGroup.RoleEntities, result, characterData, uToken); result.ValidManualAssignmentRoles.AddRange(foundGroup.Group.ManualAssignmentRoles.Where(a => !result.ValidManualAssignmentRoles.Contains(a))); result.GroupName = foundGroup.GroupName; result.Group = foundGroup.Group; } else { await AuthInfoLog(characterData, $"[RG] Group not found", true); } return(result); } catch (Exception ex) { await LogHelper.LogError($"EXCEPTION: {ex.Message} CHARACTER: {characterData.character_id} [{characterData?.name}][{characterData?.corporation_id}]", LogCat.AuthCheck); throw; } }
public static async Task <RoleSearchResult> GetRoleGroup(long characterID, ulong discordUserId, bool isManualAuth = false, string refreshToken = null) { var result = new RoleSearchResult(); var discordGuild = APIHelper.DiscordAPI.GetGuild(); var u = discordGuild.GetUser(discordUserId); var characterData = await APIHelper.ESIAPI.GetCharacterData("authCheck", characterID, true); try { if (characterData == null) { return(result); } if (u != null) { result.UpdatedRoles.Add(u.Roles.FirstOrDefault(x => x.Name == "@everyone")); } var groupsToCheck = new Dictionary <string, WebAuthGroup>(); var authData = await SQLHelper.GetAuthUserByCharacterId(characterID); #region Select groups to check if (!string.IsNullOrEmpty(authData?.GroupName)) { //check specified group for roles var group = SettingsManager.Settings.WebAuthModule.AuthGroups.FirstOrDefault(a => a.Key == authData.GroupName); if (group.Value != null) { //process upgrade groups first if (group.Value.UpgradeGroupNames.Any()) { foreach (var item in group.Value.UpgradeGroupNames) { var(key, value) = SettingsManager.Settings.WebAuthModule.AuthGroups.FirstOrDefault(a => a.Key == item); if (value != null) { groupsToCheck.Add(key, value); } } } //add root group groupsToCheck.Add(group.Key, group.Value); //add downgrade groups (checked last) if (authData.IsAuthed && group.Value.DowngradeGroupNames.Any()) { foreach (var item in group.Value.DowngradeGroupNames) { var(key, value) = SettingsManager.Settings.WebAuthModule.AuthGroups.FirstOrDefault(a => a.Key == item); if (value != null) { groupsToCheck.Add(key, value); } } } } } else //no auth group specifies - fresh general auth { //check only GENERAL auth groups for roles //non-general group auth should have group name supplied foreach (var(key, value) in SettingsManager.Settings.WebAuthModule.AuthGroups.Where(a => !a.Value.ESICustomAuthRoles.Any())) { groupsToCheck.Add(key, value); } } #endregion string groupName = null; //refresh token var uToken = string.IsNullOrEmpty(refreshToken) ? null : await APIHelper.ESIAPI.RefreshToken(refreshToken, SettingsManager.Settings.WebServerModule.CcpAppClientId, SettingsManager.Settings.WebServerModule.CcpAppSecret); var foundGroup = await GetAuthGroupByCharacter(groupsToCheck, characterID); if (foundGroup != null) { await UpdateResultRolesWithTitles(discordGuild, foundGroup.RoleEntities, result, characterID, uToken); result.ValidManualAssignmentRoles.AddRange(foundGroup.Group.ManualAssignmentRoles.Where(a => !result.ValidManualAssignmentRoles.Contains(a))); result.GroupName = foundGroup.GroupName; result.Group = foundGroup.Group; groupsToCheck.Clear(); groupsToCheck.Add(foundGroup.GroupName, foundGroup.Group); } // var hasAuth = foundGroup != null; // Check for Character Roles /*var authResultCharacter = await GetAuthGroupByCharacterId(groupsToCheck, characterID); * if (authResultCharacter != null) * { * await UpdateResultRolesWithTitles(discordGuild, authResultCharacter.RoleEntity, result, characterID, uToken); * result.ValidManualAssignmentRoles.AddRange(authResultCharacter.Group.ManualAssignmentRoles.Where(a => !result.ValidManualAssignmentRoles.Contains(a))); * groupName = SettingsManager.Settings.WebAuthModule.AuthGroups.FirstOrDefault(a => a.Value == authResultCharacter.Group).Key; * hasAuth = true; * groupsToCheck.Clear(); * groupsToCheck.Add(groupName, authResultCharacter.Group); * } * * if (authResultCharacter == null || (authResultCharacter.Group != null && !authResultCharacter.Group.UseStrictAuthenticationMode)) * { * // Check for Corporation Roles * var authResultCorporation = await GetAuthGroupByCorpId(groupsToCheck, characterData.corporation_id); * if (authResultCorporation != null) * { * await UpdateResultRolesWithTitles(discordGuild, authResultCorporation.RoleEntity, result, characterID, uToken); * result.ValidManualAssignmentRoles.AddRange(authResultCorporation.Group.ManualAssignmentRoles.Where(a => !result.ValidManualAssignmentRoles.Contains(a))); * groupName = SettingsManager.Settings.WebAuthModule.AuthGroups.FirstOrDefault(a => a.Value == authResultCorporation.Group).Key; * hasAuth = true; * groupsToCheck.Clear(); * groupsToCheck.Add(groupName, authResultCorporation.Group); * } * * var group = authResultCharacter?.Group ?? authResultCorporation?.Group; * * if (group == null || !group.UseStrictAuthenticationMode) * { * // Check for Alliance Roles * var authResultAlliance = await GetAuthGroupByAllyId(groupsToCheck, characterData.alliance_id ?? 0); * if (authResultAlliance != null) * { * await UpdateResultRolesWithTitles(discordGuild, authResultAlliance.RoleEntity, result, characterID, uToken); * result.ValidManualAssignmentRoles.AddRange(authResultAlliance.Group.ManualAssignmentRoles.Where(a => !result.ValidManualAssignmentRoles.Contains(a))); * groupName = SettingsManager.Settings.WebAuthModule.AuthGroups.FirstOrDefault(a => a.Value == authResultAlliance.Group).Key; * hasAuth = true; * } * } * }*/ /* if (!hasAuth) * { * result.UpdatedRoles = result.UpdatedRoles.Distinct().ToList(); * result.ValidManualAssignmentRoles = result.ValidManualAssignmentRoles.Distinct().ToList(); * //search for personal stands * var grList = groupsToCheck.Where(a => a.Value.StandingsAuth != null).ToList(); * if (grList.Count > 0) * { * var ar = await GetAuthGroupByCharacterId(groupsToCheck, characterID); * if (ar != null) * { * var aRoles = discordGuild.Roles.Where(a => ar.RoleEntity.DiscordRoles.Contains(a.Name)).ToList(); * if (aRoles.Count > 0) * result.UpdatedRoles.AddRange(aRoles); * result.ValidManualAssignmentRoles.AddRange(ar.Group.ManualAssignmentRoles); * groupName = SettingsManager.Settings.WebAuthModule.AuthGroups.FirstOrDefault(a => a.Value == ar.Group).Key; * * } * } * }*/ /* if (!hasAuth && (isManualAuth || !string.IsNullOrEmpty(authData?.GroupName))) * { * var token = await SQLHelper.GetAuthUserByCharacterId(characterID); * if (token != null && !string.IsNullOrEmpty(token.GroupName) && SettingsManager.Settings.WebAuthModule.AuthGroups.ContainsKey(token.GroupName)) * { * var group = SettingsManager.Settings.WebAuthModule.AuthGroups[token.GroupName]; * if ((!group.AllowedMembers.Any() || group.AllowedMembers.Values.All(a => a.Entities.All(b => b.ToString().All(char.IsDigit) && (long)b == 0))) * && group.StandingsAuth == null) * { * groupName = token.GroupName; * var l = group.AllowedMembers.SelectMany(a => a.Value.DiscordRoles); * var aRoles = discordGuild.Roles.Where(a => l.Contains(a.Name)).ToList(); * result.UpdatedRoles.AddRange(aRoles); * } * } * * //ordinary guest * if (string.IsNullOrEmpty(groupName)) * { * var grp = SettingsManager.Settings.WebAuthModule.AuthGroups.FirstOrDefault(a => * a.Value.AllowedMembers.Values.All(b => b.Entities.All(c => c.ToString().All(char.IsDigit) && (long)c == 0))); * if (grp.Value != null) * { * groupName = grp.Key; * var l = grp.Value.AllowedMembers.SelectMany(a => a.Value.DiscordRoles); * var aRoles = discordGuild.Roles.Where(a => l.Contains(a.Name)).ToList(); * result.UpdatedRoles.AddRange(aRoles); * } * } * }*/ // result.UpdatedRoles = result.UpdatedRoles.Distinct().ToList(); // result.GroupName = groupName; return(result); } catch (Exception ex) { await LogHelper.LogError($"EXCEPTION: {ex.Message} CHARACTER: {characterID} [{characterData?.name}][{characterData?.corporation_id}]", LogCat.AuthCheck); throw; } }
private static async Task UpdateResultRolesWithTitles(SocketGuild discordGuild, List <AuthRoleEntity> roleEntities, RoleSearchResult result, JsonClasses.CharacterData ch, string uToken) { //process titles in priority //TODO titles and general mix? var titleEntity = roleEntities.FirstOrDefault(a => a.Titles.Any()); if (titleEntity != null) { if (string.IsNullOrEmpty(uToken)) { await AuthWarningLog(ch, $"User has no ESI token but is being checked against group with Titles! Titles require `esi-characters.read_titles.v1` permissions!"); return; } var userTitles = (await APIHelper.ESIAPI.GetCharacterTitles("AuthCheck", ch.character_id, uToken))?.Select(a => a.name).ToList(); if (userTitles != null && userTitles.Any()) { foreach (var roleTitle in titleEntity.Titles.Values) { if (!roleTitle.TitleNames.ContainsAnyFromList(userTitles)) { continue; } foreach (var roleName in roleTitle.DiscordRoles) { var role = APIHelper.DiscordAPI.GetGuildRole(roleName); if (role != null && !result.UpdatedRoles.Contains(role)) { result.UpdatedRoles.Add(role); } } } } else { var forEmpty = titleEntity.Titles.FirstOrDefault(a => a.Value.TitleNames.Count == 0 && a.Value.DiscordRoles.Any()).Value; if (forEmpty != null) { foreach (var roleName in forEmpty.DiscordRoles) { var role = APIHelper.DiscordAPI.GetGuildRole(roleName); if (role != null && !result.UpdatedRoles.Contains(role)) { result.UpdatedRoles.Add(role); } } } } } else { var foundRoles = roleEntities.SelectMany(a => a.DiscordRoles).Distinct(); var aRoles = discordGuild.Roles.Where(a => foundRoles.Contains(a.Name) && !result.UpdatedRoles.Contains(a)); foreach (var role in aRoles) { if (!result.UpdatedRoles.Contains(role)) { result.UpdatedRoles.Add(role); } } } }
public static async Task <RoleSearchResult> GetRoleGroup(long characterID, ulong discordUserId, bool isManualAuth = false) { var discordGuild = APIHelper.DiscordAPI.GetGuild(); var u = discordGuild.GetUser(discordUserId); var characterData = await APIHelper.ESIAPI.GetCharacterData("authCheck", characterID, true); var result = new RoleSearchResult(); if (u != null) { result.UpdatedRoles.Add(u.Roles.FirstOrDefault(x => x.Name == "@everyone")); } #region Get personalized foundList var groupsToCheck = new List <WebAuthGroup>(); var authData = await SQLHelper.GetAuthUserByCharacterId(characterID); if (!string.IsNullOrEmpty(authData?.GroupName)) { //check specified group for roles var group = SettingsManager.Settings.WebAuthModule.AuthGroups.FirstOrDefault(a => a.Key == authData.GroupName).Value; if (group != null) { groupsToCheck.Add(group); } } if (!groupsToCheck.Any()) { //check only GENERAL auth groups for roles //non-general group auth should have group name supplied groupsToCheck.AddRange(SettingsManager.Settings.WebAuthModule.AuthGroups.Values.Where(a => !a.ESICustomAuthRoles.Any() && a.StandingsAuth == null)); } #endregion string groupName = null; var hasAuth = false; // Check for Character Roles var authResultCharacter = await GetAuthGroupByCharacterId(groupsToCheck, characterID); if (authResultCharacter != null) { var aRoles = discordGuild.Roles.Where(a => authResultCharacter.RoleEntity.DiscordRoles.Contains(a.Name) && !result.UpdatedRoles.Contains(a)).ToList(); if (aRoles.Count > 0) { result.UpdatedRoles.AddRange(aRoles); } result.ValidManualAssignmentRoles.AddRange(authResultCharacter.Group.ManualAssignmentRoles.Where(a => !result.ValidManualAssignmentRoles.Contains(a))); groupName = SettingsManager.Settings.WebAuthModule.AuthGroups.FirstOrDefault(a => a.Value == authResultCharacter.Group).Key; hasAuth = true; groupsToCheck.Clear(); groupsToCheck.Add(authResultCharacter.Group); } if (authResultCharacter == null || (authResultCharacter.Group != null && !authResultCharacter.Group.UseStrictAuthenticationMode)) { // Check for Corporation Roles var authResultCorporation = await GetAuthGroupByCorpId(groupsToCheck, characterData.corporation_id); if (authResultCorporation != null) { var aRoles = discordGuild.Roles.Where(a => authResultCorporation.RoleEntity.DiscordRoles.Contains(a.Name) && !result.UpdatedRoles.Contains(a)).ToList(); if (aRoles.Count > 0) { result.UpdatedRoles.AddRange(aRoles); } result.ValidManualAssignmentRoles.AddRange(authResultCorporation.Group.ManualAssignmentRoles.Where(a => !result.ValidManualAssignmentRoles.Contains(a))); groupName = SettingsManager.Settings.WebAuthModule.AuthGroups.FirstOrDefault(a => a.Value == authResultCorporation.Group).Key; hasAuth = true; groupsToCheck.Clear(); groupsToCheck.Add(authResultCorporation.Group); } var group = authResultCharacter?.Group ?? authResultCorporation?.Group; if (group == null || !group.UseStrictAuthenticationMode) { // Check for Alliance Roles var authResultAlliance = await GetAuthGroupByAllyId(groupsToCheck, characterData.alliance_id ?? 0); if (authResultAlliance != null) { var aRoles = discordGuild.Roles.Where(a => authResultAlliance.RoleEntity.DiscordRoles.Contains(a.Name) && !result.UpdatedRoles.Contains(a)).ToList(); if (aRoles.Count > 0) { result.UpdatedRoles.AddRange(aRoles); } result.ValidManualAssignmentRoles.AddRange(authResultAlliance.Group.ManualAssignmentRoles.Where(a => !result.ValidManualAssignmentRoles.Contains(a))); groupName = SettingsManager.Settings.WebAuthModule.AuthGroups.FirstOrDefault(a => a.Value == authResultAlliance.Group).Key; hasAuth = true; } } } if (!hasAuth) { result.UpdatedRoles = result.UpdatedRoles.Distinct().ToList(); result.ValidManualAssignmentRoles = result.ValidManualAssignmentRoles.Distinct().ToList(); //search for personal stands var grList = groupsToCheck.Where(a => a.StandingsAuth != null).ToList(); if (grList.Count > 0) { var ar = await GetAuthGroupByCharacterId(groupsToCheck, characterID); if (ar != null) { var aRoles = discordGuild.Roles.Where(a => ar.RoleEntity.DiscordRoles.Contains(a.Name)).ToList(); if (aRoles.Count > 0) { result.UpdatedRoles.AddRange(aRoles); } result.ValidManualAssignmentRoles.AddRange(ar.Group.ManualAssignmentRoles); groupName = SettingsManager.Settings.WebAuthModule.AuthGroups.FirstOrDefault(a => a.Value == ar.Group).Key; } } } if (!hasAuth && (isManualAuth || !string.IsNullOrEmpty(authData?.GroupName))) { var token = await SQLHelper.GetAuthUserByCharacterId(characterID); if (token != null && !string.IsNullOrEmpty(token.GroupName) && SettingsManager.Settings.WebAuthModule.AuthGroups.ContainsKey(token.GroupName)) { var group = SettingsManager.Settings.WebAuthModule.AuthGroups[token.GroupName]; if ((!group.AllowedAlliances.Any() || group.AllowedAlliances.Values.All(a => a.Id.All(b => b == 0))) && (!group.AllowedCorporations.Any() || group.AllowedCorporations.Values.All(a => a.Id.All(b => b == 0))) && (!group.AllowedCharacters.Any() || group.AllowedCharacters.Values.Any(a => a.Id.All(b => b == 0))) && group.StandingsAuth == null) { groupName = token.GroupName; } } //ordinary guest if (string.IsNullOrEmpty(groupName)) { var grp = SettingsManager.Settings.WebAuthModule.AuthGroups.FirstOrDefault(a => a.Value.AllowedAlliances.Values.All(b => b.Id.All(c => c == 0)) && a.Value.AllowedCorporations.Values.All(b => b.Id.All(c => c == 0))); if (grp.Value != null) { groupName = grp.Key; var l = grp.Value.AllowedCorporations.SelectMany(a => a.Value.DiscordRoles); var aRoles = discordGuild.Roles.Where(a => l.Contains(a.Name)).ToList(); result.UpdatedRoles.AddRange(aRoles); l = grp.Value.AllowedAlliances.SelectMany(a => a.Value.DiscordRoles); aRoles = discordGuild.Roles.Where(a => l.Contains(a.Name)).ToList(); result.UpdatedRoles.AddRange(aRoles); } } } result.UpdatedRoles = result.UpdatedRoles.Distinct().ToList(); result.GroupName = groupName; return(result); }