Exemplo n.º 1
0
 public static RavenJArray ToJArray <T>(IEnumerable <T> result)
 {
     return((RavenJArray)RavenJArray.FromObject(result, CreateDefaultJsonSerializer()));
 }
Exemplo n.º 2
0
        public void ForDocumenting()
        {
            var userSession = new User();
            var parent      = new User();

            using (var documentStore = new DocumentStore())
            {
                #region expiration1

                var expiry = DateTime.Now.AddMinutes(5);
                using (var session = documentStore.OpenSession())
                {
                    session.Store(userSession);
                    session.Advanced.GetMetadataFor(userSession)["Raven-Expiration-Date"] = new RavenJValue(expiry.ToUniversalTime());
                    session.SaveChanges();
                }

                #endregion

                #region cascadedelete1
                using (var session = documentStore.OpenSession())
                {
                    session.Store(parent);
                    session.Advanced.GetMetadataFor(parent)["Raven-Cascade-Delete-Documents"]   = RavenJArray.FromObject(new[] { "childId1", "childId2" });
                    session.Advanced.GetMetadataFor(parent)["Raven-Cascade-Delete-Attachments"] = RavenJArray.FromObject(new[] { "attachmentId1", " attachmentId2" });
                    session.SaveChanges();
                }
                #endregion

                #region replicationconflicts1
                using (var session = documentStore.OpenSession())
                {
                    try
                    {
                        var user = session.Load <User>("users/ayende");
                        Console.WriteLine(user.Name);
                    }
                    catch (ConflictException e)
                    {
                        Console.WriteLine("Choose which document you want to preserver:");
                        var list = new List <JsonDocument>();
                        for (int i = 0; i < e.ConflictedVersionIds.Length; i++)
                        {
                            var doc = documentStore.DatabaseCommands.Get(e.ConflictedVersionIds[i]);
                            list.Add(doc);
                            Console.WriteLine("{0}. {1}", i, doc.DataAsJson.ToString(Formatting.None));
                        }
                        var select   = int.Parse(Console.ReadLine());
                        var resolved = list[select];
                        documentStore.DatabaseCommands.Put("users/ayende", null, resolved.DataAsJson, resolved.Metadata);
                    }
                }
                #endregion
                using (var session = documentStore.OpenSession())
                {
                    #region authorization1

                    // Allow nurses to schedule appointment for patients
                    session.Store(new AuthorizationRole
                    {
                        Id          = "Authorization/Roles/Nurses",
                        Permissions =
                        {
                            new OperationPermission
                            {
                                Allow     = true,
                                Operation = "Appointment/Schedule",
                                Tags      = new List <string> {
                                    "Patient"
                                }
                            }
                        }
                    });

                    // Allow doctors to authorize hospitalizations
                    session.Store(new AuthorizationRole
                    {
                        Id          = "Authorization/Roles/Doctors",
                        Permissions =
                        {
                            new OperationPermission
                            {
                                Allow     = true,
                                Operation = "Hospitalization/Authorize",
                                Tags      = new List <string> {
                                    "Patient"
                                }
                            }
                        }
                    });
                    #endregion

                    #region authorization2
                    // Associate Patient with clinic
                    session.SetAuthorizationFor(session.Load <Patient>("Patients/MaryMallon"), new DocumentAuthorization
                    {
                        Tags = { "Clinics/Kirya", "Patient" }
                    });

                    // Associate Doctor with clinic
                    session.Store(new AuthorizationUser
                    {
                        Id          = "Authorization/Users/DrHowser",
                        Name        = "Doogie Howser",
                        Roles       = { "Authorization/Roles/Doctors" },
                        Permissions =
                        {
                            new OperationPermission
                            {
                                Allow     = true,
                                Operation = "Patient/View",
                                Tags      = new List <string> {
                                    "Clinics/Kirya"
                                }
                            },
                        }
                    });
                    #endregion

                    #region authorization3
                    session.SecureFor("Authorization/Users/DrHowser", "Hospitalization/Authorize");
                    var mary = session.Load <Patient>("Patients/MaryMallon");
                    mary.AuthorizeHospitalization();
                    session.SaveChanges();
                    #endregion
                }
            }
        }