public ActionResult Test_Login()
 {
     //get data from client
     NhanVienController uc = new NhanVienController();
     String username = TextLibrary.ToString(Request["user_username"]);
     String password = TextLibrary.ToString(Request["user_password"]);
     Boolean remember = TextLibrary.ToBoolean(Request["user_remember"]);
     //validate
     //NhanVienController c = new NhanVienController();
     if (uc.login(username,password))
     {
         NhanVien obj = uc.get_by_username(username);
         if (remember)
         {
             //set Cookies
             HttpCookie _tmp = new HttpCookie("nhanvien");
             _tmp["user_id"] = obj.id.ToString();
             _tmp["user_password"] = obj.matkhau;
             _tmp.Expires = DateTime.Now.AddDays(1);
             Response.Cookies.Add(_tmp);
         }
         else
         {
             //set session
             Session["nhanvien"] = obj;
         }
         //redirect
         return RedirectToAction("Index","AdminHome");
     }
     //load view
     List<String> validate= new List<String>();
     validate.Add("fail");
     ViewBag.State = validate;
     return View("Index");
 }
        //
        // GET: /AdminLogin/
        public ActionResult Index()
        {
            if (Session["nhanvien"] != null)
            {
                return RedirectToAction("Index", "AdminHome");
            }
            int uid = 0;
            String password = "";
            //lay thong tin tu cookies
            HttpCookie _tmp = Request.Cookies.Get("nhanvien");
            if (_tmp != null)
            {
                uid = TextLibrary.ToInt(_tmp["user_id"].ToString());
                password = TextLibrary.ToString(_tmp["user_password"].ToString());
            }

            //lay thong tin user theo yeu cau dang nhap
            NhanVienController ctr = new NhanVienController();
            this._user = ctr.get_by_id_hash_password(uid, password);
            //nếu đăng nhập roi thì chuyển tới trang đăng nhập
            if (this._user != null)
            {
                return RedirectToAction("Index", "AdminHome");
            }

            //hien thi form login
            ViewBag.State = new List<string>();
            return View();
        }
 public ActionResult Add()
 {
     if (!this._nhanvien_permission.Contains("user_add"))
     {
         return _fail_permission("user_add");
     }
     NhanVienController ctr=new NhanVienController();
     NhanVien nv = new NhanVien();
     nv.loainhanvien = ctr._db.ds_loainhanvien.FirstOrDefault();
     ViewBag.NhanVien = nv;
     ViewBag.Title += " - Add";
     ViewBag.LoaiNhanVien_List = ctr._db.ds_loainhanvien.ToList();
     return View("Index");
 }
 public ActionResult Request_Password_Change(String session)
 {
     Debug.WriteLine("Request change password from session " + session);
     NhanVienController ctr = new NhanVienController();
     NhanVien obj = ctr.timkiem("", "", "","", "", "",session).FirstOrDefault();
     if(obj!=null)
     {
         ViewBag.NhanVien = obj;
         return View();
     }
     else
     {
         return RedirectToAction("Index","Admin");
     }
 }
 public ActionResult Request_Password_Change_Submit()
 {
     int obj_id = TextLibrary.ToInt(Request["nhanvien_id"]);
     String session = TextLibrary.ToString(Request["nhanvien_session"]);
     String new_pass = TextLibrary.ToString(Request["nhanvien_matkhau"]);
     NhanVienController ctr = new NhanVienController();
     if (ctr.set_password_by_session(obj_id, session, new_pass))
     {
         ViewBag.Message = "Mật khẩu đã được khôi phục lại.";
     }
     else
     {
         ViewBag.Message = "Oops. Are you trying to hack my system ?";
     }
     return View();
 }
 public ActionResult Submit()
 {
     //generate session for NhanVien
     NhanVienController ctr=new NhanVienController();
     String email = TextLibrary.ToString(Request["user_email"]);
     String session = "";
     Boolean valid_session = ctr.generate_forgot_password_session(email, out session);
     if (valid_session && ValidateLibrary.is_valid_email(email))
     {
         GMailLibrary gmail = new GMailLibrary();
         gmail.receive_email = email;
         gmail.Generate_ForgotPassword_Html(Url.Action("Request_Password_Change", "AdminForgotPassword", new { session = session }, this.Request.Url.Scheme));
         gmail.Send();
         ViewBag.Message = "Đường link khôi phục mật khẩu đã được gửi vào email.";
     }
     else
     {
         ViewBag.Message = "Không tìm thấy nhân viên nào có email đó cả.";
     }
     return View("Request_Password_Change_Submit");
 }
 protected override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     base.OnActionExecuting(filterContext);
     NhanVienController ctr = new NhanVienController();
     if (Session["nhanvien"] != null)
     {
         this._nhanvien = ctr.get_by_id(((NhanVien)Session["nhanvien"]).id);
     }
     else
     {
         int uid = 0;
         String password = "";
         //lay thong tin tu cookies
         HttpCookie _tmp = Request.Cookies.Get("nhanvien");
         if (_tmp != null)
         {
             uid = TextLibrary.ToInt(_tmp["user_id"].ToString());
             password = TextLibrary.ToString(_tmp["user_password"].ToString());
         }
         //lay thong tin user theo yeu cau dang nhap
         this._nhanvien = ctr.get_by_id_hash_password(uid, password);
     }
 }
        //
        // GET: /AdminUser/
        public ActionResult Index(int id=0)
        {
            if (this._nhanvien.id == id)
            {
                //owner override
            }
            else
            if (!this._nhanvien_permission.Contains("user_view"))
            {
                return _fail_permission("user_view");
            }
            NhanVienController ctr = new NhanVienController();
            NhanVien u = ctr.get_by_id(id);

            if (u == null)
            {
                //user khong ton tai
                return RedirectToAction("Index", "AdminUsers");
            }
            ViewBag.NhanVien = u;
            ViewBag.Title += " - View";
            ViewBag.LoaiNhanVien_List = ctr._db.ds_loainhanvien.ToList();
            return View();
        }
        public ActionResult Submit()
        {
            //get nv id first
            int obj_id = TextLibrary.ToInt(Request["nhanvien_id"]);
            NhanVienController ctr=new NhanVienController();
            NhanVien obj;
            //check mode
            Boolean edit_mode = true;
            if (obj_id == 0)
            {
                //add mode
                if (!this._nhanvien_permission.Contains("user_add"))
                {
                    return _fail_permission("user_add");
                }

                obj = new NhanVien();
                edit_mode = false;
            }
            else
            {
                if (this._nhanvien.id == obj_id)
                {
                    //owner override
                }
                else
                //edit mode
                if (!this._nhanvien_permission.Contains("user_edit"))
                {
                    return _fail_permission("user_edit");
                }

                if (ctr.is_exist(obj_id))
                {
                    //update model
                    //get instance of record of table
                    obj = ctr.get_by_id(obj_id);
                }
                else
                {
                    //nvid khong ton tai
                    return RedirectToAction("Index","AdminUsers");
                }
            }
            //assign value
            obj.email = TextLibrary.ToString(Request["nhanvien_email"]);
            obj.tendangnhap = TextLibrary.ToString(Request["nhanvien_tendangnhap"]);
            obj.bad = TextLibrary.ToBoolean(Request["nhanvien_bad"]);
            obj.tendaydu = TextLibrary.ToString(Request["nhanvien_tendaydu"]);
            //validate properties
            List<String> validate = ctr.validate(obj,
                TextLibrary.ToString(Request["nhanvien_matkhau"]),
                TextLibrary.ToString(Request["nhanvien_matkhau2"]));
            //xét ràng buộc
                if (edit_mode)
                {
                    if (this._nhanvien.id != obj.id)
                    {
                        //active
                        obj.active = TextLibrary.ToBoolean(Request["nhanvien_active"]);
                        //loainhanvien
                        int lnv_id = TextLibrary.ToInt(Request["nhanvien_loainhanvien_id"]);
                        LoaiNhanVien loai = ctr._db.ds_loainhanvien.Where(x => x.id == lnv_id).FirstOrDefault();
                        obj.loainhanvien = loai;
                        if (obj.loainhanvien == null)
                        {
                            return RedirectToAction("Index", "AdminUsers");
                        }
                    }
                    else
                    {
                        //bản thân không thể tự thay đổi active hoặc nhóm người dùng
                        if (obj.active != TextLibrary.ToBoolean(Request["nhanvien_active"]))
                        {
                            validate.Add("self_active_edit_fail");
                        }
                        if (obj.loainhanvien.id != TextLibrary.ToInt(Request["nhanvien_loainhanvien_id"]))
                        {
                            validate.Add("self_loainguoidung_edit_fail");
                        }
                    }

                }
                else
                {
                    //add mode
                    obj.active = TextLibrary.ToBoolean(Request["nhanvien_active"]);
                    //loainhanvien
                    int lnv_id = TextLibrary.ToInt(Request["nhanvien_loainhanvien_id"]);
                    LoaiNhanVien loai = ctr._db.ds_loainhanvien.Where(x => x.id == lnv_id).FirstOrDefault();
                    obj.loainhanvien = loai;
                    if (obj.loainhanvien == null)
                    {
                        return RedirectToAction("Index", "AdminUsers");
                    }
                }

            //action
            if (validate.Count==0)
            {
                if (edit_mode)
                {
                    //update properties first
                    ctr._db.SaveChanges();
                    //call set password
                    ctr.set_password(obj.id, TextLibrary.ToString(Request["nhanvien_matkhau"]));
                    this._state.Add("edit_ok");
                }
                else
                {
                    //hash password before add
                    obj.matkhau = TextLibrary.ToString( Request["nhanvien_matkhau"] );
                    //call add
                    int maxid = ctr.add(obj);
                    //re assign id
                    obj.id = maxid;
                    this._state.Add("add_ok");
                }
            }
            this._state.AddRange(validate);
            ViewBag.State = this._state;
            ViewBag.NhanVien = obj;
            ViewBag.Title += " - Submit";
            ViewBag.LoaiNhanVien_List = ctr._db.ds_loainhanvien.ToList();
            return View("Index");
        }
 //
 // GET: /AdminUsers/
 public ActionResult Index()
 {
     //check
     if (!this._nhanvien_permission.Contains("user_view"))
     {
         return this._fail_permission("user_view");
     }
     NhanVienController ctr = new NhanVienController();
     //Chọn danh sách nhân viên để hiển thị theo cookies tìm kiếm
     ViewBag.User_List = ctr.timkiem(
         timkiem_nhanvien["id"],
         timkiem_nhanvien["tendangnhap"],
         timkiem_nhanvien["tendaydu"],
         timkiem_nhanvien["email"],
         timkiem_nhanvien["active"],
         timkiem_nhanvien["loainhanvien_id"]);
     //set search cookies
     ViewBag.User_Search = this.timkiem_nhanvien;
     ViewBag.LoaiNhanVien_List = ctr._db.ds_loainhanvien.ToList();
     //return View(this._db.Users.ToList());
     //this._build_common_data();
     ViewBag.Title += " - Management";
     return View();
 }