2016-07-10 02:11:50 +00:00
|
|
|
[<AutoOpen>]
|
2016-07-27 04:17:13 +00:00
|
|
|
module MyWebLog.ModuleExtensions
|
2016-07-09 04:18:44 +00:00
|
|
|
|
2016-07-27 04:17:13 +00:00
|
|
|
open MyWebLog.Entities
|
2016-07-09 04:18:44 +00:00
|
|
|
open Nancy
|
|
|
|
open Nancy.Security
|
2016-09-18 20:01:16 +00:00
|
|
|
open System
|
|
|
|
open System.Security.Claims
|
2016-07-09 04:18:44 +00:00
|
|
|
|
|
|
|
/// Parent class for all myWebLog Nancy modules
|
2016-07-10 02:11:50 +00:00
|
|
|
type NancyModule with
|
2016-07-09 04:18:44 +00:00
|
|
|
|
|
|
|
/// Strongly-typed access to the web log for the current request
|
|
|
|
member this.WebLog = this.Context.Items.[Keys.WebLog] :?> WebLog
|
|
|
|
|
|
|
|
/// Display a view using the theme specified for the web log
|
2016-07-26 00:54:45 +00:00
|
|
|
member this.ThemedView view (model : MyWebLogModel) : obj =
|
2016-07-27 04:17:13 +00:00
|
|
|
upcast this.View.[(sprintf "themes/%s/%s" this.WebLog.ThemePath view), model]
|
2016-07-09 04:18:44 +00:00
|
|
|
|
|
|
|
/// Return a 404
|
2016-07-26 00:54:45 +00:00
|
|
|
member this.NotFound () : obj = upcast HttpStatusCode.NotFound
|
2016-07-09 04:18:44 +00:00
|
|
|
|
2016-07-10 02:11:50 +00:00
|
|
|
/// Redirect a request, storing messages in the session if they exist
|
2016-07-26 00:54:45 +00:00
|
|
|
member this.Redirect url (model : MyWebLogModel) : obj =
|
2016-07-27 04:17:13 +00:00
|
|
|
match List.length model.Messages with
|
2016-07-10 02:11:50 +00:00
|
|
|
| 0 -> ()
|
2016-07-27 04:17:13 +00:00
|
|
|
| _ -> this.Session.[Keys.Messages] <- model.Messages
|
2016-07-26 00:54:45 +00:00
|
|
|
upcast this.Response.AsRedirect(url).WithStatusCode HttpStatusCode.TemporaryRedirect
|
2016-07-10 02:11:50 +00:00
|
|
|
|
2016-07-09 04:18:44 +00:00
|
|
|
/// Require a specific level of access for the current web log
|
|
|
|
member this.RequiresAccessLevel level =
|
2016-09-18 20:01:16 +00:00
|
|
|
let findClaim = new Predicate<Claim>(fun claim -> claim.Type = ClaimTypes.Role && claim.Value = sprintf "%s|%s" this.WebLog.Id level)
|
2016-07-09 04:18:44 +00:00
|
|
|
this.RequiresAuthentication()
|
2016-09-18 20:01:16 +00:00
|
|
|
this.RequiresClaims [| findClaim |]
|