public ActionResult EditPage(string versionId)
        {
            bool viewFlag = false;
            bool editFlag = true;

            Meanstream.Portal.Core.Content.PageVersion page = Meanstream.Portal.Core.Content.ContentService.Current.GetPageVersion(new Guid(versionId));

            if (page == null)
                return View();

            List<Meanstream.Portal.Core.Membership.Role> roles = null;
            Meanstream.Portal.Core.Membership.Role AllUsersRole = Meanstream.Portal.Core.Membership.MembershipService.Current.GetRoleByName(Meanstream.Portal.Core.AppConstants.ALLUSERS);

            if (Request.IsAuthenticated)
            {
                roles = Meanstream.Portal.Core.Membership.MembershipService.Current.GetRolesForUser(HttpContext.Profile.UserName);

                foreach (Meanstream.Portal.Core.Membership.Role Role in roles)
                {
                    if (HasViewPagePermissionsVersion(page.Id, Role.Id))
                    {
                        viewFlag = true;
                    }

                    if (HasEditPagePermissionsVersion(page.Id, Role.Id) | Meanstream.Portal.Core.Membership.MembershipService.Current.IsUserInRole(HttpContext.Profile.UserName, Meanstream.Portal.Core.AppConstants.ADMINISTRATOR))
                    {
                        editFlag = true;
                    }
                }
            }
            else
            {
                viewFlag = false;
                editFlag = false;
            }

            if (viewFlag == false)
            {
                //go to error page
                FormsAuthentication.RedirectToLoginPage();
            }
            
            PortalPageViewModel model = new PortalPageViewModel();
            model.Id = page.PageId;
            model.PortalId = page.PortalId;
            model.VersionId = page.Id;
            model.Layout = page.Skin.Path;
            model.MetaDescription = page.MetaDescription;
            model.MetaKeywords = page.MetaKeywords;
            model.MetaTitle = page.MetaTitle;
            model.Author = page.Author;
            model.EndDate = page.EndDate;
            model.StartDate = page.StartDate;
            model.Url = page.Url;
            model.Name = page.Name;

            foreach (Meanstream.Portal.Core.Content.SkinZone zone in page.Skin.Zones)
            {
                //get widgets for zone and sort
                List<WidgetVersion> zoneWidgets = new List<WidgetVersion>();
                IEnumerable<WidgetVersion> widgets = page.Widgets.Where(w => w.SkinPaneId == zone.Id);
                widgets = from widget in widgets orderby widget.DisplayOrder select widget;

                foreach (WidgetVersion widget in widgets)
                {
                    viewFlag = false;
                    editFlag = false;

                    if (Request.IsAuthenticated)
                    {
                        foreach (Meanstream.Portal.Core.Membership.Role role in roles)
                        {
                            if (HasViewModulePermissionsVersion(widget.Id, role.Id))
                            {
                                viewFlag = true;
                            }

                            if (HasEditModulePermissionsVersion(widget.Id, role.Id) | Meanstream.Portal.Core.Membership.MembershipService.Current.IsUserInRole(HttpContext.Profile.UserName, Meanstream.Portal.Core.AppConstants.ADMINISTRATOR))
                            {
                                editFlag = true;
                            }
                        }
                    }
                    else
                    {
                        if (HasViewModulePermissionsVersion(widget.Id, AllUsersRole.Id))
                        {
                            viewFlag = true;
                        }
                    }

                    if (viewFlag)
                    {
                        //add to widget zone list
                        zoneWidgets.Add(widget);
                    }
                }

                model.WidgetVersions.Add(zone.Pane, zoneWidgets);
            }

            model.IsPreview = false;
            model.IsEditable = true;
            model.IsVersion = true;

            return View(model.Layout, model);
        }
        public ActionResult PreviewSkin(string skinId)
        {
            Meanstream.Portal.Core.Content.Skin skin = Meanstream.Portal.Core.Content.ContentService.Current.GetSkin(new Guid(skinId));

            PortalPageViewModel model = new PortalPageViewModel();
            model.Id = new Guid();
            model.PortalId = new Guid();
            model.VersionId = new Guid();
            model.Layout = skin.Path;
            model.MetaDescription = "";
            model.MetaKeywords = "";
            model.MetaTitle = "";
            model.Author = "";
            model.Name = "";
            model.IsPreview = true;
            model.IsEditable = false;
            model.IsVersion = true;

            return View(skin.Path, model);
        }
        public ActionResult Page(string url)
        {
            if (string.IsNullOrEmpty(url))
                url = "home";

            //get page from viewname/url
            bool allowCaching = true;
            bool viewFlag = false;
            Guid portalId = Meanstream.Portal.Core.PortalContext.Current.PortalId;
            Meanstream.Portal.Core.Content.Page page = Meanstream.Portal.Core.Content.ContentService.Current.GetPageByUrl(portalId, url);
 
            //bad request
            if (page == null) 
                return View();

            //bad request
            if (page.IsDeleted)
                FormsAuthentication.RedirectToLoginPage();

            Meanstream.Portal.Core.Entities.TList<Meanstream.Portal.Core.Entities.AspnetRoles> roles = null;
            Meanstream.Portal.Core.Entities.TList<Meanstream.Portal.Core.Entities.MeanstreamPagePermission> pagePermissions = GetPagePermission(page.Id, allowCaching);
            Meanstream.Portal.Core.Membership.Role AllUsersRole = GetAllUsersRole(allowCaching);

            if (Request.IsAuthenticated)
            {
                roles = GetUserRoleCache(System.Web.HttpContext.Current.Profile.UserName, allowCaching);
                foreach (Meanstream.Portal.Core.Entities.AspnetRoles role in roles)
                {
                    if (HasViewPagePermissions(pagePermissions, role.RoleId))
                    {
                        viewFlag = true;
                        break; 
                    }
                }
            }
            else
            {
                if (HasViewPagePermissions(pagePermissions, AllUsersRole.Id))
                {
                    viewFlag = true;
                }
            }

            //unauthorized
            if (!viewFlag)
                FormsAuthentication.RedirectToLoginPage();

            PortalPageViewModel model = new PortalPageViewModel();
            model.Id = page.Id;
            model.PortalId = page.PortalId;
            model.VersionId = page.VersionId;
            model.Layout = page.Skin.Path;
            model.MetaDescription = page.MetaDescription;
            model.MetaKeywords = page.MetaKeywords;
            model.MetaTitle = page.MetaTitle;
            model.Author = page.Author;
            model.EndDate = page.EndDate;
            model.PublishDate = page.PublishDate;
            model.StartDate = page.StartDate;
            model.Url = page.Url;
            model.Name = page.Name;

            foreach (Meanstream.Portal.Core.Content.SkinZone zone in page.Skin.Zones) 
            {
               //get widgets for zone and sort
                List<Widget> zoneWidgets = new List<Widget>();
                IEnumerable<Widget> widgets = page.Widgets.Where(w => w.SkinPaneId == zone.Id);
                widgets = from widget in widgets orderby widget.DisplayOrder select widget;

                foreach (Widget widget in widgets) 
                {
                    viewFlag = false;

                    Meanstream.Portal.Core.Entities.TList<Meanstream.Portal.Core.Entities.MeanstreamModulePermission> widgetPermissions = GetWidgetPermissions(widget.Id);

                    if (Request.IsAuthenticated)
                    {
                        foreach (Meanstream.Portal.Core.Entities.AspnetRoles role in roles) 
                        {
                            if (HasViewModulePermissions(widgetPermissions, widget.Id, role.RoleId)) 
                            {
                                viewFlag = true;
                                break;
                            }
                        }
                    }
                    else 
                    {
                        if (HasViewModulePermissions(widgetPermissions, widget.Id, AllUsersRole.Id))
                        {
                            viewFlag = true;
                        }
                    }

                    if (viewFlag) 
                    {
                        //add to widget zone list
                        zoneWidgets.Add(widget);
                    }
                }

                model.Widgets.Add(zone.Pane, zoneWidgets);
            }

            model.IsVersion = false;
            model.IsPreview = false;
            model.IsEditable = false;

            return View(model.Layout, model);
        }