Exemple #1
0
        public object Post([FromBody] dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            if (model.authorization == null || !(model.authorization is JObject))
            {
                throw new ApiArgumentException("authorization");
            }

            string authorizationUuid = DynamicHelper.Value(model.authorization.id);

            if (authorizationUuid == null)
            {
                throw new ApiArgumentException("authorization.id");
            }

            // Get the feature id
            AuthorizationId authId = new AuthorizationId(authorizationUuid);
            Site            site   = authId.SiteId == null ? null : SiteHelper.GetSite(authId.SiteId.Value);

            if (authId.SiteId != null && site == null)
            {
                return(NotFound());
            }

            string configPath = ManagementUnit.ResolveConfigScope(model);
            var    section    = AuthorizationHelper.GetSection(site, authId.Path, configPath);

            Rule rule = AuthorizationHelper.CreateRule(model, section);

            if (AuthorizationHelper.GetRule(site, authId.Path, rule.Users, rule.Roles, rule.Verbs) != null)
            {
                throw new AlreadyExistsException("rule");
            }

            section.Rules.Add(rule.AccessType, rule.Users, rule.Roles, rule.Verbs);

            ManagementUnit.Current.Commit();

            dynamic r = AuthorizationHelper.RuleToJsonModel(rule, site, authId.Path);

            return(Created(AuthorizationHelper.GetRuleLocation(r.id), r));
        }