Exemplo n.º 1
0
        private LoginModel UserLogin(LoginModel model)
        {
            try
            {
                model.ResponseCode = 99;
                Business.HR.EmployeeMaster objEmployeeMaster = new Business.HR.EmployeeMaster();
                Entity.HR.EmployeeMaster   employeeMaster    = new Entity.HR.EmployeeMaster();
                Entity.Common.Auth         auth = new Auth();
                employeeMaster = objEmployeeMaster.AuthenticateUser(model.UserName);

                if (employeeMaster != null)
                {
                    string passowrd = employeeMaster.Password;
                    string userId   = employeeMaster.UserId.ToString();

                    if (passowrd.Equals(model.Password.Trim().EncodePasswordToBase64()))
                    {
                        DataTable dtDevices = objEmployeeMaster.LinkedDevices_GetByUserId(employeeMaster.UserId);
                        if (dtDevices != null && dtDevices.Rows.Count > 0)
                        {
                            model.ResponseCode = 99;
                            model.Message      = "A device is already linked with you. Please contact admin to change device.";
                        }
                        else if (employeeMaster.IsPasswordChangeRequired)
                        {
                            model.ResponseCode = 99;
                            model.Message      = "Reset password needed. Please visit aegiscrm.in to reset password.";
                        }
                        else if (!employeeMaster.IsLoginActive)
                        {
                            model.ResponseCode = 99;
                            model.Message      = "Login blocked by admin.";
                        }
                        else
                        {
                            model.Name         = employeeMaster.EmployeeName + " (" + employeeMaster.EmployeeCode + ")";
                            model.UserId       = Convert.ToInt32(userId);
                            model.ResponseCode = 200;
                            model.Message      = "Success";

                            auth.UserId = Convert.ToInt32(userId);
                            auth.IP     = GetIP();
                            auth.Status = Entity.Common.LoginStatus.Success;
                            auth.Client = GetClient();
                            objEmployeeMaster.Login_Save(auth);
                        }
                    }
                    else
                    {
                        model.Message = "Invalid username/password.";

                        auth.UserId         = Convert.ToInt32(userId);
                        auth.IP             = GetIP();
                        auth.Status         = Entity.Common.LoginStatus.WrongPassword;
                        auth.Client         = GetClient();
                        auth.FailedUserName = model.UserName;
                        auth.FailedPassword = model.Password;
                        objEmployeeMaster.Login_Save(auth);
                    }
                }
                else
                {
                    model.Message = "Invalid username/password.";

                    auth.IP             = GetIP();
                    auth.Status         = Entity.Common.LoginStatus.Failed;
                    auth.Client         = GetClient();
                    auth.FailedUserName = model.UserName;
                    auth.FailedPassword = model.Password;
                    objEmployeeMaster.Login_Save(auth);
                }
            }
            catch (Exception ex)
            {
                new Logger().LogException(ex, "UserLogin");
                model.Message = ex.Message;
            }
            return(model);
        }