Esempio n. 1
0
        public ActionResult Refresh()
        {
            try
            {
                // get all organizations and their respective subscription for a given tenant
                var orgs = AzureResourceManagerUtil.GetUserOrganizations();
                Dictionary <Organization, List <Subscription> > dictionary = new Dictionary <Organization, List <Subscription> >();
                foreach (var item in orgs)
                {
                    if (!dictionary.ContainsKey(item))
                    {
                        var subscriptions = AzureResourceManagerUtil.GetUserSubscriptions(item.Id);
                        dictionary.Add(item, subscriptions);
                    }
                }

                // check if these subscriptions are already added in the storage
                var repo = new EntityRepo <UserSubscription>();
                var list = repo.Get(USER_SUBSCRIPTION_TABLE_PARTITION_KEY, null, "");
                var existingSubscriptions = new Dictionary <string, string>();
                foreach (var item in list)
                {
                    existingSubscriptions.Add(item.SubscriptionId, item.SubscriptionId);
                }

                // list of new subscription to add
                var listOfUserSubscription = new List <UserSubscription>();
                foreach (var subscriptions in dictionary.Values)
                {
                    foreach (var subscription in subscriptions)
                    {
                        UserSubscription userSubscription = new UserSubscription(subscription.Id, subscription.OrganizationId);
                        userSubscription.DisplayName = subscription.DisplayName;

                        // if the subscription is not already in the storage add them
                        // otherwise the one in the storage should have latest info
                        if (!existingSubscriptions.ContainsKey(userSubscription.SubscriptionId))
                        {
                            listOfUserSubscription.Add(userSubscription);
                        }
                    }
                }

                // if one or more subscriptions are discovered, add them
                if (listOfUserSubscription.Count > 0)
                {
                    repo.Insert(listOfUserSubscription);
                }
                return(Json(dictionary.ToList(), JsonRequestBehavior.AllowGet));
            }
            catch (Exception exp)
            {
                Logger.Log("Subscription-Web-API", "Error", exp.Message, exp.ToString());
                return(new HttpStatusCodeResult(500, exp.Message));
            }
        }
Esempio n. 2
0
        public ActionResult Index()
        {
            HomeIndexViewModel model = null;

            if (ClaimsPrincipal.Current.Identity.IsAuthenticated)
            {
                model = new HomeIndexViewModel();
                model.UserOrganizations = new Dictionary <string, Organization>();
                model.UserSubscriptions = new Dictionary <string, Subscription>();
                model.UserCanManageAccessForSubscriptions = new List <string>();
                model.DisconnectedUserOrganizations       = new List <string>();

                var orgnaizations = AzureResourceManagerUtil.GetUserOrganizations();
                foreach (Organization org in orgnaizations)
                {
                    model.UserOrganizations.Add(org.Id, org);
                    var subscriptions = AzureResourceManagerUtil.GetUserSubscriptions(org.Id);

                    if (subscriptions != null)
                    {
                        foreach (var subscription in subscriptions)
                        {
                            Subscription s = db.Subscriptions.Find(subscription.Id);
                            if (s != null)
                            {
                                subscription.IsConnected = true;
                                subscription.ConnectedOn = s.ConnectedOn;
                                subscription.ConnectedBy = s.ConnectedBy;
                                subscription.AzureAccessNeedsToBeRepaired = AzureResourceManagerUtil.ServicePrincipalHasReadAccessToSubscription(subscription.Id, org.Id);
                                subscription.UsageString = AzureResourceManagerUtil.GetUsage(s.Id, org.Id);
                                //Deserialize the usage response into the usagePayload object
                                subscription.usagePayload = JsonConvert.DeserializeObject <UsagePayload>(subscription.UsageString);
                                List <UsageAggregate> UsageAggregateList = subscription.usagePayload.value;
                            }
                            else
                            {
                                subscription.IsConnected = false;
                            }
                            model.UserSubscriptions.Add(subscription.Id, subscription);

                            if (AzureResourceManagerUtil.UserCanManageAccessForSubscription(subscription.Id, org.Id))
                            {
                                model.UserCanManageAccessForSubscriptions.Add(subscription.Id);
                            }
                        }
                    }
                    else
                    {
                        model.DisconnectedUserOrganizations.Add(org.Id);
                    }
                }
            }
            return(View(model));
        }
Esempio n. 3
0
        public ActionResult Index()
        {
            HomeIndexViewModel model = null;

            if (ClaimsPrincipal.Current.Identity.IsAuthenticated)
            {
                model = new HomeIndexViewModel();
                model.UserOrganizations = new Dictionary <Guid, Organization>();
                model.UserSubscriptions = new Dictionary <Guid, Subscription>();
                model.UserCanManageAccessForSubscriptions = new List <Guid>();
                model.DisconnectedUserOrganizations       = new List <Guid>();

                var organizations = AzureResourceManagerUtil.GetUserOrganizations();
                foreach (Organization org in organizations)
                {
                    model.UserOrganizations.Add(org.Id, org);
                    var subscriptions = AzureResourceManagerUtil.GetUserSubscriptions(org.Id);

                    if (subscriptions != null)
                    {
                        foreach (var subscription in subscriptions)
                        {
                            Subscription s = db.Subscriptions.Find(subscription.Id);

                            if (s != null)
                            {
                                subscription.IsConnected = true;
                                subscription.ConnectedOn = s.ConnectedOn;
                                subscription.ConnectedBy = s.ConnectedBy;
                                subscription.DisplayTag  = s.DisplayTag;
                                subscription.AzureAccessNeedsToBeRepaired = !AzureResourceManagerUtil.ServicePrincipalHasReadAccessToSubscription(subscription.Id, org.Id);
                            }
                            else
                            {
                                subscription.IsConnected = false;
                            }

                            model.UserSubscriptions.Add(subscription.Id, subscription);

                            if (AzureResourceManagerUtil.UserCanManageAccessForSubscription(subscription.Id, org.Id))
                            {
                                model.UserCanManageAccessForSubscriptions.Add(subscription.Id);
                            }
                        }
                    }
                    else
                    {
                        model.DisconnectedUserOrganizations.Add(org.Id);
                    }
                }
            }

            return(View(model));
        }
Esempio n. 4
0
        public List <Subscription> Get()
        {
            var tenantId        = ClaimsPrincipal.Current.TenantId();
            var subscriptions   = AzureResourceManagerUtil.GetUserSubscriptions(tenantId);
            var dbSubscriptions = this.coreRepository.GetSubscriptionListByOrgId(tenantId);

            foreach (var subscription in subscriptions)
            {
                var dbSub = dbSubscriptions.FirstOrDefault(x => x.Id == subscription.Id);
                if (dbSub != null)
                {
                    subscription.ConnectedOn       = dbSub.ConnectedOn;
                    subscription.ConnectedBy       = dbSub.ConnectedBy;
                    subscription.StorageName       = dbSub.StorageName;
                    subscription.IsEnrolled        = dbSub.IsEnrolled;
                    subscription.IsConnected       = dbSub.IsConnected;
                    subscription.ContributorGroups = dbSub.ContributorGroups;
                }
            }
            return(subscriptions);
        }
        public ActionResult Index()
        {
            HomeIndexViewModel model = null;

            if (ClaimsPrincipal.Current.Identity.IsAuthenticated)
            {
                model = new HomeIndexViewModel();
                model.UserSubscriptions = new Dictionary <string, Subscription>();
                model.UserCanManageAccessForSubscriptions = new List <string>();

                var subscriptions = AzureResourceManagerUtil.GetUserSubscriptions(ConfigurationManager.AppSettings["AADId"]);

                if (subscriptions != null)
                {
                    foreach (var subscription in subscriptions)
                    {
                        Subscription s = db.Subscriptions.Find(subscription.Id);
                        if (s != null)
                        {
                            subscription.IsConnected = true;
                            subscription.ConnectedOn = s.ConnectedOn;
                            subscription.ConnectedBy = s.ConnectedBy;
                            subscription.AzureAccessNeedsToBeRepaired = !AzureResourceManagerUtil.ServicePrincipalHasReadAccessToSubscription
                                                                            (subscription.Id, ConfigurationManager.AppSettings["AADId"]);
                        }
                        else
                        {
                            subscription.IsConnected = false;
                        }

                        model.UserSubscriptions.Add(subscription.Id, subscription);
                        if (AzureResourceManagerUtil.UserCanManageAccessForSubscription(subscription.Id, ConfigurationManager.AppSettings["AADId"]))
                        {
                            model.UserCanManageAccessForSubscriptions.Add(subscription.Id);
                        }
                    }
                }
            }
            return(View(model));
        }
Esempio n. 6
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);
        }