/// <summary> /// Gives out adialog pane /// </summary> public static void GivePane(this WebContext wc, short status, Action <HtmlContent> main = null, bool?shared = null, short maxage = 12, string title = null) { var h = new HtmlContent(true, 8 * 1024) { Web = wc }; h.Add("<!DOCTYPE html>"); h.Add("<html>"); h.Add("<head>"); if (title != null) { h.Add("<title>"); h.Add(title); h.Add("</title>"); } h.Add("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"); h.Add("<link rel=\"stylesheet\" href=\"/uikit.min.css\">"); h.Add("<link rel=\"stylesheet\" href=\"/app.min.css\">"); h.Add("<script src=\"/uikit.min.js\"></script>"); h.Add("<script src=\"/uikit-icons.min.js\"></script>"); h.Add("<script src=\"/app.min.js\"></script>"); h.Add("</head>"); h.Add("<body class=\"uk-pane\">"); main?.Invoke(h); h.Add("<script>"); if (main != null) // enable the ok button { h.Add("var btn = window.parent.document.getElementById('okbtn');"); h.Add("if (btn) btn.disabled = (document.forms.length == 0);"); } else // trigger click on the close-button { h.Add("closeUp(true);"); } h.Add("</script>"); h.Add("</body>"); h.Add("</html>"); wc.Give(status, h, shared, maxage); }
/// <summary> /// Gives a frame page. /// </summary> public static void GivePage(this WebContext wc, short status, Action <HtmlContent> main, bool?shared = null, short maxage = 12, string title = null, bool manifest = false) { var h = new HtmlContent(true, 32 * 1024) { Web = wc }; h.Add("<!DOCTYPE html>"); h.Add("<html lang=\"zh-CN\">"); h.Add("<head>"); h.Add("<title>"); h.Add(title ?? wc.Work.Label); h.Add("</title>"); h.Add("<link rel=\"shortcut icon\" href=\"/favicon.ico\" />"); if (manifest) { h.Add("<link rel=\"manifest\" href=\"manifest.json\" />"); } h.Add("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\">"); h.Add("<link rel=\"stylesheet\" href=\"/uikit.min.css\">"); h.Add("<link rel=\"stylesheet\" href=\"/app.min.css\">"); h.Add("<script src=\"/uikit.min.js\"></script>"); h.Add("<script src=\"/uikit-icons.min.js\"></script>"); h.Add("<script src=\"/app.min.js\"></script>"); h.Add("</head>"); h.Add("<body>"); main(h); h.Add("</body>"); h.Add("</html>"); wc.Give(status, h, shared, maxage); }
public static void GiveFrame(this WebContext wc, int status, bool?shared = null, short maxage = 60, string title = null, byte group = 0) { var h = new HtmlContent(true, 8 * 1024) { Web = wc }; h.Add("<!DOCTYPE html>"); h.Add("<html lang=\"zh-CN\" style=\"height:100%;\">"); h.Add("<head>"); h.Add("<title>"); h.Add(title); h.Add("</title>"); h.Add("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\">"); h.Add("<link rel=\"shortcut icon\" href=\"/favicon.ico\" />"); h.Add("<link rel=\"stylesheet\" href=\"/uikit.min.css\">"); h.Add("<link rel=\"stylesheet\" href=\"/app.min.css\">"); h.Add("<script src=\"/uikit.min.js\"></script>"); h.Add("<script src=\"/uikit-icons.min.js\"></script>"); h.Add("<script src=\"/app.min.js\"></script>"); h.Add("</head>"); h.Add("<body style=\"height:100%; overflow-y: hidden\">"); var wrk = wc.Work; var subwrks = wrk.Works; // render tabs h.Add("<ul class=\"uk-tab\" uk-tab>"); h.Add("<li class=\"uk-active\"><a href=\"#\">"); h.Add(wrk.Label); h.Add("</a></li>"); for (int i = 0; i < subwrks?.Count; i++) { var sub = subwrks.ValueAt(i); if (sub.Group != 0 && (sub.Group & group) != sub.Group) { continue; } if (sub.Ui == null || !sub.DoAuthorize(wc, true)) { continue; } h.Add("<li><a href=\"#\">"); h.Add(sub.Label); h.Add("</a></li>"); } h.Add("</ul>"); // tabs content h.Add("<ul class=\"uk-switcher\" style=\"height: calc(100% - 42px); height: -webkit-calc(100% - 42px);\">"); // the first panel h.Add("<li class=\"uk-active\" style=\"height: 100%\">"); h.Add("<iframe src=\"?inner=true\" title=\"main content\" frameborder=\"0\" style=\"width: 100%; height: 100%;\"></iframe>"); h.Add("</li>"); // the sub-level panels for (int i = 0; i < subwrks?.Count; i++) { var sub = subwrks.ValueAt(i); if (sub.Group != 0 && (sub.Group & group) != sub.Group) { continue; } if (sub.Ui == null || !sub.DoAuthorize(wc, true)) { continue; } h.Add("<li style=\"height: 100%\"><iframe id=\""); h.Add(sub.Key); h.Add("/\" title=\"main content\" frameborder=\"0\" style=\"width:100%; height:100%;\"></iframe></li>"); } h.Add(" </ul>"); // lazy set src for iframes h.Add("<script>"); h.Add("var lis = document.querySelector('.uk-switcher').children;"); h.Add("for (var i = 0; i < lis.length; i++) {"); h.Add("lis[i].addEventListener('show', function(e) {"); h.Add("if (!this.firstChild.src) this.firstChild.src = this.firstChild.id;"); h.Add("});"); h.Add("}"); h.Add("</script>"); h.Add("</body>"); h.Add("</html>"); wc.Give(status, h, shared, maxage); }
public abstract Task <bool> DoAsync(WebContext wc);
public static void GiveOffCanvas(this WebContext wc, short status, bool?shared = null, short maxage = 60, string title = null) { var h = new HtmlContent(true, 8 * 1024) { Web = wc }; h.Add("<!DOCTYPE html>"); h.Add("<html style=\"height:100%;\">"); h.Add("<head>"); h.Add("<title>"); h.Add(title ?? wc.Work.Label); h.Add("</title>"); h.Add("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\">"); h.Add("<link rel=\"stylesheet\" href=\"/uikit.min.css\">"); h.Add("<link rel=\"stylesheet\" href=\"/app.min.css\">"); h.Add("<script src=\"/uikit.min.js\"></script>"); h.Add("<script src=\"/uikit-icons.min.js\"></script>"); h.Add("<script src=\"/app.min.js\"></script>"); h.Add("</head>"); h.Add("<body style=\"height:100%; overflow-y: hidden\">"); h.Add("<div class=\"uk-offcanvas-content uk-height-1-1\">"); h.Add("<a class=\"uk-icon-link uk-offcanvas-toggle\" uk-icon=\"icon: chevron-right; ratio: 1.5\" uk-toggle=\"target: #offcanvas-push\"></a>"); h.Add("<div id=\"offcanvas-push\" uk-offcanvas=\"mode: push; overlay: true\">"); h.Add("<div class=\"uk-offcanvas-bar\">"); h.Add("<button class=\"uk-offcanvas-close\" type=\"button\" uk-close></button>"); var wrk = wc.Work; var subs = wrk.Works; // tabs h.Add("<ul class=\"uk-tab uk-tab-right\" uk-tab=\"connect: #iswitcher; media: 270\">"); h.Add("<li class=\"uk-active\"><a href=\"#\">"); h.Add(wrk.Label); h.Add("</a></li>"); if (subs != null) { for (int i = 0; i < subs.Count; i++) { var sub = subs.ValueAt(i); if (!sub.DoAuthorize(wc, true)) { continue; } h.Add("<li><a href=\"#\">"); h.Add(sub.Label); h.Add("</a></li>"); } } h.Add("</ul>"); h.Add("</div>"); h.Add("</div>"); // switcher h.Add("<ul id=\"iswitcher\" class=\"uk-switcher uk-height-1-1\">"); // the first panel h.Add("<li class=\"uk-active\" style=\"height: 100%\">"); h.Add("<iframe src=\"?inner=true\" frameborder=\"0\" style=\"width: 100%; height: 100%;\"></iframe>"); h.Add("</li>"); if (subs != null) { // the sub-level panels for (int i = 0; i < subs.Count; i++) { var sub = subs.ValueAt(i); if (!sub.DoAuthorize(wc, true)) { continue; } h.Add("<li style=\"height: 100%\"><iframe src=\""); h.Add(sub.Key); h.Add("/\" frameborder=\"0\" style=\"width:100%; height:100%;\"></iframe></li>"); } } h.Add(" </ul>"); h.Add("</div>"); h.Add("</body>"); h.Add("</html>"); wc.Give(status, h, shared, maxage); }
public abstract bool Do(WebContext wc);
/// <summary> /// The asynchronous version of authentication check. /// </summary> /// <remarks>The method only tries to establish principal identity within current web context, not responsible for any related user interaction.</remarks> /// <param name="wc">current web request/response context</param> /// <returns>true to indicate the web context should continue with processing; false otherwise</returns> public virtual Task <bool> DoAsync(WebContext wc) => throw new NotImplementedException();
/// <summary> /// The synchronous version of authentication check. /// </summary> /// <remarks>The method only tries to establish principal identity within current web context, not responsible for any related user interaction.</remarks> /// <param name="wc">current web request/response context</param> /// <returns>true to indicate the web context should continue with processing; false otherwise</returns> public virtual bool Do(WebContext wc) => throw new NotImplementedException();
internal async Task HandleAsync(string rsc, WebContext wc) { wc.Work = this; if (!DoAuthorize(wc, false)) { throw new WebException("authorize failed: " + Name) { Code = wc.Principal == null ? 401 : 403 }; } int slash = rsc.IndexOf('/'); if (slash == -1) // this work is the target work { if (Before != null) { if (Before.IsAsync && !await Before.DoAsync(wc) || !Before.IsAsync && Before.Do(wc)) { return; } } // // resolve the resource string name = rsc; int subscpt = 0; int dash = rsc.LastIndexOf('-'); if (dash != -1) { name = rsc.Substring(0, dash); wc.Subscript = subscpt = rsc.Substring(dash + 1).ToInt(); } var act = this[name]; if (act == null) { wc.Give(404, "action not found", true, 30); return; } wc.Action = act; if (!act.DoAuthorize(wc, false)) { throw new WebException("Do authorize failure: " + act.Name) { Code = wc.Principal == null ? 401 : 403 }; } // try in the cache first if (!Service.TryGiveFromCache(wc)) { // invoke action method if (act.IsAsync) { await act.DoAsync(wc, subscpt); } else { act.Do(wc, subscpt); } Service.TryAddForCache(wc); } wc.Action = null; if (After != null) { if (After.IsAsync && !await After.DoAsync(wc) || !After.IsAsync && After.Do(wc)) { return; } } } else // sub works { string key = rsc.Substring(0, slash); var wrk = GetSubWork(wc, key); if (wrk != null) // if child { // do necessary authentication before entering if (wc.Principal == null && !await wrk.DoAuthenticate(wc)) { return; } wc.AppendSeg(wrk, key); await wrk.HandleAsync(rsc.Substring(slash + 1), wc); } else if (varwork != null) // if variable-key subwork { // do necessary authentication before entering if (wc.Principal == null && !await varwork.DoAuthenticate(wc)) { return; } var prin = wc.Principal; object accessor; if (key.Length == 0) { if (prin == null) { throw AuthReq; } accessor = varwork.GetAccessor(prin, null); if (accessor == null) { throw AccessorReq; } } else { accessor = varwork.GetAccessor(prin, key); } // append the segment wc.AppendSeg(varwork, key, accessor); await varwork.HandleAsync(rsc.Substring(slash + 1), wc); } } }
public abstract bool Do(WebContext wc, bool mock);