//
        // 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 int add(NhanVien obj)
 {
     //hash password first
     obj.matkhau = TextLibrary.GetSHA1HashData(obj.matkhau);
     //call add
     this._db.ds_nhanvien.Add(obj);
     //commit
     this._db.SaveChanges();
     //return ma moi nhat
     return this._db.ds_nhanvien.Max(x => x.id);
 }
 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");
 }
Ejemplo n.º 4
0
 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);
     }
 }
        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");
        }
 public List<String> validate(NhanVien obj, String matkhau = "", String matkhau2 = "")
 {
     //
     List<String> re = new List<string>();
     //check
     if (!this.can_use_tendangnhap(obj.id, obj.tendangnhap))
     {
         re.Add("tendangnhap_exist_fail");
     }
     if (!this.can_use_email(obj.id, obj.email))
     {
         re.Add("email_exist_fail");
     }
     if (obj.email.Equals("") || !ValidateLibrary.is_valid_email(obj.email))
     {
         re.Add("email_fail");
     }
     if (obj.tendangnhap.Equals(""))
     {
         re.Add("tendangnhap_fail");
     }
     if (obj.tendaydu.Equals(""))
     {
         re.Add("tendaydu_fail");
     }
     if (!matkhau.Equals(matkhau2))
     {
         re.Add("matkhau_fail");
     }
     return re;
 }
Ejemplo n.º 7
0
 public WebController()
 {
     this._nhanvien = null;
 }