コード例 #1
0
        /// <summary>
        /// 解锁指定用户
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userName">登录名(用户名)</param>
        /// <returns>大于0解锁成功</returns>
        public int UnLockUser(UserInfo userInfo, string userName)
        {
            var parameter = ParameterUtil.CreateWithMessage(userInfo, MethodBase.GetCurrentMethod(), this.serviceName, RDIFrameworkMessage.LogOnService_UnLockUser);
            int result    = 0;

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                var manager    = new PiUserManager(userInfo);
                var parameters = new List <KeyValuePair <string, object> > {
                    new KeyValuePair <string, object>(PiUserTable.FieldUserName, userName),
                    new KeyValuePair <string, object>(PiUserTable.FieldEnabled, 1),
                    new KeyValuePair <string, object>(PiUserTable.FieldDeleteMark, 0)
                };
                var entity = BaseEntity.Create <PiUserEntity>(manager.GetDT(parameters, 0, null));
                if ((entity != null) && !string.IsNullOrEmpty(entity.Id))
                {
                    var longOnmanager             = new PiUserLogOnManager();
                    PiUserLogOnEntity lonOnentity = longOnmanager.GetEntity(entity.Id);
                    lonOnentity.LockStartDate     = null;
                    lonOnentity.LockEndDate       = null;
                    result = longOnmanager.Update(lonOnentity);
                }
            });
            return(result);
        }
コード例 #2
0
        /// <summary>
        /// 通过单点登录标识登录
        /// </summary>
        /// <param name="openId">单点登录标识</param>
        /// <param name="ipAddress">IP地址</param>
        /// <param name="macAddress">Mac地址</param>
        /// <returns>用户信息</returns>
        public UserInfo LogOnByOpenId(string openId, string ipAddress = null, string macAddress = null)
        {
            UserInfo userInfo = null;

            // 用户没有找到状态
            this.ReturnStatusCode = StatusCode.UserNotFound.ToString();
            // 检查是否有效的合法的参数
            if (!String.IsNullOrEmpty(openId))
            {
                List <KeyValuePair <string, object> > parameters = new List <KeyValuePair <string, object> >();
                if (!string.IsNullOrEmpty(openId))
                {
                    parameters.Add(new KeyValuePair <string, object>(PiUserLogOnTable.FieldOpenId, openId));
                }
                // 若是单点登录,那就不能判断ip地址,因为不是直接登录,是间接登录
                if (!string.IsNullOrEmpty(ipAddress))
                {
                    parameters.Add(new KeyValuePair <string, object>(PiUserLogOnTable.FieldIPAddress, ipAddress));
                }
                if (!string.IsNullOrEmpty(macAddress))
                {
                    parameters.Add(new KeyValuePair <string, object>(PiUserLogOnTable.FieldMACAddress, macAddress));
                }

                var dt = new PiUserLogOnManager(this.DBProvider, this.UserInfo).GetDT(parameters);
                if (dt.Rows.Count == 1)
                {
                    PiUserEntity      userEntity      = this.GetEntity(dt.Rows[0][PiUserLogOnTable.FieldId].ToString());
                    PiUserLogOnEntity userLogOnEntity = new PiUserLogOnManager(this.DBProvider, this.UserInfo).GetEntity(userEntity.Id);
                    userInfo = this.LogOn(userEntity.UserName, userLogOnEntity.UserPassword, openId, false, ipAddress, macAddress, false);
                }
            }
            return(userInfo);
        }
コード例 #3
0
        /// <summary>
        /// 通用用户名登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="ipAddress">IP地址</param>
        /// <param name="macAddress">Mac地址</param>
        /// <returns>用户信息</returns>
        public UserInfo LogOnByUserName(string userName, string ipAddress = null, string macAddress = null)
        {
            UserInfo userInfo = null;

            // 用户没有找到状态
            this.ReturnStatusCode = StatusCode.UserNotFound.ToString();
            // 检查是否有效的合法的参数
            if (!String.IsNullOrEmpty(userName))
            {
                var parameters = new List <KeyValuePair <string, object> >
                {
                    new KeyValuePair <string, object>(PiUserTable.FieldUserName, userName),
                    new KeyValuePair <string, object>(PiUserTable.FieldDeleteMark, 0),
                    new KeyValuePair <string, object>(PiUserTable.FieldEnabled, 1)
                };
                var dt = this.GetDT(parameters);
                if (dt.Rows.Count == 1)
                {
                    var entity = BaseEntity.Create <PiUserEntity>(dt.Rows[0]);
                    PiUserLogOnEntity userLogOnEntity = new PiUserLogOnManager(this.DBProvider, this.UserInfo).GetEntity(entity.Id);
                    userInfo = this.LogOn(entity.UserName, userLogOnEntity.UserPassword, string.Empty, false, ipAddress, macAddress, false);
                }
            }
            return(userInfo);
        }
コード例 #4
0
        /// <summary>
        /// 用户离职
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userName">离职人员用户名</param>
        /// <param name="dimissionCause">离职原因</param>
        /// <param name="dimissionDate">离职日期</param>
        /// <param name="dimissionWhither">离职去向</param>
        /// <returns>大于0操作成功</returns>
        public int UserDimission(UserInfo userInfo, string userName, string dimissionCause, DateTime?dimissionDate, string dimissionWhither = null)
        {
            var parameter = ParameterUtil.CreateWithMessage(userInfo, MethodBase.GetCurrentMethod(), this.serviceName, RDIFrameworkMessage.LogOnService_UserDimission);
            int result    = 0;

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                var manager    = new PiUserManager(userInfo);
                var parameters = new List <KeyValuePair <string, object> > {
                    new KeyValuePair <string, object>(PiUserTable.FieldUserName, userName),
                    new KeyValuePair <string, object>(PiUserTable.FieldEnabled, 1),
                    new KeyValuePair <string, object>(PiUserTable.FieldDeleteMark, 0)
                };
                var entity = BaseEntity.Create <PiUserEntity>(manager.GetDT(parameters, 0, null));
                if ((entity != null) && !string.IsNullOrEmpty(entity.Id))
                {
                    entity.Enabled          = 0;
                    entity.IsDimission      = 1;
                    entity.DimissionCause   = dimissionCause;
                    entity.DimissionWhither = dimissionWhither;
                    entity.DimissionDate    = dimissionDate ?? DateTime.Now;
                    result = manager.Update(entity);
                    //离职的员工就不能登录系统了,应该锁定了
                    var longOnmanager             = new PiUserLogOnManager();
                    PiUserLogOnEntity lonOnentity = longOnmanager.GetEntity(entity.Id);
                    lonOnentity.LockStartDate     = DateTime.Now;
                    result += longOnmanager.Update(lonOnentity);
                }
            });
            return(result);
        }
コード例 #5
0
        /// <summary>
        /// 获取在线用户列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <returns>数据表</returns>
        public DataTable GetOnLineState(UserInfo userInfo)
        {
            var parameter = ParameterUtil.CreateWithLog(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessRDIWriteDbWithLock(userInfo, parameter, locker, (dbProvider, getOnLine) =>
            {
                var manager = new PiUserLogOnManager(dbProvider);
                // 设置为在线状态
                manager.OnLine(userInfo.Id);
                if (MessageService.LastCheckOnLineState == DateTime.MinValue)
                {
                    getOnLine = true;
                }
                else
                {
                    TimeSpan timeSpan = DateTime.Now - MessageService.LastCheckOnLineState;
                    if ((timeSpan.Minutes * 60 + timeSpan.Seconds) >= SystemInfo.OnLineCheck)
                    {
                        getOnLine = true;
                    }
                }

                if (OnLineStateDT == null || getOnLine)
                {
                    // 检查用户在线状态(服务器专用)
                    manager.CheckOnLine();
                    // 获取在线状态列表
                    OnLineStateDT                       = manager.GetOnLineStateDT();
                    OnLineStateDT.TableName             = PiUserTable.TableName;
                    MessageService.LastCheckOnLineState = DateTime.Now;
                }
                return(getOnLine);
            });
            return(OnLineStateDT);
        }
コード例 #6
0
        /// <summary>
        /// 设置密码
        /// </summary>
        /// <param name="userId">被设置的员工主键</param>
        /// <param name="password">新密码</param>
        /// <returns>影响行数</returns>
        public virtual int SetPassword(string userId, string password)
        {
            int returnValue = 0;

            // 密码强度检查

            /*
             * if (SystemInfo.EnableCheckPasswordStrength)
             * {
             *  if (password.Length == 0)
             *  {
             *      this.ReturnStatusCode = StatusCode.PasswordCanNotBeNull.ToString();
             *      return returnValue;
             *  }
             * }
             */
            // 加密密码
            if (SystemInfo.EnableEncryptServerPassword)
            {
                password = this.EncryptUserPassword(password);
            }

            PiUserLogOnManager userLonOnManager = new PiUserLogOnManager(this.DBProvider, this.UserInfo);
            //先判断是否有记录,没有就增加
            PiUserLogOnEntity logOnEntity = userLonOnManager.GetEntity(userId);

            if (logOnEntity == null || (logOnEntity != null && string.IsNullOrEmpty(logOnEntity.Id)))
            {
                PiUserLogOnEntity entity = new PiUserLogOnEntity()
                {
                    Id = userId,
                    //MultiUserLogin = SystemInfo.CheckOnLine ? 0 : 1,
                    CheckIPAddress = SystemInfo.EnableCheckIPAddress ? 1 : 0,
                    UserPassword   = password,
                    OpenId         = BusinessLogic.NewGuid()
                };
                string value = userLonOnManager.Add(entity);
                this.ReturnStatusCode = !string.IsNullOrEmpty(value) ? StatusCode.SetPasswordOK.ToString() : StatusCode.ErrorDeleted.ToString();
                returnValue           = !string.IsNullOrEmpty(value) ? 1 : 0;
            }
            else
            {
                // 设置密码字段
                string[] targetFields =
                {
                    PiUserLogOnTable.FieldUserPassword,
                    PiUserLogOnTable.FieldChangePasswordDate,
                    PiUserLogOnTable.FieldOpenId
                };
                Object[] targetValues = { password, null, BusinessLogic.NewGuid() };
                returnValue           = new PiUserLogOnManager(this.DBProvider, this.UserInfo).SetProperty(userId, targetFields, targetValues);
                this.ReturnStatusCode = returnValue == 1 ? StatusCode.SetPasswordOK.ToString(): StatusCode.ErrorDeleted.ToString();
            }

            return(returnValue);
        }
コード例 #7
0
        /// <summary>
        /// 用户在线
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="onLineState">用户在线状态</param>
        public void OnLine(UserInfo userInfo, int onLineState = 1)
        {
            var parameter = ParameterUtil.CreateWithLog(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessRDIWriteDbWithLock(userInfo, parameter, locker, dbProvider =>
            {
                var manager = new PiUserLogOnManager(dbProvider, userInfo);
                manager.OnLine(userInfo.Id, onLineState);
            });
        }
コード例 #8
0
        /// <summary>
        /// 用户离线
        /// </summary>
        /// <param name="userInfo">用户</param>
        public void OnExit(UserInfo userInfo)
        {
            var parameter = ParameterUtil.CreateWithMessage(userInfo, MethodBase.GetCurrentMethod(), this.serviceName, RDIFrameworkMessage.LogOnService_OnExit);

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                var manager = new PiUserLogOnManager(dbProvider, userInfo);
                manager.OnExit(userInfo.Id);
            });
        }
コード例 #9
0
        /// <summary>
        /// 重新设置数据
        /// </summary>
        /// <returns>影响行数</returns>
        public int Reset()
        {
            int returnValue = 0;

            returnValue += this.ResetData();
            PiUserLogOnManager manager = new PiUserLogOnManager(this.DBProvider, this.UserInfo);

            returnValue += manager.ResetVisitInfo();
            return(returnValue);
        }
コード例 #10
0
        /// <summary>
        /// 更新密码
        /// </summary>
        /// <param name="oldPassword">原密码</param>
        /// <param name="newPassword">新密码</param>
        /// <param name="statusCode">返回状态码</param>
        /// <returns>影响行数</returns>
        public virtual int ChangePassword(string oldPassword, string newPassword, out string statusCode)
        {
            int returnValue = 0;

            // 密码强度检查
            if (SystemInfo.EnableCheckPasswordStrength)
            {
                if (String.IsNullOrEmpty(newPassword))
                {
                    statusCode = StatusCode.PasswordCanNotBeNull.ToString();
                    return(returnValue);
                }

                //最小长度、字母数字组合等强度检查
                if (!ValidateUtil.EnableCheckPasswordStrength(newPassword))
                {
                    statusCode = StatusCode.PasswordNotStrength.ToString();
                    return(returnValue);
                }
            }

            // 加密密码
            if (SystemInfo.EnableEncryptServerPassword)
            {
                oldPassword = this.EncryptUserPassword(oldPassword);
                newPassword = this.EncryptUserPassword(newPassword);
            }

            // 判断输入原始密码是否正确
            PiUserLogOnEntity entity = new PiUserLogOnManager(this.DBProvider, this.UserInfo).GetEntity(UserInfo.Id);

            if (entity.UserPassword == null)
            {
                entity.UserPassword = string.Empty;
            }

            // 密码错误
            if (!entity.UserPassword.Equals(oldPassword))
            {
                statusCode = StatusCode.OldPasswordError.ToString();
                return(returnValue);
            }

            // 更改密码,同时修改密码的修改日期,这里需要兼容多数据库
            var sqlBuilder = new SQLBuilder(this.DBProvider);

            sqlBuilder.BeginUpdate(PiUserLogOnTable.TableName);
            sqlBuilder.SetValue(PiUserLogOnTable.FieldUserPassword, newPassword);
            sqlBuilder.SetDBNow(PiUserLogOnTable.FieldChangePasswordDate);
            sqlBuilder.SetWhere(PiUserLogOnTable.FieldId, UserInfo.Id);
            returnValue = sqlBuilder.EndUpdate();
            statusCode  = returnValue == 1 ? StatusCode.ChangePasswordOK.ToString() : StatusCode.ErrorDeleted.ToString();
            return(returnValue);
        }
コード例 #11
0
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="id">主键</param>
        /// <returns>实体</returns>
        public PiUserLogOnEntity GetEntity(UserInfo userInfo, string id)
        {
            PiUserLogOnEntity entity = null;
            var parameter            = ParameterUtil.CreateWithLog(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                var manager = new PiUserLogOnManager(dbProvider, userInfo);
                entity      = manager.GetEntity(id);
            });
            return(entity);
        }
コード例 #12
0
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="entity">实体</param>
        /// <returns>影响行数</returns>
        public int Update(UserInfo userInfo, PiUserLogOnEntity entity)
        {
            int returnValue = 0;
            var parameter   = ParameterUtil.CreateWithLog(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                var manager = new PiUserLogOnManager(dbProvider, userInfo);
                returnValue = manager.Update(entity);
            });

            return(returnValue);
        }
コード例 #13
0
        /// <summary>
        /// 检查在线状态
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="onLineState">用户在线状态</param>
        /// <returns>离线人数</returns>
        public int CheckOnLine(UserInfo userInfo, int onLineState)
        {
            int returnValue = 0;
            var parameter   = ParameterUtil.CreateWithLog(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                var manager = new PiUserLogOnManager(dbProvider);
                // 设置为在线状态
                manager.OnLine(userInfo.Id, onLineState);
                returnValue = manager.CheckOnLine();
            });
            return(returnValue);
        }
コード例 #14
0
        /// <summary>
        /// 获取消息状态
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="onLineState">用户在线状态</param>
        /// <param name="lastChekDate">最后检查日期</param>
        /// <returns>消息状态数组</returns>
        public string[] MessageChek(UserInfo userInfo, int onLineState, string lastChekDate)
        {
            string[] returnValue = null;
            var      parameter   = ParameterUtil.CreateWithMessage(userInfo, MethodBase.GetCurrentMethod(), this.serviceName, RDIFrameworkMessage.MessageService_MessageChek);

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                // 设置为在线状态
                var manager = new PiUserLogOnManager(dbProvider, userInfo);
                manager.OnLine(userInfo.Id, onLineState);
                // 读取信息状态
                var messageManager = new CiMessageManager(dbProvider, userInfo);
                returnValue        = messageManager.MessageChek();
            });
            return(returnValue);
        }
コード例 #15
0
        /// <summary>
        /// 扮演用户
        /// </summary>
        /// <param name="id">用户主键</param>
        /// <returns>用户类</returns>
        public UserInfo Impersonation(string id, out string statusCode)
        {
            UserInfo userInfo = null;
            // 获得登录信息
            PiUserLogOnEntity entity = new PiUserLogOnManager(this.DBProvider, this.UserInfo).GetEntity(id);

            // 只允许登录一次,需要检查是否自己重新登录了,或者自己扮演自己了
            if (!UserInfo.Id.Equals(id))
            {
                if (SystemInfo.CheckOnLine)
                {
                    if (entity.UserOnLine > 0)
                    {
                        statusCode = StatusCode.ErrorOnLine.ToString();
                        return(userInfo);
                    }
                }
            }

            PiUserEntity userEntity = this.GetEntity(id);

            userInfo = this.ConvertToUserInfo(userEntity);
            if (userEntity.IsStaff.Equals("1"))
            {
                // 获得员工的信息
                PiStaffEntity  staffEntity    = new PiStaffEntity();
                PiStaffManager staffManager   = new PiStaffManager(DBProvider, UserInfo);
                DataTable      dataTableStaff = staffManager.GetDTById(id);
                staffEntity.GetFrom(dataTableStaff);
                userInfo = staffManager.ConvertToUserInfo(staffEntity, userInfo);
            }
            statusCode = StatusCode.OK.ToString();
            // 登录、重新登录、扮演时的在线状态进行更新
            PiUserLogOnManager userLogOnManager = new PiUserLogOnManager(this.DBProvider, this.UserInfo);

            userLogOnManager.ChangeOnLine(id);
            return(userInfo);
        }
コード例 #16
0
        /// <summary>
        /// 获取用户的新信息
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="openId">单点登录标识</param>
        /// <returns>数据表</returns>
        public DataTable GetDTNew(UserInfo userInfo, out string openId)
        {
            var    dataTable = new DataTable(CiMessageTable.TableName);
            string myOpenId  = userInfo.OpenId;
            var    parameter = ParameterUtil.CreateWithLog(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessRDIReadDb(userInfo, parameter, dbProvider =>
            {
                if (!SystemInfo.CheckOnLine)
                {
                    var manager = new PiUserLogOnManager(dbProvider, userInfo);
                    myOpenId    = manager.GetProperty(userInfo.Id, PiUserLogOnTable.FieldOpenId);
                }

                if (userInfo.OpenId.Equals(myOpenId))
                {
                    var messageManager  = new CiMessageManager(dbProvider, userInfo);
                    dataTable           = messageManager.GetDTNew();
                    dataTable.TableName = CiMessageTable.TableName;
                }
            });
            openId = myOpenId;
            return(dataTable);
        }
コード例 #17
0
        /// <summary>
        /// 进行登录操作
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="openId">单点登录标识</param>
        /// <param name="createNewOpenId"></param>
        /// <param name="ipAddress">IP地址</param>
        /// <param name="macAddress">MAC地址</param>
        /// <param name="checkUserPassword">是否要检查用户密码</param>
        /// <returns>用户信息</returns>
        public UserInfo LogOn(string userName, string password, string openId = null, bool createNewOpenId = false, string ipAddress = null, string macAddress = null, bool checkUserPassword = true)
        {
            UserInfo userInfo = null;

            string realName = string.Empty;

            if (UserInfo != null)
            {
                realName = UserInfo.RealName;
                if (string.IsNullOrEmpty(ipAddress))
                {
                    ipAddress = UserInfo.IPAddress;
                }
                if (string.IsNullOrEmpty(macAddress))
                {
                    macAddress = UserInfo.MACAddress;
                }
            }

            PiUserLogOnManager userLogOnManager = new PiUserLogOnManager(this.DBProvider, this.UserInfo);

            // 01: 系统是否采用了在线用户的限制
            if (SystemInfo.OnLineLimit > 0 && userLogOnManager.CheckOnLineLimit())
            {
                this.ReturnStatusCode = StatusCode.ErrorOnLineLimit.ToString();
                // LogManager.Instance.Add(DBProvider, userName, realName, "LogOn", RDIFrameworkMessage.UserManager,"LogOn", RDIFrameworkMessage.UserManager_LogOn, userName, ipAddress,RDIFrameworkMessage.MSG0089 + SystemInfo.OnLineLimit.ToString());
                return(userInfo);
            }

            // 02. 默认为用户没有找到状态,查找用户
            // 这是为了达到安全要求,不能提示用户未找到,那容易让别人猜测到帐户
            this.ReturnStatusCode = SystemInfo.EnableCheckPasswordStrength ? StatusCode.ErrorLogOn.ToString() : StatusCode.UserNotFound.ToString();

            // 03. 查询数据库中的用户数据?只查询未被删除的
            string[]  names     = { PiUserTable.FieldDeleteMark, PiUserTable.FieldUserName };
            Object[]  values    = { 0, userName };
            DataTable dataTable = this.GetDT(names, values);

            if (dataTable.Rows.Count == 0)
            {
                //TODO:若没数据再工号、邮件、手机号等方式登录
            }

            PiUserEntity      userEntity      = null;
            PiUserLogOnEntity userLogOnEntity = null;

            if (dataTable.Rows.Count > 1)
            {
                this.ReturnStatusCode = StatusCode.UserDuplicate.ToString();
            }
            else if (dataTable.Rows.Count == 1)
            {
                // 05. 判断密码,是否允许登录,是否离职是否正确
                userEntity = BaseEntity.Create <PiUserEntity>(dataTable.Rows[0]);
                if (!string.IsNullOrEmpty(userEntity.AuditStatus) && userEntity.AuditStatus.EndsWith(AuditStatus.WaitForAudit.ToString()))
                {
                    this.ReturnStatusCode = AuditStatus.WaitForAudit.ToString();
                    //LogManager.Instance.Add(DBProvider, userName, realName, "LogOn", RDIFrameworkMessage.UserManager, "LogOn", RDIFrameworkMessage.UserManager_LogOn, userName, ipAddress, RDIFrameworkMessage.MSG0078);
                    return(userInfo);
                }

                // 用户无效、已离职的
                if (userEntity.IsDimission == 1 || userEntity.Enabled == 0)
                {
                    this.ReturnStatusCode = StatusCode.LogOnDeny.ToString();
                    return(userInfo);
                }

                // 用户是否有效的
                if (userEntity.Enabled == -1)
                {
                    this.ReturnStatusCode = StatusCode.UserNotActive.ToString();
                    //LogManager.Instance.Add(DBProvider, userName, realName, "LogOn", RDIFrameworkMessage.UserManager, "LogOn", RDIFrameworkMessage.UserManager_LogOn, userName, ipAddress, RDIFrameworkMessage.MSG0080);
                    return(userInfo);
                }
                userLogOnEntity = userLogOnManager.GetEntity(userEntity.Id);
                if (string.IsNullOrEmpty(userEntity.UserName) || !userEntity.UserName.Equals("Administrator"))
                {
                    // 06. 允许登录时间是否有限制
                    if (userLogOnEntity.AllowEndTime != null)
                    {
                        userLogOnEntity.AllowEndTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, userLogOnEntity.AllowEndTime.Value.Hour, userLogOnEntity.AllowEndTime.Value.Minute, userLogOnEntity.AllowEndTime.Value.Second);
                    }

                    if (userLogOnEntity.AllowStartTime != null)
                    {
                        userLogOnEntity.AllowStartTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, userLogOnEntity.AllowStartTime.Value.Hour, userLogOnEntity.AllowStartTime.Value.Minute, userLogOnEntity.AllowStartTime.Value.Second);
                        if (DateTime.Now < userLogOnEntity.AllowStartTime)
                        {
                            this.ReturnStatusCode = StatusCode.UserLocked.ToString();
                            //LogManager.Instance.Add(DBProvider, userName, realName, "LogOn", RDIFrameworkMessage.UserManager, "LogOn", RDIFrameworkMessage.UserManager_LogOn, userName, ipAddress, RDIFrameworkMessage.MSG0081 + userLogOnEntity.AllowStartTime.Value.ToString("HH:mm"));
                            return(userInfo);
                        }
                    }

                    if (userLogOnEntity.AllowEndTime != null)
                    {
                        if (DateTime.Now > userLogOnEntity.AllowEndTime)
                        {
                            this.ReturnStatusCode = StatusCode.UserLocked.ToString();
                            //LogManager.Instance.Add(DBProvider, userName, realName, "LogOn", RDIFrameworkMessage.UserManager, "LogOn", RDIFrameworkMessage.UserManager_LogOn, userName, ipAddress, RDIFrameworkMessage.MSG0082 + userLogOnEntity.AllowEndTime.Value.ToString("HH:mm"));
                            return(userInfo);
                        }
                    }

                    // 07. 锁定日期是否有限制
                    if (userLogOnEntity.LockStartDate != null && DateTime.Now > userLogOnEntity.LockStartDate)
                    {
                        if (userLogOnEntity.LockEndDate == null || DateTime.Now < userLogOnEntity.LockEndDate)
                        {
                            this.ReturnStatusCode = StatusCode.UserLocked.ToString();
                            //LogManager.Instance.Add(DBProvider, userName, realName, "LogOn",RDIFrameworkMessage.UserManager, "LogOn", RDIFrameworkMessage.UserManager_LogOn,userName, ipAddress,RDIFrameworkMessage.MSG0083 + userLogOnEntity.LockStartDate.Value.ToString("yyyy-MM-dd"));
                            return(userInfo);
                        }
                    }

                    if (userLogOnEntity.LockEndDate != null && DateTime.Now < userLogOnEntity.LockEndDate)
                    {
                        this.ReturnStatusCode = StatusCode.UserLocked.ToString();
                        //LogManager.Instance.Add(DBProvider, userName, realName, "LogOn", RDIFrameworkMessage.UserManager,"LogOn", RDIFrameworkMessage.UserManager_LogOn, userName, ipAddress,RDIFrameworkMessage.MSG0084 + userEntity.LockEndDate.Value.ToString("yyyy-MM-dd"));
                        return(userInfo);
                    }
                }

                // 08. 是否检查用户IP地址,是否进行访问限制?管理员不检查IP. && !this.IsAdministrator(userEntity.Id.ToString()
                if (SystemInfo.EnableCheckIPAddress && userLogOnEntity.CheckIPAddress == 1 && (!(userEntity.UserName.Equals("Administrator") || userEntity.Code.Equals("Administrator"))))
                {
                    var parameterManager = new CiParameterManager(this.DBProvider);
                    var nameArr          = new string[2];
                    var valueArr         = new string[2];
                    nameArr[0]  = CiParameterTable.FieldParameterId;
                    nameArr[1]  = CiParameterTable.FieldCategoryKey;
                    valueArr[0] = userEntity.Id.ToString();
                    // 没有设置IP地址时不检查
                    valueArr[1] = "IPAddress";
                    if (!string.IsNullOrEmpty(ipAddress))
                    {
                        if (parameterManager.Exists(nameArr, valueArr))
                        {
                            if (!this.CheckIPAddress(ipAddress, userEntity.Id.ToString()))
                            {
                                var parameters = new List <KeyValuePair <string, object> >
                                {
                                    new KeyValuePair <string, object>(PiUserLogOnTable.FieldIPAddress, ipAddress)
                                };
                                //this.SetProperty(userEntity.Id, PiUserLogOnTable.FieldIPAddress, ipAddress);
                                this.ReturnStatusCode = StatusCode.ErrorIPAddress.ToString();
                                //LogManager.Instance.Add(DBProvider, userName, realName, "LogOn",RDIFrameworkMessage.UserManager, "LogOn", RDIFrameworkMessage.UserManager_LogOn,ipAddress, ipAddress, RDIFrameworkMessage.MSG0085);
                                return(userInfo);
                            }
                        }
                    }

                    // 没有设置MAC地址时不检查
                    valueArr[1] = "MacAddress";
                    if (!string.IsNullOrEmpty(macAddress))
                    {
                        if (parameterManager.Exists(nameArr, valueArr))
                        {
                            if (!this.CheckMacAddress(macAddress, userEntity.Id.ToString()))
                            {
                                this.ReturnStatusCode = StatusCode.ErrorMacAddress.ToString();
                                //this.SetProperty(userEntity.Id, PiUserLogOnTable.FieldMACAddress, macAddress);
                                //LogManager.Instance.Add(DBProvider, userName, realName, "LogOn",RDIFrameworkMessage.UserManager, "LogOn", RDIFrameworkMessage.UserManager_LogOn,macAddress, ipAddress, RDIFrameworkMessage.MSG0086);
                                return(userInfo);
                            }
                        }
                    }
                }

                // 10. 只允许登录一次,需要检查是否自己重新登录了,或者自己扮演自己了
                if ((UserInfo != null) && (!UserInfo.Id.Equals(userEntity.Id.ToString())))
                {
                    if (SystemInfo.CheckOnLine && userLogOnEntity.MultiUserLogin == 0 && userLogOnEntity.UserOnLine > 0)
                    {
                        // 自己是否登录了2次,在没下线的情况下
                        bool isSelf = false;
                        if (!string.IsNullOrEmpty(openId))
                        {
                            if (!string.IsNullOrEmpty(userLogOnEntity.OpenId))
                            {
                                if (userLogOnEntity.OpenId.Equals(openId))
                                {
                                    isSelf = true;
                                }
                            }
                        }
                        if (!isSelf)
                        {
                            this.ReturnStatusCode = StatusCode.ErrorOnLine.ToString();
                            //LogManager.Instance.Add(DBProvider, userName, realName, "LogOn",RDIFrameworkMessage.UserManager, "LogOn", RDIFrameworkMessage.UserManager_LogOn,userName, ipAddress, RDIFrameworkMessage.MSG0087);
                            return(userInfo);
                        }
                    }
                }

                // 04. 系统是否采用了密码加密策略?
                if (checkUserPassword && SystemInfo.EnableEncryptServerPassword)
                {
                    password = this.EncryptUserPassword(password);
                }


                // 11. 密码是否正确(null 与空看成是相等的)
                if (!(string.IsNullOrEmpty(userLogOnEntity.UserPassword) && string.IsNullOrEmpty(password)))
                {
                    bool userPasswordOK = true;
                    // 用户密码是空的
                    if (string.IsNullOrEmpty(userLogOnEntity.UserPassword))
                    {
                        // 但是输入了不为空的密码
                        if (!string.IsNullOrEmpty(password))
                        {
                            userPasswordOK = false;
                        }
                    }
                    else
                    {
                        // 用户的密码不为空,但是用户是输入了密码、 再判断用户的密码与输入的是否相同
                        userPasswordOK = !string.IsNullOrEmpty(password) && userLogOnEntity.UserPassword.Equals(password);
                    }
                    // 用户的密码不相等
                    if (!userPasswordOK)
                    {
                        userLogOnEntity.PasswordErrorCount = userLogOnEntity.PasswordErrorCount + 1;
                        if (SystemInfo.PasswordErrorLockLimit > 0 &&
                            userLogOnEntity.PasswordErrorCount >= SystemInfo.PasswordErrorLockLimit)

                        {
                            if (SystemInfo.PasswordErrorLockCycle == 0) //密码错误锁定周期若为0,直接设帐号无效,需要管理员审核
                            {
                                string[] names1  = { PiUserTable.FieldEnabled, PiUserTable.FieldAuditStatus };
                                object[] values1 = { "0", AuditStatus.WaitForAudit.ToString() };
                                this.SetProperty(userEntity.Id, names1, values1);
                            }
                            else
                            {
                                //进行帐号锁定
                                userLogOnEntity.LockStartDate = DateTime.Now;
                                userLogOnEntity.LockEndDate   = DateTime.Now.AddMinutes(SystemInfo.PasswordErrorLockCycle);
                                string[] names2  = { PiUserLogOnTable.FieldLockStartDate, PiUserLogOnTable.FieldLockEndDate };
                                object[] values2 = { userLogOnEntity.LockStartDate, userLogOnEntity.LockEndDate };
                                userLogOnManager.SetProperty(userEntity.Id, names2, values2);
                            }
                        }
                        else
                        {
                            userLogOnManager.SetProperty(userEntity.Id, PiUserLogOnTable.FieldPasswordErrorCount, userLogOnEntity.PasswordErrorCount);
                        }
                        //密码错误后处理:
                        //  11.1:记录日志
                        //LogManager.Instance.Add(DBProvider, userEntity.Id.ToString(), userEntity.RealName, "LogOn", RDIFrameworkMessage.UserManager, "LogOn", RDIFrameworkMessage.UserManager_LogOn, userEntity.RealName, ipAddress, RDIFrameworkMessage.MSG0088);
                        // TODO: 11.2:看当天(24小时内)输入错误密码多少次了?
                        // TODO: 11.3:若输错密码数量已经超过了系统限制,则用户被锁定系统设定的小时数。
                        // TODO: 11.4:同时处理返回值,由于输入错误密码次数过多导致被锁定,登录时应读取这个状态比较,时间过期后应处理下状态。
                        // 密码强度检查,若是要有安全要求比较高的,返回的提醒消息要进行特殊处理,不能返回非常明确的提示信息。
                        this.ReturnStatusCode = SystemInfo.EnableCheckPasswordStrength ? StatusCode.ErrorLogOn.ToString() : StatusCode.PasswordError.ToString();
                        return(userInfo);
                    }
                }

                // 12. 更新IP地址,更新MAC地址
                userLogOnEntity.PasswordErrorCount = 0;
                if (!string.IsNullOrEmpty(ipAddress))
                {
                    userLogOnEntity.IPAddress = ipAddress;
                }
                if (!string.IsNullOrEmpty(macAddress))
                {
                    userLogOnEntity.MACAddress = macAddress;
                }
                userLogOnManager.SetProperty(userEntity.Id, new string[] { PiUserLogOnTable.FieldPasswordErrorCount, PiUserLogOnTable.FieldIPAddress, PiUserLogOnTable.FieldMACAddress }, new object[] { 0, ipAddress, macAddress });
                // 可以正常登录了
                this.ReturnStatusCode = StatusCode.OK.ToString();

                // 13. 登录、重新登录、扮演时的在线状态进行更新
                //userLogOnManager.ChangeOnLine(userEntity.Id);

                userInfo            = this.ConvertToUserInfo(userEntity, userLogOnEntity);
                userInfo.IPAddress  = ipAddress;
                userInfo.MACAddress = macAddress;
                userInfo.Password   = password;
                // 这里是判断用户是否为系统管理员的
                userInfo.IsAdministrator = IsAdministrator(userEntity);
                // 数据找到了,就可以退出循环了

                /*
                 * // 获得员工的信息
                 * if (userEntity.IsStaff == 1)
                 * {
                 *  PiStaffManager staffManager = new PiStaffManager(DBProvider, UserInfo);
                 *  //这里需要按 员工的用户ID来进行查找对应的员工-用户关系
                 *  PiStaffEntity staffEntity = new PiStaffEntity(staffManager.GetDT(PiStaffTable.FieldUserId, userEntity.Id));
                 *  if (!string.IsNullOrEmpty(staffEntity.Id))
                 *  {
                 *      userInfo = staffManager.ConvertToUserInfo(staffEntity, userInfo);
                 *  }
                 *
                 * }*/
            }

            // 14. 记录系统访问日志
            if (this.ReturnStatusCode == StatusCode.OK.ToString())
            {
                //LogManager.Instance.Add(DBProvider, userEntity.Id.ToString(), userEntity.RealName, "LogOn", RDIFrameworkMessage.UserManager, "LogOn", RDIFrameworkMessage.UserManager_LogOn, userEntity.RealName, ipAddress, RDIFrameworkMessage.UserManager_LogOnSuccess);
                if (string.IsNullOrEmpty(userInfo.OpenId))
                {
                    createNewOpenId = true;
                }
                if (createNewOpenId)
                {
                    userInfo.OpenId = userLogOnManager.UpdateVisitDate(userEntity.Id.ToString(), createNewOpenId);
                }
                else
                {
                    userLogOnManager.UpdateVisitDate(userEntity.Id.ToString());
                }
            }
            else
            {
                //LogManager.Instance.Add(DBProvider, userName, realName, "LogOn", RDIFrameworkMessage.UserManager, "LogOn", RDIFrameworkMessage.UserManager_LogOn, userName, ipAddress, RDIFrameworkMessage.MSG0090);
            }
            return(userInfo);
        }
コード例 #18
0
        /// <summary>
        /// 进行密码验证
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>是否通过验证</returns>
        public bool ValidateUser(string userName, string password)
        {
            // 先按用户名登录
            var dt = this.GetDT(new KeyValuePair <string, object>(PiUserTable.FieldUserName, userName)
                                , new KeyValuePair <string, object>(PiUserTable.FieldDeleteMark, 0)
                                , new KeyValuePair <string, object>(PiUserTable.FieldEnabled, 1));

            if (dt.Rows.Count == 0)
            {
                // 若没数据再按工号登录
                dt = this.GetDT(new KeyValuePair <string, object>(PiUserTable.FieldCode, userName)
                                , new KeyValuePair <string, object>(PiUserTable.FieldDeleteMark, 0)
                                , new KeyValuePair <string, object>(PiUserTable.FieldEnabled, 1));

                if (dt.Rows.Count == 0)
                {
                    // 若没数据再按邮件登录
                    dt = this.GetDT(new KeyValuePair <string, object>(PiUserTable.FieldEmail, userName)
                                    , new KeyValuePair <string, object>(PiUserTable.FieldDeleteMark, 0)
                                    , new KeyValuePair <string, object>(PiUserTable.FieldEnabled, 1));
                }

                if (dt.Rows.Count == 0)
                {
                    // 若没数据再按手机号码登录
                    dt = this.GetDT(new KeyValuePair <string, object>(PiUserTable.FieldMobile, userName)
                                    , new KeyValuePair <string, object>(PiUserTable.FieldDeleteMark, 0)
                                    , new KeyValuePair <string, object>(PiUserTable.FieldEnabled, 1));
                }

                if (dt.Rows.Count == 0)
                {
                    // 若没数据再按手机号码登录
                    dt = this.GetDT(new KeyValuePair <string, object>(PiUserTable.FieldTelephone, userName)
                                    , new KeyValuePair <string, object>(PiUserTable.FieldDeleteMark, 0)
                                    , new KeyValuePair <string, object>(PiUserTable.FieldEnabled, 1));
                }
            }

            PiUserEntity      userEntity      = null;
            PiUserLogOnEntity userLogOnEntity = null;
            var parameters = new List <KeyValuePair <string, object> >();

            if (dt.Rows.Count > 1)
            {
                return(false);
            }
            else if (dt.Rows.Count == 1)
            {
                // 05. 判断密码,是否允许登录,是否离职是否正确
                userEntity = BaseEntity.Create <PiUserEntity>(dt.Rows[0]);//new PiUserEntity(dt.Rows[0]);
                if (!string.IsNullOrEmpty(userEntity.AuditStatus) &&
                    userEntity.AuditStatus.EndsWith(AuditStatus.WaitForAudit.ToString()) &&
                    userLogOnEntity.PasswordErrorCount == 0)
                {
                    return(false);
                }
                PiUserLogOnManager userLogOnManager = new PiUserLogOnManager(this.DBProvider, this.UserInfo);
                userLogOnEntity = userLogOnManager.GetEntity(userEntity.Id);
                // 06. 允许登录时间是否有限制
                if (userLogOnEntity.AllowEndTime != null)
                {
                    userLogOnEntity.AllowEndTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, userLogOnEntity.AllowEndTime.Value.Hour, userLogOnEntity.AllowEndTime.Value.Minute, userLogOnEntity.AllowEndTime.Value.Second);
                }
                if (userLogOnEntity.AllowStartTime != null)
                {
                    userLogOnEntity.AllowStartTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, userLogOnEntity.AllowStartTime.Value.Hour, userLogOnEntity.AllowStartTime.Value.Minute, userLogOnEntity.AllowStartTime.Value.Second);
                    if (DateTime.Now < userLogOnEntity.AllowStartTime)
                    {
                        return(false);
                    }
                }
                if (userLogOnEntity.AllowEndTime != null)
                {
                    if (DateTime.Now > userLogOnEntity.AllowEndTime)
                    {
                        return(false);
                    }
                }

                // 07. 锁定日期是否有限制
                if (userLogOnEntity.LockStartDate != null)
                {
                    if (DateTime.Now > userLogOnEntity.LockStartDate)
                    {
                        if (userLogOnEntity.LockEndDate == null || DateTime.Now < userLogOnEntity.LockEndDate)
                        {
                            return(false);
                        }
                    }
                }
                if (userLogOnEntity.LockEndDate != null)
                {
                    if (DateTime.Now < userLogOnEntity.LockEndDate)
                    {
                        return(false);
                    }
                }

                // 03. 系统是否采用了密码加密策略?
                if (SystemInfo.EnableEncryptServerPassword)
                {
                    password = this.EncryptUserPassword(password);
                }

                // 11. 密码是否正确(null 与空看成是相等的)
                if (!(string.IsNullOrEmpty(userLogOnEntity.UserPassword) && string.IsNullOrEmpty(password)))
                {
                    bool userPasswordOK = true;
                    // 用户密码是空的
                    if (string.IsNullOrEmpty(userLogOnEntity.UserPassword))
                    {
                        // 但是输入了不为空的密码
                        if (!string.IsNullOrEmpty(password))
                        {
                            userPasswordOK = false;
                        }
                    }
                    else
                    {
                        // 用户的密码不为空,但是用户是输入了密码
                        userPasswordOK = !string.IsNullOrEmpty(password) && userLogOnEntity.UserPassword.Equals(password);
                    }
                    // 用户的密码不相等
                    if (!userPasswordOK)
                    {
                        // 这里更新用户连续输入错误密码次数
                        return(false);
                    }
                }
            }
            return(true);
        }