Example #1
0
        private VariableRule GetVariableRule(VariableRule request)
        {
            var          id    = request?.Id;
            VariableRule ret   = null;
            var          query = DocQuery.ActiveQuery ?? Execute;

            DocPermissionFactory.SetSelect <VariableRule>(currentUser, "VariableRule", request.Select);

            DocEntityVariableRule entity = null;

            if (id.HasValue)
            {
                entity = DocEntityVariableRule.Get(id.Value);
            }
            if (null == entity)
            {
                throw new HttpError(HttpStatusCode.NotFound, $"No VariableRule found for Id {id.Value}");
            }

            if (!DocPermissionFactory.HasPermission(entity, currentUser, DocConstantPermission.VIEW))
            {
                throw new HttpError(HttpStatusCode.Forbidden, "You do not have VIEW permission for this route.");
            }

            ret = entity?.ToDto();
            return(ret);
        }
Example #2
0
        public VariableRule Post(VariableRuleCopy request)
        {
            VariableRule ret = null;

            using (Execute)
            {
                Execute.Run(ssn =>
                {
                    var entity = DocEntityVariableRule.Get(request?.Id);
                    if (null == entity)
                    {
                        throw new HttpError(HttpStatusCode.NoContent, "The COPY request did not succeed.");
                    }
                    if (!DocPermissionFactory.HasPermission(entity, currentUser, DocConstantPermission.ADD))
                    {
                        throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route.");
                    }

                    var pChildren   = entity.Children.ToList();
                    var pDefinition = entity.Definition;
                    var pInstances  = entity.Instances.ToList();
                    var pName       = entity.Name;
                    if (!DocTools.IsNullOrEmpty(pName))
                    {
                        pName += " (Copy)";
                    }
                    var pOwner  = entity.Owner;
                    var pRule   = entity.Rule;
                    var pScopes = entity.Scopes.ToList();
                    var pType   = entity.Type;
                    var copy    = new DocEntityVariableRule(ssn)
                    {
                        Hash         = Guid.NewGuid()
                        , Definition = pDefinition
                        , Name       = pName
                        , Owner      = pOwner
                        , Rule       = pRule
                        , Type       = pType
                    };
                    foreach (var item in pChildren)
                    {
                        entity.Children.Add(item);
                    }

                    foreach (var item in pInstances)
                    {
                        entity.Instances.Add(item);
                    }

                    foreach (var item in pScopes)
                    {
                        entity.Scopes.Add(item);
                    }

                    copy.SaveChanges(DocConstantPermission.ADD);
                    ret = copy.ToDto();
                });
            }
            return(ret);
        }