/// <summary>
        /// Retrieves the entities with the specified IDs.
        /// </summary>
        /// <param name="ids">The IDs of the entities to retrieve.</param>
        /// <returns>An array of the entities matching the provided IDs.</returns>
        public virtual T[] Index <T>(Guid[] ids)
            where T : IEntity
        {
            Collection <T> list = new Collection <T>();

            IRetrieveStrategy retriever = RetrieveStrategy.New <T>(RequireAuthorisation);

            foreach (Guid id in ids)
            {
                T entity = retriever.Retrieve <T>(id);

                list.Add(entity);
            }

            T[] entities = list.ToArray();

            if (RequireAuthorisation)
            {
                AuthoriseIndexStrategy.New <T>().EnsureAuthorised(ref entities);
            }

            React(entities);

            return(entities);
        }
Example #2
0
        public IEntity GetParent(string parentTypeName, Guid parentID, string parentUniqueKey)
        {
            IEntity parent = null;

            using (LogGroup logGroup = LogGroup.Start("Retrieving the parent for entity being created.", NLog.LogLevel.Debug))
            {
                LogWriter.Debug("Parent ID: " + parentID.ToString());
                LogWriter.Debug("Parent unique key: " + parentUniqueKey);
                LogWriter.Debug("Parent type: " + parentTypeName);

                IRetrieveStrategy retrieveStrategy = RetrieveStrategy.New(parentTypeName, RequireAuthorisation);

                if (parentUniqueKey != String.Empty)
                {
                    parent = retrieveStrategy.Retrieve("UniqueKey", parentUniqueKey);
                }
                else if (parentID != Guid.Empty)
                {
                    parent = retrieveStrategy.Retrieve("ID", parentID);
                }
                else
                {
                    throw new Exception("No unique key or ID found for the parent.");
                }
            }
            return(parent);
        }
Example #3
0
        /// <summary>
        /// Creates a new strategy for retrieving the specified type.
        /// </summary>
        /// <param name="typeName">The short name of the type involved in the strategy.</param>
        /// <param name="requiresAuthorisation">A value indicating whether the strategy requires authorisation.</param>
        static public IRetrieveStrategy New(string typeName, bool requiresAuthorisation)
        {
            IRetrieveStrategy strategy = StrategyState.Strategies.Creator.NewRetriever(typeName);

            strategy.RequireAuthorisation = requiresAuthorisation;
            return(strategy);
        }
Example #4
0
        /// <summary>
        /// Authenticates the user with the provided username and password.
        /// </summary>
        /// <param name="username">The username of the user to authenticate.</param>
        /// <param name="password">The unencrypted password of the user to authenticate.</param>
        /// <returns>A value indicating whether the user's credentials are authentic.</returns>
        public bool Authenticate(string username, string password)
        {
            string encryptedPassword = Crypter.EncryptPassword(password);

            Dictionary <string, object> parameters = new Dictionary <string, object>();

            parameters.Add("Username", username);
            parameters.Add("Password", encryptedPassword);

            IRetrieveStrategy strategy = RetrieveStrategy.New <User>(false);

            User user = strategy.Retrieve <User>(parameters);

            bool isAuthenticated = (user != null) &&
                                   user.IsApproved &&
                                   !user.IsLockedOut;

            if (isAuthenticated)
            {
                user.Activate();
                user.LastLoginDate = DateTime.Now;
                UpdateStrategy.New(user, false).Update(user);
            }

            return(isAuthenticated);
        }
Example #5
0
        public void Test_Retrieve_ByUniqueKey()
        {
            TestArticle article = new TestArticle();

            article.ID = Guid.NewGuid();

            DataAccess.Data.Saver.Save(article);

            IRetrieveStrategy strategy = RetrieveStrategy.New <TestArticle>(false);

            IEntity foundArticle = strategy.Retrieve <TestArticle>(article.UniqueKey);

            Assert.IsNotNull(foundArticle, "Test article wasn't retrieved.");
        }
Example #6
0
        public void Test_Retrieve_ByCustomProperty()
        {
            TestArticle article = new TestArticle();

            article.ID    = Guid.NewGuid();
            article.Title = "Test Title";

            DataAccess.Data.Saver.Save(article);

            IRetrieveStrategy strategy = RetrieveStrategy.New <TestArticle>(false);

            IEntity foundArticle = strategy.Retrieve <TestArticle>("Title", article.Title);

            Assert.IsNotNull(foundArticle, "Test article wasn't retrieved.");
        }
        /// <summary>
        /// Checks whether the specified property of the provided entity is unique.
        /// </summary>
        /// <param name="entity">The entity to validate by checking whether the provided property value is unique.</param>
        /// <param name="property">The property that is to remain unique.</param>
        /// <param name="attribute">The validate property attribute that caused the validation.</param>
        /// <returns>A value to indicate whether the provided value is unique to the provided property.</returns>
        public override bool IsValid(IEntity entity, PropertyInfo property, IValidatePropertyAttribute attribute)
        {
            bool isTaken = false;

            using (LogGroup logGroup = LogGroup.StartDebug("Validating the provided entity by ensuring the specified property value is unique."))
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                if (property == null)
                {
                    throw new ArgumentNullException("property");
                }

                LogWriter.Debug("Property name: " + property.Name);

                LogWriter.Debug("Entity: " + entity.GetType().FullName);

                IRetrieveStrategy strategy = RetrieveStrategy.New(entity.ShortTypeName, false);

                object propertyValue = property.GetValue(entity, null);

                LogWriter.Debug("Property value: " + (propertyValue != null ? propertyValue.ToString() : String.Empty));

                IEntity existingEntity = (IEntity)strategy.Retrieve(property.Name, propertyValue);

                LogWriter.Debug("Existing entity found: " + (existingEntity != null).ToString());

                LogWriter.Debug("Provided entity ID: " + entity.ID.ToString());
                LogWriter.Debug("Existing entity ID: " + (existingEntity == null ? "[null]" : existingEntity.ID.ToString()));

                isTaken = (existingEntity != null && !existingEntity.ID.Equals(entity.ID));

                LogWriter.Debug("Is taken: " + isTaken);
            }

            return(!isTaken);
        }