update TWA Management v0.0.1
This commit is contained in:
55
TWASys-App/Models/Login/LoginCheckFilter.cs
Normal file
55
TWASys-App/Models/Login/LoginCheckFilter.cs
Normal file
@ -0,0 +1,55 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using System.Text.Encodings.Web;
|
||||
|
||||
namespace TWASys_App.Models.Login
|
||||
{
|
||||
public sealed class LoginCheckFilter : IAsyncActionFilter, IOrderedFilter
|
||||
{
|
||||
public int Order => int.MinValue;
|
||||
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
||||
{
|
||||
|
||||
var http = context.HttpContext;
|
||||
|
||||
// 1) Bỏ qua nếu endpoint cho phép anonymous
|
||||
var endpoint = http.GetEndpoint();
|
||||
if (endpoint?.Metadata.GetMetadata<IAllowAnonymous>() != null ||
|
||||
context.Filters.OfType<IAllowAnonymousFilter>().Any())
|
||||
{
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
// 2) Nếu chưa đăng nhập → redirect (web) hoặc 401 (API/Ajax)
|
||||
var uid = http.Session.GetString("userID");
|
||||
if (string.IsNullOrEmpty(uid))
|
||||
{
|
||||
// API/Ajax → trả 401 JSON; Web → redirect login
|
||||
var accepts = http.Request.Headers.Accept.ToString();
|
||||
var isApi = accepts.Contains("application/json", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
if (isApi)
|
||||
{
|
||||
context.Result = new JsonResult(new MessageHeader
|
||||
{
|
||||
ID = 1000,
|
||||
Message = "Required Login",
|
||||
Status = 0
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
var returnUrl = UrlEncoder.Default.Encode(http.Request.Path + http.Request.QueryString);
|
||||
context.Result = new RedirectResult($"/login?returnUrl={returnUrl}");
|
||||
}
|
||||
return;
|
||||
|
||||
}
|
||||
await next();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,6 @@
|
||||
namespace TWASys_App.Models
|
||||
using MySqlConnector;
|
||||
|
||||
namespace TWASys_App.Models
|
||||
{
|
||||
public abstract class ModelBase: IPaging
|
||||
{
|
||||
@ -12,6 +14,6 @@
|
||||
public int PageNumber { get; set; }
|
||||
public int MaxRow { get; set; }
|
||||
|
||||
|
||||
protected MySqlConnection _connection = null!;
|
||||
}
|
||||
}
|
||||
|
||||
12
TWASys-App/Models/Shared.cs
Normal file
12
TWASys-App/Models/Shared.cs
Normal file
@ -0,0 +1,12 @@
|
||||
namespace TWASys_App.Models
|
||||
{
|
||||
public class Shared
|
||||
{
|
||||
private static List<string> _list = new List<string> { "80633.jpg", "806372.jpg", "806459.jpg" };
|
||||
public static string RandomImages()
|
||||
{
|
||||
var r = new Random();
|
||||
return _list[r.Next(0, _list.Count - 1)];
|
||||
}
|
||||
}
|
||||
}
|
||||
159
TWASys-App/Models/StorageModel.cs
Normal file
159
TWASys-App/Models/StorageModel.cs
Normal file
@ -0,0 +1,159 @@
|
||||
|
||||
using Microsoft.AspNetCore.Razor.Language.Intermediate;
|
||||
using Microsoft.VisualBasic;
|
||||
using MySqlConnector;
|
||||
using System.Data.Common;
|
||||
using System.Xml.Linq;
|
||||
using TWASys_App.Dapper.AExtentions;
|
||||
using TWASys_App.DBModels;
|
||||
|
||||
namespace TWASys_App.Models
|
||||
{
|
||||
public class StorageModel : ModelBase
|
||||
{
|
||||
public long StorageTypeID { set; get; }
|
||||
|
||||
public string ControllerName { set; get; } = "";
|
||||
|
||||
public string StorageName { set; get; } = "";
|
||||
|
||||
public ICollection<ValidationDomain> ValidationDomains { set; get; } = new List<ValidationDomain>();
|
||||
|
||||
public string IpPublic { set; get; } = "";
|
||||
|
||||
public string IpPrivatev4 { set; get; } = "";
|
||||
|
||||
public string IpPrivatev6 { set; get; } = "";
|
||||
|
||||
public string OsName { set; get; } = "";
|
||||
|
||||
public string OsPlatform { set; get; } = "";
|
||||
|
||||
public string OsVersion { set; get; } = "";
|
||||
|
||||
public string OsKernel { set; get; } = "";
|
||||
|
||||
public string OsArch { set; get; } = "";
|
||||
|
||||
public string BiosSN { set; get; } = "";
|
||||
public string BiosVendor { set; get; } = "";
|
||||
|
||||
public string BiosUUID { set; get; } = "";
|
||||
|
||||
public int SocketN { set; get; }
|
||||
|
||||
public string CpuName { set; get; } = "";
|
||||
|
||||
public float TotalRam { set; get; }
|
||||
|
||||
public string TokenID { set; get; } = "";
|
||||
|
||||
public string TokenValue { set; get; } = "";
|
||||
|
||||
public string RTokenID { set; get; } = "";
|
||||
|
||||
public string RTokenValue { set; get; } = "";
|
||||
|
||||
public override async Task<MessageHeader> AddAsync()
|
||||
{
|
||||
await using var con = new MySqlConnector.MySqlConnection(DBManagement.GetConnectionString(true));
|
||||
|
||||
await con.OpenAsync();
|
||||
var trans = await con.BeginTransactionAsync();
|
||||
var f = new MessageHeader();
|
||||
try
|
||||
{
|
||||
var localServer = new LocalServer
|
||||
{
|
||||
IpPrivateServer = IpPrivatev4,
|
||||
IpPrivateServerv6 = IpPrivatev6,
|
||||
IpPublicServer = IpPublic,
|
||||
PathServer = "",
|
||||
SerialNumber = BiosSN,
|
||||
OsVersion = OsVersion,
|
||||
OsName = OsName,
|
||||
OsArch = OsArch,
|
||||
OsKernal = OsKernel,
|
||||
SocketNum = SocketN,
|
||||
CpuName = CpuName,
|
||||
TotalRam = TotalRam,
|
||||
BiosVender = BiosVendor,
|
||||
ProductUuid = BiosUUID,
|
||||
Status = 0
|
||||
};//idTypeStorageServer
|
||||
var storageServer = new StorageServer
|
||||
{
|
||||
IdEmp = null,
|
||||
IdTypeStorageServer = StorageTypeID,
|
||||
StorageName = StorageName,
|
||||
CreateDate = DateTime.UtcNow,
|
||||
ControllerID = ControllerName,
|
||||
Status = 0
|
||||
};
|
||||
var ss_lc = new StorageServer_has_LocalServer
|
||||
{
|
||||
IdStorageServer = storageServer.IdStorageServer,
|
||||
IdLocalServer = localServer.IdLocalServer,
|
||||
CreateDate = DateTime.UtcNow,
|
||||
ModifyDate = null,
|
||||
Status = 0
|
||||
};
|
||||
|
||||
var serverAuthorization = new ServerAuthorization
|
||||
{
|
||||
IdStorageServer = storageServer.IdStorageServer,
|
||||
CreateDate = DateTime.UtcNow,
|
||||
Count = 1,
|
||||
Status = 0
|
||||
};
|
||||
var token = new Token
|
||||
{
|
||||
IdToken = TokenID,
|
||||
AccessToken = TokenValue,
|
||||
CreateDate = DateTime.UtcNow,
|
||||
ExpireDate = DateTime.UtcNow.AddMonths(3),
|
||||
Status = 0
|
||||
};
|
||||
var rT = new RefreshToken
|
||||
{
|
||||
IdRefreshToken = RTokenID,
|
||||
IdServerAuthorization = serverAuthorization.IdServerAuthorization,
|
||||
__RefreshToken = RTokenValue,
|
||||
CreateDate = DateTime.UtcNow,
|
||||
ExpireDate = DateTime.UtcNow.AddMonths(9),
|
||||
Status = 0
|
||||
};
|
||||
BatchInsert bi = new BatchInsert(con, trans);
|
||||
bi.AddRow(localServer);
|
||||
bi.AddRow(storageServer);
|
||||
bi.AddRow(ss_lc);
|
||||
bi.AddRow(serverAuthorization);
|
||||
bi.AddRow(token);
|
||||
bi.AddRow(rT);
|
||||
await bi.ExcuteQuery();
|
||||
await trans.CommitAsync();
|
||||
f.Status = 1;
|
||||
f.Message = "OK";
|
||||
}
|
||||
catch (DbException ex)
|
||||
{
|
||||
await trans.RollbackAsync();
|
||||
await trans.DisposeAsync();
|
||||
f.Status = 0;
|
||||
f.Message = ex.Message;
|
||||
f.ID = 61031;
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
public override Task<MessageHeader> DeleteAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override Task<MessageHeader> UpdateAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user