Example #1
0
 internal override void OnPreRenderComplete(IDotvvmRequestContext context)
 {
     eventLog?.Add(new ControlLifeCycleEvent(this, LifeCycleEventType.PreRenderComplete, true));
     PreRenderCompleteAction(this, context);
     eventLog?.Add(new ControlLifeCycleEvent(this, LifeCycleEventType.PreRenderComplete, false));
     base.OnPreRenderComplete(context);
 }
Example #2
0
 internal override void OnPreInit(IDotvvmRequestContext context)
 {
     eventLog?.Add(new ControlLifeCycleEvent(this, LifeCycleEventType.PreInit, true));
     PreInitAction(this, context);
     eventLog?.Add(new ControlLifeCycleEvent(this, LifeCycleEventType.PreInit, false));
     base.OnPreInit(context);
 }
Example #3
0
 /// <summary>
 /// Renders the resource in the specified <see cref="IHtmlWriter" />.
 /// </summary>
 public override void Render(IHtmlWriter writer, IDotvvmRequestContext context)
 {
     writer.AddAttribute("href", GetUrl());
     writer.AddAttribute("rel", "stylesheet");
     writer.AddAttribute("type", "text/css");
     writer.RenderSelfClosingTag("link");
 }
Example #4
0
 protected override void RenderControl(IHtmlWriter writer, IDotvvmRequestContext context)
 {
     if (!RenderOnServer || GetIEnumerableFromDataSource(DataSource)?.GetEnumerator()?.MoveNext() != true)
     {
         base.RenderControl(writer, context);
     }
 }
Example #5
0
 /// <summary>
 /// Renders the contents inside the control begin and end tags.
 /// </summary>
 protected override void RenderContents(IHtmlWriter writer, IDotvvmRequestContext context)
 {
     // render template
     writer.AddKnockoutDataBind("text", "errorMessage");
     writer.RenderBeginTag("li");
     writer.RenderEndTag();
 }
Example #6
0
        /// <summary>
        /// Renders the resource in the specified <see cref="IHtmlWriter" />.
        /// </summary>
        public override void Render(IHtmlWriter writer, IDotvvmRequestContext context)
        {
            if (CdnUrl != null)
            {
                writer.AddAttribute("src", CdnUrl);
                writer.AddAttribute("type", "text/javascript");
                writer.RenderBeginTag("script");
                writer.RenderEndTag();

                if (Url != null && GlobalObjectName != null)
                {
                    writer.RenderBeginTag("script");

                    var url = context.TranslateVirtualPath(GetUrl());
                    writer.WriteUnencodedText(string.Format(CdnFallbackScript, GlobalObjectName, url));
                    writer.RenderEndTag();
                }
            }
            else if (Url != null)
            {
                writer.AddAttribute("src", GetUrl());
                writer.AddAttribute("type", "text/javascript");
                writer.RenderBeginTag("script");
                writer.RenderEndTag();
            }
        }
        protected override void OnCommandException(IDotvvmRequestContext context, ActionInfo actionInfo, Exception ex)
        {
            ((ActionFilterErrorHandlingViewModel) context.ViewModel).Result = "error was handled";
            context.IsCommandExceptionHandled = true;

            base.OnCommandException(context, actionInfo, ex);
        }
Example #8
0
        public void VerifyToken(IDotvvmRequestContext context, string token)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));
            if (string.IsNullOrWhiteSpace(token)) throw new SecurityException("CSRF protection token is missing.");

            // Construct protector with purposes
            var userIdentity = ProtectionHelpers.GetUserIdentity(context);
            var requestIdentity = ProtectionHelpers.GetRequestIdentity(context);
            var protector = this.protectionProvider.Create(PURPOSE_TOKEN, userIdentity, requestIdentity);

            // Get token
            byte[] tokenSid;
            try
            {
                var tokenData = Convert.FromBase64String(token);
                tokenSid = protector.Unprotect(tokenData);
            }
            catch (Exception ex)
            {
                // Incorrect Base64 formatting of crypto protection error
                throw new SecurityException("CSRF protection token is invalid.", ex);
            }

            // Get SID from cookie and compare with token one
            var cookieSid = this.GetOrCreateSessionId(context, canGenerate: false); // should not generate new token
            if (!cookieSid.SequenceEqual(tokenSid)) throw new SecurityException("CSRF protection token is invalid.");
        }
 /// <summary>
 /// Called after the command is invoked.
 /// </summary>
 protected internal override void OnCommandExecuted(IDotvvmRequestContext context, ActionInfo actionInfo, Exception exception)
 {
     if (exception != null)
     {
         OnCommandException(context, actionInfo, exception);
     }
 }
        protected override void OnPageException(IDotvvmRequestContext context, Exception exception)
        {
            context.IsPageExceptionHandled = true;
            context.RedirectToUrl("/error500");

            base.OnPageException(context, exception);
        }
Example #11
0
 protected internal override void OnLoad(IDotvvmRequestContext context)
 {
     eventLog?.Add(new ControlLifeCycleEvent(this, LifeCycleEventType.Load, true));
     LoadAction(this, context);
     eventLog?.Add(new ControlLifeCycleEvent(this, LifeCycleEventType.Load, false));
     base.OnLoad(context);
 }
Example #12
0
        /// <summary>
        /// Adds all attributes that should be added to the control begin tag.
        /// </summary>
        protected override void AddAttributesToRender(IHtmlWriter writer, IDotvvmRequestContext context)
        {
            object id;
            if (!IsPropertySet(ClientIDProperty))
            {
                SetValueRaw(ClientIDProperty, id = CreateClientId());
            }
            else
            {
                id = GetValueRaw(ClientIDProperty);
            }
            if (id != null) Attributes["id"] = id;

            CheckInnerTextUsage();

            // verify that the properties are used only where they should
            if (!RendersHtmlTag)
            {
                EnsureNoAttributesSet();
            }
            else
            {
                var attrBindingGroup = new KnockoutBindingGroup();
                // render hard-coded HTML attributes
                foreach (var attribute in Attributes)
                {
                    if (attribute.Value is IValueBinding)
                    {
                        var binding = attribute.Value as IValueBinding;
                        attrBindingGroup.Add(attribute.Key, binding.GetKnockoutBindingExpression());
                        if (!RenderOnServer)
                            continue;
                    }
                    AddHtmlAttribute(writer, attribute.Key, attribute.Value);
                }

                if (!attrBindingGroup.IsEmpty)
                {
                    writer.AddKnockoutDataBind("attr", attrBindingGroup);
                }

                // handle Visible property
                AddVisibleAttributeOrBinding(writer);

                // handle Text property
                writer.AddKnockoutDataBind("text", this, InnerTextProperty, () =>
                {
                    // inner Text is rendered as attribute only if contains binding
                    // otherwise it is rendered directly as encoded content
                    if (!string.IsNullOrWhiteSpace(InnerText))
                    {
                        Children.Clear();
                        Children.Add(new Literal(InnerText));
                    }
                });
            }

            base.AddAttributesToRender(writer, context);
        }
Example #13
0
        protected override void OnPreRender(IDotvvmRequestContext context)
        {
            //SetValue(SanitizedMessageProperty, OriginalMessage.Replace("<","") ); //FIXME: this causes null it should not since default of OriginalMessage is set to ""

            //let's say there is something more sophisticated IRL
            SetValue(SanitizedMessageProperty, OriginalMessage?.Replace("<", "") ?? "");
            base.OnPreRender(context);
        }
Example #14
0
 /// <summary>
 /// Renders the resource in the specified <see cref="IHtmlWriter" />.
 /// </summary>
 public override void Render(IHtmlWriter writer, IDotvvmRequestContext context)
 {
     if (string.IsNullOrWhiteSpace(Code)) return;
     writer.AddAttribute("type", "text/javascript");
     writer.RenderBeginTag("script");
     writer.WriteUnencodedText(Code);
     writer.RenderEndTag();
 }
Example #15
0
 /// <summary>
 /// Renders the control into the specified writer.
 /// </summary>
 protected override void RenderControl(IHtmlWriter writer, IDotvvmRequestContext context)
 {
     // render resource links
     var resources = context.ResourceManager.GetResourcesInOrder().Where(r => r.GetRenderPosition() == ResourceRenderPosition.Head);
     foreach (var resource in resources)
     {
         resource.Render(writer, context);
     }
 }
Example #16
0
        /// <summary>
        /// Adds all attributes that should be added to the control begin tag.
        /// </summary>
        protected override void AddAttributesToRender(IHtmlWriter writer, IDotvvmRequestContext context)
        {
            RenderEnabledProperty(writer);
            RenderOptionsProperties(writer);
            RenderChangedEvent(writer);
            RenderSelectedValueProperty(writer);

            base.AddAttributesToRender(writer, context);
        }
Example #17
0
        public override void CreateControls(IDotvvmRequestContext context, DotvvmControl container)
        {
            var literal = new Literal();
            literal.FormatString = FormatString;
            literal.ValueType = ValueType;
            literal.SetBinding(Literal.TextProperty, GetValueBinding(ValueBindingProperty));

            container.Children.Add(literal);
        }
Example #18
0
 protected internal override void OnPreRender(IDotvvmRequestContext context)
 {
     context.ResourceManager.AddRequiredResource(ResourceConstants.DotvvmResourceName);
     if (context.Configuration.Debug)
     {
         context.ResourceManager.AddRequiredResource(ResourceConstants.DotvvmDebugResourceName);
     }
     base.OnPreRender(context);
 }
Example #19
0
        /// <summary>
        /// Adds all attributes that should be added to the control begin tag.
        /// </summary>
        protected override void AddAttributesToRender(IHtmlWriter writer, IDotvvmRequestContext context)
        {
            var expression = KnockoutHelper.GetValidationTargetExpression(this);
            if (expression != null)
            {
                writer.AddKnockoutDataBind("foreach", "dotvvm.validation.getValidationErrors(" + expression + ", " + IncludeErrorsFromChildren.ToString().ToLower() + ")");
            }

            base.AddAttributesToRender(writer, context);
        }
Example #20
0
        /// <summary>
        /// Gets the markup file virtual path from the current request URL.
        /// </summary>
        public string GetMarkupFileVirtualPath(IDotvvmRequestContext context)
        {
            // get file name
            var fileName = context.Route != null ? context.Route.VirtualPath : context.OwinContext.Request.Uri.LocalPath;
            if (!fileName.EndsWith(MarkupFile.ViewFileExtension, StringComparison.CurrentCultureIgnoreCase))
            {
                throw new Exception("The view must be a file with the .dothtml extension!");     // TODO: exception handling
            }

            return fileName;
        }
Example #21
0
 protected override void RenderBeginTag(IHtmlWriter writer, IDotvvmRequestContext context)
 {
     if (RenderWrapperTag)
     {
         base.RenderBeginTag(writer, context);
     }
     else if (HasBinding(HtmlProperty))
     {
         writer.WriteKnockoutDataBindComment("html", this, HtmlProperty);
     }
 }
Example #22
0
 protected internal override void OnInit(IDotvvmRequestContext context)
 {
     var isAuthenticated = context.OwinContext.Request?.User?.Identity?.IsAuthenticated;
     if (isAuthenticated == true)
     {
         AuthenticatedTemplate?.BuildContent(context, this);
     }
     else
     {
         NotAuthenticatedTemplate?.BuildContent(context, this);
     }
 }
Example #23
0
        /// <summary>
        /// Adds all attributes that should be added to the control begin tag.
        /// </summary>
        protected override void AddAttributesToRender(IHtmlWriter writer, IDotvvmRequestContext context)
        {
            writer.AddKnockoutDataBind("dotvvmEnable", this, EnabledProperty, () =>
            {
                if (!Enabled)
                {
                    writer.AddAttribute("disabled", "disabled");
                }
            });

            base.AddAttributesToRender(writer, context);
        }
Example #24
0
        protected override void AddAttributesToRender(IHtmlWriter writer, IDotvvmRequestContext context)
        {
            if (!RenderOnServer)
            {
                if (!string.IsNullOrWhiteSpace(EmptyItemText))
                {
                    writer.AddKnockoutDataBind("optionsCaption", KnockoutHelper.MakeStringLiteral(EmptyItemText));
                }
            }

            base.AddAttributesToRender(writer, context);
        }
Example #25
0
 public static void WriteRouteLinkHrefAttribute(string routeName, HtmlGenericControl control, DotvvmProperty urlSuffixProperty, IHtmlWriter writer, IDotvvmRequestContext context)
 {
     if (!control.RenderOnServer)
     {
         var group = new KnockoutBindingGroup();
         group.Add("href", GenerateKnockoutHrefExpression(routeName, control, urlSuffixProperty, context));
         writer.AddKnockoutDataBind("attr", group);
     }
     else
     {
         writer.AddAttribute("href", EvaluateRouteUrl(routeName, control, urlSuffixProperty, context));
     }
 }
Example #26
0
        protected override void AddAttributesToRender(IHtmlWriter writer, IDotvvmRequestContext context)
        {
            RouteLinkHelpers.WriteRouteLinkHrefAttribute(RouteName, this, UrlSuffixProperty, writer, context);

            writer.AddKnockoutDataBind("text", this, TextProperty, () =>
            {
                shouldRenderText = true;
            });
            var enabledBinding = GetValueBinding(EnabledProperty);
            if (enabledBinding != null) WriteEnabledBinding(writer, enabledBinding);

            base.AddAttributesToRender(writer, context);
        }
Example #27
0
 public static string GetUserIdentity(IDotvvmRequestContext context)
 {
     var user = context.OwinContext.Request.User;
     
     if (user != null && user.Identity.IsAuthenticated)
     {
         return "user" + user.Identity.Name ?? "";
     }
     else
     {
         return "anonymous";
     }
 }
Example #28
0
        protected override void RenderContents(IHtmlWriter writer, IDotvvmRequestContext context)
        {
            var properties =
                GetDeclaredProperties()
                .Where(p => !p.DeclaringType.IsAssignableFrom(typeof(DotvvmMarkupControl)))
                .Select(GetPropertySerializationInfo)
                .Where(p => p.IsSerializable)
                .Select(p => JsonConvert.SerializeObject(p.Property.Name) + ": " + p.Js);

            writer.WriteKnockoutDataBindComment("dotvvm_withControlProperties", "{ " + string.Join(", ", properties) + " }");
            base.RenderContents(writer, context);
            writer.WriteKnockoutDataBindEndComment();
        }
Example #29
0
        public static string EvaluateRouteUrl(string routeName, HtmlGenericControl control, DotvvmProperty urlSuffixProperty, IDotvvmRequestContext context)
        {
            var coreUrl = GenerateRouteUrlCore(routeName, control, context) + (control.GetValue(urlSuffixProperty) as string ?? "");

            if ((bool)control.GetValue(Internal.IsSpaPageProperty))
            {
                return "#!/" + coreUrl;
            }
            else
            {
                return context.TranslateVirtualPath(coreUrl);
            }
        }
Example #30
0
        protected override void AddAttributesToRender(IHtmlWriter writer, IDotvvmRequestContext context)
        {
            if (!RenderOnServer)
            {
                writer.AddKnockoutDataBind("visible", $"!({ GetForeachDataBindJavascriptExpression() }).length");

                if (DataSource != null && GetIEnumerableFromDataSource(DataSource).OfType<object>().Any())
                {
                    writer.AddStyleAttribute("display", "none");
                }
            }

            base.AddAttributesToRender(writer, context);
        }