Skip to content

Commit

Permalink
Plugin Base WebViewPage #22 & Authorization #24
Browse files Browse the repository at this point in the history
Plugins have a base WebViewPage to inherit, this offers integration with
various Disco services. Plugins can also add Authorization attributes to
their Web Handlers and Controller Methods.
  • Loading branch information
garysharp committed Oct 14, 2013
1 parent 4b822d3 commit 9784c5d
Show file tree
Hide file tree
Showing 21 changed files with 496 additions and 112 deletions.
17 changes: 6 additions & 11 deletions Disco.Services/Authorization/DiscoAuthorizeAllAttribute.cs
Expand Up @@ -8,7 +8,7 @@

namespace Disco.Services.Authorization
{
public class DiscoAuthorizeAllAttribute : AuthorizeAttribute
public class DiscoAuthorizeAllAttribute : DiscoAuthorizeBaseAttribute
{
string[] authorizedClaims;

Expand All @@ -20,22 +20,17 @@ public DiscoAuthorizeAllAttribute(params string[] AuthorisedClaims)
this.authorizedClaims = AuthorisedClaims;
}

protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
public override bool IsAuthorized(System.Web.HttpContextBase httpContext)
{
if (httpContext == null)
throw new ArgumentNullException("httpContext");

var authToken = UserService.CurrentAuthorization;

if (authToken == null)
if (Token == null)
return false; // No Current User

return authToken.HasAll(authorizedClaims);
return Token.HasAll(authorizedClaims);
}

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
public override string HandleUnauthorizedMessage()
{
filterContext.Result = new HttpUnauthorizedResult(AuthorizationToken.BuildRequireAllMessage(authorizedClaims));
return AuthorizationToken.BuildRequireAllMessage(authorizedClaims);
}
}
}
17 changes: 6 additions & 11 deletions Disco.Services/Authorization/DiscoAuthorizeAnyAttribute.cs
Expand Up @@ -8,7 +8,7 @@

namespace Disco.Services.Authorization
{
public class DiscoAuthorizeAnyAttribute : AuthorizeAttribute
public class DiscoAuthorizeAnyAttribute : DiscoAuthorizeBaseAttribute
{
string[] authorizedClaims;

Expand All @@ -20,22 +20,17 @@ public DiscoAuthorizeAnyAttribute(params string[] AuthorisedClaims)
this.authorizedClaims = AuthorisedClaims;
}

protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
public override bool IsAuthorized(System.Web.HttpContextBase httpContext)
{
if (httpContext == null)
throw new ArgumentNullException("httpContext");

var authToken = UserService.CurrentAuthorization;

if (authToken == null)
if (Token == null)
return false; // No Current User

return authToken.HasAny(authorizedClaims);
return Token.HasAny(authorizedClaims);
}

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
public override string HandleUnauthorizedMessage()
{
filterContext.Result = new HttpUnauthorizedResult(AuthorizationToken.BuildRequireAnyMessage(authorizedClaims));
return AuthorizationToken.BuildRequireAnyMessage(authorizedClaims);
}
}
}
19 changes: 7 additions & 12 deletions Disco.Services/Authorization/DiscoAuthorizeAttribute.cs
Expand Up @@ -8,7 +8,7 @@

namespace Disco.Services.Authorization
{
public class DiscoAuthorizeAttribute : AuthorizeAttribute
public class DiscoAuthorizeAttribute : DiscoAuthorizeBaseAttribute
{
string authorizedClaim;

Expand All @@ -19,23 +19,18 @@ public DiscoAuthorizeAttribute(string AuthorisedClaim)
this.authorizedClaim = AuthorisedClaim;
}

protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
public override bool IsAuthorized(System.Web.HttpContextBase httpContext)
{
if (httpContext == null)
throw new ArgumentNullException("httpContext");

var authToken = UserService.CurrentAuthorization;

if (authToken == null)
if (Token == null)
return false; // No Current User

if (authorizedClaim == null)
return authToken.RoleTokens.Count > 0; // Just Authenticate - no Authorization (but require at least 1 role)
return Token.RoleTokens.Count > 0; // Just Authenticate - no Authorization (but require at least 1 role)
else
return authToken.Has(authorizedClaim);
return Token.Has(authorizedClaim);
}

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
public override string HandleUnauthorizedMessage()
{
string resultMessage;

Expand All @@ -47,7 +42,7 @@ protected override void HandleUnauthorizedRequest(AuthorizationContext filterCon
else
resultMessage = AuthorizationToken.BuildRequireMessage(authorizedClaim);

filterContext.Result = new HttpUnauthorizedResult(resultMessage);
return resultMessage;
}
}
}
39 changes: 39 additions & 0 deletions Disco.Services/Authorization/DiscoAuthorizeBaseAttribute.cs
@@ -0,0 +1,39 @@
using Disco.Services.Users;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Disco.Services.Authorization
{
public abstract class DiscoAuthorizeBaseAttribute : AuthorizeAttribute
{
protected AuthorizationToken Token
{
get
{
return UserService.CurrentAuthorization;
}
}

public abstract bool IsAuthorized(System.Web.HttpContextBase httpContext);
public abstract string HandleUnauthorizedMessage();

protected sealed override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
{
if (httpContext == null)
throw new ArgumentNullException("httpContext");

return IsAuthorized(httpContext);
}

protected sealed override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
string resultMessage = HandleUnauthorizedMessage();

filterContext.Result = new HttpUnauthorizedResult(resultMessage);
}
}
}
5 changes: 4 additions & 1 deletion Disco.Services/Disco.Services.csproj
Expand Up @@ -92,6 +92,7 @@
<Compile Include="Authorization\AccessDeniedException.cs" />
<Compile Include="Authorization\AuthorizationToken.cs" />
<Compile Include="Authorization\ClaimNavigatorItem.cs" />
<Compile Include="Authorization\DiscoAuthorizeBaseAttribute.cs" />
<Compile Include="Authorization\DiscoAuthorizeAllAttribute.cs" />
<Compile Include="Authorization\DiscoAuthorizeAnyAttribute.cs" />
<Compile Include="Authorization\DiscoAuthorizeAttribute.cs" />
Expand Down Expand Up @@ -174,6 +175,8 @@
<Compile Include="Plugins\PluginWebHandlerController.cs" />
<Compile Include="Plugins\UninstallPluginTask.cs" />
<Compile Include="Plugins\UnknownPluginException.cs" />
<Compile Include="Plugins\WebHelper.cs" />
<Compile Include="Plugins\PluginWebViewPage.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tasks\ScheduledTask.cs" />
<Compile Include="Tasks\ScheduledTasks.cs" />
Expand Down Expand Up @@ -220,7 +223,7 @@
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
<VisualStudio>
<UserProperties BuildVersion_UseGlobalSettings="False" BuildVersion_DetectChanges="False" BuildVersion_BuildAction="Both" BuildVersion_StartDate="2011/7/1" BuildVersion_BuildVersioningStyle="None.DeltaBaseYear.MonthAndDayStamp.TimeStamp" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_UpdateFileVersion="True" />
<UserProperties BuildVersion_UpdateFileVersion="True" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_BuildVersioningStyle="None.DeltaBaseYear.MonthAndDayStamp.TimeStamp" BuildVersion_StartDate="2011/7/1" BuildVersion_BuildAction="Both" BuildVersion_DetectChanges="False" BuildVersion_UseGlobalSettings="False" />
</VisualStudio>
</ProjectExtensions>
<PropertyGroup>
Expand Down
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Disco.Services.Plugins.Features.UIExtension.Results
{
Expand All @@ -18,7 +19,7 @@ public MultipleResult(PluginFeatureManifest Source, params UIExtensionResult[] R
this.results = Results;
}

public override void ExecuteResult<T>(System.Web.Mvc.WebViewPage<T> page)
public override void ExecuteResult<T>(WebViewPage<T> page)
{
foreach (var result in this.results)
{
Expand Down
@@ -1,9 +1,12 @@
using System;
using Disco.Services.Web.Bundles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Disco.Services.Plugins.Features.UIExtension.Results
{
Expand All @@ -16,13 +19,13 @@ public PluginResourceCssResult(PluginFeatureManifest Source, string Resource)
: base(Source)
{
this._resource = Resource;
this._resourceUrl = HttpContext.Current.Request.RequestContext.DiscoPluginResourceUrl(Resource, false, Source.PluginManifest);
this._resourceUrl = new HtmlString(Source.PluginManifest.WebResourceUrl(Resource));

var deferredBundles = HttpContext.Current.Items["Bundles.UIExtensionCss"] as List<HtmlString>;
var deferredBundles = HttpContext.Current.Items[Bundle.UIExtensionCssKey] as List<HtmlString>;
if (deferredBundles == null)
{
deferredBundles = new List<HtmlString>();
HttpContext.Current.Items["Bundles.UIExtensionCss"] = deferredBundles;
HttpContext.Current.Items[Bundle.UIExtensionCssKey] = deferredBundles;
}
if (!deferredBundles.Contains(this._resourceUrl))
deferredBundles.Add(this._resourceUrl);
Expand Down
@@ -1,4 +1,5 @@
using System;
using Disco.Services.Web.Bundles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -17,16 +18,16 @@ public PluginResourceScriptResult(PluginFeatureManifest Source, string Resource,
: base(Source)
{
this._resource = Resource;
this._resourceUrl = HttpContext.Current.Request.RequestContext.DiscoPluginResourceUrl(Resource, false, Source.PluginManifest);
this._resourceUrl = new HtmlString(Source.PluginManifest.WebResourceUrl(Resource));
this._placeInPageHead = PlaceInPageHead;

if (this._placeInPageHead)
{
var deferredBundles = HttpContext.Current.Items["Bundles.UIExtensionScripts"] as List<HtmlString>;
var deferredBundles = HttpContext.Current.Items[Bundle.UIExtensionScriptsKey] as List<HtmlString>;
if (deferredBundles == null)
{
deferredBundles = new List<HtmlString>();
HttpContext.Current.Items["Bundles.UIExtensionScripts"] = deferredBundles;
HttpContext.Current.Items[Bundle.UIExtensionScriptsKey] = deferredBundles;
}
if (!deferredBundles.Contains(this._resourceUrl))
deferredBundles.Add(this._resourceUrl);
Expand Down

0 comments on commit 9784c5d

Please sign in to comment.