??
This commit is contained in:
@@ -1,72 +1,72 @@
|
||||
using Google.Apis.Auth;
|
||||
using Google.Apis.Http;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Identity.Client.Platforms.Features.DesktopOs.Kerberos;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Configuration;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using WebAPI.Models;
|
||||
|
||||
namespace WebAPI.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
// [Authorize]
|
||||
public class AuthController : Controller
|
||||
{
|
||||
private readonly AppDbContext db;
|
||||
private readonly IConfiguration configuration;
|
||||
public AuthController(
|
||||
AppDbContext _db, IConfiguration _configuration)
|
||||
{ db = _db; configuration = _configuration; }
|
||||
|
||||
[HttpPost]
|
||||
[Route("apiToken")]
|
||||
public async Task<IActionResult> apiToken([FromBody] string credential)
|
||||
{
|
||||
var settings = new GoogleJsonWebSignature.ValidationSettings()
|
||||
{
|
||||
Audience = new List<string> { configuration.GetValue<string>("GoogleClientId") }
|
||||
};
|
||||
var payload = await GoogleJsonWebSignature.ValidateAsync(credential, settings);
|
||||
var user = db.Users.Where(x => x.Email == payload.Email).FirstOrDefault();
|
||||
if (user != null)
|
||||
{
|
||||
return Ok(JWTGenerator(user));
|
||||
}
|
||||
else
|
||||
{
|
||||
return Unauthorized();
|
||||
}
|
||||
}
|
||||
|
||||
private dynamic JWTGenerator(User user)
|
||||
{
|
||||
var key = Encoding.ASCII.GetBytes(configuration.GetValue<string>("Secret"));
|
||||
var expirationTime = DateTime.UtcNow.AddMinutes(5);
|
||||
var tokenDescriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(new[]
|
||||
{
|
||||
new Claim("Id", Guid.NewGuid().ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Jti,
|
||||
Guid.NewGuid().ToString())
|
||||
}),
|
||||
Expires = expirationTime,
|
||||
SigningCredentials = new SigningCredentials
|
||||
(new SymmetricSecurityKey(key),
|
||||
SecurityAlgorithms.HmacSha512Signature)
|
||||
};
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
var jwtToken = tokenHandler.WriteToken(token);
|
||||
var stringToken = tokenHandler.WriteToken(token);
|
||||
return new { token = stringToken, id = user.Id, expirationTime };
|
||||
}
|
||||
}
|
||||
using Google.Apis.Auth;
|
||||
using Google.Apis.Http;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Identity.Client.Platforms.Features.DesktopOs.Kerberos;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Configuration;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using WebAPI.Models;
|
||||
|
||||
namespace WebAPI.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
// [Authorize]
|
||||
public class AuthController : Controller
|
||||
{
|
||||
private readonly AppDbContext db;
|
||||
private readonly IConfiguration configuration;
|
||||
public AuthController(
|
||||
AppDbContext _db, IConfiguration _configuration)
|
||||
{ db = _db; configuration = _configuration; }
|
||||
|
||||
[HttpPost]
|
||||
[Route("apiToken")]
|
||||
public async Task<IActionResult> apiToken([FromBody] string credential)
|
||||
{
|
||||
var settings = new GoogleJsonWebSignature.ValidationSettings()
|
||||
{
|
||||
Audience = new List<string> { configuration.GetValue<string>("GoogleClientId") }
|
||||
};
|
||||
var payload = await GoogleJsonWebSignature.ValidateAsync(credential, settings);
|
||||
var user = db.Users.Where(x => x.Email == payload.Email).FirstOrDefault();
|
||||
if (user != null)
|
||||
{
|
||||
return Ok(JWTGenerator(user));
|
||||
}
|
||||
else
|
||||
{
|
||||
return Unauthorized();
|
||||
}
|
||||
}
|
||||
|
||||
private dynamic JWTGenerator(User user)
|
||||
{
|
||||
var key = Encoding.ASCII.GetBytes(configuration.GetValue<string>("Secret"));
|
||||
var expirationTime = DateTime.UtcNow.AddMinutes(5);
|
||||
var tokenDescriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(new[]
|
||||
{
|
||||
new Claim("Id", Guid.NewGuid().ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Jti,
|
||||
Guid.NewGuid().ToString())
|
||||
}),
|
||||
Expires = expirationTime,
|
||||
SigningCredentials = new SigningCredentials
|
||||
(new SymmetricSecurityKey(key),
|
||||
SecurityAlgorithms.HmacSha512Signature)
|
||||
};
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
var jwtToken = tokenHandler.WriteToken(token);
|
||||
var stringToken = tokenHandler.WriteToken(token);
|
||||
return new { token = stringToken, id = user.Id, expirationTime };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,182 +1,182 @@
|
||||
using Google.Apis.Auth;
|
||||
using Google.Apis.Http;
|
||||
using Google.Apis.Sheets.v4;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Identity.Client.Platforms.Features.DesktopOs.Kerberos;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using WebAPI.dataParsers;
|
||||
using WebAPI.Exports;
|
||||
using WebAPI.Models;
|
||||
|
||||
namespace WebAPI.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
[Authorize]
|
||||
public class LayersController : Controller
|
||||
{
|
||||
private readonly AppDbContext db;
|
||||
private SpreadsheetsResource.ValuesResource googleSheetValues;
|
||||
private GoogleDriveHelper googleDriveHelper;
|
||||
private readonly IConfiguration configuration;
|
||||
public LayersController(
|
||||
AppDbContext _db,
|
||||
GoogleSheetsHelper _googleSheetsHelper,
|
||||
GoogleDriveHelper _googleDriveHelper,
|
||||
IConfiguration _configuration)
|
||||
{
|
||||
db = _db;
|
||||
googleSheetValues = _googleSheetsHelper.Service.Spreadsheets.Values;
|
||||
googleDriveHelper = _googleDriveHelper;
|
||||
configuration = _configuration;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult GetAll()
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(db.Layers.Where(x => !x.IsDeleted).ToList());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
[HttpPost]
|
||||
public IActionResult Save(Layer input)
|
||||
{
|
||||
try
|
||||
{
|
||||
Request.Headers.TryGetValue("userId", out var value);
|
||||
Guid currentUserId = new Guid(value!);
|
||||
return Ok(AddLayer(input, currentUserId).Id);
|
||||
} catch (Exception e)
|
||||
{
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("{id}")]
|
||||
public IActionResult Get(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(db.Layers
|
||||
.Include(x => x.CreatedBy)
|
||||
.Include(x => x.Records)
|
||||
.Where(x => x.Id == id && !x.IsDeleted).First());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("parseGoogleSheet/{sheetId}")]
|
||||
public IActionResult ParseGoogleSheet(string sheetId)
|
||||
{
|
||||
|
||||
string sheetName = "KOSZTY";
|
||||
|
||||
Layer layer = new Layer();
|
||||
layer.Source = "GoogleSheet";
|
||||
layer.Number = db.Layers.Count() + 1;
|
||||
var parser = new googleSheetParser(googleSheetValues);
|
||||
dynamic parsedSheet = parser.parse(sheetId);
|
||||
layer.Records = parsedSheet.records;
|
||||
layer.Name = $"W{layer.Number}-I-{sheetName}-{parsedSheet.date}-{DateTime.Now.ToString("yyyyMMddHHmm")}";
|
||||
|
||||
return Ok(layer);
|
||||
}
|
||||
[HttpPost]
|
||||
[DisableRequestSizeLimit]
|
||||
[Route("parseFile")]
|
||||
public IActionResult ParseFile()
|
||||
{
|
||||
var parser = new csvParser();
|
||||
return Ok(parser.parse(Request.Form.Files[0]));
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("exportToGoogleSheet/{id}")]
|
||||
public IActionResult ExportToGoogleSheet(Guid id)
|
||||
{
|
||||
Layer layer = db.Layers
|
||||
.Include(x => x.Records)
|
||||
.Where(x => x.Id == id && !x.IsDeleted).First();
|
||||
|
||||
var export = new googleSheetExport(googleDriveHelper, googleSheetValues);
|
||||
export.export(layer);
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("autoImport/{apiKey}")]
|
||||
[AllowAnonymous]
|
||||
public IActionResult autoImport(string apiKey)
|
||||
{
|
||||
if (Request.Host.Value != "localhost:5400" || apiKey != configuration["apiKey"])
|
||||
{
|
||||
return Unauthorized();
|
||||
}
|
||||
string sheetId = "1G_Hu8DTP-PSPNXTaVYhc_ppnTQi6HWoA4oXSSdUmM9E";
|
||||
string sheetName = "KOSZTY";
|
||||
|
||||
Layer layer = new Layer();
|
||||
layer.Source = "GoogleSheet";
|
||||
layer.Number = db.Layers.Count() + 1;
|
||||
var parser = new googleSheetParser(googleSheetValues);
|
||||
dynamic parsedSheet = parser.parse(sheetId);
|
||||
layer.Records = parsedSheet.records;
|
||||
layer.Name = $"W{layer.Number}-I-{sheetName}-{parsedSheet.date}-{DateTime.Now.ToString("yyyyMMddHHmm")}";
|
||||
AddLayer(layer, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
|
||||
return Ok("OK");
|
||||
}
|
||||
|
||||
//
|
||||
private Layer AddLayer(Layer input, Guid currentUserId)
|
||||
{
|
||||
input.Number = db.Layers.Count() + 1;
|
||||
input.CreatedById = currentUserId;
|
||||
input.ModifiedById = currentUserId;
|
||||
input.CreatedAt = DateTime.UtcNow;
|
||||
input.ModifiedAt = DateTime.UtcNow;
|
||||
|
||||
db.Layers.Add(input);
|
||||
SaveRecords(input.Id, input.Records!, currentUserId);
|
||||
db.SaveChanges();
|
||||
return input;
|
||||
}
|
||||
|
||||
private void SaveRecords(Guid id, ICollection<Models.Record> records, Guid currentUserId)
|
||||
{
|
||||
try
|
||||
{
|
||||
List<Guid> ids = new List<Guid>();
|
||||
foreach (Record record in records)
|
||||
{
|
||||
record.CreatedById = currentUserId;
|
||||
record.CreatedAt = DateTime.UtcNow;
|
||||
record.ModifiedById = currentUserId;
|
||||
record.ModifiedAt = DateTime.UtcNow;
|
||||
record.LayerId= id;
|
||||
|
||||
db.Records.Add(record);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
using Google.Apis.Auth;
|
||||
using Google.Apis.Http;
|
||||
using Google.Apis.Sheets.v4;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Identity.Client.Platforms.Features.DesktopOs.Kerberos;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using WebAPI.dataParsers;
|
||||
using WebAPI.Exports;
|
||||
using WebAPI.Models;
|
||||
|
||||
namespace WebAPI.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
[Authorize]
|
||||
public class LayersController : Controller
|
||||
{
|
||||
private readonly AppDbContext db;
|
||||
private SpreadsheetsResource.ValuesResource googleSheetValues;
|
||||
private GoogleDriveHelper googleDriveHelper;
|
||||
private readonly IConfiguration configuration;
|
||||
public LayersController(
|
||||
AppDbContext _db,
|
||||
GoogleSheetsHelper _googleSheetsHelper,
|
||||
GoogleDriveHelper _googleDriveHelper,
|
||||
IConfiguration _configuration)
|
||||
{
|
||||
db = _db;
|
||||
googleSheetValues = _googleSheetsHelper.Service.Spreadsheets.Values;
|
||||
googleDriveHelper = _googleDriveHelper;
|
||||
configuration = _configuration;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult GetAll()
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(db.Layers.Where(x => !x.IsDeleted).ToList());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
[HttpPost]
|
||||
public IActionResult Save(Layer input)
|
||||
{
|
||||
try
|
||||
{
|
||||
Request.Headers.TryGetValue("userId", out var value);
|
||||
Guid currentUserId = new Guid(value!);
|
||||
return Ok(AddLayer(input, currentUserId).Id);
|
||||
} catch (Exception e)
|
||||
{
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("{id}")]
|
||||
public IActionResult Get(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(db.Layers
|
||||
.Include(x => x.CreatedBy)
|
||||
.Include(x => x.Records)
|
||||
.Where(x => x.Id == id && !x.IsDeleted).First());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("parseGoogleSheet/{sheetId}")]
|
||||
public IActionResult ParseGoogleSheet(string sheetId)
|
||||
{
|
||||
|
||||
string sheetName = "KOSZTY";
|
||||
|
||||
Layer layer = new Layer();
|
||||
layer.Source = "GoogleSheet";
|
||||
layer.Number = db.Layers.Count() + 1;
|
||||
var parser = new googleSheetParser(googleSheetValues);
|
||||
dynamic parsedSheet = parser.parse(sheetId);
|
||||
layer.Records = parsedSheet.records;
|
||||
layer.Name = $"W{layer.Number}-I-{sheetName}-{parsedSheet.date}-{DateTime.Now.ToString("yyyyMMddHHmm")}";
|
||||
|
||||
return Ok(layer);
|
||||
}
|
||||
[HttpPost]
|
||||
[DisableRequestSizeLimit]
|
||||
[Route("parseFile")]
|
||||
public IActionResult ParseFile()
|
||||
{
|
||||
var parser = new csvParser();
|
||||
return Ok(parser.parse(Request.Form.Files[0]));
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("exportToGoogleSheet/{id}")]
|
||||
public IActionResult ExportToGoogleSheet(Guid id)
|
||||
{
|
||||
Layer layer = db.Layers
|
||||
.Include(x => x.Records)
|
||||
.Where(x => x.Id == id && !x.IsDeleted).First();
|
||||
|
||||
var export = new googleSheetExport(googleDriveHelper, googleSheetValues);
|
||||
export.export(layer);
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("autoImport/{apiKey}")]
|
||||
[AllowAnonymous]
|
||||
public IActionResult autoImport(string apiKey)
|
||||
{
|
||||
if (Request.Host.Value != "localhost:5400" || apiKey != configuration["apiKey"])
|
||||
{
|
||||
return Unauthorized();
|
||||
}
|
||||
string sheetId = "1G_Hu8DTP-PSPNXTaVYhc_ppnTQi6HWoA4oXSSdUmM9E";
|
||||
string sheetName = "KOSZTY";
|
||||
|
||||
Layer layer = new Layer();
|
||||
layer.Source = "GoogleSheet";
|
||||
layer.Number = db.Layers.Count() + 1;
|
||||
var parser = new googleSheetParser(googleSheetValues);
|
||||
dynamic parsedSheet = parser.parse(sheetId);
|
||||
layer.Records = parsedSheet.records;
|
||||
layer.Name = $"W{layer.Number}-I-{sheetName}-{parsedSheet.date}-{DateTime.Now.ToString("yyyyMMddHHmm")}";
|
||||
AddLayer(layer, Guid.Parse("F392209E-123E-4651-A5A4-0B1D6CF9FF9D"));
|
||||
|
||||
return Ok("OK");
|
||||
}
|
||||
|
||||
//
|
||||
private Layer AddLayer(Layer input, Guid currentUserId)
|
||||
{
|
||||
input.Number = db.Layers.Count() + 1;
|
||||
input.CreatedById = currentUserId;
|
||||
input.ModifiedById = currentUserId;
|
||||
input.CreatedAt = DateTime.UtcNow;
|
||||
input.ModifiedAt = DateTime.UtcNow;
|
||||
|
||||
db.Layers.Add(input);
|
||||
SaveRecords(input.Id, input.Records!, currentUserId);
|
||||
db.SaveChanges();
|
||||
return input;
|
||||
}
|
||||
|
||||
private void SaveRecords(Guid id, ICollection<Models.Record> records, Guid currentUserId)
|
||||
{
|
||||
try
|
||||
{
|
||||
List<Guid> ids = new List<Guid>();
|
||||
foreach (Record record in records)
|
||||
{
|
||||
record.CreatedById = currentUserId;
|
||||
record.CreatedAt = DateTime.UtcNow;
|
||||
record.ModifiedById = currentUserId;
|
||||
record.ModifiedAt = DateTime.UtcNow;
|
||||
record.LayerId= id;
|
||||
|
||||
db.Records.Add(record);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,36 @@
|
||||
using Google.Apis.Auth;
|
||||
using Google.Apis.Http;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Identity.Client.Platforms.Features.DesktopOs.Kerberos;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Configuration;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using WebAPI.Models;
|
||||
|
||||
namespace WebAPI.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
[Authorize]
|
||||
public class PingController : Controller
|
||||
{
|
||||
private readonly IConfiguration configuration;
|
||||
public PingController(
|
||||
IConfiguration _configuration)
|
||||
{
|
||||
configuration = _configuration;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("Ping")]
|
||||
[AllowAnonymous]
|
||||
public IActionResult Ping()
|
||||
{
|
||||
return Ok(configuration["PONG"]);
|
||||
}
|
||||
}
|
||||
using Google.Apis.Auth;
|
||||
using Google.Apis.Http;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Identity.Client.Platforms.Features.DesktopOs.Kerberos;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Configuration;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using WebAPI.Models;
|
||||
|
||||
namespace WebAPI.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
[Authorize]
|
||||
public class PingController : Controller
|
||||
{
|
||||
private readonly IConfiguration configuration;
|
||||
public PingController(
|
||||
IConfiguration _configuration)
|
||||
{
|
||||
configuration = _configuration;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("Ping")]
|
||||
[AllowAnonymous]
|
||||
public IActionResult Ping()
|
||||
{
|
||||
return Ok(configuration["PONG"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user