/// <summary> /// 忘记密码 /// </summary> /// <param name="code"></param> public void ForgetPassword(string code) { var user = Broker.Retrieve <user_info>("SELECT * FROM user_info WHERE code = @mail OR mailbox = @mail", new Dictionary <string, object>() { { "@mail", code } }); AssertUtil.CheckNull <SpException>(user, "用户不存在", "5E507D9C-47BC-4586-880D-D9E42D02FEA4"); UserIdentityUtil.SetCurrentUser(MapperHelper.Map <CurrentUserModel>(user)); var id = Guid.NewGuid().ToString(); var sms = new mail_vertification() { Id = id, name = "重置密码", content = $@"你好,<br/><br/> 请在两小时内点击该<a href=""{ SystemConfig.Config.Protocol }://{SystemConfig.Config.Domain}/api/MailVertification/ResetPassword?id={id}"">链接</a>重置密码 ", expire_time = DateTime.Now.AddHours(2), is_active = false, mail_address = user.mailbox, mail_type = MailType.ResetPassword.ToString() }; Broker.Create(sms); }
public override void OnActionExecuting(ActionExecutingContext context) { base.OnActionExecuting(context); Log(context); var tokenHeader = context.HttpContext.Request.Headers["Authorization"].ToString()?.Replace("Bearer ", ""); var user = JwtHelper.SerializeJwt(tokenHeader); if (user != null) { UserIdentityUtil.SetCurrentUser(MapperHelper.Map <CurrentUserModel>(user)); } else { UserIdentityUtil.SetCurrentUser(UserIdentityUtil.GetAnonymous()); } }
/// <summary> /// 任务执行 /// </summary> /// <param name="context"></param> /// <returns></returns> public Task Execute(IJobExecutionContext context) { var user = context.JobDetail.JobDataMap.Get("User") as CurrentUserModel; return(Task.Factory.StartNew(() => { Logger.Debug($"作业:{Name} 开始执行"); var stopWatch = new Stopwatch(); stopWatch.Start(); var broker = PersistBrokerFactory.GetPersistBroker(); UserIdentityUtil.SetCurrentUser(user); try { broker.ExecuteTransaction(() => { Executing(context); // 更新下次执行时间 var nextTime = JobHelpers.GetJobNextTime(Name); var nextTimeSql = ""; var paramList = new Dictionary <string, object>() { { "@time", DateTime.Now }, { "@name", Name } }; paramList.Add("@nextTime", nextTime.UtcDateTime); nextTimeSql = ", nextruntime = @nextTime"; broker.Execute($"UPDATE job SET lastruntime = @time {nextTimeSql} WHERE name = @name", paramList); }); } catch (Exception e) { Logger.Error($"作业:{Name}执行异常", e); throw e; } stopWatch.Stop(); Logger.Debug($"作业:{Name} 执行结束,耗时{stopWatch.ElapsedMilliseconds}ms"); })); }
public Task Execute(IJobExecutionContext context) { var user = context.JobDetail.JobDataMap.Get("User") as CurrentUserModel; return(Task.Factory.StartNew(() => { var stopWatch = new Stopwatch(); stopWatch.Start(); Logger.Debug($"作业:{Name} 开始执行"); try { UserIdentityUtil.SetCurrentUser(user); Executing(context); } catch (Exception e) { Logger.Error($"作业:{Name}执行异常", e); } stopWatch.Stop(); Logger.Debug($"作业:{Name} 执行结束,耗时{stopWatch.ElapsedMilliseconds}ms"); })); }
public string ResetPassword(string id) { UserIdentityUtil.SetCurrentUser(UserIdentityUtil.GetSystem()); return(new MailVertificationService().ResetPassword(id)); }
public string ActivateUser(string id) { UserIdentityUtil.SetCurrentUser(UserIdentityUtil.GetSystem()); return(new MailVertificationService().ActivateUser(id)); }
public static IApplicationBuilder UseEntityWatcher(this IApplicationBuilder app) { var logger = LogFactory.GetLogger("entity"); UserIdentityUtil.SetCurrentUser(UserIdentityUtil.GetSystem()); var broker = PersistBrokerFactory.GetPersistBroker(); var dialect = broker.DbClient.Driver; var entityList = ServiceContainer.ResolveAll <IEntity>().OrderByDescending(item => item.GetEntityName() == typeof(sys_attrs).Name); broker.ExecuteTransaction(() => { #region 创建表 entityList.Each(item => { var entity = broker.Query(dialect.GetTable(item.GetEntityName())); var attrs = item.GetAttrs(); if (entity == null || entity.Rows.Count == 0) { var attrSql = attrs .Select(e => { return($"{e.Name} {e.Type.GetDescription()}{(e.Length != null ? $"({e.Length.Value})" : "")} {(e.IsRequire.HasValue && e.IsRequire.Value ? "NOT NULL" : "")}{(e.Name == $"{item.GetEntityName()}id" ? " PRIMARY KEY" : "")}"); }) .Aggregate((a, b) => a + ",\r\n" + b); // 创建表 var sql = $@"CREATE TABLE public.{item.GetEntityName()} ({attrSql})"; broker.Execute(sql); logger.Info($"实体{item.GetLogicalName()}({item.GetEntityName()})创建成功"); } }); #endregion #region 创建实体记录和实体字段数据 entityList.Each(item => { #region 实体添加自动写入记录 var entityName = item.GetEntityName(); var entity = broker.Retrieve <sys_entity>("select * from sys_entity where code = @code", new Dictionary <string, object>() { { "@code", entityName } }); if (entity == null) { entity = new sys_entity() { Id = Guid.NewGuid().ToString(), name = item.GetLogicalName(), code = item.GetEntityName(), is_sys = item.IsSystemEntity() }; broker.Create(entity, false); } #endregion var attrs = item.GetAttrs(); var attrsList = new SysEntityService(broker).GetEntityAttrs(entity.Id).Select(e => e.code); #region 实体字段变更(删除字段) attrsList.Each(attr => { if (!attrs.Any(item => item.Name.ToLower() == attr.ToLower())) { var sql = @"DELETE FROM sys_attrs WHERE lower(code) = @code AND entityid = @entityid"; broker.Execute(sql, new Dictionary <string, object>() { { "@code", attr.ToLower() }, { "@entityid", EntityCache.GetEntity(item.GetEntityName())?.Id } }); sql = broker.DbClient.Driver.GetDropColumnSql(item.GetEntityName(), new List <Column>() { new Column() { Name = attr } }); broker.Execute(sql); logger.Debug($"实体{item.GetLogicalName()} ({item.GetEntityName()})删除字段:{attr}"); } }); #endregion #region 实体字段变更(新增字段) attrs.Each(attr => { if (!attrsList.Contains(attr.Name)) { var _attr = new sys_attrs() { Id = Guid.NewGuid().ToString(), name = attr.LogicalName, code = attr.Name, entityid = entity.Id, entityidname = entity.name, entityCode = entity.code, attr_type = attr.Type.ToString().ToLower(), attr_length = attr.Length, isrequire = attr.IsRequire.HasValue && attr.IsRequire.Value, default_value = ConvertUtil.ConToString(attr.DefaultValue) }; broker.Create(_attr); logger.Debug($"实体{item.GetLogicalName()}({item.GetEntityName()})创建字段:{attr.LogicalName}({attr.Name})成功"); } }); #endregion }); #endregion #region 执行版本更新脚本 { var vLogger = LogFactory.GetLogger("version"); FileHelper.GetFileList("*.sql", FolderType.Version) .OrderBy(item => Path.GetFileName(item)) .ToList() .Each(sqlFile => { try { var count = new VersionScriptExecutionLogService(broker).ExecuteScript(sqlFile); if (count == 1) { vLogger.Info($"脚本:{Path.GetFileName(sqlFile)}执行成功"); } } catch (Exception ex) { vLogger.Error($"脚本:{Path.GetFileName(sqlFile)}执行失败", ex); } }); } #endregion }); return(app); }
/// <summary> /// 登录 /// </summary> /// <param name="model"></param> /// <returns></returns> public LoginResponse Login(LoginRequest model) { var code = model.code; var pwd = model.password; var publicKey = model.publicKey; UserIdentityUtil.SetCurrentUser(UserIdentityUtil.GetSystem()); var authUser = Broker.Retrieve <auth_user>("SELECT * FROM auth_user WHERE lower(code) = lower(@code)", new Dictionary <string, object>() { { "@code", code } }); if (authUser == null) { return(new LoginResponse() { result = false, message = "用户名或密码错误" }); } if (authUser.is_lock) { return(new LoginResponse() { result = false, message = "用户已被锁定,请联系管理员" }); } if (string.IsNullOrEmpty(pwd) || string.IsNullOrEmpty(publicKey) || !string.Equals(authUser.password, RSAUtil.Decrypt(pwd, publicKey)) ) { var message = "用户名或密码错误"; if (!authUser.try_times.HasValue) { authUser.try_times = 1; } else { authUser.try_times += 1; if (authUser.try_times > 1) { message = $"用户名或密码已连续错误{authUser.try_times}次,超过五次账号锁定"; } } if (authUser.try_times >= 5) { authUser.is_lock = true; message = $"用户已被锁定,请联系管理员"; } Broker.Update(authUser); return(new LoginResponse() { result = false, message = message }); } if (authUser.try_times > 0) { authUser.try_times = 0; } authUser.last_login_time = DateTime.Now; Broker.Update(authUser); // 返回登录结果、用户信息、用户验证票据信息 var oUser = new LoginResponse { result = true, userName = code, token = JwtHelper.CreateToken(new JwtTokenModel() { Code = authUser.code, Name = authUser.name, Role = authUser.code, Uid = authUser.Id }), userId = authUser.user_infoid, message = "登录成功" }; return(oUser); }
public LoginResponse SignInOrSignUp(LoginRequest model) { UserIdentityUtil.SetCurrentUser(UserIdentityUtil.GetSystem()); return(new SystemService().SignInOrSignUp(model)); }