Skip to content

Commit 5792769

Browse files
authored
Merge pull request #31 from leungkimming/michael3
Add Telerik Documents and fix Antiforgrey
2 parents 3456027 + bf2782c commit 5792769

29 files changed

+1257
-40
lines changed

API/Controllers/DocumentProcessingController.cs

Lines changed: 595 additions & 0 deletions
Large diffs are not rendered by default.

API/Controllers/LoginController.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,36 @@ public LoginController(IConfiguration config,
2828
public async Task<IActionResult> Get() {
2929
JwtSecurityToken jwtToken;
3030
string token;
31+
AuthResult authResult;
3132

32-
if (HttpContext.User.Identity.Name == "" || HttpContext.User.Identity.Name == null) {
33+
if (HttpContext.User.Identity!.Name == "" || HttpContext.User.Identity.Name == null) {
3334
throw new InvalidUserException();
3435
}
3536

3637
if (jwtUtil.ValidateToken(HttpContext.Request, out jwtToken, out token)) {
37-
return Ok(new AuthResult() {
38+
if (HttpContext.User.Identity.Name == jwtToken.Claims
39+
.Where(c => c.Type == ClaimTypes.Name)
40+
.Select(c => c.Value).SingleOrDefault()) {
41+
Array.ForEach(jwtToken.Claims.Where(c => c.Type == ClaimTypes.Role)
42+
.ToArray(), c => ((ClaimsIdentity)HttpContext.User.Identity).AddClaim(c));
43+
}
44+
authResult = new AuthResult() {
3845
Token = token,
3946
Success = true,
4047
RefreshToken = ""
41-
});
48+
};
49+
} else {
50+
List<Claim>? claims = _service.GetUserClaims(HttpContext.User.Identity.Name);
51+
52+
ClaimsIdentity claimsIdentity = (ClaimsIdentity)HttpContext.User.Identity;
53+
Array.ForEach(claims.Where(c => c.Type == ClaimTypes.Role).ToArray(),
54+
c => claimsIdentity.AddClaim(c));
55+
authResult = jwtUtil.GenerateJwtToken(HttpContext.User.Identity.Name, claims);
4256
}
4357

44-
List<Claim>? claims = _service.GetUserClaims(HttpContext.User.Identity.Name);
45-
46-
ClaimsIdentity claimsIdentity = (ClaimsIdentity)HttpContext.User.Identity;
47-
Array.ForEach(claims.Where(c => c.Type == ClaimTypes.Role).ToArray(),
48-
c => claimsIdentity.AddClaim(c));
49-
5058
AntiforgeryTokenSet? tokens = antiforgery.GetAndStoreTokens(HttpContext);
51-
HttpContext.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken, new CookieOptions() { HttpOnly = false });
59+
HttpContext.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken!, new CookieOptions() { HttpOnly = false });
5260

53-
return Ok(jwtUtil.GenerateJwtToken(HttpContext.User.Identity.Name, claims));
61+
return Ok(authResult);
5462
}
5563
}

API/Controllers/SystemParameters/SystemParametersController.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace API {
88
[ApiController]
99
[Route("systemparameters")]
10+
[AutoValidateAntiforgeryToken]
1011
public class SystemParametersController : ControllerBase {
1112
private readonly SystemParametersService _service;
1213
private readonly ILogger<UserController> _logger;
@@ -27,7 +28,6 @@ public async Task<IActionResult> SearchAll([FromBody]SystemParametersSearchReque
2728
[HttpPost]
2829
[Route("addsystemparameter")]
2930
[AccessCodeAuthorize("SP02")]
30-
[ValidateAntiForgeryToken]
3131
public async Task<IActionResult> AddSystemParameter([FromBody]AddSystemParameterRequest request) {
3232
AddDataResponse response;
3333
request.Refresh(HttpContext.User.Identity.Name,DateTime.Now);
@@ -37,7 +37,6 @@ public async Task<IActionResult> AddSystemParameter([FromBody]AddSystemParameter
3737
[HttpPost]
3838
[Route("editsystemparameter")]
3939
[AccessCodeAuthorize("SP03")]
40-
[ValidateAntiForgeryToken]
4140
public async Task<IActionResult> EditSystemParameter([FromBody] EditSystemParameterRequest request) {
4241
EditDataResponse response;
4342
request.Refresh(HttpContext.User.Identity.Name, DateTime.Now);
@@ -47,7 +46,6 @@ public async Task<IActionResult> EditSystemParameter([FromBody] EditSystemParame
4746
[HttpGet]
4847
[Route("deletesystemparameter")]
4948
[AccessCodeAuthorize("SP04")]
50-
[ValidateAntiForgeryToken]
5149
public async Task<IActionResult> DeleteSystemParameter([FromQuery] DeleteSystemParameterRequest request) {
5250
EditDataResponse response;
5351
response = await _service.DeleteSystemParameterAsync(request);

API/Controllers/UserController.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace API {
99
[ApiController]
1010
[Route("users")]
11+
[AutoValidateAntiforgeryToken]
1112
public class UserController : ControllerBase {
1213
private readonly UserService _service;
1314
private readonly ILogger<UserController> _logger;
@@ -40,7 +41,6 @@ public async Task<IActionResult> Get([FromBody] GetUserRequest request) {
4041

4142
[HttpPost(Name = "AddNewUser")]
4243
[AccessCodeAuthorize("AB01")]
43-
[ValidateAntiForgeryToken]
4444
public async Task<IActionResult> Add([FromBody] AddUserRequest request) {
4545
AddUserResponse response;
4646
request.Refresh(HttpContext.User.Identity.Name, DateTime.Now);
@@ -50,7 +50,6 @@ public async Task<IActionResult> Add([FromBody] AddUserRequest request) {
5050

5151
[HttpPost("Addpayslip")]
5252
[AccessCodeAuthorize("AC01")]
53-
[ValidateAntiForgeryToken]
5453
public async Task<IActionResult> AddPayslip([FromBody] AddPayslipRequest request) {
5554
AddPayslipResponse _response;
5655
request.Refresh(HttpContext.User.Identity.Name, DateTime.Now);

API/P1.API.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,17 @@
3939
</PackageReference>
4040
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
4141
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
42+
<PackageReference Include="Telerik.Documents.Core" Version="2022.1.217" />
43+
<PackageReference Include="Telerik.Documents.Fixed" Version="2022.1.217" />
44+
<PackageReference Include="Telerik.Documents.Flow" Version="2022.1.217" />
45+
<PackageReference Include="Telerik.Documents.Spreadsheet" Version="2022.1.217" />
4246
<PackageReference Include="Telerik.Reporting.Services.AspNetCore" Version="16.0.22.225" />
4347
</ItemGroup>
4448

4549
<ItemGroup>
4650
<ProjectReference Include="..\Client\Client.csproj" />
4751
<ProjectReference Include="..\Data\P3.Data.csproj" />
52+
<ProjectReference Include="..\DocumentProcessing\P8.DocumentProcessing.csproj" />
4853
<ProjectReference Include="..\Migrator\P7.Migrator.csproj" />
4954
<ProjectReference Include="..\Service\P2.Service.csproj" />
5055
</ItemGroup>

API/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@
9191
} else {
9292
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
9393
.AddNegotiate();
94-
builder.Services.AddMvc(options => {
95-
options.Filters.Add<ValidateAntiForgeryTokenAttribute>();
96-
});
9794
}
95+
builder.Services.AddMvc(options => {
96+
options.Filters.Add<ValidateAntiForgeryTokenAttribute>();
97+
});
9898
builder.Services.AddAuthorization(options => {
9999
options.FallbackPolicy = options.DefaultPolicy;
100100
});

API/RegisterModule.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Service;
55
using MediatR;
66
using Microsoft.AspNetCore.Authorization;
7+
using DocumentProcessing;
78

89
namespace API {
910
public class RegisterModule : Module {
@@ -50,6 +51,10 @@ protected override void Load(ContainerBuilder builder) {
5051
builder.RegisterType<AuthorizationResultTransformer>().As<IAuthorizationMiddlewareResultHandler>()
5152
.SingleInstance();
5253
//builder.RegisterType<JWTUtil>().As<IJWTUtil>().SingleInstance();
54+
builder.RegisterType<PdfProcessing>().As<IPdfProcessing>();
55+
builder.RegisterType<WordProcessing>().As<IWordProcessing>();
56+
builder.RegisterType<SpreadProcessing>().As<ISpreadProcessing>();
57+
builder.RegisterType<ZipProcessing>().As<IZipProcessing>();
5358
}
5459
}
5560

Client/Client.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<PackageReference Include="Blazored.LocalStorage" Version="4.2.0" />
2222
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.3" />
2323
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.3" PrivateAssets="all" />
24+
<PackageReference Include="Microsoft.Authentication.WebAssembly.Msal" Version="6.0.5" />
2425
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
2526
<PackageReference Include="Telerik.ReportViewer.Blazor" Version="16.0.22.225" />
2627
<PackageReference Include="Telerik.UI.for.Blazor" Version="3.1.0" />
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
@page "/documentprocessing"
2+
@using System.Diagnostics
3+
@inject HttpUtil _httpUtil
4+
@inject IJSRuntime JsRuntime
5+
6+
<h3>Document Processing</h3>
7+
8+
<TelerikButton OnClick="@ExportPDF" Icon="file-pdf" Title="Export PDF"></TelerikButton>
9+
<TelerikButton OnClick="@MergePDF" Icon="file-ascx" Title="Merge PDF"></TelerikButton>
10+
<TelerikButton OnClick="@ExportDocx" Icon="file-word" Title="Export Docx"></TelerikButton>
11+
<TelerikButton OnClick="@ExportXlsx" Icon="file-excel" Title="Export Xlsx"></TelerikButton>
12+
<div class="k-form k-form-horizontal div-container">
13+
<label>Zip Files Demo</label>
14+
<div class="k-form-field-wrap">
15+
<TelerikUpload SaveUrl="@SaveUrl"
16+
AllowedExtensions="@AllowedExtensions"
17+
AutoUpload="false"
18+
MinFileSize="@MinFileSize"
19+
MaxFileSize="@MaxFileSize"
20+
Multiple="true"
21+
OnUpload="@UploadHandler">
22+
</TelerikUpload>
23+
<div class="k-form-hint">Accepted files: <strong>DOCX, PDF, JPG and PNG</strong></div>
24+
<div calss="div-container">
25+
<TelerikTextBox Width="200px" Value="@Password" Title="Password" ValueChanged="@PasswordChanged" PlaceHolder="Password for protect."></TelerikTextBox>
26+
<TelerikButton OnClick="@ZipFiles" Icon="file-zip" Title="Zip all uploaded files."></TelerikButton>
27+
</div>
28+
29+
</div>
30+
</div>
31+
<div class="loader-container">
32+
<TelerikLoader Class="loader-indicator" Type="@LoaderType.InfiniteSpinner" Size="@(ThemeConstants.Loader.Size.Large)" Visible="@IsLoad"></TelerikLoader>
33+
</div>
34+
35+
@code {
36+
public bool IsLoad { get; set; } = false;
37+
public string Password { get; set; }
38+
public async Task PasswordChanged(string value) {
39+
Password = value;
40+
}
41+
public async Task ExportPDF() {
42+
IsLoad = true;
43+
var response = await _httpUtil.GetAsync("documentprocessing/exporttopdf");
44+
var fileData = await response.Content.ReadAsByteArrayAsync();
45+
Save(fileData, "application/pdf", "Sample Document.pdf");
46+
}
47+
48+
public async Task MergePDF() {
49+
IsLoad = true;
50+
var response = await _httpUtil.GetAsync("documentprocessing/mergepdf");
51+
var fileData = await response.Content.ReadAsByteArrayAsync();
52+
Save(fileData, "application/pdf", "Merge Sample Document.pdf");
53+
}
54+
public async Task ExportDocx() {
55+
IsLoad = true;
56+
var response = await _httpUtil.GetAsync("documentprocessing/exporttodocx");
57+
var fileData = await response.Content.ReadAsByteArrayAsync();
58+
Save(fileData, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Sample Document.docx");
59+
}
60+
public async Task ExportXlsx() {
61+
IsLoad = true;
62+
var response = await _httpUtil.GetAsync("documentprocessing/exporttoxlsx");
63+
var fileData = await response.Content.ReadAsByteArrayAsync();
64+
Save(fileData, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Sample Document.xlsx");
65+
66+
}
67+
public string SaveUrl => "documentprocessing/uploadfiles";
68+
public List<string> AllowedExtensions { get; set; } = new List<string>() { ".docx", ".pdf", ".jpg", ".png" };
69+
public int MinFileSize { get; set; } = 1024;
70+
public int MaxFileSize { get; set; } = 4 * 1024 * 1024;
71+
public async Task UploadHandler(UploadEventArgs e) {
72+
e.RequestHeaders = new Dictionary<string, object>();
73+
await _httpUtil.RefreshToken(e.RequestHeaders);
74+
}
75+
public async Task ZipFiles() {
76+
IsLoad = true;
77+
var response = await _httpUtil.GetAsync($"documentprocessing/zipfiles?password={Password}");
78+
var fileData = await response.Content.ReadAsByteArrayAsync();
79+
Save(fileData, "application/x-zip-compressed", "ZipDemo.zip");
80+
}
81+
public void Save(byte[] byteData, string mimeType, string fileName) {
82+
if (byteData == null) {
83+
return;
84+
}
85+
var fileBase64=Convert.ToBase64String(byteData);
86+
JsRuntime.InvokeVoidAsync("saveFile", fileBase64, mimeType, fileName);
87+
IsLoad = false;
88+
}
89+
}

Client/Shared/NavMenu/DrawerMenu.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static class DrawerMenu {
1818
new DrawerItem{ Text = "Search User", Icon = "dollar", Url="/dotnet6EAA/searchuser", Group = "app"},
1919
new DrawerItem{ Text = "Report", Icon = "dollar", Url="/dotnet6EAA/Report", Group = "app"},
2020
new DrawerItem{ Text = "System Parameters", Icon = "tell-a-friend", Url="/dotnet6EAA/systemparameters/searchdatas", Group = "app"},
21+
new DrawerItem{ Text = "Document Processing", Icon = "tell-a-friend", Url="/dotnet6EAA/documentprocessing", Group = "app"},
2122
new DrawerItem{ Text = "Swagger UI", Icon="gear", Url="/dotnet6EAA/swagger/index.html",Group="settings",Target= MenuTarget.Blank.Code},
2223
};
2324
}

0 commit comments

Comments
 (0)