Ejemplo n.º 1
0
        public async Task ExecuteAsync(RecipeExecutionContext context)
        {
            if (!String.Equals(context.Name, "FeatureProfiles", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            if (context.Step.Property("FeatureProfiles")?.Value is JObject featureProfiles)
            {
                foreach (var property in featureProfiles.Properties())
                {
                    var name  = property.Name;
                    var value = property.Value.ToObject <FeatureProfile>();

                    await _featureProfilesManager.UpdateFeatureProfileAsync(name, value);
                }
            }
        }
        public async Task <IActionResult> CreatePost(FeatureProfileViewModel model, string submit)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageTenantFeatureProfiles))
            {
                return(Forbid());
            }

            List <FeatureRule> featureRules = null;

            if (ModelState.IsValid)
            {
                if (String.IsNullOrWhiteSpace(model.Name))
                {
                    ModelState.AddModelError(nameof(FeatureProfileViewModel.Name), S["The name is mandatory."]);
                }
                else
                {
                    var featureProfilesDocument = await _featureProfilesManager.GetFeatureProfilesDocumentAsync();

                    if (featureProfilesDocument.FeatureProfiles.ContainsKey(model.Name))
                    {
                        ModelState.AddModelError(nameof(FeatureProfileViewModel.Name), S["A profile with the same name already exists."]);
                    }
                }

                if (String.IsNullOrEmpty(model.FeatureRules))
                {
                    ModelState.AddModelError(nameof(FeatureProfileViewModel.FeatureRules), S["The feature rules are mandatory."]);
                }
                else
                {
                    try
                    {
                        featureRules = JsonConvert.DeserializeObject <List <FeatureRule> >(model.FeatureRules);
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError(nameof(FeatureProfileViewModel.FeatureRules), S["Invalid json supplied."]);
                    }
                }
            }

            if (ModelState.IsValid)
            {
                var template = new FeatureProfile
                {
                    FeatureRules = featureRules
                };

                await _featureProfilesManager.UpdateFeatureProfileAsync(model.Name, template);

                if (submit == "SaveAndContinue")
                {
                    return(RedirectToAction(nameof(Edit), new { name = model.Name }));
                }
                else
                {
                    return(RedirectToAction(nameof(Index)));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }