예제 #1
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);
     }
 }
        //
        // 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");
        }