Ejemplo n.º 1
0
        public async Task <UserSubscriptionInfo> GetUserDetailsFull()
        {
            var subscriptionInfo = new UserSubscriptionInfo();

            subscriptionInfo.UserName             = ClaimsPrincipal.Current.Identity.Name;
            subscriptionInfo.FirstName            = ClaimsPrincipal.Current.FirstName();
            subscriptionInfo.LastName             = ClaimsPrincipal.Current.LastName();
            subscriptionInfo.DefaultAdGroup       = Config.DefaultAdGroup;
            subscriptionInfo.DefaultResourceGroup = Config.DefaultResourceGroup;
            string tenantId             = ClaimsPrincipal.Current.TenantId();
            string signedInUserUniqueId = ClaimsPrincipal.Current.SignedInUserName();

            var userGroupsRoles = AzureADGraphApiUtil.GetUserGroups(signedInUserUniqueId, tenantId);

            subscriptionInfo.IsGlobalAdministrator = AzureADGraphApiUtil.IsGlobalAdministrator(userGroupsRoles);

            var org   = GetOrganization(tenantId);
            var dbOrg = await this.coreRepository.GetOrganization(tenantId);

            List <Subscription> dbSubscriptions = null;

            if (dbOrg != null)
            {
                org.DeployGroup        = dbOrg.DeployGroup;
                org.CreateProductGroup = dbOrg.CreateProductGroup;
                org.AdminGroup         = dbOrg.AdminGroup;
                //var userGroups = AzureADGraphApiUtil.GetUserGroups(signedInUserUniqueId, org.Id);
                subscriptionInfo.CanCreate = userGroupsRoles.Any(x => x.Id == dbOrg.CreateProductGroup);
                subscriptionInfo.CanDeploy = userGroupsRoles.Any(x => x.Id == dbOrg.DeployGroup);
                subscriptionInfo.CanAdmin  = userGroupsRoles.Any(x => x.Id == dbOrg.AdminGroup);
                dbSubscriptions            = this.coreRepository.GetSubscriptionListByOrgId(tenantId);

                if (dbSubscriptions != null && dbSubscriptions.Count > 0)
                {
                    subscriptionInfo.IsActivatedByAdmin = (dbSubscriptions.Any(x => x.IsConnected));
                }
            }

            subscriptionInfo.Organization = org;

            var orgGroups = AzureADGraphApiUtil.GetAllGroupsForOrganization(org.Id);

            subscriptionInfo.OrganizationADGroups = orgGroups;
            var subscriptions = AzureResourceManagerUtil.GetUserSubscriptions(org.Id);

            if (subscriptions != null)
            {
                foreach (var subscription in subscriptions)
                {
                    var userDetailVM = new UserDetailsViewModel();
                    userDetailVM.CanCreate = subscriptionInfo.CanCreate;
                    userDetailVM.CanDeploy = subscriptionInfo.CanDeploy;
                    userDetailVM.CanAdmin  = subscriptionInfo.CanAdmin;
                    userDetailVM.Name      = subscriptionInfo.UserName;

                    userDetailVM.IsAdminOfSubscription = AzureResourceManagerUtil.UserCanManageAccessForSubscription(subscription.Id);
                    userDetailVM.SubscriptionName      = subscription.DisplayName;
                    userDetailVM.SubscriptionId        = subscription.Id;
                    userDetailVM.OrganizationId        = org.Id;
                    userDetailVM.ServicePrincipalId    = org.ObjectIdOfCloudSenseServicePrincipal;
                    userDetailVM.OrganizationName      = org.DisplayName;

                    Subscription dbSubscription = null;
                    if (dbSubscriptions != null && dbSubscriptions.Count > 0)
                    {
                        dbSubscription = dbSubscriptions.FirstOrDefault(x => x.Id == subscription.Id) ?? null;
                    }

                    if (dbSubscription != null)
                    {
                        userDetailVM.SubscriptionIsConnected = dbSubscription.IsConnected;// true;
                        userDetailVM.IsEnrolled = dbSubscription.IsEnrolled;
                        userDetailVM.SubscriptionNeedsRepair = !AzureResourceManagerUtil.ServicePrincipalHasReadAccessToSubscription(dbSubscription.Id);
                        if (userDetailVM.SubscriptionIsConnected)
                        {
                            string organizationId = dbSubscription.OrganizationId;
                            string storageName    = dbSubscription.StorageName;
                            try
                            {
                                string storageKey = await AzureResourceManagerUtil.GetStorageAccountKeysArm(dbSubscription.Id, dbSubscription.StorageName);

                                CacheDetails(userDetailVM, storageKey, storageName, organizationId, signedInUserUniqueId);
                            }
                            catch (Exception ex)
                            {
                                Trace.TraceError(ex.Message);
                                Trace.TraceError($"Storage account: {storageName} was not found!");
                                userDetailVM.SubscriptionIsConnected = false;
                                userDetailVM.IsEnrolled = false;
                            }
                        }
                    }
                    subscriptionInfo.Subscriptions.Add(userDetailVM);
                }
            }
            return(subscriptionInfo);
        }