Ejemplo n.º 1
0
        public void GetAnnotations_MultipleTypes()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");
            o.AddAnnotation("A string 2!");
            o.AddAnnotation("A string 3!");
            o.AddAnnotation(new Uri("http://www.google.com/"));

            IList <object> l = o.Annotations <object>().ToList();

            Assert.AreEqual(4, l.Count);
            Assert.AreEqual("A string!", l[0]);
            Assert.AreEqual("A string 2!", l[1]);
            Assert.AreEqual("A string 3!", l[2]);
            Assert.AreEqual(new Uri("http://www.google.com/"), l[3]);

            l = o.Annotations(typeof(object)).ToList();

            Assert.AreEqual(4, l.Count);
            Assert.AreEqual("A string!", l[0]);
            Assert.AreEqual("A string 2!", l[1]);
            Assert.AreEqual("A string 3!", l[2]);
            Assert.AreEqual(new Uri("http://www.google.com/"), l[3]);
        }
Ejemplo n.º 2
0
        public void RemoveAnnotation_Multiple_NonGeneric()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");
            o.AddAnnotation("A string 2!");

            o.RemoveAnnotations(typeof(string));

            string s = o.Annotation <string>();

            Assert.AreEqual(null, s);
        }
Ejemplo n.º 3
0
        public void AddAnnotation_MultipleOfTheSameType()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");
            o.AddAnnotation("Another string!");

            string s = o.Annotation <string>();

            Assert.AreEqual("A string!", s);

            s = (string)o.Annotation(typeof(string));
            Assert.AreEqual("A string!", s);
        }
        ResponseContext IAccessControlPrivacyService.ExecuteSecurityProcess(JObject user, JObject[] resource, string action, string collectionName, JObject environment)
        {
            _user           = user;
            _collectionName = collectionName;
            _action         = action;
            _environment    = environment;

            environment.AddAnnotation(action);
            EffectResult effect = AccessControlCollectionPolicyProcessing();

            if (effect == EffectResult.Deny)
            {
                return(new ResponseContext(EffectResult.Deny, null));
            }

            var accessControlRecordPolicies = _accessControlPolicyRepository.GetPolicies(collectionName, action, true);

            _policyCombining = accessControlRecordPolicies.Count > 0 ? _accessControlPolicyRepository.GetPolicyCombining(accessControlRecordPolicies)
                                                                    : "permit-overrides";

            _resource = new List <JObject>();

            foreach (var record in resource)
            {
                if (AccessControlRecordPolicyProcessing(record, _policyCombining, accessControlRecordPolicies) != null)
                {
                    _resource.Add(record);
                }
            }

            if (_resource.Count == 0)
            {
                return(new ResponseContext(EffectResult.Deny, null));
            }

            if (!action.Equals("read"))
            {
                return(new ResponseContext(EffectResult.Permit, _resource));
            }

            _collectionPrivacyRules = GetFieldCollectionRules();
            var privacyRecords = new JArray();

            //Parallel.ForEach(_resource, record =>
            //{
            //    var privacyField = GetPrivacyRecordField(record, policies);
            //    var privacyRecord = PrivacyProcessing(record, privacyField);
            //    Console.WriteLine(privacyRecord);
            //    privacyRecords.Add(privacyRecord);
            //});
            foreach (var record in _resource)
            {
                Console.WriteLine(DateTime.Now.Millisecond);
                var privacyFields = GetPrivacyRecordField(record);
                var privacyRecord = PrivacyProcessing(record, privacyFields);
                Console.WriteLine(DateTime.Now.Millisecond);
                privacyRecords.Add(privacyRecord);
            }
            return(new ResponseContext(EffectResult.Permit, privacyRecords));
        }
Ejemplo n.º 5
0
        public void Example()
        {
            #region Usage
            JObject o = JObject.Parse(@"{
              'name': 'Bill G',
              'age': 58,
              'country': 'United States',
              'employer': 'Microsoft'
            }");

            o.AddAnnotation(new HashSet <string>());
            o.PropertyChanged += (sender, args) => o.Annotation <HashSet <string> >().Add(args.PropertyName);

            o["age"]      = 59;
            o["employer"] = "Bill & Melinda Gates Foundation";


            HashSet <string> changedProperties = o.Annotation <HashSet <string> >();
            // age
            // employer
            #endregion

            Assert.AreEqual(true, changedProperties.Contains("age"));
            Assert.AreEqual(true, changedProperties.Contains("employer"));
        }
Ejemplo n.º 6
0
        public void AnnotationsAreCopied()
        {
            JObject o = new JObject();

            o.AddAnnotation("string!");
            AssertCloneCopy(o, "string!");

            JProperty p = new JProperty("Name", "Content");

            p.AddAnnotation("string!");
            AssertCloneCopy(p, "string!");

            JArray a = new JArray();

            a.AddAnnotation("string!");
            AssertCloneCopy(a, "string!");

            JConstructor c = new JConstructor("Test");

            c.AddAnnotation("string!");
            AssertCloneCopy(c, "string!");

            JValue v = new JValue(true);

            v.AddAnnotation("string!");
            AssertCloneCopy(v, "string!");

            JRaw r = new JRaw("raw");

            r.AddAnnotation("string!");
            AssertCloneCopy(r, "string!");
        }
Ejemplo n.º 7
0
        ResponseContext IAccessControlService.ExecuteProcess(JObject user, JObject[] resource, string action, string collectionName, JObject environment)
        {
            _user           = user;
            _collectionName = collectionName;
            _action         = action;
            _environment    = environment;

            environment.AddAnnotation(action);

            EffectResult effect = CollectionAccessControlProcess();

            if (effect == EffectResult.Deny)
            {
                return(new ResponseContext(EffectResult.Deny, null));
            }
            else if (effect == EffectResult.Permit)
            {
                return(new ResponseContext(EffectResult.Permit, resource));
            }

            var accessControlRecordPolicies = _accessControlPolicyRepository.GetPolicies(collectionName, action, true);

            if (accessControlRecordPolicies.Count == 0)
            {
                return(new ResponseContext(EffectResult.Deny, null));
            }

            string policyCombining = _accessControlPolicyRepository.GetPolicyCombining(accessControlRecordPolicies);

            ICollection <JObject> _resource = new List <JObject>();

            if (resource.Length > 1000)
            {
                Parallel.ForEach(resource, record =>
                {
                    if (RowAccessControlProcess(record, policyCombining, accessControlRecordPolicies) != null)
                    {
                        lock (_resource)
                            _resource.Add(record);
                    }
                });
            }
            else
            {
                foreach (var record in resource)
                {
                    if (RowAccessControlProcess(record, policyCombining, accessControlRecordPolicies) != null)
                    {
                        _resource.Add(record);
                    }
                }
            }
            if (_resource.Count == 0)
            {
                return(new ResponseContext(EffectResult.Deny, null));
            }

            return(new ResponseContext(EffectResult.Permit, _resource));
        }
Ejemplo n.º 8
0
 public RequestContext(CollectionRequestInfo subject, CollectionRequestInfo resource, string action, JObject environment)
 {
     Subject  = subject;
     Resource = resource;
     Action   = action;
     environment.AddAnnotation(Action);
     EnvironmentData = environment;
 }
Ejemplo n.º 9
0
        public void RemoveAnnotation_MultipleWithDifferentTypes_NonGeneric()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");
            o.AddAnnotation(new Uri("http://www.google.com/"));

            o.RemoveAnnotations(typeof(string));

            string s = o.Annotation <string>();

            Assert.AreEqual(null, s);

            Uri i = o.Annotation <Uri>();

            Assert.AreEqual(new Uri("http://www.google.com/"), i);
        }
Ejemplo n.º 10
0
        public void RemoveAnnotation_MultipleCalls()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");
            o.AddAnnotation(new Uri("http://www.google.com/"));

            o.RemoveAnnotations <string>();
            o.RemoveAnnotations <Uri>();

            string s = o.Annotation <string>();

            Assert.AreEqual(null, s);

            Uri i = o.Annotation <Uri>();

            Assert.AreEqual(null, i);
        }
Ejemplo n.º 11
0
        public void MultipleAnnotationsAreCopied()
        {
            Version version = new Version(1, 2, 3, 4);

            JObject o = new JObject();

            o.AddAnnotation("string!");
            o.AddAnnotation(version);

            JObject o2 = (JObject)o.DeepClone();

            Assert.AreEqual("string!", o2.Annotation <string>());
            Assert.AreEqual(version, o2.Annotation <Version>());

            o2.RemoveAnnotations <Version>();
            Assert.AreEqual(1, o.Annotations <Version>().Count());
            Assert.AreEqual(0, o2.Annotations <Version>().Count());
        }
Ejemplo n.º 12
0
        public void TestJObject()
        {
            JObject jo = new JObject();

            jo.Add("foo", "bar");

            jo.AddAnnotation("foobar2");
            string  serial = JsonConvert.SerializeObject(jo, Newtonsoft.Json.Formatting.Indented);
            JObject jo2    = JObject.Parse(serial);
        }
Ejemplo n.º 13
0
        private JObject CreateEntityObject(string entityPluralName, IDictionary <string, object> fields)
        {
            JObject entityObject = new JObject();

            entityObject.AddAnnotation(entityPluralName);
            foreach (var field in fields)
            {
                entityObject.Add(field.Key, new JValue(field.Value));
            }

            return(entityObject);
        }
Ejemplo n.º 14
0
        public void AddAnnotation_MultipleOfDifferentTypes()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");
            o.AddAnnotation(new Uri("http://www.google.com/"));

            string s = o.Annotation <string>();

            Assert.AreEqual("A string!", s);

            s = (string)o.Annotation(typeof(string));
            Assert.AreEqual("A string!", s);

            Uri i = o.Annotation <Uri>();

            Assert.AreEqual(new Uri("http://www.google.com/"), i);

            i = (Uri)o.Annotation(typeof(Uri));
            Assert.AreEqual(new Uri("http://www.google.com/"), i);
        }
Ejemplo n.º 15
0
        public void RemoveAnnotation()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");

            o.RemoveAnnotations <string>();

            string s = o.Annotation <string>();

            Assert.AreEqual(null, s);
        }
Ejemplo n.º 16
0
        public void GetAnnotations()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");
            o.AddAnnotation("A string 2!");
            o.AddAnnotation("A string 3!");

            IList <string> l = o.Annotations <string>().ToList();

            Assert.AreEqual(3, l.Count);
            Assert.AreEqual("A string!", l[0]);
            Assert.AreEqual("A string 2!", l[1]);
            Assert.AreEqual("A string 3!", l[2]);

            l = o.Annotations(typeof(string)).Cast <string>().ToList();

            Assert.AreEqual(3, l.Count);
            Assert.AreEqual("A string!", l[0]);
            Assert.AreEqual("A string 2!", l[1]);
            Assert.AreEqual("A string 3!", l[2]);
        }
Ejemplo n.º 17
0
        public void AddAnnotation()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");

            string s = o.Annotation <string>();

            Assert.AreEqual("A string!", s);

            s = (string)o.Annotation(typeof(string));
            Assert.AreEqual("A string!", s);
        }
Ejemplo n.º 18
0
        ResponseContext IPrivacyService.ExecuteProcess(JObject user, JObject[] resource, string action, string collectionName, JObject environment)
        {
            _user           = user;
            _collectionName = collectionName;
            _action         = action;
            _environment    = environment;

            environment.AddAnnotation(action);

            _collectionPrivacyRules = GetFieldCollectionRules();
            var privacyRecords = new JArray();
            int count          = 0;

            if (resource.Length > 1000)
            {
                Parallel.ForEach(resource, record =>
                {
                    var privacyFields = GetPrivacyRecordField(record);
                    if (privacyFields.Count > 0)
                    {
                        var privacyRecord = PrivacyProcessing(record, privacyFields);
                        //Console.WriteLine("Executing privacy for record: " + record["_id"]["$oid"]);
                        lock (privacyRecords)
                            privacyRecords.Add(privacyRecord);

                        ++count;
                    }
                });
            }
            else
            {
                foreach (var record in resource)
                {
                    var privacyFields = GetPrivacyRecordField(record);
                    if (privacyFields.Count > 0)
                    {
                        var privacyRecord = PrivacyProcessing(record, privacyFields);
                        privacyRecords.Add(privacyRecord);
                        ++count;
                    }
                }
            }
            Console.WriteLine(count);
            Console.WriteLine(privacyRecords.Count);
            if (privacyRecords.Count == 0)
            {
                return(new ResponseContext(EffectResult.Permit, privacyRecords, "No privacy rules is satisfied"));
            }

            return(new ResponseContext(EffectResult.Permit, privacyRecords));
        }
Ejemplo n.º 19
0
        public void RemoveAnnotation_Multiple()
        {
            JObject o = new JObject();

            o.AddAnnotation("A string!");
            o.AddAnnotation("A string 2!");
            o.AddAnnotation("A string 3!");

            o.RemoveAnnotations <string>();

            string s = o.Annotation <string>();

            Assert.AreEqual(null, s);

            o.AddAnnotation("A string 4!");

            s = o.Annotation <string>();
            Assert.AreEqual("A string 4!", s);

            Uri i = (Uri)o.Annotation(typeof(Uri));

            Assert.AreEqual(null, i);
        }
Ejemplo n.º 20
0
        public static void ShowAnnotations()
        {
            Console.Clear();
            Console.WriteLine("*** Annotations ***");

            string courseJson = @"{
                                'name': 'Solr',
                                'secondsWatched': 0
                                }";
            //create
            JObject course = JObject.Parse(courseJson);

            course.AddAnnotation(new Dictionary<DateTime, int>());
            course.PropertyChanged += (sender, arguments) =>
            {
                DateTime annotationDate = DateTime.Now;
                Console.WriteLine("Adding new annotation at: " + annotationDate);

                course.Annotation<Dictionary<DateTime, int>>().Add(annotationDate,
                    ((JObject)sender)["secondsWatched"].Value<int>());
            };

            //Make a change
            course["secondsWatched"] = 10;
           
            //I showed you the event being raised, make another change
            course["secondsWatched"] = 150;

            //And another one
            course["secondsWatched"] = 250;

            Dictionary<DateTime, int> changesDone = course.Annotation<Dictionary<DateTime, int>>();
            foreach (KeyValuePair<DateTime, int> change in changesDone)
            {
                Console.WriteLine("Changed on: " + change.Key + ": " + change.Value);
            }

            Console.WriteLine();
        }
        public static void Observe
        (
            this JObject target
            , Action
            <
                JObject
                , string
                , JObject
                , PropertyChangedEventArgs
                , JValue
                , JValue
            >
            onPropertyChangedProcessAction
        )
        {
            //target.Annotation<Action<string>>().
            // HashSet --> Dictionary
            target
            .AddAnnotation
            (
                new Dictionary <string, JObject>()
            );
            target
            .PropertyChanged +=
                (
                    (sender, args) =>
            {
                if (!target.Annotation <Dictionary <string, JObject> >().ContainsKey(args.PropertyName))
                {
                    JObject jDelegate = new JObject
                                        (
                        new JProperty
                        (
                            "methodName"
                            , onPropertyChangedProcessAction
                            .Method
                            .Name
                        )
                        , new JProperty
                        (
                            "isFlag"
                            ,
                            (
                                onPropertyChangedProcessAction
                                ==
                                null
                                                        ?
                                true
                                                        :
                                false
                            )
                        )
                                        );
                    target
                    .Annotation
                    <
                        Dictionary <string, JObject>
                    >()
                    .Add
                    (
                        args
                        .PropertyName
                        , jDelegate
                    );
                }
            }
                );

            Travel
            (
                target
                , null
                , (root, path, paropertyName, current) =>
            {
                JValue lastValue = null;
                current
                .PropertyChanging +=
                    (
                        (sender, args) =>
                {
                    if (sender is JObject jo)
                    {
                        lastValue = jo[args.PropertyName] as JValue;
                    }
                }
                    );
                JValue newValue = null;
                current
                .PropertyChanged +=
                    (
                        (sender, args) =>
                {
                    if (sender is JObject jo)
                    {
                        newValue = jo[args.PropertyName] as JValue;
                    }
                    onPropertyChangedProcessAction
                    (
                        root
                        , path
                        , current
                        , args
                        , lastValue
                        , newValue
                    );
                }
                    );
            }
            );
        }