Exemplo n.º 1
0
        /// <summary>
        /// Method: Add
        /// Description: It is used to add new resource to resources table when provisioning the addon
        /// </summary>
        /// <param name="item"></param>
        public void Add(Resources item, int expiryInDays, bool isPrivatePlan, HerokuAuthToken authToken)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    //Get vendor app info
                    Console.WriteLine("Get vendor app info starts");
                    var appInfo = HerokuApi.GetAppInfo(item.app_name, authToken.access_token);
                    Console.WriteLine("Get vendor app info ended");
                    if (appInfo.IsNull())
                    {
                        throw new ArgumentNullException("Main app info is null");
                    }

                    item.app_name  = appInfo.name;
                    item.heroku_id = appInfo.id;
                    if (!appInfo.owner.IsNull())
                    {
                        item.user_email = appInfo.owner.email;
                    }
                    if (appInfo.organization.HasValue)
                    {
                        item.user_organization = appInfo.organization.Value.name;
                    }
                    if (!appInfo.region.IsNull())
                    {
                        item.region = appInfo.region.name;
                    }
                    if (isPrivatePlan && (!appInfo.space.HasValue || (appInfo.space.HasValue && string.IsNullOrEmpty(appInfo.space.Value.name))))
                    {
                        throw new Exception(string.Format("The {0} plan is not supported for the user account.", item.plan));
                    }

                    //set plan expiry date based on plan configured in addon_plans.json which is in app root
                    item.expired_at = DateTime.UtcNow.AddDays(expiryInDays);
                    _context.Resources.Add(item);

                    AuthTokens authTokens = authToken.ToAuthToken();
                    _context.AuthTokens.Add(authTokens);
                    _context.SaveChanges();

                    transaction.Commit();

                    //Update addon app config-var
                    if (!authToken.IsNull())
                    {
                        var task = HerokuApi.UpdateVendorAppConfigVarByResourceId(item.uuid, authToken.access_token);
                        task.Wait();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: {0}", ex.Message);
                    transaction.Rollback();
                    throw;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// ActionFilter: OnActionExecuting
        /// Description: It is used to check current user role where admin or owner for allowing user to access resources based on role.
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            base.OnActionExecuting(context);
            if (string.IsNullOrEmpty(context.HttpContext.GetClaimValue(Dedup.Common.Constants.HEROKU_MAIN_APP_NAME)))
            {
                //local variables
                var    claims     = new List <Claim>();
                string orgId      = string.Empty;
                string appId      = string.Empty;
                string authToken  = string.Empty;
                string resourceId = string.Empty;

                //get resourceId
                resourceId = context.HttpContext.GetClaimValue(ClaimTypes.NameIdentifier);

                //get heroku auth token
                authToken = context.HttpContext.GetClaimValue(Dedup.Common.Constants.HEROKU_ACCESS_TOKEN);

                //get app id using resourceId
                appId = HerokuApi.GetHerokuAppIdByAddonId(context.HttpContext.GetClaimValue(ClaimTypes.NameIdentifier), authToken).Result;
                if (!string.IsNullOrEmpty(appId))
                {
                    //get app info using app id
                    AppInfo appInfo = HerokuApi.GetAppInfo(appId, authToken);
                    if (appInfo.IsNull())
                    {
                        claims    = null;
                        authToken = null;
                        Console.WriteLine("UPF-appInfo is null by {0}", resourceId);
                        context.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "home", action = "forbidden" }));
                        return;
                    }
                    claims.Add(new Claim(Dedup.Common.Constants.HEROKU_MAIN_APP_NAME, appInfo.name));
                    claims.Add(new Claim(Dedup.Common.Constants.HEROKU_MAIN_APP_ID, appInfo.id));
                    if (appInfo.organization.HasValue)
                    {
                        orgId = appInfo.organization?.id;
                        claims.Add(new Claim(Dedup.Common.Constants.HEROKU_ORG_ID, appInfo.organization?.id));
                        claims.Add(new Claim(Dedup.Common.Constants.HEROKU_ORG_NAME, appInfo.organization?.name));
                    }

                    var resourceEntity = _resourcesRepository.Find(resourceId);
                    if (resourceEntity != null)
                    {
                        bool isUpdate = false;
                        if (!appInfo.name.Equals(resourceEntity.app_name, StringComparison.OrdinalIgnoreCase))
                        {
                            resourceEntity.app_name = appInfo.name;
                            isUpdate = true;
                        }
                        if (!appInfo.id.Equals(resourceEntity.heroku_id, StringComparison.OrdinalIgnoreCase))
                        {
                            resourceEntity.heroku_id = appInfo.id;
                            isUpdate = true;
                        }
                        if (!appInfo.owner.IsNull() && !appInfo.owner.email.Equals(resourceEntity.user_email, StringComparison.OrdinalIgnoreCase))
                        {
                            resourceEntity.user_email = appInfo.owner.email;
                            isUpdate = true;
                        }
                        if (appInfo.organization.HasValue && !appInfo.organization.Value.name.Equals(resourceEntity.user_organization, StringComparison.OrdinalIgnoreCase))
                        {
                            resourceEntity.user_organization = appInfo.organization?.name;
                            isUpdate = true;
                        }
                        if (isUpdate)
                        {
                            _resourcesRepository.UpdateAppAndUserInfo(resourceEntity);
                        }
                    }

                    //update user identity
                    if (claims != null && claims.Count() > 0)
                    {
                        context.HttpContext.AddUpdateClaims(claims);
                    }
                }
                claims = null;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Action: GetAccountInfoAsync
        /// Description: It is used to get the current user name of heroku account
        /// </summary>
        /// <returns></returns>
        private async Task <string> GetAccountInfoAsync()
        {
            try
            {
                var resourceId = HttpContext.GetClaimValue(ClaimTypes.NameIdentifier);
                if (!string.IsNullOrEmpty(resourceId))
                {
                    //validate account by resource id
                    var resources = _resourcesRepository.Find(resourceId);
                    if (resources != null)
                    {
                        //Update name/email in resource table based on resource-id if null
                        if (string.IsNullOrEmpty(resources.user_organization) || string.IsNullOrEmpty(resources.user_name) || string.IsNullOrEmpty(resources.user_email))
                        {
                            //Get heroku auth token
                            var herokuAuthToken = HttpContext.GetClaimValue(Constants.HEROKU_ACCESS_TOKEN);
                            if (!string.IsNullOrEmpty(herokuAuthToken))
                            {
                                if (string.IsNullOrEmpty(resources.user_email) || string.IsNullOrEmpty(resources.app_name))
                                {
                                    //Get app info using heroku api
                                    var appInfo = HerokuApi.GetVendorAppInfoByResourceId(resources.uuid);
                                    if (!appInfo.IsNull())
                                    {
                                        if (string.IsNullOrEmpty(resources.app_name))
                                        {
                                            resources.app_name = appInfo.name;
                                        }

                                        if (string.IsNullOrEmpty(resources.user_email))
                                        {
                                            resources.user_email = appInfo.owner_email;
                                        }
                                    }
                                }

                                if (!string.IsNullOrEmpty(resources.app_name) && string.IsNullOrEmpty(resources.user_organization))
                                {
                                    //Get app info using heroku api
                                    var appInfo = HerokuApi.GetAppInfo(resources.app_name, herokuAuthToken);
                                    if (!appInfo.IsNull())
                                    {
                                        if (string.IsNullOrEmpty(resources.user_organization) && appInfo.organization.HasValue)
                                        {
                                            resources.user_organization = ((AppOrganization)appInfo.organization).name;
                                        }
                                    }
                                }

                                if (string.IsNullOrEmpty(resources.user_name))
                                {
                                    //Get username using heroku api
                                    var accInfo = HerokuApi.GetAccountInfo(herokuAuthToken);
                                    if (!accInfo.IsNull())
                                    {
                                        //Assign user name
                                        resources.user_name = accInfo.name;
                                    }
                                }

                                //Update user organization/name/email
                                _resourcesRepository.Update(resources);
                            }
                        }

                        var claims = new List <Claim>();
                        if (!string.IsNullOrEmpty(resources.user_name) && HttpContext.GetClaimValue(ClaimTypes.Name) != resources.user_name)
                        {
                            claims.Add(new Claim(ClaimTypes.Name, resources.user_name));
                        }
                        if (!string.IsNullOrEmpty(resources.user_email) && HttpContext.GetClaimValue(ClaimTypes.Email) != resources.user_email)
                        {
                            claims.Add(new Claim(ClaimTypes.Email, resources.user_email));
                        }

                        if (claims != null && claims.Count > 0)
                        {
                            HttpContext.AddUpdateClaims(claims);
                        }

                        return(await Task.FromResult(HttpContext.GetClaimValue(ClaimTypes.Name)));
                    }
                }
            }
            catch (Exception ex)
            {
                if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == Environments.Development)
                {
                    _logger.LogError(ex.Message, ex);
                }
                else
                {
                    Console.WriteLine("ERROR: {0}", ex.Message);
                }
            }

            return(await Task.FromResult(string.Empty));
        }