Пример #1
0
        public async Task <ResponseMessage <UserRole> > CanAssignRole(string role)
        {
            if (string.IsNullOrWhiteSpace(role))
            {
                throw new ArgumentException($"'{nameof(role)}' cannot be null or whitespace.", nameof(role));
            }

            if (!ApiIdentity.IsAuthenticated())
            {
                return new ResponseMessage <UserRole> {
                           Success = false, Message = "User not Authenticated"
                }
            }
            ;

            var authRoles = await GetRoles(ApiIdentity.GetUserName()).ConfigureAwait(false);

            if (authRoles.IsNullOrEmptyCollection())
            {
                return new ResponseMessage <UserRole> {
                           Success = false, Message = "Can not perform action, User has no Role."
                }
            }
            ;

            if (!Enum.TryParse(role, true, out RoleEnumType req))
            {
                return new ResponseMessage <UserRole> {
                           Success = false, Message = $"Can not perform action, {role} is not supported"
                }
            }
            ;

            HashSet <RoleEnumType> authEnums = new();

            foreach (var item in authRoles)
            {
                if (Enum.TryParse(item.RoleName, true, out RoleEnumType cur) && cur != RoleEnumType.Unknown)
                {
                    authEnums.Add(cur);
                }
            }

            var maxAuth = authEnums.HasAnyInCollection() ? authEnums.Max() : 0;

            if (maxAuth < req)
            {
                return new ResponseMessage <UserRole> {
                           Success = false, Message = "Can not perform action, User has low privilages."
                }
            }
            ;

            return(new ResponseMessage <UserRole> {
                Success = true, Message = "Ok"
            });
        }
Пример #2
0
        public VmTicketRepository(
            IAzureVmManagementService azureVmManagementService,
            IRepository <VmTicket> repository,
            AppVersionContext context,
            IVmSdkService vmSdkService,
            IConfiguration configuration,
            IApiIdentity apiIdentity) : base(context, apiIdentity)
        {
            _azureVmManagementService = azureVmManagementService;
            _repository    = repository;
            _context       = context;
            _vmSdkService  = vmSdkService;
            _configuration = configuration;
            _userId        = ApiIdentity.GetUserName();

            VmRebootDelay = configuration.GetValue <int>("VmRebootDelay");
        }
Пример #3
0
 public AdoProjectAccessRepository(
     IConfiguration configuration,
     IRepository <AdoProjectAccess> repository,
     AdoContext context,
     //IAdoProjectHistoryRepository adoProjectHistoryRepository,
     IMapper mapper,
     ILogger <AdoProjectAccessRepository> logger,
     IMessageBus messageBus,
     IApiIdentity apiIdentity) : base(context, apiIdentity)
 {
     _repository = repository;
     _context    = context;
     //_adoProjectHistoryRepository = adoProjectHistoryRepository;
     _mapper           = mapper;
     _logger           = logger;
     _messageBus       = messageBus;
     _userId           = ApiIdentity.GetUserName();
     _serviceBusConfig = configuration.GetSection(nameof(ServiceBusConfig)).Get <ServiceBusConfig>();
 }
Пример #4
0
        public async Task <ServiceIssuesResult> GetAzureResourceHealthForSubs(ServiceIssuesReguest issuesReguest)
        {
            try
            {
                var subs = await _subscriptionRepository.GetCachedSubcriptions(); //v3

                if (subs.HasAnyInCollection())
                {
                    var id  = ApiIdentity.GetUserName();
                    var ids = subs.Select(a => a.Id).ToList();
#if DEBUG
                    //int addDuplicatesForTesting = 100;
                    int addDuplicatesForTesting = 0;
                    if (addDuplicatesForTesting > 0)
                    {
                        List <string> list = new();
                        Enumerable.Range(1, addDuplicatesForTesting).ToList().ForEach(a => list.AddRange(ids));
                        ids = list;
                    }
#endif
                    int cachefor = (issuesReguest.CacheInForMinutes.HasValue && issuesReguest.CacheInForMinutes.Value > 0) ? issuesReguest.CacheInForMinutes.Value : 60;

                    #region v3 split into chunks

                    var keyhePartitioned = GetCommonCacheKey + issuesReguest.QueryStartTime.ToShortDateString() + ids.Count;
                    if (issuesReguest.ResetCache)
                    {
                        //_cacheProvider.ClearCache(keyhePartitioned, id);
                        ResetIssuesCache();
                    }

                    if (issuesReguest.CacheInForMinutes.HasValue && issuesReguest.CacheInForMinutes.Value > 0)
                    {
                        var cache = _cacheProvider.GetFromCache <ServiceIssuesResult>(keyhePartitioned, id);
                        if (cache != null)
                        {
                            return(cache);
                        }
                    }

                    var parti = await _azureHealthService.GetResourceHealthPartitioned(ids, issuesReguest.QueryStartTime.ToShortDateString());

                    var res = new ServiceIssuesResult {
                        ServiceIssues = parti, TotalSubs = subs.Count, IssuedTimeUtc = DateTime.UtcNow
                    };
                    _cacheProvider.SetCache(keyhePartitioned, id, res, cachefor * 60);

                    var commonCache = _cacheProvider.GetFromCache <HoldIssuesCache>(GetCommonCacheKey);
                    if (commonCache == null)
                    {
                        commonCache = new HoldIssuesCache();
                    }
                    if (commonCache.IdListKeysPairs.TryGetValue(id, out List <string> keysList))
                    {
                        keysList.Add(keyhePartitioned);
                    }
                    else
                    {
                        commonCache.IdListKeysPairs.Add(id, new List <string> {
                            keyhePartitioned
                        });
                    }

                    _cacheProvider.SetCache(GetCommonCacheKey, commonCache, cachefor * 60);
                    return(res);

                    #endregion

                    #region Old Code
                    //bool testBothMethods = false;
                    //#region v2 parallel, improvemn can be 10X times  faster, from 120 sec down to 20 sec in list of 1500 subs

                    //var keyhePar = nameof(_azureHealthService.GetResourceHealthParallel) + reguest.QueryStartTime + ids.Count;
                    //var vparalelel = await _cacheProvider.GetOrAddAsync(keyhePar, id, cachefor * 60, () => _azureHealthService.GetResourceHealthParallel(ids, reguest.QueryStartTime.ToShortDateString()));

                    //#endregion

                    //#region v1 code to do verificatio for paraller and normal for each await call

                    //if (testBothMethods)
                    //{
                    //    var keyhe = nameof(_azureHealthService.GetResourceHealth) + reguest.QueryStartTime + ids.Count;
                    //    var vornmal = await _cacheProvider.GetOrAddAsync(keyhe, id, cachefor * 60, () => _azureHealthService.GetResourceHealth(ids, reguest.QueryStartTime.ToShortDateString()));

                    //    //foreach (var item in vornmal)
                    //    //{
                    //    //    var par = vparalelel.FirstOrDefault(a => item.TrackingId.Equals(a.TrackingId));
                    //    //    if (par == null)
                    //    //        ;//bug does not exist
                    //    //    else
                    //    //    {
                    //    //        if (par.ImpactedSubscriptions.Length != item.ImpactedSubscriptions.Length)
                    //    //            ; //bad
                    //    //        if (par.ImpactedServices.Length != item.ImpactedServices.Length)
                    //    //            ; //bad
                    //    //        if (par.ImpactedRegions.Length != item.ImpactedRegions.Length)
                    //    //            ; //bad
                    //    //    }
                    //    //}
                    //}

                    //#endregion

                    //return new ServiceIssuesResult { ServiceIssues = vparalelel, TotalSubs = subs.Count };
                    #endregion
                }
                return(null);
            }
            catch (Exception ex)
            {
                throw;
            }
        }