public HttpResponseMessage Login([FromBody] LoginRequest request)
        {
            var response = new LoginResponse();

            if (BusinessHandler.AccountBUS.LoginValidate(request, ref response))
            {
                try
                {
                    var user = NhanVienRepository.GetInstance().IsExistAndGet(request.Username, Security.Encrypt(request.Password));
                    if (user == null)
                    {
                        response.Errors.Add("Đăng nhập không thành công.");
                        response.IsError = true;
                    }
                    else
                    {
                        response.Data = new
                        {
                            Token           = Token.Create(user, DateTime.Now.Ticks.ToString()),
                            FullName        = user.HoTen,
                            PermissionLevel = user.CapPQ,
                        };
                    }
                }
                catch
                {
                    response.Errors.Add("Lỗi hệ thống.");
                    response.IsError = true;
                }
            }
            return(Request.CreateResponse(HttpStatusCode.OK, response));
        }
        public HttpResponseMessage Forget([FromBody] FPassRequest request)
        {
            var response = new FPassResponse();

            try
            {
                var user = NhanVienRepository.GetInstance().GetUserByEmail(request.Email);
                if (user != null)
                {
                    SendMail.SendTo(new EmailFormModel
                    {
                        MailTitle         = "Drink Smile - Thay đổi mật khẩu.",
                        MailBody          = string.Format("<p>Nhấn vào đường dẫn sau để đổi mật khẩu:</p><p>{0}</p><p>Không thể đổi mật khẩu bằng đường dẫn này sau 10 phút tính từ khi mail được gởi đi.</p>", string.Format(Configs.CHANGE_FORGOT_PASS_URL, Token.Create(user, DateTime.Now.Ticks.ToString(), 10, false))),
                        FromName          = Configs.MAIL_SENDER_NAME,
                        FromEmail         = Configs.MAIL_SENDER,
                        FromEmailPassword = Configs.MAIL_SENDER_PASSWORD,
                        ToEmail           = request.Email,
                    });
                }
                response.Data = "Kiểm tra email của bạn để lấy lại mật khẩu.";
            }
            catch
            {
                response.Errors.Add("Lỗi hệ thống.");
                response.IsError = true;
            }
            return(Request.CreateResponse(HttpStatusCode.OK, response));
        }
 public HttpResponseMessage Update([FromBody] NhanVien obj)
 {
     try
     {
         return(Request.CreateResponse(HttpStatusCode.OK, NhanVienRepository.GetInstance().Update(obj)));
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
     }
 }
 public HttpResponseMessage GetAll(string id)
 {
     try
     {
         return(Request.CreateResponse(HttpStatusCode.OK, NhanVienRepository.GetInstance().GetFrom(id)));
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
     }
 }
        public HttpResponseMessage Sync([FromBody] SyncRequest request)
        {
            var response = new SyncResponse();

            try
            {
                int result = -1;
                switch (request.SyncType)
                {
                case 1:
                    result = NhanVienRepository.GetInstance().Add(new NhanVien()
                    {
                        MaNV        = request.Id,
                        HoTen       = request.FullName,
                        Email       = request.Email,
                        TenTaiKhoan = request.Username,
                        MatKhau     = request.Password,
                        CapPQ       = request.PermissionLevel,
                        MaCN        = request.AgencyId,
                        MaPQ        = request.PermissionId,
                    });
                    break;

                case 0:
                    result = NhanVienRepository.GetInstance().Update(new NhanVien()
                    {
                        MaNV        = request.Id,
                        HoTen       = request.FullName,
                        Email       = request.Email,
                        TenTaiKhoan = request.Username,
                        MatKhau     = request.Password,
                        CapPQ       = request.PermissionLevel,
                        MaCN        = request.AgencyId,
                        MaPQ        = request.PermissionId,
                    });
                    break;

                case -1:
                    result = NhanVienRepository.GetInstance().Delete(request.Id);
                    break;

                default:
                    response.IsError = true;
                    break;
                }
                response.IsError = result != 1;
            }
            catch
            {
                response.IsError = true;
            }
            return(Request.CreateResponse(HttpStatusCode.NoContent, response));
        }
        public HttpResponseMessage ChangeRole([FromBody] ChangeRoleRequest request)
        {
            var response = new ChangeRoleResponse();

            try
            {
                var token = Token.Get(request.Token);
                if (token == null)
                {
                    response.IsTokenTimeout = true;
                }
                else
                {
                    if (AccountBUS.CheckRole3(token.MaNV))
                    {
                        response.Data = true;
                        if (request.IsUp)
                        {
                            var temp = NhanVienRepository.GetInstance().GetUserById(token.MaNV);
                            if (temp != null)
                            {
                                token.CapPQ = 3;
                                token.MaCN  = temp.MaCN;
                            }
                            else
                            {
                                response.IsError = true;
                            }
                        }
                        else
                        {
                            token.CapPQ = 2;
                            token.MaCN  = request.AgencyId;
                        }
                    }
                }
            }
            catch
            {
                response.IsError = true;
            }
            return(Request.CreateResponse(HttpStatusCode.OK, response));
        }
        public HttpResponseMessage Change([FromBody] CPassRequest request)
        {
            var response = new CPassResponse();

            if (BusinessHandler.AccountBUS.CPassValidate(request, ref response))
            {
                try
                {
                    var tokenValue = Token.Get(request.Token) as NhanVien;
                    if (tokenValue == null)
                    {
                        response.IsTokenTimeout = true;
                    }
                    else
                    {
                        var oldp   = Security.Encrypt(request.OldPassword);
                        var newp   = Security.Encrypt(request.NewPassword);
                        var result = NhanVienRepository.GetInstance().ChangePass(tokenValue.MaNV, oldp, newp);
                        if (result == 1)
                        {
                            BusinessHandler.AccountBUS.SyncPassword2ManagementServiceAsync(new ChangePasswordRequest
                            {
                                UserId  = tokenValue.MaNV,
                                NewPass = newp
                            });
                            response.Data = "Thay đổi mật khẩu thành công.";
                        }
                        else
                        {
                            response.Errors.Add("Thay đổi mật khẩu thất bại.");
                            response.IsError = true;
                        }
                    }
                }
                catch
                {
                    response.Errors.Add("Lỗi hệ thống.");
                    response.IsError = true;
                }
            }
            return(Request.CreateResponse(HttpStatusCode.OK, response));
        }