Ejemplo n.º 1
0
    private DynamicClassView GenerateView()
    {
        if (ObjectType == null || UserID == null)
        {
            throw new InvalidOperationException("Both ObjectType and UserID property must be specified");
        }
        var ot  = ObjectType.Value;
        var ui  = UserID.Value;
        var now = DateTime.Now;

        var permissions = ServerModel.DB.Load <TblPermissions>(PermissionsManager.GetPermissions(ot, ui, now, null));

        if (permissions.Count > 0)
        {
            Type pType   = typeof(TblPermissions);
            var  refProp = pType.GetProperty(ot.GetSecurityAtr().Name + "Ref");
            var  ids     = new List <int>(permissions.Select(p => refProp.GetValue(p, null)).NonNull().Cast <int>().Distinct());
            var  objs    =
                new DataObjectDictionary(
                    (IEnumerable)
                    DatabaseModel.LOAD_LIST_METHOD.MakeGenericMethod(new[] { ot.GetSecurityAtr().RuntimeClass }).
                    Invoke(ServerModel.DB, new[] { ids }));

            var possibleOps = PermissionsManager.GetPossibleOperations(ot);

            var view = new DynamicClassView(possibleOps.Count);
            view.DefineProperty("Name", typeof(string));
            view.DefineProperty("Date From", typeof(DateTime), false);
            view.DefineProperty("Date To", typeof(DateTime), false);
            foreach (var op in possibleOps)
            {
                view.DefineProperty(op.Name, typeof(bool));
            }

            foreach (ISecuredDataObject o in objs.Values)
            {
                var operations = PermissionsManager.GetOperationsForObject(ot, ui, o.ID, now);

                foreach (var op in operations)
                {
                    var d = view.Add();
                    foreach (var i in possibleOps)
                    {
                        d.AddProperty(i.Name, i.ID == op);
                    }
                    d.AddProperty("Name", o.Name);
                    // TODO: Implement datefrom and dateto retrieving
                    d.AddProperty("Date From", null);
                    d.AddProperty("Date To", null);

                    view.Add(d);
                }
            }

            return(view);
        }
        return(null);
    }
Ejemplo n.º 2
0
    private DynamicClassView GenerateView()
    {
        if (ObjectType == null || UserID == null)
        {
            throw new InvalidOperationException("Both ObjectType and UserID property must be specified");
        }
        var ot = ObjectType.Value;
        var ui = UserID.Value;
        var now = DateTime.Now;

        var permissions = ServerModel.DB.Load<TblPermissions>(PermissionsManager.GetPermissions(ot, ui, now, null));
        if (permissions.Count > 0)
        {
            Type pType = typeof (TblPermissions);
            var refProp = pType.GetProperty(ot.GetSecurityAtr().Name + "Ref");
            var ids = new List<int>(permissions.Select(p => refProp.GetValue(p, null)).NonNull().Cast<int>().Distinct());
            var objs =
                new DataObjectDictionary(
                    (IEnumerable)
                    DatabaseModel.LOAD_LIST_METHOD.MakeGenericMethod(new[] {ot.GetSecurityAtr().RuntimeClass}).
                        Invoke(ServerModel.DB, new[] {ids}));

            var possibleOps = PermissionsManager.GetPossibleOperations(ot);

            var view = new DynamicClassView(possibleOps.Count);
            view.DefineProperty("Name", typeof (string));
            view.DefineProperty("Date From", typeof (DateTime), false);
            view.DefineProperty("Date To", typeof (DateTime), false);
            foreach (var op in possibleOps)
            {
                view.DefineProperty(op.Name, typeof (bool));
            }

            foreach (ISecuredDataObject o in objs.Values)
            {
                var operations = PermissionsManager.GetOperationsForObject(ot, ui, o.ID, now);

                foreach (var op in operations)
                {
                    var d = view.Add();
                    foreach (var i in possibleOps)
                    {
                        d.AddProperty(i.Name, i.ID == op);
                    }
                    d.AddProperty("Name", o.Name);
                    // TODO: Implement datefrom and dateto retrieving
                    d.AddProperty("Date From", null);
                    d.AddProperty("Date To", null);

                    view.Add(d);
                }
            }

            return view;
        }
        return null;
    }
        public void PostNavigationPropertiesTest()
        {
            string[] детейлPropertiesNames =
            {
                Information.ExtractPropertyPath <Детейл>(x => x.prop1)
            };
            string[] masterPropertiesNames =
            {
                Information.ExtractPropertyPath <Master>(x => x.property)
            };
            string[] мастерPropertiesNames =
            {
                Information.ExtractPropertyPath <Мастер>(x => x.prop)
            };
            string[] мастер2PropertiesNames =
            {
                Information.ExtractPropertyPath <Мастер2>(x => x.свойство2)
            };
            string[] наследникPropertiesNames =
            {
                Information.ExtractPropertyPath <Наследник>(x => x.Свойство),
                Information.ExtractPropertyPath <Наследник>(x => x.Свойство1),
                Information.ExtractPropertyPath <Наследник>(x => x.Свойство2)
            };
            var детейлDynamicView    = new View(new ViewAttribute("детейлDynamicView", детейлPropertiesNames), typeof(Детейл));
            var masterDynamicView    = new View(new ViewAttribute("masterDynamicView", masterPropertiesNames), typeof(Master));
            var мастерDynamicView    = new View(new ViewAttribute("мастерDynamicView", мастерPropertiesNames), typeof(Мастер));
            var мастер2DynamicView   = new View(new ViewAttribute("мастер2DynamicView", мастер2PropertiesNames), typeof(Мастер2));
            var наследникDynamicView = new View(new ViewAttribute("наследникDynamicView", наследникPropertiesNames), typeof(Наследник));

            // Объекты для тестирования создания.
            var наследник = new Наследник()
            {
                Свойство = 1234.5, Свойство1 = "str", Свойство2 = 22
            };
            var детейл = new Детейл()
            {
                prop1 = 1
            };
            var мастер = new Мастер()
            {
                prop = "str3"
            };
            var мастер2 = new Мастер2()
            {
                свойство2 = -1
            };
            var master = new Master()
            {
                property = "str4"
            };

            наследник.Master = master;
            наследник.Мастер = мастер;
            мастер.Мастер2   = мастер2;
            наследник.Детейл.Add(детейл);
            ActODataService(args =>
            {
                string requestUrl;
                string receivedJsonMaster, receivedJsonМастер, receivedJsonНаследник;
                string requestJsonData = master.ToJson(masterDynamicView, args.Token.Model);
                requestUrl             = string.Format("http://localhost/odata/{0}", args.Token.Model.GetEdmEntitySet(typeof(Master)).Name);
                using (HttpResponseMessage response = args.HttpClient.PostAsJsonStringAsync(requestUrl, requestJsonData).Result)
                {
                    // Убедимся, что запрос завершился успешно.
                    Assert.Equal(HttpStatusCode.Created, response.StatusCode);

                    // Получим строку с ответом (в ней должна вернуться созданная сущность).
                    receivedJsonMaster = response.Content.ReadAsStringAsync().Result.Beautify();
                }

                requestJsonData = мастер.ToJson(мастерDynamicView, args.Token.Model);
                requestUrl      = string.Format("http://localhost/odata/{0}", args.Token.Model.GetEdmEntitySet(typeof(Мастер)).Name);
                using (HttpResponseMessage response = args.HttpClient.PostAsJsonStringAsync(requestUrl, requestJsonData).Result)
                {
                    // Убедимся, что запрос завершился успешно.
                    Assert.Equal(HttpStatusCode.Created, response.StatusCode);

                    // Получим строку с ответом (в ней должна вернуться созданная сущность).
                    receivedJsonМастер = response.Content.ReadAsStringAsync().Result.Beautify();
                }

                requestJsonData = наследник.ToJson(наследникDynamicView, args.Token.Model);
                DataObjectDictionary objJsonНаследник    = DataObjectDictionary.Parse(requestJsonData, наследникDynamicView, args.Token.Model);
                Dictionary <string, object> receivedDict = new JavaScriptSerializer().Deserialize <Dictionary <string, object> >(receivedJsonMaster);
                objJsonНаследник.Add("*****@*****.**", string.Format(
                                         "{0}({1})",
                                         args.Token.Model.GetEdmEntitySet(typeof(Master)).Name,
                                         receivedDict["__PrimaryKey"]));
                receivedDict = new JavaScriptSerializer().Deserialize <Dictionary <string, object> >(receivedJsonМастер);
                objJsonНаследник.Add("*****@*****.**", string.Format(
                                         "{0}({1})",
                                         args.Token.Model.GetEdmEntitySet(typeof(Мастер)).Name,
                                         receivedDict["__PrimaryKey"]));
                objJsonНаследник.Add("*****@*****.**", null);
                requestJsonData = objJsonНаследник.Serialize();
                requestUrl      = string.Format("http://localhost/odata/{0}", args.Token.Model.GetEdmEntitySet(typeof(Наследник)).Name);
                using (HttpResponseMessage response = args.HttpClient.PostAsJsonStringAsync(requestUrl, requestJsonData).Result)
                {
                    // Убедимся, что запрос завершился успешно.
                    Assert.Equal(HttpStatusCode.Created, response.StatusCode);

                    // Получим строку с ответом (в ней должна вернуться созданная сущность).
                    receivedJsonНаследник = response.Content.ReadAsStringAsync().Result.Beautify();
                }
            });
        }