mirror of https://github.com/Squidex/squidex.git
20 changed files with 419 additions and 131 deletions
@ -0,0 +1,77 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using Microsoft.AspNetCore.Http; |
|||
using Squidex.Infrastructure.Security; |
|||
using Squidex.Shared; |
|||
using Squidex.Shared.Identity; |
|||
|
|||
namespace Squidex.Web |
|||
{ |
|||
public static class PermissionExtensions |
|||
{ |
|||
private sealed class PermissionFeature |
|||
{ |
|||
public PermissionSet Permissions { get; } |
|||
|
|||
public PermissionFeature(PermissionSet permissions) |
|||
{ |
|||
Permissions = permissions; |
|||
} |
|||
} |
|||
|
|||
public static PermissionSet GetPermissions(this HttpContext httpContext) |
|||
{ |
|||
var feature = httpContext.Features.Get<PermissionFeature>(); |
|||
|
|||
if (feature == null) |
|||
{ |
|||
feature = new PermissionFeature(httpContext.User.Permissions()); |
|||
|
|||
httpContext.Features.Set(feature); |
|||
} |
|||
|
|||
return feature.Permissions; |
|||
} |
|||
|
|||
public static bool HasPermission(this HttpContext httpContext, Permission permission) |
|||
{ |
|||
return httpContext.GetPermissions().Includes(permission); |
|||
} |
|||
|
|||
public static bool HasPermission(this HttpContext httpContext, string id, string app = "*", string schema = "*") |
|||
{ |
|||
return httpContext.GetPermissions().Includes(Permissions.ForApp(id, app, schema)); |
|||
} |
|||
|
|||
public static bool HasPermission(this ApiController controller, Permission permission) |
|||
{ |
|||
return controller.HttpContext.GetPermissions().Includes(permission); |
|||
} |
|||
|
|||
public static bool HasPermission(this ApiController controller, string id, string app = "*", string schema = "*") |
|||
{ |
|||
if (app == "*") |
|||
{ |
|||
if (controller.RouteData.Values.TryGetValue("app", out var value) && value is string s) |
|||
{ |
|||
app = s; |
|||
} |
|||
} |
|||
|
|||
if (schema == "*") |
|||
{ |
|||
if (controller.RouteData.Values.TryGetValue("name", out var value) && value is string s) |
|||
{ |
|||
schema = s; |
|||
} |
|||
} |
|||
|
|||
return controller.HttpContext.GetPermissions().Includes(Permissions.ForApp(id, app, schema)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,52 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using Newtonsoft.Json; |
|||
using System.Collections.Generic; |
|||
using System.ComponentModel.DataAnnotations; |
|||
using System.Net.Http; |
|||
|
|||
namespace Squidex.Web |
|||
{ |
|||
public abstract class Resource |
|||
{ |
|||
[JsonProperty("_links")] |
|||
[Required] |
|||
[Display(Description = "The links.")] |
|||
public Dictionary<string, ResourceLink> Links { get; } = new Dictionary<string, ResourceLink>(); |
|||
|
|||
public void AddSelfLink(string href) |
|||
{ |
|||
AddGetLink("self", href); |
|||
} |
|||
|
|||
public void AddGetLink(string rel, string href) |
|||
{ |
|||
AddLink(rel, HttpMethod.Get, href); |
|||
} |
|||
|
|||
public void AddPostLink(string rel, string href) |
|||
{ |
|||
AddLink(rel, HttpMethod.Post, href); |
|||
} |
|||
|
|||
public void AddPutLink(string rel, string href) |
|||
{ |
|||
AddLink(rel, HttpMethod.Put, href); |
|||
} |
|||
|
|||
public void AddDeleteLink(string rel, string href) |
|||
{ |
|||
AddLink(rel, HttpMethod.Delete, href); |
|||
} |
|||
|
|||
public void AddLink(string rel, HttpMethod method, string href) |
|||
{ |
|||
Links[rel] = new ResourceLink { Href = href, Method = method }; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.ComponentModel.DataAnnotations; |
|||
using System.Net.Http; |
|||
|
|||
namespace Squidex.Web |
|||
{ |
|||
public class ResourceLink |
|||
{ |
|||
[Required] |
|||
[Display(Description = "The link url.")] |
|||
public string Href { get; set; } |
|||
|
|||
[Required] |
|||
[Display(Description = "The link method.")] |
|||
public HttpMethod Method { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using Microsoft.AspNetCore.Mvc; |
|||
using System; |
|||
using System.Linq.Expressions; |
|||
using System.Reflection; |
|||
|
|||
namespace Squidex.Web |
|||
{ |
|||
public static class UrlHelperExtensions |
|||
{ |
|||
private static class NameOf<T> |
|||
{ |
|||
public static readonly string Controller; |
|||
|
|||
static NameOf() |
|||
{ |
|||
const string suffix = "Controller"; |
|||
|
|||
var name = typeof(T).Name; |
|||
|
|||
if (name.EndsWith(suffix)) |
|||
{ |
|||
name = name.Substring(0, name.Length - suffix.Length); |
|||
} |
|||
|
|||
Controller = name; |
|||
} |
|||
} |
|||
|
|||
public static string Url<T>(this IUrlHelper urlHelper, Func<T, string> action, object values = null) where T : Controller |
|||
{ |
|||
return urlHelper.Action(action(null), NameOf<T>.Controller, values); |
|||
} |
|||
|
|||
public static string Url<T>(this Controller controller, Func<T, string> action, object values = null) where T : Controller |
|||
{ |
|||
return controller.Url.Url<T>(action, values); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
import { Pipe, PipeTransform } from '@angular/core'; |
|||
|
|||
import { Resource } from '@app/framework/internal'; |
|||
|
|||
@Pipe({ |
|||
name: 'sqxHasLink', |
|||
pure: true |
|||
}) |
|||
export class HasLinkPipe implements PipeTransform { |
|||
public transform(value: Resource, rel: string) { |
|||
return value._links && !!value._links[rel]; |
|||
} |
|||
} |
|||
|
|||
@Pipe({ |
|||
name: 'sqxHasNoLink', |
|||
pure: true |
|||
}) |
|||
export class HasNoLinkPipe implements PipeTransform { |
|||
public transform(value: Resource, rel: string) { |
|||
return !value._links || !value._links[rel]; |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
export interface Resource { |
|||
_links?: { [rel: string]: ResourceLink }; |
|||
} |
|||
|
|||
export type ResourceLinks = { [rel: string]: ResourceLink }; |
|||
export type ResourceLink = { href: string; method: ResourceMethod; }; |
|||
|
|||
export function withLinks<T extends Resource>(value: T, source: Resource) { |
|||
value._links = source._links; |
|||
|
|||
return value; |
|||
} |
|||
|
|||
export type ResourceMethod = |
|||
'get' | |
|||
'post' | |
|||
'put' | |
|||
'delete'; |
|||
Loading…
Reference in new issue