/// <summary>
        /// Builds criteria for querying documents.
        /// </summary>
        /// <param name="query"></param>
        /// <param name="whereDocument"></param>
        /// <param name="whereBody"></param>
        private static void BuildDocumentQueryCriteria(ConfigurationDocumentQuery query, ConfigurationDocumentSearchCriteria whereDocument, ConfigurationDocumentBodySearchCriteria whereBody)
        {
            var currentUser = Thread.CurrentPrincipal == null ? null : Thread.CurrentPrincipal.Identity.Name;

            // user type
            if (query.UserType == ConfigurationDocumentQuery.DocumentUserType.User)
            {
                // to query for user documents, we must have an authenticated user
                if (string.IsNullOrEmpty(currentUser))
                {
                    ThrowNotAuthorized();
                }

                // current users documents only
                whereDocument.User.EqualTo(currentUser);
            }
            else
            {
                // shared documents only!
                // (authentication is irrelevant - shared documents can be queried anonymously)
                whereDocument.User.IsNull();
            }

            // document name
            if (query.DocumentName.IsSet && !string.IsNullOrEmpty(query.DocumentName.Value))
            {
                if (query.DocumentName.Operator == ConfigurationDocumentQuery.StringOperator.StartsWith)
                {
                    whereDocument.DocumentName.StartsWith(query.DocumentName.Value);
                }
                else if (query.DocumentName.Operator == ConfigurationDocumentQuery.StringOperator.Exact)
                {
                    whereDocument.DocumentName.EqualTo(query.DocumentName.Value);
                }
            }

            // document version
            if (query.Version.IsSet)
            {
                whereDocument.DocumentVersionString.EqualTo(VersionUtils.ToPaddedVersionString(query.Version.Value));
            }

            // instance key
            if (query.InstanceKey.IsSet)
            {
                if (query.InstanceKey.Value == null)
                {
                    whereDocument.InstanceKey.IsNull();                     // default instances only!
                }
                else
                {
                    whereDocument.InstanceKey.EqualTo(query.InstanceKey.Value);
                }
            }

            // creation time
            if (query.CreationTime.IsSet)
            {
                if (query.CreationTime.Operator == ConfigurationDocumentQuery.DateTimeOperator.After)
                {
                    whereDocument.CreationTime.MoreThan(query.CreationTime.Value);
                }
                else if (query.CreationTime.Operator == ConfigurationDocumentQuery.DateTimeOperator.Before)
                {
                    whereDocument.CreationTime.LessThan(query.CreationTime.Value);
                }
            }

            // modified time
            if (query.ModifiedTime.IsSet)
            {
                if (query.ModifiedTime.Operator == ConfigurationDocumentQuery.DateTimeOperator.After)
                {
                    whereBody.ModifiedTime.MoreThan(query.ModifiedTime.Value);
                }
                else if (query.ModifiedTime.Operator == ConfigurationDocumentQuery.DateTimeOperator.Before)
                {
                    whereBody.ModifiedTime.LessThan(query.ModifiedTime.Value);
                }
            }
        }
Пример #2
0
        internal static SystemIdentityStore Load()
        {
            if (_store != null)
            {
                return(_store);
            }

            lock (_syncroot)
            {
                if (_store == null)
                {
                    const string userName      = "******";
                    var          serializer    = new XmlSerializer(typeof(SystemIdentityStore));
                    var          documentName  = typeof(SystemIdentityStore).FullName;
                    var          versionString = VersionUtils.ToPaddedVersionString(new Version(0, 0), false, false);

                    var criteria = new ConfigurationDocumentSearchCriteria();
                    criteria.User.EqualTo(userName);
                    criteria.DocumentName.EqualTo(documentName);
                    criteria.DocumentVersionString.EqualTo(versionString);

                    SystemIdentityStore store = null;
                    using (var scope = new PersistenceScope(PersistenceContextType.Read))
                    {
                        var broker   = scope.Context.GetBroker <IConfigurationDocumentBroker>();
                        var document = broker.Find(criteria).FirstOrDefault();
                        if (document != null)
                        {
                            try
                            {
                                using (var reader = new StringReader(document.Body.DocumentText))
                                    store = (SystemIdentityStore)serializer.Deserialize(reader);
                            }
                            catch (Exception)
                            {
                                store = null;
                            }
                        }
                        scope.Complete();
                    }

                    if (store == null || store.SecretKey == null || store.SecretKey.Length == 0)
                    {
                        if (store == null)
                        {
                            store = new SystemIdentityStore();
                        }
                        store.SecretKey = new byte[128];
                        using (var crng = new RNGCryptoServiceProvider())
                            crng.GetBytes(store.SecretKey);

                        using (var scope = new PersistenceScope(PersistenceContextType.Update))
                            using (var writer = new StringWriter())
                            {
                                serializer.Serialize(writer, store);

                                var broker   = scope.Context.GetBroker <IConfigurationDocumentBroker>();
                                var document = broker.Find(criteria).FirstOrDefault();
                                if (document != null)
                                {
                                    document.Body.DocumentText = writer.ToString();
                                }
                                else
                                {
                                    document = new ConfigurationDocument(documentName, versionString, userName, null);
                                    document.Body.DocumentText = writer.ToString();
                                    scope.Context.Lock(document, DirtyState.New);
                                }
                                scope.Complete();
                            }
                    }

                    Interlocked.Exchange(ref _store, store);
                }
                return(_store);
            }
        }