private async Task <SINSearchGroupResult> SearchForGroupsTask(string groupname) { try { SINSearchGroupResult ssgr = null; var client = StaticUtils.GetClient(); var res = await client.GetSearchGroupsWithHttpMessagesAsync(groupname, null, null); var result = await Backend.Utils.HandleError(res, res.Body) as ResultGroupGetSearchGroups; if (result == null) { return(null); } if (result.CallSuccess == true) { return(result.MySearchGroupResult); } return(null); } catch (ArgumentNullException) { throw; } catch (Exception e) { Log.Error(e); throw; } return(null); }
public async Task <ActionResult <SINSearchGroupResult> > GetGroupMembers(Guid groupid) { _logger.LogTrace("GetGroupMembers: " + groupid + "."); try { if (!ModelState.IsValid) { var errors = ModelState.Select(x => x.Value.Errors) .Where(y => y.Count > 0) .ToList(); string msg = "ModelState is invalid: "; foreach (var err in errors) { foreach (var singleerr in err) { msg += Environment.NewLine + "\t" + singleerr.ToString(); } } return(BadRequest(new HubException(msg))); } SINSearchGroupResult result = new SINSearchGroupResult(); var range = await GetSinSearchGroupResultById(groupid); result.SINGroups.Add(range); result.SINGroups = RemovePWHashRecursive(result.SINGroups); Ok(result); } catch (Exception e) { try { var user = await _signInManager.UserManager.GetUserAsync(User); var tc = new Microsoft.ApplicationInsights.TelemetryClient(); Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry telemetry = new Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry(e); telemetry.Properties.Add("User", user?.Email); telemetry.Properties.Add("groupid", groupid.ToString()); tc.TrackException(telemetry); } catch (Exception ex) { _logger.LogError(ex.ToString()); } if (e is HubException) { return(BadRequest(e)); } HubException hue = new HubException("Exception in GetSearchGroups: " + e.Message, e); return(BadRequest(hue)); } return(BadRequest()); }
private async void TlpGroupSearch_VisibleChanged(object sender, EventArgs e) { if (Visible == false) { return; } this.lSINnerName.Text = "not set"; cbShowMembers.Checked = MyCE == null ? true : false; if (this.MyCE == null) { return; } this.lSINnerName.Text = MyCE.MySINnerFile.Alias; if (MyCE?.MySINnerFile.MyGroup != null) { using (new CursorWait(true, this)) { try { using (new CursorWait(true, this)) { //MySINSearchGroupResult = null; if (String.IsNullOrEmpty(this.tbSearchGroupname.Text)) { var temp = new SINSearchGroupResult(MyCE?.MySINnerFile.MyGroup); MySINSearchGroupResult = temp; } else { MySINSearchGroupResult = await SearchForGroups(this.tbSearchGroupname.Text); } } } catch (ArgumentNullException ex) { Log.Info( "No group found with name: " + MyCE?.MySINnerFile.MyGroup.Groupname); if (MyCE?.MySINnerFile != null) { MyCE.MySINnerFile.MyGroup = null; } MyParentForm?.MyParentForm?.CheckSINnerStatus(); } catch (Exception exception) { Log.Error(exception); Program.MainForm.ShowMessageBox(exception.Message); } finally { TvGroupSearchResult_AfterSelect(sender, new TreeViewEventArgs(new TreeNode())); } } } }
private async Task <SINSearchGroupResult> SearchForGroupsTask(string groupname, string userName, string sinnername) { try { SINSearchGroupResult ssgr = null; var client = await StaticUtils.GetClient(); var response = await client.GetSearchGroupsWithHttpMessagesAsync(groupname, userName, sinnername); if ((response.Response.StatusCode == HttpStatusCode.OK)) { return((SINSearchGroupResult)response.Body); } else if ((response.Response.StatusCode == HttpStatusCode.NotFound)) { var rescontent = await response.Response.Content.ReadAsStringAsync(); string msg = "StatusCode: " + response.Response.StatusCode + Environment.NewLine; msg += rescontent; throw new ArgumentNullException(nameof(groupname), msg); } else { var rescontent = await response.Response.Content.ReadAsStringAsync(); Exception e = null; try { e = Newtonsoft.Json.JsonConvert.DeserializeObject <Exception>(rescontent); } catch (Exception exception) { throw new ArgumentException(rescontent); } if (e != null) { throw e; } } return(ssgr); } catch (ArgumentNullException) { throw; } catch (Exception e) { System.Diagnostics.Trace.TraceError(e.Message, e); throw; } return(null); }
private async void TlpGroupSearch_VisibleChanged(object sender, EventArgs e) { if (this.MyCE == null) { return; } if (Visible == false) { return; } if (MyCE?.MySINnerFile.MyGroup != null) { using (new CursorWait(true, this)) { try { using (new CursorWait(true, this)) { //MySINSearchGroupResult = null; if (String.IsNullOrEmpty(this.tbSearchGroupname.Text)) { var temp = new SINSearchGroupResult(MyCE?.MySINnerFile.MyGroup); MySINSearchGroupResult = temp; } else { MySINSearchGroupResult = await SearchForGroups(this.tbSearchGroupname.Text, null, null); } } } catch (ArgumentNullException ex) { System.Diagnostics.Trace.TraceInformation( "No group found with name: " + MyCE?.MySINnerFile.MyGroup.Groupname); if (MyCE?.MySINnerFile != null) { MyCE.MySINnerFile.MyGroup = null; } MyParentForm?.MyParentForm?.CheckSINnerStatus(); } catch (Exception exception) { MessageBox.Show(exception.Message); } finally { TvGroupSearchResult_AfterSelect(sender, new TreeViewEventArgs(new TreeNode())); } } } }
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member 'AccountController.GetSinnerAsAdmin()' public async Task <ActionResult <ResultGroupGetSearchGroups> > GetSinnerAsAdmin() #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member 'AccountController.GetSinnerAsAdmin()' { SINSearchGroupResult ret = new SINSearchGroupResult(); ResultAccountGetSinnersByAuthorization res = new ResultAccountGetSinnersByAuthorization(ret); SINnerGroup sg = new SINnerGroup(); var user = await _signInManager.UserManager.GetUserAsync(User); if (user == null) { var e = new AuthenticationException("User is not authenticated."); res = new ResultAccountGetSinnersByAuthorization(e) { ErrorText = "Unauthorized" }; return(BadRequest(res)); } user.FavoriteGroups = user.FavoriteGroups.GroupBy(a => a.FavoriteGuid).Select(b => b.First()).ToList(); SINnerSearchGroup ssg = new SINnerSearchGroup(sg, user) { MyMembers = new List <SINnerSearchGroupMember>() }; try { var roles = await _userManager.GetRolesAsync(user); ret.Roles = roles.ToList(); ssg.Groupname = user.Email; ssg.Id = Guid.Empty; //get all from visibility List <SINner> mySinners = await _context.SINners.Include(a => a.MyGroup) .Include(a => a.SINnerMetaData.Visibility.UserRights) .OrderByDescending(a => a.UploadDateTime) .Take(200) .ToListAsync(); foreach (var sin in mySinners) { SINnerSearchGroupMember ssgm = new SINnerSearchGroupMember(user, sin); ssg.MyMembers.Add(ssgm); if (sin.MyGroup != null) { SINnerSearchGroup ssgFromSIN = ssg.MySINSearchGroups.FirstOrDefault(a => a.Id == sin.MyGroup.Id); if (ssgFromSIN == null) { ssgFromSIN = new SINnerSearchGroup(sin.MyGroup, user); ssg.MySINSearchGroups.Add(ssgFromSIN); } //add all members of his group var members = await sin.MyGroup.GetGroupMembers(_context, false); foreach (var member in members) { member.MyGroup = sin.MyGroup; member.MyGroup.MyGroups = new List <SINnerGroup>(); SINnerSearchGroupMember sinssgGroupMember = new SINnerSearchGroupMember(user, member); ssgFromSIN.MyMembers.Add(sinssgGroupMember); } sin.MyGroup.PasswordHash = string.Empty; sin.MyGroup.MyGroups = new List <SINnerGroup>(); } } ret.SINGroups.Add(ssg); res = new ResultAccountGetSinnersByAuthorization(ret); return(Ok(res)); } catch (Exception e) { try { await _signInManager.UserManager.GetUserAsync(User); //var tc = new Microsoft.ApplicationInsights.TelemetryClient(); ExceptionTelemetry et = new ExceptionTelemetry(e); et.Properties.Add("user", User.Identity.Name); tc.TrackException(et); } catch (Exception ex) { _logger?.LogError(ex.ToString()); } res = new ResultAccountGetSinnersByAuthorization(e); return(BadRequest(res)); } }
private async Task <ActionResult <ResultAccountGetSinnersByAuthorization> > GetSINnersByAuthorizationInternal() { Stopwatch sw = new Stopwatch(); sw.Start(); //var tc = new Microsoft.ApplicationInsights.TelemetryClient(); ResultAccountGetSinnersByAuthorization res = null; SINSearchGroupResult ret = new SINSearchGroupResult(); res = new ResultAccountGetSinnersByAuthorization(ret); SINnerGroup sg = new SINnerGroup(); var user = await _signInManager.UserManager.GetUserAsync(User); if (user == null) { var e = new AuthenticationException("User is not authenticated."); res = new ResultAccountGetSinnersByAuthorization(e) { ErrorText = "Unauthorized" }; return(BadRequest(res)); } user.FavoriteGroups = user.FavoriteGroups.GroupBy(a => a.FavoriteGuid).Select(b => b.First()).ToList(); SINnerSearchGroup ssg = new SINnerSearchGroup(sg, user) { MyMembers = new List <SINnerSearchGroupMember>() }; using (var t = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted }, TransactionScopeAsyncFlowOption.Enabled)) { try { var roles = await _userManager.GetRolesAsync(user); ret.Roles = roles.ToList(); ssg.Groupname = user.UserName; ssg.Id = Guid.Empty; var worklist = user.FavoriteGroups.Select(a => a.FavoriteGuid).ToList(); var groupworklist = _context.SINnerGroups .Include(a => a.MyGroups) .ThenInclude(b => b.MyGroups) .ThenInclude(c => c.MyGroups) .ThenInclude(d => d.MyGroups) .Where(a => a.Id != null && worklist.Contains(a.Id.Value)); ssg.MySINSearchGroups = await RecursiveBuildGroupMembers(groupworklist, user); var memberworklist = _context.SINners .Include(a => a.MyGroup) .Include(a => a.SINnerMetaData.Visibility) .Where(a => a.Id != null && worklist.Contains(a.Id.Value)); foreach (var member in memberworklist) { if (member.SINnerMetaData?.Visibility?.IsGroupVisible == false) { if (member.SINnerMetaData?.Visibility.UserRights.Any(a => !string.IsNullOrEmpty(a.EMail)) == true) { if (member.SINnerMetaData?.Visibility.UserRights.Any(a => user.NormalizedEmail.Equals(a.EMail, StringComparison.OrdinalIgnoreCase)) == false) { //dont show this guy! continue; } } } member.LastDownload = DateTime.Now; if (member.MyGroup == null) { member.MyGroup = new SINnerGroup(); } if (member.MyGroup.MyGroups == null) { member.MyGroup.MyGroups = new List <SINnerGroup>(); } SINnerSearchGroupMember sinssgGroupMember = new SINnerSearchGroupMember(user, member) { MySINner = member }; ssg.MyMembers.Add(sinssgGroupMember); } await _context.SaveChangesAsync(); ret.SINGroups.Add(ssg); res = new ResultAccountGetSinnersByAuthorization(ret); return(Ok(res)); } catch (Exception e) { try { await _signInManager.UserManager.GetUserAsync(User); ExceptionTelemetry et = new ExceptionTelemetry(e); et.Properties.Add("user", User.Identity.Name); tc.TrackException(et); } catch (Exception ex) { _logger?.LogError(ex.ToString()); } res = new ResultAccountGetSinnersByAuthorization(e); return(BadRequest(res)); } finally { AvailabilityTelemetry telemetry = new AvailabilityTelemetry("GetSINnersByAuthorization", DateTimeOffset.Now, sw.Elapsed, "Azure", res.CallSuccess, res.ErrorText); tc?.TrackAvailability(telemetry); } } }
private async Task <ActionResult <ResultAccountGetSinnersByAuthorization> > GetSINnersByAuthorizationInternal() { Stopwatch sw = new Stopwatch(); sw.Start(); //var tc = new Microsoft.ApplicationInsights.TelemetryClient(); ResultAccountGetSinnersByAuthorization res = null; SINSearchGroupResult ret = new SINSearchGroupResult(); res = new ResultAccountGetSinnersByAuthorization(ret); SINnerGroup sg = new SINnerGroup(); SINnerSearchGroup ssg = new SINnerSearchGroup(sg) { MyMembers = new List <SINnerSearchGroupMember>() }; using (var t = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }, TransactionScopeAsyncFlowOption.Enabled)) { try { var user = await _signInManager.UserManager.GetUserAsync(User); if (user == null) { var e = new AuthenticationException("User is not authenticated."); res = new ResultAccountGetSinnersByAuthorization(e) { ErrorText = "Unauthorized" }; return(BadRequest(res)); } var roles = await _userManager.GetRolesAsync(user); ret.Roles = roles.ToList(); ssg.Groupname = user.UserName; ssg.Id = Guid.Empty; //get all from visibility List <SINner> mySinners = await SINner.GetSINnersFromUser(user, _context, true); MetricTelemetry mt = new MetricTelemetry("GetSINersByAuthorization", "SINners found", mySinners.Count, 0, 0, 0, 0); tc.TrackMetric(mt); foreach (var sin in mySinners) { //check if that char is already added: var foundseq = (from a in ssg.MyMembers where a.MySINner.Id == sin.Id select a); if (foundseq.Any()) { continue; } sin.LastDownload = DateTime.Now; SINnerSearchGroupMember ssgm = new SINnerSearchGroupMember { MySINner = sin, Username = user.UserName }; if (sin.MyGroup?.Id != null) { if (!user.FavoriteGroups.Any(a => a.FavoriteGuid == sin.MyGroup.Id.Value)) { user.FavoriteGroups.Add(new ApplicationUserFavoriteGroup() { FavoriteGuid = sin.MyGroup.Id.Value }); } } else { ssg.MyMembers.Add(ssgm); } } user.FavoriteGroups = user.FavoriteGroups.GroupBy(a => a.FavoriteGuid).Select(b => b.First()).ToList(); foreach (var singroupId in user.FavoriteGroups) { SINnerSearchGroup ssgFromSIN; var singroup = await _context.SINnerGroups.FirstOrDefaultAsync(a => a.Id == singroupId.FavoriteGuid); if (ssg.MySINSearchGroups.Any(a => a.Id == singroupId.FavoriteGuid)) { ssgFromSIN = ssg.MySINSearchGroups.FirstOrDefault(a => a.Id == singroupId.FavoriteGuid); } else { ssgFromSIN = new SINnerSearchGroup(singroup); ssg.MySINSearchGroups.Add(ssgFromSIN); } //add all members of his group var members = await singroup.GetGroupMembers(_context, false); foreach (var member in members) { member.LastDownload = DateTime.Now; member.MyGroup = singroup; member.MyGroup.MyGroups = new List <SINnerGroup>(); SINnerSearchGroupMember sinssgGroupMember = new SINnerSearchGroupMember { MySINner = member }; //check if it is already added: var groupseq = from a in ssgFromSIN.MyMembers where a.MySINner == member select a; if (groupseq.Any()) { continue; } ssgFromSIN.MyMembers.Add(sinssgGroupMember); //} } singroup.PasswordHash = ""; singroup.MyGroups = new List <SINnerGroup>(); } await _context.SaveChangesAsync(); ret.SINGroups.Add(ssg); res = new ResultAccountGetSinnersByAuthorization(ret); return(Ok(res)); } catch (Exception e) { try { var user = await _signInManager.UserManager.GetUserAsync(User); ExceptionTelemetry et = new ExceptionTelemetry(e); et.Properties.Add("user", User.Identity.Name); tc.TrackException(et); } catch (Exception ex) { _logger?.LogError(ex.ToString()); } res = new ResultAccountGetSinnersByAuthorization(e); return(BadRequest(res)); } finally { Microsoft.ApplicationInsights.DataContracts.AvailabilityTelemetry telemetry = new Microsoft.ApplicationInsights.DataContracts.AvailabilityTelemetry("GetSINnersByAuthorization", DateTimeOffset.Now, sw.Elapsed, "Azure", res?.CallSuccess ?? false, res?.ErrorText); tc.TrackAvailability(telemetry); } #pragma warning disable CS0162 // Unreachable code detected t.Complete(); #pragma warning restore CS0162 // Unreachable code detected } }
private async Task <SINSearchGroupResult> JoinGroupTask(SINnerSearchGroup searchgroup, CharacterExtended myCE) { bool exceptionlogged = false; SINSearchGroupResult ssgr = null; try { DialogResult result = DialogResult.Cancel; frmSINnerGroupEdit groupEdit = null; if (searchgroup.HasPassword == true) { SINnerGroup joinGroup = new SINnerGroup(searchgroup); PluginHandler.MainForm.DoThreadSafe(() => { groupEdit = new frmSINnerGroupEdit(joinGroup, true); result = groupEdit.ShowDialog(this); }); } if ((result == DialogResult.OK) || (searchgroup.HasPassword == false)) { try { using (new CursorWait(true, this)) { var client = StaticUtils.GetClient(); var response = await client.PutSINerInGroupWithHttpMessagesAsync(searchgroup.Id, myCE.MySINnerFile.Id, groupEdit?.MySINnerGroupCreate?.MyGroup?.PasswordHash); if ((response.Response.StatusCode != HttpStatusCode.OK)) { var rescontent = await response.Response.Content.ReadAsStringAsync(); if (response.Response.StatusCode == HttpStatusCode.BadRequest) { if (rescontent.Contains("PW is wrong!")) { throw new ArgumentException("Wrong Password provided!"); } string searchfor = "NoUserRightException\",\"Message\":\""; if (rescontent.Contains(searchfor)) { string msg = rescontent.Substring(rescontent.IndexOf(searchfor) + searchfor.Length); msg = msg.Substring(0, msg.IndexOf("\"")); throw new ArgumentException(msg); } throw new ArgumentException(rescontent); } else { string msg = "StatusCode: " + response.Response.StatusCode + Environment.NewLine; msg += rescontent; throw new ArgumentException(msg); } } else { var found = await client.GetGroupByIdWithHttpMessagesAsync(searchgroup.Id, null, CancellationToken.None); var res = Backend.Utils.HandleError(found); if (found?.Response?.IsSuccessStatusCode == true) { ssgr = new SINSearchGroupResult(found.Body.MyGroup); } } } } catch (Exception e) { Log.Error(e); exceptionlogged = true; throw; } finally { MyParentForm?.MyParentForm?.CheckSINnerStatus(); } } } catch (Exception e) { if (!exceptionlogged) { Log.Error(e); } throw; } return(ssgr); }
/// <summary> /// Generates a character cache, which prevents us from repeatedly loading XmlNodes or caching a full character. /// </summary> /// <param name="strFile"></param> internal async static Task <IEnumerable <TreeNode> > GetCharacterRosterTreeNode(bool forceUpdate, Func <Task <HttpOperationResponse <SINSearchGroupResult> > > myGetSINnersFunction) { if ((MyTreeNodeList != null) && !forceUpdate) { return(MyTreeNodeList); } MyTreeNodeList = new List <TreeNode>(); try { PluginHandler.MainForm.DoThreadSafe(() => { MyTreeNodeList.Clear(); }); HttpOperationResponse <SINSearchGroupResult> response = null; try { response = await myGetSINnersFunction(); } catch (Microsoft.Rest.SerializationException e) { if (e.Content.Contains("Log in - ChummerHub")) { TreeNode node = new TreeNode("Online, not logged in"); node.ToolTipText = "Please log in (Options -> Plugins -> Sinners (Cloud) -> Login"; PluginHandler.MainForm.DoThreadSafe(() => { MyTreeNodeList.Add(node); }); return(MyTreeNodeList); } else { string msg = "Could not load response from SINners:" + Environment.NewLine; msg += e.Message; if (e.InnerException != null) { msg += Environment.NewLine + e.InnerException.Message; } System.Diagnostics.Trace.TraceWarning(msg); PluginHandler.MainForm.DoThreadSafe(() => { MyTreeNodeList.Add(new TreeNode() { ToolTipText = msg }); }); return(MyTreeNodeList); } } if (response == null || (response.Response.StatusCode == HttpStatusCode.BadRequest)) { string msg = "Could not load online Sinners: " + response?.Response.ReasonPhrase; if (response != null) { var content = await response.Response.Content.ReadAsStringAsync(); msg += Environment.NewLine + "Content: " + content; } System.Diagnostics.Trace.TraceWarning(msg); var errornode = new TreeNode() { Text = "Error contacting SINners" }; CharacterCache errorCache = new CharacterCache { ErrorText = "Error is copied to clipboard!" + Environment.NewLine + Environment.NewLine + msg, CharacterAlias = "Error loading SINners" }; errorCache.OnMyAfterSelect += (sender, args) => { PluginHandler.MainForm.DoThreadSafe(() => { System.Windows.Forms.Clipboard.SetText(msg); }); }; errornode.Tag = errorCache; PluginHandler.MainForm.DoThreadSafe(() => { MyTreeNodeList.Add(errornode); }); return(MyTreeNodeList); } SINSearchGroupResult result = response.Body as SINSearchGroupResult; if (result?.Roles != null) { StaticUtils.UserRoles = result.Roles?.ToList(); } System.Diagnostics.Trace.TraceInformation("Connected to SINners in version " + result?.Version?.AssemblyVersion + "."); var groups = result?.SinGroups.ToList(); MyTreeNodeList = CharacterRosterTreeNodifyGroupList(groups); return(MyTreeNodeList); } catch (Exception ex) { System.Diagnostics.Trace.TraceError(ex.ToString()); throw; } }
private async void mnuSINnersArchetypes_Click(object sender, EventArgs e) { SINSearchGroupResult ssgr = null; HttpOperationResponse <ResultGroupGetSearchGroups> res = null; try { using (new CursorWait(true, MainForm)) { var client = StaticUtils.GetClient(); res = await client.GetPublicGroupWithHttpMessagesAsync("Archetypes", null, null); var result = await ChummerHub.Client.Backend.Utils.HandleError(res, res.Body) as ResultGroupGetSearchGroups; if (result == null) { return; } if (result.CallSuccess == true) { ssgr = result.MySearchGroupResult; var ssgr1 = ssgr; PluginHandler.MainForm.CharacterRoster.DoThreadSafe(() => { using (new CursorWait(true, MainForm)) { if (ssgr1 != null && ssgr1.SinGroups?.Any() == true) { var list = ssgr1.SinGroups.Where(a => a.Groupname == "Archetypes").ToList(); var nodelist = ChummerHub.Client.Backend.Utils.CharacterRosterTreeNodifyGroupList(list); foreach (var node in nodelist) { PluginHandler.MyTreeNodes2Add.AddOrUpdate(node.Name, node, (key, oldValue) => node); } PluginHandler.MainForm.CharacterRoster.LoadCharacters(false, false, false, true); PluginHandler.MainForm.CharacterRoster.treCharacterList.SelectedNode = nodelist.FirstOrDefault(a => a.Name == "Archetypes"); PluginHandler.MainForm.BringToFront(); } else { MessageBox.Show("No archetypes found!"); } } }); } ssgr = null; } } catch (ArgumentNullException) { throw; } catch (Exception ex) { var result = await ChummerHub.Client.Backend.Utils.HandleError(res, res.Body) as ResultGroupGetSearchGroups; if (result == null) { return; } } }
private async Task <ActionResult <SINSearchGroupResult> > GetSearchGroupsInternal(string Groupname, string UsernameOrEmail, string sINnerName, string language) { ApplicationUser user = null; try { if (!ModelState.IsValid) { var errors = ModelState.Select(x => x.Value.Errors) .Where(y => y.Count > 0) .ToList(); string msg = "ModelState is invalid: "; foreach (var err in errors) { foreach (var singleerr in err) { msg += Environment.NewLine + "\t" + singleerr.ToString(); } } return(BadRequest(new HubException(msg))); } SINSearchGroupResult result = new SINSearchGroupResult(); var groupfoundseq = await(from a in _context.SINnerGroups where a.Groupname.ToLowerInvariant().Contains(Groupname.ToLowerInvariant()) && (a.Language == language || String.IsNullOrEmpty(language)) select a.Id).ToListAsync(); if (!groupfoundseq.Any()) { return(NotFound(Groupname)); } foreach (var groupid in groupfoundseq) { var ssg = await GetSinSearchGroupResultById(groupid); result.SINGroups.Add(ssg); } if (!String.IsNullOrEmpty(UsernameOrEmail)) { List <SINner> byUser = new List <SINner>(); ApplicationUser bynameuser = await _userManager.FindByNameAsync(UsernameOrEmail); if (bynameuser != null) { var usersinners = await SINner.GetSINnersFromUser(bynameuser, _context, true); byUser.AddRange(usersinners); } ApplicationUser byemailuser = await _userManager.FindByEmailAsync(UsernameOrEmail); if ((byemailuser != null) && (byemailuser != bynameuser)) { var usersinners = await SINner.GetSINnersFromUser(byemailuser, _context, true); byUser.AddRange(usersinners); } foreach (var sin in byUser) { if (sin.MyGroup != null) { SINnerSearchGroup ssg = null; var foundseq = (from a in result.SINGroups where a.Groupname?.ToLowerInvariant() == sin.MyGroup?.Groupname.ToLowerInvariant() select a).ToList(); if (foundseq.Any()) { ssg = foundseq.FirstOrDefault(); } if (ssg == null) { ssg = new SINnerSearchGroup(sin.MyGroup); } ssg.Id = sin.MyGroup?.Id; SINnerSearchGroupMember ssgm = new SINnerSearchGroupMember(); ssgm.MySINner = sin; if (byemailuser != null) { ssgm.Username = byemailuser?.UserName; } if (bynameuser != null) { ssgm.Username = bynameuser?.UserName; } ssg.MyMembers.Add(ssgm); } } } result.SINGroups = RemovePWHashRecursive(result.SINGroups); return(Ok(result)); } catch (Exception e) { try { var tc = new Microsoft.ApplicationInsights.TelemetryClient(); Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry telemetry = new Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry(e); telemetry.Properties.Add("User", user?.Email); telemetry.Properties.Add("Groupname", Groupname?.ToString()); tc.TrackException(telemetry); } catch (Exception ex) { _logger.LogError(ex.ToString()); } if (e is HubException) { return(BadRequest(e)); } HubException hue = new HubException("Exception in GetSearchGroups: " + e.Message, e); return(BadRequest(hue)); } }
public async Task <ActionResult <SINSearchGroupResult> > GetSINnersByAuthorization() { SINSearchGroupResult ret = new SINSearchGroupResult(); SINnerGroup sg = new SINnerGroup(); SINnerSearchGroup ssg = new SINnerSearchGroup(sg) { MyMembers = new List <SINnerSearchGroupMember>() }; try { var user = await _signInManager.UserManager.GetUserAsync(User); if (user == null) { ret.ErrorText = "Unauthorized"; throw new AuthenticationException("User is not authenticated."); } var roles = await _userManager.GetRolesAsync(user); ret.Roles = roles.ToList(); ssg.Groupname = user.Email; ssg.Id = Guid.Empty; //get all from visibility //SINnersList list = new SINnersList(); List <SINner> mySinners = await SINner.GetSINnersFromUser(user, _context, true); foreach (var sin in mySinners) { SINnerSearchGroupMember ssgm = new SINnerSearchGroupMember { MySINner = sin, Username = user.UserName }; ssg.MyMembers.Add(ssgm); if (sin.MyGroup != null) { SINnerSearchGroup ssgFromSIN; if (ssg.MySINSearchGroups.Any(a => a.Id == sin.MyGroup.Id)) { ssgFromSIN = ssg.MySINSearchGroups.FirstOrDefault(a => a.Id == sin.MyGroup.Id); } else { ssgFromSIN = new SINnerSearchGroup(sin.MyGroup); ssg.MySINSearchGroups.Add(ssgFromSIN); } //add all members of his group var members = await sin.MyGroup.GetGroupMembers(_context); foreach (var member in members) { if ((member.SINnerMetaData.Visibility.IsGroupVisible == true) || (member.SINnerMetaData.Visibility.IsPublic) ) { member.MyGroup = sin.MyGroup; member.MyGroup.MyGroups = new List <SINnerGroup>(); SINnerSearchGroupMember sinssgGroupMember = new SINnerSearchGroupMember { MySINner = member }; ssgFromSIN.MyMembers.Add(sinssgGroupMember); } } sin.MyGroup.PasswordHash = ""; sin.MyGroup.MyGroups = new List <SINnerGroup>(); } } ret.SINGroups.Add(ssg); return(Ok(ret)); } catch (Exception e) { if (e is HubException) { return(BadRequest(e)); } HubException hue = new HubException(e.Message, e); return(BadRequest(hue)); } }