forked from OData/RESTier
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add action to be executed without additional controller.
- Loading branch information
1 parent
f402889
commit 2123cab
Showing
25 changed files
with
936 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/Microsoft.Restier.Core/Conventions/ConventionBasedOperationAuthorizer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Restier.Core.Operation; | ||
|
||
namespace Microsoft.Restier.Core.Conventions | ||
{ | ||
/// <summary> | ||
/// A convention-based operation authorizer. | ||
/// </summary> | ||
internal class ConventionBasedOperationAuthorizer : IOperationAuthorizer | ||
{ | ||
private Type targetType; | ||
|
||
private ConventionBasedOperationAuthorizer(Type targetType) | ||
{ | ||
Ensure.NotNull(targetType, "targetType"); | ||
this.targetType = targetType; | ||
} | ||
|
||
public static void ApplyTo( | ||
IServiceCollection services, | ||
Type targetType) | ||
{ | ||
Ensure.NotNull(services, "services"); | ||
Ensure.NotNull(targetType, "targetType"); | ||
services.AddService<IOperationAuthorizer>((sp, next) => new ConventionBasedOperationAuthorizer(targetType)); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public Task<bool> AuthorizeAsync( | ||
OperationContext context, | ||
CancellationToken cancellationToken) | ||
{ | ||
Ensure.NotNull(context, "context"); | ||
bool result = true; | ||
|
||
Type returnType = typeof(bool); | ||
var methodName = ConventionBasedChangeSetConstants.AuthorizeMethodActionInvocationExecute + | ||
context.OperationName; | ||
MethodInfo method = this.targetType.GetQualifiedMethod(methodName); | ||
|
||
if (method != null && method.IsFamily && | ||
method.ReturnType == returnType) | ||
{ | ||
object target = null; | ||
if (!method.IsStatic) | ||
{ | ||
target = context.GetApiService<ApiBase>(); | ||
if (target == null || | ||
!this.targetType.IsInstanceOfType(target)) | ||
{ | ||
return Task.FromResult(result); | ||
} | ||
} | ||
|
||
var parameters = method.GetParameters(); | ||
if (parameters.Length == 0) | ||
{ | ||
result = (bool)method.Invoke(target, null); | ||
} | ||
} | ||
|
||
return Task.FromResult(result); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/Microsoft.Restier.Core/Operation/IOperationAuthorizer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Restier.Core.Submit; | ||
|
||
namespace Microsoft.Restier.Core.Operation | ||
{ | ||
/// <summary> | ||
/// Represents a operation authorizer. | ||
/// </summary> | ||
public interface IOperationAuthorizer | ||
{ | ||
/// <summary> | ||
/// Asynchronously authorizes the Operation. | ||
/// </summary> | ||
/// <param name="context"> | ||
/// The operation context. | ||
/// </param> | ||
/// <param name="cancellationToken"> | ||
/// A cancellation token. | ||
/// </param> | ||
/// <returns> | ||
/// A task that represents the asynchronous operation. | ||
/// </returns> | ||
Task<bool> AuthorizeAsync( | ||
OperationContext context, | ||
CancellationToken cancellationToken); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Microsoft.Restier.Core/Operation/IOperationExecutor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Restier.Core.Operation | ||
{ | ||
/// <summary> | ||
/// Represents a service that executes an operation. | ||
/// </summary> | ||
public interface IOperationExecutor | ||
{ | ||
/// <summary> | ||
/// Asynchronously executes an operation. | ||
/// </summary> | ||
/// <param name="instanceImplementMethod"> | ||
/// A class instance with have operation implemented and will be used for reflection call. | ||
/// </param> | ||
/// <param name="context"> | ||
/// The operation context. | ||
/// </param> | ||
/// <param name="cancellationToken"> | ||
/// A cancellation token. | ||
/// </param> | ||
/// <returns> | ||
/// A task that represents the asynchronous | ||
/// operation whose result is a operation result. | ||
/// </returns> | ||
Task<IQueryable> ExecuteOperationAsync( | ||
object instanceImplementMethod, OperationContext context, CancellationToken cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Linq; | ||
|
||
namespace Microsoft.Restier.Core.Operation | ||
{ | ||
/// <summary> | ||
/// Represents context under which a operation is executed. | ||
/// </summary> | ||
public class OperationContext : InvocationContext | ||
{ | ||
private readonly string operationName; | ||
private readonly Func<string, object> getParameterValueFunc; | ||
private readonly bool isFunction; | ||
private readonly IQueryable bindingParameterValue; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="OperationContext" /> class. | ||
/// </summary> | ||
/// <param name="apiContext"> | ||
/// An API context. | ||
/// </param> | ||
/// <param name="getParameterValueFunc"> | ||
/// The function that used to retrieve the parameter value name. | ||
/// </param> | ||
/// <param name="operationName"> | ||
/// The operation name. | ||
/// </param> | ||
/// <param name="isFunction"> | ||
/// A flag indicates this is a function call or action call. | ||
/// </param> | ||
/// <param name="bindingParameterValue"> | ||
/// A queryable for binding parameter value and if it is function/action import, the value will be null. | ||
/// </param> | ||
public OperationContext( | ||
ApiContext apiContext, | ||
Func<string, object> getParameterValueFunc, | ||
string operationName, | ||
bool isFunction, | ||
IQueryable bindingParameterValue) | ||
: base(apiContext) | ||
{ | ||
this.getParameterValueFunc = getParameterValueFunc; | ||
this.operationName = operationName; | ||
this.isFunction = isFunction; | ||
this.bindingParameterValue = bindingParameterValue; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the operation name. | ||
/// </summary> | ||
public string OperationName | ||
{ | ||
get | ||
{ | ||
return this.operationName; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the function that used to retrieve the parameter value name. | ||
/// </summary> | ||
public Func<string, object> GetParameterValueFunc | ||
{ | ||
get | ||
{ | ||
return this.getParameterValueFunc; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether it is a function call or action call. | ||
/// </summary> | ||
public bool IsFunction | ||
{ | ||
get | ||
{ | ||
return this.isFunction; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the queryable for binding parameter value, | ||
/// and if it is function/action import, the value will be null. | ||
/// </summary> | ||
public IQueryable BindingParameterValue | ||
{ | ||
get | ||
{ | ||
return this.bindingParameterValue; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.