mirror of https://github.com/Squidex/squidex.git
19 changed files with 335 additions and 59 deletions
@ -0,0 +1,49 @@ |
|||||
|
// ==========================================================================
|
||||
|
// ScopesProcessor.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Reflection; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Authorization; |
||||
|
using NSwag; |
||||
|
using NSwag.SwaggerGeneration.Processors; |
||||
|
using NSwag.SwaggerGeneration.Processors.Contexts; |
||||
|
using Squidex.Infrastructure.Tasks; |
||||
|
|
||||
|
// ReSharper disable InvertIf
|
||||
|
|
||||
|
namespace Squidex.Config.Swagger |
||||
|
{ |
||||
|
public class ScopesProcessor : IOperationProcessor |
||||
|
{ |
||||
|
public Task<bool> ProcessAsync(OperationProcessorContext context) |
||||
|
{ |
||||
|
if (context.OperationDescription.Operation.Security == null) |
||||
|
{ |
||||
|
context.OperationDescription.Operation.Security = new List<SwaggerSecurityRequirement>(); |
||||
|
} |
||||
|
|
||||
|
var authorizeAttributes = |
||||
|
context.MethodInfo.GetCustomAttributes(true).OfType<AuthorizeAttribute>().Union( |
||||
|
context.MethodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(true).OfType<AuthorizeAttribute>()).ToArray(); |
||||
|
|
||||
|
if (authorizeAttributes.Any()) |
||||
|
{ |
||||
|
var scopes = authorizeAttributes.Where(a => a.Roles != null).SelectMany(a => a.Roles.Split(',')).Distinct().ToList(); |
||||
|
|
||||
|
context.OperationDescription.Operation.Security.Add(new SwaggerSecurityRequirement |
||||
|
{ |
||||
|
{ Constants.SecurityDefinition, scopes } |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
return TaskHelper.True; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,79 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; |
||||
|
import { inject, TestBed } from '@angular/core/testing'; |
||||
|
|
||||
|
import { |
||||
|
ApiUrlConfig, |
||||
|
UIService, |
||||
|
UISettingsDto |
||||
|
} from './../'; |
||||
|
|
||||
|
describe('UIService', () => { |
||||
|
beforeEach(() => { |
||||
|
TestBed.configureTestingModule({ |
||||
|
imports: [ |
||||
|
HttpClientTestingModule |
||||
|
], |
||||
|
providers: [ |
||||
|
UIService, |
||||
|
{ provide: ApiUrlConfig, useValue: new ApiUrlConfig('http://service/p/') } |
||||
|
] |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
afterEach(inject([HttpTestingController], (httpMock: HttpTestingController) => { |
||||
|
httpMock.verify(); |
||||
|
})); |
||||
|
|
||||
|
it('should make get request to get settings', |
||||
|
inject([UIService, HttpTestingController], (uiService: UIService, httpMock: HttpTestingController) => { |
||||
|
|
||||
|
let settings1: UISettingsDto | null = null; |
||||
|
let settings2: UISettingsDto | null = null; |
||||
|
|
||||
|
uiService.getSettings().subscribe(result => { |
||||
|
settings1 = result; |
||||
|
}); |
||||
|
|
||||
|
const response: UISettingsDto = { regexSuggestions: [] }; |
||||
|
|
||||
|
const req = httpMock.expectOne('http://service/p/api/ui/settings'); |
||||
|
|
||||
|
expect(req.request.method).toEqual('GET'); |
||||
|
expect(req.request.headers.get('If-Match')).toBeNull(); |
||||
|
|
||||
|
req.flush(response); |
||||
|
|
||||
|
uiService.getSettings().subscribe(result => { |
||||
|
settings2 = result; |
||||
|
}); |
||||
|
|
||||
|
expect(settings1).toEqual(response); |
||||
|
expect(settings2).toEqual(response); |
||||
|
})); |
||||
|
|
||||
|
it('should return default settings when error occurs', |
||||
|
inject([UIService, HttpTestingController], (uiService: UIService, httpMock: HttpTestingController) => { |
||||
|
|
||||
|
let settings: UISettingsDto | null = null; |
||||
|
|
||||
|
uiService.getSettings().subscribe(result => { |
||||
|
settings = result; |
||||
|
}); |
||||
|
|
||||
|
const req = httpMock.expectOne('http://service/p/api/ui/settings'); |
||||
|
|
||||
|
expect(req.request.method).toEqual('GET'); |
||||
|
expect(req.request.headers.get('If-Match')).toBeNull(); |
||||
|
|
||||
|
req.error(new ErrorEvent('500')); |
||||
|
|
||||
|
expect(settings.regexSuggestions).toEqual([]); |
||||
|
})); |
||||
|
}); |
||||
@ -0,0 +1,49 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import { HttpClient } from '@angular/common/http'; |
||||
|
import { Injectable } from '@angular/core'; |
||||
|
import { Observable } from 'rxjs'; |
||||
|
|
||||
|
import 'framework/angular/http-extensions'; |
||||
|
|
||||
|
import { ApiUrlConfig } from 'framework'; |
||||
|
|
||||
|
export interface UISettingsDto { |
||||
|
regexSuggestions: UIRegexSuggestionDto[]; |
||||
|
} |
||||
|
|
||||
|
export interface UIRegexSuggestionDto { |
||||
|
name: string; pattern: string; |
||||
|
} |
||||
|
|
||||
|
@Injectable() |
||||
|
export class UIService { |
||||
|
private settings: UISettingsDto; |
||||
|
|
||||
|
constructor( |
||||
|
private readonly http: HttpClient, |
||||
|
private readonly apiUrl: ApiUrlConfig |
||||
|
) { |
||||
|
} |
||||
|
|
||||
|
public getSettings(): Observable<UISettingsDto> { |
||||
|
if (this.settings) { |
||||
|
return Observable.of(this.settings); |
||||
|
} else { |
||||
|
const url = this.apiUrl.buildUrl(`api/ui/settings`); |
||||
|
|
||||
|
return this.http.get<UISettingsDto>(url) |
||||
|
.catch(error => { |
||||
|
return Observable.of({ regexSuggestions: [] }); |
||||
|
}) |
||||
|
.do(settings => { |
||||
|
this.settings = settings; |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue