Пример #1
0
        public virtual bool ExecuteSave(ISubEntity entity)
        {
            bool success = false;

            using (LogGroup logGroup = LogGroup.Start("Saving the new entity.", NLog.LogLevel.Debug))
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                if (entity.Parent == null)
                {
                    ActivateStrategy.New <ISubEntity>().Activate(entity, entity.ParentPropertyName);
                }

                if (entity.Parent == null)
                {
                    throw new Exception("No parent found for entity with ID: " + entity.ID.ToString());
                }

                DataSource = entity;

                success = base.ExecuteSave(entity);
            }

            return(success);
        }
Пример #2
0
        public override void NavigateAfterUpdate()
        {
            using (LogGroup logGroup = LogGroup.Start("Navigating after a update operation.", NLog.LogLevel.Debug))
            {
                if (DataSource == null)
                {
                    throw new InvalidOperationException("The DataSource property isn't set.");
                }

                ISubEntity entity = (ISubEntity)DataSource;

                if (entity.Parent == null)
                {
                    LogWriter.Debug("No parent found. Activating entity.Parent.");

                    ActivateStrategy.New <ISubEntity>().Activate(entity, entity.ParentPropertyName);
                }

                if (entity.Parent == null)
                {
                    throw new Exception("No parent assigned to entity.");
                }

                Navigator.Current.NavigateAfterOperation("View", entity.Parent);
            }
        }
Пример #3
0
        /// <summary>
        /// Executes the index operation using the provided entities.
        /// </summary>
        /// <param name="entities"></param>
        protected virtual void ExecuteIndex(IEntity[] entities)
        {
            using (LogGroup logGroup = LogGroup.StartDebug("Preparing to display an index of entities."))
            {
                if (entities == null)
                {
                    throw new ArgumentNullException("entities");
                }

                LogWriter.Debug("# of entities: " + entities.Length);

                Type type = entities.GetType().GetElementType();

                OperationManager.StartOperation("Index" + type.Name, null);

                // There will likely be an authorisation check before this function is called, checking based on just the type,
                // but it's necessary to check the authorisation for the actual entities
                entities = Authorise(entities);

                // TODO: See if performance can be boosted by being able to disable activation or specify specific properties
                ActivateStrategy.New(Command.TypeName).Activate(entities);

                DataSource = entities;
            }
        }
Пример #4
0
        /// <summary>
        /// Checks to see whether the current user is in the specified role.
        /// </summary>
        /// <param name="roleName">The name of the role to check if the current user is in.</param>
        /// <returns>A flag indicating whether the current user is in the specified role.</returns>
        static public bool UserIsInRole(string roleName)
        {
            bool isInRole = false;

            using (LogGroup logGroup = LogGroup.StartDebug("Checking to see whether the current user is in the '" + roleName + "' role."))
            {
                LogWriter.Debug("User is authenticated: " + IsAuthenticated.ToString());

                if (IsAuthenticated)
                {
                    User user = AuthenticationState.User;

                    if (user != null)
                    {
                        LogWriter.Debug("Username: "******"Roles");
                        }

                        isInRole = user.IsInRole(roleName);
                    }
                }

                LogWriter.Debug("Is in role: " + isInRole);
            }

            return(isInRole);
        }
Пример #5
0
        public void SendEmail(Exception exception)
        {
            try
            {
                if (new ModeDetector().IsRelease)
                {
                    if (StrategyState.IsInitialized)
                    {
                        UserRole role = RetrieveStrategy.New <UserRole>(false).Retrieve <UserRole>("Name", "Administrator");

                        if (role != null)
                        {
                            ActivateStrategy.New(role, false).Activate(role, "Users");

                            foreach (User user in role.Users)
                            {
                                string subject = "Exception";
                                string message = "An exception occurred...\n";

                                if (HttpContext.Current.Request != null && HttpContext.Current.Request.Url != null)
                                {
                                    message = "URL:\n"
                                              + HttpContext.Current.Request.Url.ToString() + "\n\n";
                                }

                                if (HttpContext.Current.Request.UrlReferrer != null)
                                {
                                    message = message + "Referrer:\n"
                                              + HttpContext.Current.Request.UrlReferrer.ToString() + "\n\n";
                                }

                                if (HttpContext.Current.Request != null)
                                {
                                    message = message + Environment.NewLine
                                              + "User Agent:"
                                              + HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"] + Environment.NewLine;
                                }

                                message = message + "Site:\n"
                                          + new UrlConverter().ToAbsolute(HttpContext.Current.Request.ApplicationPath) + "\n\n"
                                          + "Time:\n"
                                          + DateTime.Now.ToLongDateString() + " - " + DateTime.Now.ToLongTimeString() + "\n\n"
                                          + "Exception:\n"
                                          + exception.ToString() + "\n\n"
                                          + "(please do not reply to this email)\n";

                                Emailer.New().SendEmail(user, subject, message);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogWriter.Error("Exception occurred when trying to send error report email.");

                LogWriter.Error(ex);
            }
        }
        protected virtual bool UserIsAuthor(IAuthored entity)
        {
            ActivateStrategy.New(entity).Activate(entity, "Author");

            // If the current use is authenticated
            return(AuthenticationState.IsAuthenticated
                   // and if the current user is the author
                   && entity.Author != null &&
                   AuthenticationState.User != null &&
                   entity.Author.ID == AuthenticationState.User.ID);
        }
        public override bool IsAuthorised(SoftwareMonkeys.SiteStarter.Entities.IEntity entity)
        {
            if (!AuthenticationState.IsAuthenticated)
            {
                return(false);
            }

            IAuthored authoredEntity = (IAuthored)entity;

            ActivateStrategy.New(authoredEntity).Activate(authoredEntity, "Author");

            return(authoredEntity.IsPublic ||
                   (authoredEntity != null && authoredEntity.Author.ID == AuthenticationState.User.ID));
        }
Пример #8
0
        public void Test_Delete_Reorder()
        {
            using (LogGroup logGroup = LogGroup.StartDebug("Testing deletion of sub entities to ensure their position is updated."))
            {
                TestArticle article = CreateStrategy.New <TestArticle>(false).Create <TestArticle>();

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

                SaveStrategy.New(article, false).Save(article);

                Collection <TestArticlePage> pages = new Collection <TestArticlePage>();

                // Create and save 3 article pages associated with the article
                for (int i = 0; i < 3; i++)
                {
                    TestArticlePage page = CreateStrategy.New <TestArticlePage>(false).Create <TestArticlePage>();
                    page.Article    = article;
                    page.Title      = "Page " + (i + 1);
                    page.ID         = Guid.NewGuid();
                    page.PageNumber = i + 1;

                    pages.Add(page);

                    SaveStrategy.New(page, false).Save(page);
                }

                // Delete the second page (0 based position = 1)
                DeleteStrategy.New(pages[1], false).Delete(pages[1]);

                // Load the article from the store
                TestArticle foundArticle = RetrieveStrategy.New <TestArticle>(false).Retrieve <TestArticle>("ID", article.ID);

                // Activate the pages on the article
                ActivateStrategy.New(foundArticle, false).Activate(foundArticle, "Pages");

                Assert.IsNotNull(foundArticle.Pages, "Pages property isn't set.");

                Assert.AreEqual(2, foundArticle.Pages.Length, "Invalid number of pages.");

                foundArticle.Pages = Collection <TestArticlePage> .Sort(foundArticle.Pages, "PageNumberAscending");

                Assert.AreEqual(1, foundArticle.Pages[0].PageNumber, "First page has wrong number.");
                Assert.AreEqual("Page 1", foundArticle.Pages[0].Title, "First page has wrong title.");
                Assert.AreEqual(2, foundArticle.Pages[1].PageNumber, "Third page has wrong number (should now be 2 as it's moved up one spot).");
                Assert.AreEqual("Page 3", foundArticle.Pages[1].Title, "Third page has wrong title.");                 // Page 3 should now be at position 1 (ie. second place)
            }
        }
        public override void ExecuteDelete(IEntity e)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }

            ISubEntity entity = (ISubEntity)e;

            if (entity.Parent == null)
            {
                ActivateStrategy.New <ISubEntity>().Activate(entity, entity.ParentPropertyName);
            }

            base.ExecuteDelete(entity);
        }
Пример #10
0
        public void ReverseBind()
        {
            using (LogGroup logGroup = LogGroup.Start("Transferring data from form fields to entity.", NLog.LogLevel.Debug))
            {
                if (DataSource == null)
                {
                    LogWriter.Debug("DataSource == null - Reverse bind skipped");
                }
                else
                {
                    ActivateStrategy.New((IEntity)DataSource).Activate((IEntity)DataSource);

                    foreach (TableRow row in this.Rows)
                    {
                        if (row is EntityFormItem)
                        {
                            using (LogGroup logGroup2 = LogGroup.StartDebug("Checking row with field control ID '" + ((EntityFormItem)row).FieldControlID + "'."))
                            {
                                LogWriter.Debug("Enabled: " + row.Enabled);
                                LogWriter.Debug("Visible: " + row.Visible);

                                if (row.Enabled && row.Visible)
                                {
                                    LogWriter.Debug("Enabled and visible.");

                                    EntityFormItem item = (EntityFormItem)row;

                                    if (item.AutoBind && item.PropertyName != null && item.PropertyName != String.Empty)
                                    {
                                        LogWriter.Debug("Property name: " + item.PropertyName);

                                        WebControl field = (WebControl)FindControl(item.FieldControlID);
                                        // Skip label fields, they're not editable
                                        if (field.GetType() != typeof(Label) &&
                                            field.Enabled)
                                        {
                                            Type propertyType = Reflector.GetPropertyType(DataSource, item.PropertyName);

                                            LogWriter.Debug("Property type: " + (propertyType == null ? "[null]" : propertyType.ToString()));

                                            object value = EntityFormHelper.GetFieldValue(field, item.ControlValuePropertyName, propertyType);

                                            LogWriter.Debug("Field value type: " + (value == null ? "[null]" : value.GetType().ToString()));
                                            LogWriter.Debug("Field value: " + (value == null ? "[null]" : value.ToString()));

                                            object castValue = EntityFormHelper.Convert(value, propertyType);

                                            Reflector.SetPropertyValue(DataSource, item.PropertyName, castValue);
                                        }
                                    }
                                    else
                                    {
                                        LogWriter.Debug("Not auto binding or no property name specified.");
                                    }
                                }
                                else
                                {
                                    LogWriter.Debug("Skipping.");
                                }
                            }
                        }
                    }
                }
            }
        }