Beispiel #1
0
        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
            }
        }
Beispiel #2
0
        public void ConstructorWillCastSuccessToResult()
        {
            AvailabilityTelemetry telemetry = new AvailabilityTelemetry();

            Assert.AreEqual(telemetry.Data.success, true);
        }
        public void ConstructorWillCastSuccessToResult()
        {
            AvailabilityTelemetry telemetry = new AvailabilityTelemetry();

            Assert.Equal(telemetry.Data.result, TestResult.Pass);
        }
Beispiel #4
0
        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 = System.Transactions.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      = (from a in user.FavoriteGroups select a.FavoriteGuid).ToList();
                    var groupworklist = await(from a in _context.SINnerGroups
                                              .Include(a => a.MyGroups)
                                              .ThenInclude(b => b.MyGroups)
                                              .ThenInclude(c => c.MyGroups)
                                              .ThenInclude(d => d.MyGroups)
                                              where (a.Id != null && worklist.Contains(a.Id.Value) == true)
                                              select a).ToListAsync();
                    ssg.MySINSearchGroups = await RecursiveBuildGroupMembers(groupworklist, user);

                    var memberworklist = await(from a in _context.SINners
                                               .Include(a => a.MyGroup)
                                               .Include(a => a.SINnerMetaData.Visibility)
                                               where (a.Id != null && worklist.Contains(a.Id.Value) == true)
                                               select a
                                               ).ToListAsync();
                    foreach (var member in memberworklist)
                    {
                        if (member.SINnerMetaData?.Visibility?.IsGroupVisible == false)
                        {
                            if (member.SINnerMetaData?.Visibility.UserRights.Any(a =>
                                                                                 string.IsNullOrEmpty(a.EMail) == false) == true)
                            {
                                if (member.SINnerMetaData?.Visibility.UserRights.Any(a =>
                                                                                     a.EMail?.ToUpperInvariant() == user.NormalizedEmail) == 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
                    {
                        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);
                }
            }
        }