Beispiel #1
0
        public async Task <ActionResult <ResultAccountGetSinnersByAuthorization> > GetSINnersByAuthorization()
        {
            try
            {
                var res = await GetSINnersByAuthorizationInternal();

                return(res);
            }
            catch (Exception e)
            {
                ResultAccountGetSinnersByAuthorization error = new ResultAccountGetSinnersByAuthorization(e);
                return(error);
            }
        }
Beispiel #2
0
#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));
            }
        }
Beispiel #3
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 = 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);
                }
            }
        }
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();
            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
            }
        }