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
            }
        }
        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 <SINSearchResult> > GetSINnersByAuthorization()
        {
            SINSearchResult ret = new SINSearchResult();

            try
            {
                var user = await _signInManager.UserManager.GetUserAsync(User);

                if (user == null)
                {
                    ret.ErrorText = "Unauthorized";
                    throw new AuthenticationException("User is not authenticated.");
                }
                //get all from visibility
                SINnersList   list      = new SINnersList();
                List <SINner> mySinners = await SINner.GetSINnersFromUser(user, _context, true);

                foreach (var sin in mySinners)
                {
                    SINnerList owndSINner = new SINnerList
                    {
                        SINner = sin
                    };
                    if (sin.MyGroup != null)
                    {
                        //add all members of his group
                        var members = await sin.MyGroup.GetGroupMembers(_context);

                        foreach (var member in members)
                        {
                            if (member.Id == sin.Id)
                            {
                                continue;
                            }
                            if ((member.SINnerMetaData.Visibility.IsGroupVisible == true) ||
                                (member.SINnerMetaData.Visibility.IsPublic)
                                )
                            {
                                member.MyGroup          = sin.MyGroup;
                                member.MyGroup.MyGroups = new List <SINnerGroup>();
                                SINnerList memberlist = new SINnerList();
                                memberlist.SINner = member;
                                owndSINner.SINList.Add(memberlist);
                            }
                        }
                        sin.MyGroup.MyGroups = new List <SINnerGroup>();
                        ;
                    }
                    list.MySINnersList.Add(owndSINner);
                }

                ret.SINLists.Add(list);
                return(Ok(ret));
            }
            catch (Exception e)
            {
                if (e is HubException)
                {
                    return(BadRequest(e));
                }
                HubException hue = new HubException(e.Message, e);
                return(BadRequest(hue));
            }
        }
Beispiel #4
0
        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));
            }
        }