284 lines
10 KiB
C#
284 lines
10 KiB
C#
|
|
using AUTS.Domain.Application;
|
|||
|
|
using AUTS.Domain.Entities;
|
|||
|
|
using AUTS.Domain.ViewModels;
|
|||
|
|
using AUTS.Services.Cache;
|
|||
|
|
using AUTS.Services.Extensions;
|
|||
|
|
using AUTS.Services.Tool;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Data.Entity.Validation;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Text.RegularExpressions;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using System.Web.Mvc;
|
|||
|
|
|
|||
|
|
|
|||
|
|
namespace AUTS.Services.Manager
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 用户类
|
|||
|
|
/// </summary>
|
|||
|
|
public partial class Users
|
|||
|
|
{
|
|||
|
|
public static readonly string connectionSrt = "Server =blv-cloud-db.mysql.rds.aliyuncs.com;Database=uts_db;Uid=blv_rcu;Pwd=fnadiaJDIJ7546;charset=utf8;port=3307";
|
|||
|
|
|
|||
|
|
//private static string onUserOperationSessionName = "UserOperationSessionName";//用户当前选择库
|
|||
|
|
|
|||
|
|
#region 参数缓存
|
|||
|
|
|
|||
|
|
private static List<TBL_UTS_Manage_DBList> cacheSysDBList
|
|||
|
|
{
|
|||
|
|
// get选择器获取Lazyk懒加载数据
|
|||
|
|
get { return CacheHelp.GetSysDBList(); }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static List<DBCofinStrModel> cacheSysDBCofinStrList
|
|||
|
|
{
|
|||
|
|
// get选择器获取Lazyk懒加载数据
|
|||
|
|
get { return CacheHelp.GetDBCofinStrList(); }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//当前用户
|
|||
|
|
public static TBL_UTS_Manage_User Umodel
|
|||
|
|
{
|
|||
|
|
// get选择器获取Lazyk懒加载数据
|
|||
|
|
get { return UserLoginHelper.CurrentUser(); }
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 获取当前用户权限集合
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取当前用户权限集合
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="userID"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static List<TBL_UTS_Manage_UserAuth_Operation> GerOnUserAuthList(int userID)
|
|||
|
|
{
|
|||
|
|
string CacheKey = "OnUserAuthListCashRa-" + userID.ToString();
|
|||
|
|
if (CacheExtensions.CheckCache(CacheKey))//查找缓存是否存在
|
|||
|
|
{
|
|||
|
|
var userAuthList = CacheExtensions.GetCache<List<TBL_UTS_Manage_UserAuth_Operation>>(CacheKey);
|
|||
|
|
|
|||
|
|
return userAuthList;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
var db = new Uts_ManageEntities();
|
|||
|
|
var onAccountAuth = db.TBL_UTS_Manage_UserAuth_Operation.Where(a => a.UserID == userID).ToList();
|
|||
|
|
if (onAccountAuth != null && onAccountAuth.Count() > 0)
|
|||
|
|
{
|
|||
|
|
CacheExtensions.SetCache(CacheKey, onAccountAuth);
|
|||
|
|
return onAccountAuth;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 获取当前用户当前库权限
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取当前用户当前库权限
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static TBL_UTS_Manage_UserAuth_Operation GerOnUserAuth(int onUserID)
|
|||
|
|
{
|
|||
|
|
string CustomerCacheKey = "UserCustomerCashRa-" + onUserID.ToString();
|
|||
|
|
if (CacheExtensions.CheckCache(CustomerCacheKey))//查找缓存是否存在
|
|||
|
|
{
|
|||
|
|
var id = CacheExtensions.GetCache<int>(CustomerCacheKey);//当前库ID
|
|||
|
|
var onUserAuthList = GerOnUserAuthList(onUserID);
|
|||
|
|
if (onUserAuthList != null && onUserAuthList.Count() > 0)
|
|||
|
|
{
|
|||
|
|
return onUserAuthList.SingleOrDefault(x => x.UserID == onUserID && x.DatabaseID == id);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 获取用户当前选择库信息
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取用户当前选择库信息
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="userID">用户ID</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static TBL_UTS_Manage_DBList GerOnUserCustomer()
|
|||
|
|
{
|
|||
|
|
//var onCustomer = System.Web.HttpContext.Current.Session[onUserOperationSessionName];
|
|||
|
|
var onCustomer = CacheHelp.GetUserOperation(Umodel.UserName);
|
|||
|
|
if (onCustomer != 0)
|
|||
|
|
{
|
|||
|
|
return cacheSysDBList.SingleOrDefault(x => x.ID == onCustomer);
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static TBL_UTS_Manage_DBList GerOnUserCustomer(System.Web.HttpContext context)
|
|||
|
|
{
|
|||
|
|
//var onCustomer = context.Session[onUserOperationSessionName];
|
|||
|
|
var onCustomer = CacheHelp.GetUserOperation(Umodel.UserName);
|
|||
|
|
if (onCustomer != 0)
|
|||
|
|
{
|
|||
|
|
return cacheSysDBList.SingleOrDefault(x => x.ID == onCustomer);
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 获取用户当前选择库数据库连接串
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取用户当前选择库信息
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="userID">用户ID</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static DBCofinStrModel GerOnUserDBCofinStr()
|
|||
|
|
{
|
|||
|
|
//var onCustomer = System.Web.HttpContext.Current.Session[onUserOperationSessionName];
|
|||
|
|
var onCustomer = CacheHelp.GetUserOperation(Umodel.UserName);
|
|||
|
|
if (onCustomer != 0)
|
|||
|
|
{
|
|||
|
|
return cacheSysDBCofinStrList.SingleOrDefault(x => x.ID == onCustomer);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static DBCofinStrModel GerOnUserDBCofinStr(System.Web.HttpContext context)
|
|||
|
|
{
|
|||
|
|
//var onCustomer = context.Session[onUserOperationSessionName];
|
|||
|
|
var umodel= UserLoginHelper.CurrentUserAsync(context);
|
|||
|
|
var onCustomer = CacheHelp.GetUserOperation(umodel.UserName);
|
|||
|
|
if (onCustomer != 0)
|
|||
|
|
{
|
|||
|
|
return cacheSysDBCofinStrList.SingleOrDefault(x => x.ID == onCustomer);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 获取用户当前选择数据库连接串
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取用户当前选择数据库连接串
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string GerOnUserCofin()
|
|||
|
|
{
|
|||
|
|
var connectionString = "";
|
|||
|
|
connectionString = GerOnUserDBCofinStr().CofinStr;
|
|||
|
|
if (!String.IsNullOrEmpty(connectionString))
|
|||
|
|
{
|
|||
|
|
return connectionString;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return System.Configuration.ConfigurationManager.ConnectionStrings["Uts_Manage"].ConnectionString;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static string GerOnUserCofin(System.Web.HttpContext context)
|
|||
|
|
{
|
|||
|
|
var connectionString = "";
|
|||
|
|
connectionString = GerOnUserDBCofinStr(context).CofinStr;
|
|||
|
|
if (!String.IsNullOrEmpty(connectionString))
|
|||
|
|
{
|
|||
|
|
return connectionString;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return System.Configuration.ConfigurationManager.ConnectionStrings["Uts_Manage"].ConnectionString;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region 添加用户
|
|||
|
|
/// <summary>
|
|||
|
|
/// 添加用户
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="fc">前端窗体数据</param>
|
|||
|
|
/// <param name="entity"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static ReturnResult AddUser(FormCollection fc, TBL_UTS_Manage_User entity)
|
|||
|
|
{
|
|||
|
|
ReturnResult result = new ReturnResult();
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
//初始化参数
|
|||
|
|
string passwordconfirm = fc["confirmpassword"]; //确认登录密码
|
|||
|
|
|
|||
|
|
string[] haveDBName = new string[0];
|
|||
|
|
if (!string.IsNullOrEmpty(fc["haveDBName"]))
|
|||
|
|
{
|
|||
|
|
haveDBName = fc["haveDBName"].Split(','); //可查数据库
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
//必填项
|
|||
|
|
if (string.IsNullOrEmpty(entity.Password)) throw new CustomException("登录密码不能为空");
|
|||
|
|
if (string.IsNullOrEmpty(passwordconfirm)) throw new CustomException("确认登录密码不能为空");
|
|||
|
|
|
|||
|
|
if (passwordconfirm != entity.Password) throw new CustomException("登录密码与确认登录密码不一致");
|
|||
|
|
|
|||
|
|
//验证项
|
|||
|
|
if (!Regex.IsMatch(entity.UserName, @"^[A-Za-z0-9_]+$")) throw new CustomException("会员编号只能为字母、数字和下划线");
|
|||
|
|
if (entity.UserName.Length > 20) throw new CustomException("会员编号长度超过20个字符");
|
|||
|
|
|
|||
|
|
Uts_ManageEntities db = new Uts_ManageEntities();
|
|||
|
|
//唯一项
|
|||
|
|
if (db.TBL_UTS_Manage_User.Where(x => x.UserName == entity.UserName.Trim()).Count() > 0) throw new CustomException("用户名已被使用");
|
|||
|
|
|
|||
|
|
|
|||
|
|
//初始化参数
|
|||
|
|
entity.IsValid = true;
|
|||
|
|
entity.IsAdmin = false;
|
|||
|
|
//注册后完善
|
|||
|
|
entity.PlaintextPwd = entity.Password;
|
|||
|
|
entity.Password = entity.Password.ToMD5().ToMD5();
|
|||
|
|
entity.CreateTime = DateTime.Now;
|
|||
|
|
//将数据添加到EF,并且标记为添加标记,返回受影响的行数。
|
|||
|
|
db.TBL_UTS_Manage_User.Add(entity);
|
|||
|
|
//SaveChanges()数据保存到数据库,根据前面的标记生成对应的Sql语句,交给数据库执行。
|
|||
|
|
db.SaveChanges();
|
|||
|
|
|
|||
|
|
//向权限表添加数据
|
|||
|
|
if (haveDBName.Length > 0)
|
|||
|
|
{
|
|||
|
|
for (var i = 0; i < haveDBName.Length; i++)
|
|||
|
|
{
|
|||
|
|
db.TBL_UTS_Manage_UserAuth_Operation.Add(new TBL_UTS_Manage_UserAuth_Operation
|
|||
|
|
{
|
|||
|
|
UserID = entity.ID,
|
|||
|
|
DatabaseID = haveDBName[i].ToInt(),
|
|||
|
|
DatabaseName = cacheSysDBList.SingleOrDefault(x => x.ID == haveDBName[i].ToInt()).DatabaseName,
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
db.SaveChanges();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
CacheHelp.ClearUserAuthList();
|
|||
|
|
result.Status = 200;
|
|||
|
|
}
|
|||
|
|
catch (CustomException ex)
|
|||
|
|
{
|
|||
|
|
result.Message = ex.Message.ToString();
|
|||
|
|
}
|
|||
|
|
catch (DbEntityValidationException ex)
|
|||
|
|
{
|
|||
|
|
result.Message = "网络系统繁忙,请稍候再试!";
|
|||
|
|
//Logs.WriteErrorLog(ex);
|
|||
|
|
LogHelp.WriteExceptionLog(ex);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
result.Message = "网络系统繁忙,请稍候再试!";
|
|||
|
|
//Logs.WriteErrorLog(ex);
|
|||
|
|
LogHelp.WriteExceptionLog(ex);
|
|||
|
|
}
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|