/// <summary>
        /// 保存实体修改记录
        /// </summary>
        /// <param name="entityNew">修改后的实体对象</param>
        /// <param name="entityOld">修改前的实体对象</param>
        /// <param name="tableName">表名称</param>
        public void SaveEntityChangeLog(BaseUserContactEntity entityNew, BaseUserContactEntity entityOld, string tableName = null)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                //统一放在一个公共表 Troy.Cui 2016-08-17
                tableName = BaseChangeLogEntity.CurrentTableName;
            }
            var manager = new BaseChangeLogManager(UserInfo, tableName);

            foreach (var property in typeof(BaseUserContactEntity).GetProperties())
            {
                var oldValue         = Convert.ToString(property.GetValue(entityOld, null));
                var newValue         = Convert.ToString(property.GetValue(entityNew, null));
                var fieldDescription = property.GetCustomAttributes(typeof(FieldDescription), false).FirstOrDefault() as FieldDescription;
                //不记录创建人、修改人、没有修改的记录
                if (!fieldDescription.NeedLog || oldValue == newValue)
                {
                    continue;
                }
                var entity = new BaseChangeLogEntity
                {
                    TableName         = CurrentTableName,
                    TableDescription  = FieldExtensions.ToDescription(typeof(BaseUserContactEntity), "CurrentTableName"),
                    RecordKey         = entityOld.Id.ToString(),
                    ColumnName        = property.Name,
                    ColumnDescription = fieldDescription.Text,
                    NewValue          = newValue,
                    OldValue          = oldValue
                };
                manager.Add(entity, true, false);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 保存实体修改记录
        /// </summary>
        /// <param name="recordKey">记录主键</param>
        /// <param name="entityNew">修改后的实体对象</param>
        /// <param name="entityOld">修改前的实体对象</param>
        /// <param name="tableName">表名称</param>
        /// <param name="systemCode">子系统编码</param>
        public virtual void SaveEntityChangeLog(string recordKey, object entityOld, object entityNew, string tableName = null, string systemCode = null)
        {
            var oldType = entityOld.GetType();
            var newType = entityNew.GetType();

            if (newType.Equals(oldType) && oldType.IsClass && newType.IsClass)
            {
                if (string.IsNullOrEmpty(systemCode))
                {
                    systemCode = BaseSystemInfo.SystemCode;
                }
                if (string.IsNullOrEmpty(tableName))
                {
                    tableName = BaseChangeLogEntity.CurrentTableName;
                }
                var manager = new BaseChangeLogManager(UserInfo, tableName);
                foreach (var property in oldType.GetProperties())
                {
                    var oldValue         = Convert.ToString(property.GetValue(entityOld, null));
                    var newValue         = Convert.ToString(property.GetValue(entityNew, null));
                    var fieldDescription = property.GetCustomAttributes(typeof(FieldDescription), false).FirstOrDefault() as FieldDescription;
                    if (!fieldDescription.NeedLog || oldValue == newValue)
                    {
                        continue;
                    }
                    var entity = new BaseChangeLogEntity
                    {
                        SystemCode        = systemCode,
                        TableName         = CurrentTableName,
                        TableDescription  = CurrentTableDescription,
                        ColumnName        = property.Name,
                        ColumnDescription = fieldDescription.Text,
                        NewValue          = newValue,
                        OldValue          = oldValue,
                        RecordKey         = recordKey,
                        SortCode          = 1 // 不要排序了,加快写入速度
                    };
                    manager.Add(entity, true, false);
                }
            }
            else
            {
                LogUtil.WriteLog("比较的实体类型不一样或非实体类型", "SaveEntityChangeLog");
                //throw new ArgumentException();
            }
        }
Beispiel #3
0
        /// <summary>
        /// 更新密码
        /// </summary>
        /// <param name="userId">用户主键、方便外部系统调用,若能传递参数过来</param>
        /// <param name="oldPassword">原密码</param>
        /// <param name="newPassword">新密码</param>
        /// <returns>影响行数</returns>
        public virtual BaseUserInfo ChangePassword(string userId, string oldPassword, string newPassword)
        {
            #if (DEBUG)
            int milliStart = Environment.TickCount;
            #endif

            var encryptOldPassword = oldPassword;
            var encryptNewPassword = newPassword;

            BaseUserInfo userInfo = null;
            // 密码强度检查
            if (BaseSystemInfo.CheckPasswordStrength)
            {
                if (string.IsNullOrEmpty(newPassword))
                {
                    Status     = Status.PasswordCanNotBeNull;
                    StatusCode = Status.PasswordCanNotBeNull.ToString();
                    return(userInfo);
                }
            }
            // 判断输入原始密码是否正确
            var entity = new BaseUserLogonManager(DbHelper, UserInfo).GetEntityByUserId(UserInfo.UserId);
            if (entity.UserPassword == null)
            {
                entity.UserPassword = string.Empty;
            }

            // 加密密码
            if (BaseSystemInfo.ServerEncryptPassword)
            {
                encryptOldPassword = EncryptUserPassword(oldPassword, entity.Salt);
            }

            // 密码错误
            if (!entity.UserPassword.Equals(encryptOldPassword, StringComparison.CurrentCultureIgnoreCase))
            {
                Status     = Status.OldPasswordError;
                StatusCode = Status.OldPasswordError.ToString();
                return(userInfo);
            }
            // 对比是否最近2次用过这个密码
            if (BaseSystemInfo.CheckPasswordStrength)
            {
                /*
                 * int i = 0;
                 * BaseParameterManager manager = new BaseParameterManager(this.DbHelper, this.UserInfo);
                 * var dt = manager.GetDataTableParameterCode("User", this.UserInfo.Id, "Password");
                 * foreach (DataRow dr in dt.Rows)
                 * {
                 *  string parameter = dr[BaseParameterEntity.FieldParameterContent].ToString();
                 *  if (parameter.Equals(newPassword))
                 *  {
                 *      this.StatusCode = Status.PasswordCanNotBeRepeat.ToString();
                 *      return userInfo;
                 *  }
                 *  i++;
                 *  {
                 *      // 判断连续2个密码就是可以了
                 *      if (i > 2)
                 *      {
                 *          break;
                 *      }
                 *  }
                 * }
                 */
            }

            // 更改密码,同时修改密码的修改日期,这里需要兼容多数据库
            var salt = string.Empty;
            if (BaseSystemInfo.ServerEncryptPassword)
            {
                salt = RandomUtil.GetString(20);
                encryptNewPassword = EncryptUserPassword(newPassword, salt);
            }
            var sqlBuilder = new SqlBuilder(DbHelper);
            sqlBuilder.BeginUpdate(BaseUserLogonEntity.CurrentTableName);
            if (BaseSystemInfo.ServerEncryptPassword)
            {
                sqlBuilder.SetValue(BaseUserLogonEntity.FieldSalt, salt);
            }
            // 宋彪:此处增加更新密码强度级别
            sqlBuilder.SetValue(BaseUserLogonEntity.FieldPasswordStrength, SecretUtil.GetUserPassWordRate(newPassword));
            sqlBuilder.SetValue(BaseUserLogonEntity.FieldUserPassword, encryptNewPassword);
            // 2015-08-04 吉日嘎拉 修改了密码后,把需要修改密码字段设置为 0
            sqlBuilder.SetValue(BaseUserLogonEntity.FieldNeedModifyPassword, 0);
            sqlBuilder.SetDbNow(BaseUserLogonEntity.FieldChangePasswordTime);
            sqlBuilder.SetDbNow(BaseUserLogonEntity.FieldUpdateTime);
            if (UserInfo != null)
            {
                sqlBuilder.SetValue(BaseUserLogonEntity.FieldUpdateUserId, UserInfo.UserId);
                sqlBuilder.SetValue(BaseUserLogonEntity.FieldUpdateBy, UserInfo.RealName);
            }
            sqlBuilder.SetWhere(BaseUserLogonEntity.FieldUserId, userId);
            var result = sqlBuilder.EndUpdate();

            if (result == 1)
            {
                // 2015-12-09 吉日嘎拉 确认已经记录了修改密码日志
                // BaseLogonLogManager.AddLog(this.UserInfo, Status.ChangePassword.ToDescription());

                // 2015-12-09 吉日嘎拉 增加日志功能、谁什么时候设置了谁的密码?
                var record = new BaseChangeLogEntity
                {
                    TableName         = BaseUserLogonEntity.CurrentTableName,
                    TableDescription  = FieldExtensions.ToDescription(typeof(BaseUserLogonEntity), "CurrentTableName"),
                    ColumnName        = BaseUserLogonEntity.FieldUserPassword,
                    ColumnDescription = "用户密码",
                    RecordKey         = userId.ToString(),
                    NewValue          = "修改密码"
                };
                var changeLogManager = new BaseChangeLogManager(UserInfo);
                changeLogManager.Add(record, true, false);

                /*
                 * // 若是强类型密码检查,那就保存密码修改历史,防止最近2-3次的密码相同的功能实现。
                 * if (BaseSystemInfo.CheckPasswordStrength)
                 * {
                 *  BaseParameterManager parameterManager = new BaseParameterManager(this.DbHelper, this.UserInfo);
                 *  BaseParameterEntity parameterEntity = new BaseParameterEntity();
                 *  parameterEntity.CategoryCode = "User";
                 *  parameterEntity.ParameterId = this.UserInfo.Id;
                 *  parameterEntity.ParameterCode = "Password";
                 *  parameterEntity.ParameterContent = newPassword;
                 *  parameterEntity.Deleted = 0;
                 *  parameterEntity.Enabled = true;
                 *  parameterManager.AddEntity(parameterEntity);
                 * }
                 */

                userInfo = LogonByOpenId(UserInfo.OpenId, UserInfo.SystemCode).UserInfo;
                // 同步处理其他系统的密码修改动作
                if (BaseSystemInfo.ServerEncryptPassword)
                {
                    // AfterChangePassword(this.UserInfo.Id, salt, oldPassword, newPassword);
                }
                // 修改密码成功,写入状态
                Status     = Status.ChangePasswordOk;
                StatusCode = Status.ChangePasswordOk.ToString();
            }
            else
            {
                // 数据可能被删除
                Status     = Status.ErrorDeleted;
                StatusCode = Status.ErrorDeleted.ToString();
            }

            return(userInfo);
        }
Beispiel #4
0
        /// <summary>
        /// 设置密码
        /// </summary>
        /// <param name="userId">被设置的用户主键</param>
        /// <param name="newPassword">新密码(原始,未加密)</param>
        /// <param name="unlock">解除锁定</param>
        /// <param name="autoAdd">数据缺少自动补充登录信息</param>
        /// <param name="changeLog">记录更改</param>
        /// <returns>影响行数</returns>
        public virtual int SetPassword(int userId, string newPassword, bool?unlock = null, bool?autoAdd = null, bool changeLog = true)
        {
            var result = 0;

            // 密码强度检查

            /*
             * if (BaseSystemInfo.CheckPasswordStrength)
             * {
             *  if (password.Length == 0)
             *  {
             *      this.StatusCode = StatusCode.PasswordCanNotBeNull.ToString();
             *      return result;
             *  }
             * }
             */
            var encryptPassword = newPassword;
            var salt            = string.Empty;

            // 加密密码
            if (BaseSystemInfo.ServerEncryptPassword)
            {
                salt            = RandomUtil.GetString(20);
                encryptPassword = EncryptUserPassword(newPassword, salt);
            }
            // 设置密码字段
            var parameters = new List <KeyValuePair <string, object> >
            {
                new KeyValuePair <string, object>(BaseUserLogonEntity.FieldUserPassword, encryptPassword),

                // 2016-05-20 吉日嘎拉 把修改的痕迹保留起来
                new KeyValuePair <string, object>(BaseUserLogonEntity.FieldUpdateTime, DateTime.Now)
            };

            if (UserInfo != null)
            {
                parameters.Add(new KeyValuePair <string, object>(BaseUserLogonEntity.FieldUpdateUserId, UserInfo.UserId));
                parameters.Add(new KeyValuePair <string, object>(BaseUserLogonEntity.FieldUpdateBy, UserInfo.RealName));
            }

            //需要重新登录才可以,防止正在被人黑中,阻止已经在线上的人
            parameters.Add(new KeyValuePair <string, object>(BaseUserLogonEntity.FieldOpenId, Guid.NewGuid().ToString("N")));
            if (BaseSystemInfo.ServerEncryptPassword)
            {
                parameters.Add(new KeyValuePair <string, object>(BaseUserLogonEntity.FieldSalt, salt));
            }
            parameters.Add(new KeyValuePair <string, object>(BaseUserLogonEntity.FieldChangePasswordTime, DateTime.Now));
            if (unlock.HasValue && unlock.Value == true)
            {
                parameters.Add(new KeyValuePair <string, object>(BaseUserLogonEntity.FieldEnabled, 1));
                parameters.Add(new KeyValuePair <string, object>(BaseUserLogonEntity.FieldLockStartTime, null));
                parameters.Add(new KeyValuePair <string, object>(BaseUserLogonEntity.FieldLockEndTime, null));
            }
            var userLogonManager = new BaseUserLogonManager(DbHelper, UserInfo);

            result = userLogonManager.SetProperty(new KeyValuePair <string, object>(BaseUserLogonEntity.FieldUserId, userId), parameters);
            if (result == 0 && autoAdd.HasValue && autoAdd.Value == true)
            {
                var userLogonEntity = new BaseUserLogonEntity
                {
                    UserId             = userId,
                    ChangePasswordTime = DateTime.Now,
                    UserPassword       = encryptPassword,
                    Salt       = salt,
                    Enabled    = 1,
                    CreateTime = DateTime.Now,
                    UpdateTime = DateTime.Now
                };
                if (UserInfo != null)
                {
                    userLogonEntity.CreateUserId = UserInfo.UserId;
                    userLogonEntity.CreateBy     = UserInfo.RealName;
                }
                userLogonManager.AddEntity(userLogonEntity);
                result = 1;
            }

            // 2015-12-09 吉日嘎拉 增加日志功能、谁什么时候设置了谁的密码?
            if (changeLog)
            {
                var record = new BaseChangeLogEntity
                {
                    TableName         = BaseUserLogonEntity.CurrentTableName,
                    TableDescription  = FieldExtensions.ToDescription(typeof(BaseUserLogonEntity), "CurrentTableName"),
                    ColumnName        = BaseUserLogonEntity.FieldUserPassword,
                    ColumnDescription = "用户密码",
                    RecordKey         = userId.ToString(),
                    NewValue          = "设置密码"
                };

                var changeLogManager = new BaseChangeLogManager(UserInfo);
                changeLogManager.Add(record, true, false);
            }

            if (result == 1)
            {
                StatusCode = Status.SetPasswordOk.ToString();
                // 调用扩展
                if (BaseSystemInfo.OnInternet && BaseSystemInfo.ServerEncryptPassword)
                {
                    // AfterSetPassword(userId, salt, password);
                }
            }
            else
            {
                // 数据可能被删除
                StatusCode = Status.ErrorDeleted.ToString();
            }

            return(result);
        }