FIC_Solar/SolarPower/Controllers/MyBaseController.cs
2021-06-22 09:48:44 +08:00

129 lines
4.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using SolarPower.Models.User;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using Dapper;
using SolarPower.Models;
using System.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using SolarPower.Repository.Interface;
using System.IO;
using System.Text;
using SolarPower.Models.OperatorLogModel;
using Newtonsoft.Json;
using SolarPower.Models.Company;
using SolarPower.Models.Role;
namespace SolarPower.Controllers
{
public class MyBaseController<T> : Controller where T : MyBaseController<T>
{
private ILogger<T> _logger;
protected ILogger<T> Logger => _logger ?? (_logger = HttpContext?.RequestServices.GetService<ILogger<T>>());
private IUserRepository userRepository => HttpContext?.RequestServices.GetService<IUserRepository>();
private ICompanyRepository companyRepository => HttpContext?.RequestServices.GetService<ICompanyRepository>();
private IRoleRepository roleRepository => HttpContext?.RequestServices.GetService<IRoleRepository>();
private IOperatorLogRepository operatorLogRepository => HttpContext?.RequestServices.GetService<IOperatorLogRepository>();
protected MyUser myUser = null;
public string controllerName;
public string actionName;
public ErrorCode errorCode = new ErrorCode();
public MyBaseController()
{
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
EDFunction edFunction = new EDFunction();
var myAccount = edFunction.AESDecrypt(HttpContext.Session.GetString("MyAccount")); //取得登入後該位使用者的Account
controllerName = ControllerContext.RouteData.Values["controller"].ToString(); //controller名稱
actionName = ControllerContext.RouteData.Values["action"].ToString(); //action名稱
if (string.IsNullOrEmpty(myAccount) && myAccount.CompareTo(HttpContext.Session.GetString("MyAccount")) == 0)
{
//session 找不到account或者無法成功解密
return;
}
//取得當前登入使用者資訊
myUser = userRepository.GetMyUserInfoByAccount(myAccount);
myUser.Company = companyRepository.GetMyCompanyInfoById(myUser.CompanyId);
myUser.Role = roleRepository.GetMyRoleInfoById(myUser.RoleId);
List<string> auth_arr = new List<string>();
if (myUser.Role.Layer != 0) //判斷是否平台管理員
{
foreach (var auth in myUser.Role.Auths)
{
var per_auch_arr = auth.Split(',');
foreach (var x in per_auch_arr)
{
auth_arr.Add(x);
}
}
ViewBag.auths = auth_arr;
}
ViewBag.myUser = myUser;
#region
var content = JsonConvert.SerializeObject(filterContext.ActionArguments);
OperatorLog operatorLog = new OperatorLog()
{
ControllerName = controllerName,
ActionName = actionName,
Parameter = content.CompareTo("{}") == 0 ? null : content,
CreatedBy = myUser.Id,
};
List<string> removeParam = new List<string>() { "ChangePassword" }; //移除不紀錄參數的actionName
if (removeParam.Any(x => actionName.Contains(x)))
{
operatorLog.Parameter = "{}";
}
List<string> properties = new List<string>()
{
"ControllerName",
"ActionName",
"Parameter",
"CreatedBy",
};
operatorLogRepository.Add(operatorLog, properties);
#endregion
}
/// <summary>
/// 判斷是否維平台的管理員或平台使用者
/// </summary>
/// <param name="roleLayer"></param>
/// <returns></returns>
public bool IsPlatformLayer(byte roleLayer)
{
if (roleLayer == (int)RoleLayerEnum.PlatformAdmin || roleLayer == (int)RoleLayerEnum.PlatformUser)
{
return true;
}
return false;
}
}
}