Ejemplo n.º 1
0
        public override void OnActionExecuting(ActionExecutingContext filter)
        {
            string ActionName     = filter.RouteData.Values["action"].ToString().ToLower();
            string ControllerName = filter.RouteData.Values["controller"].ToString().ToLower();
            string AreaName       = filter.RouteData.DataTokens["area"] != null ? filter.RouteData.DataTokens["area"].ToString().ToLower() : "";
            string MethodName     = filter.HttpContext.Request.HttpMethod;

            // GET AREAS
            string Areas = "";

            string[] arrT = filter.Controller.GetType().ToString().Split('.');
            if (arrT.Length >= 3) //DreamCMS.Areas.Admin.Controllers. ...
            {
                if (arrT[1] == "Areas")
                {
                    Areas = arrT[2];
                }
            }

            HttpCookie getCookie = HttpContext.Current.Request.Cookies[DDefault.NameCookieRemember];

            if (getCookie != null)
            {
                if (Auth.GetCookie("DUser") == null)
                {
                    Auth.SetCookie("DUser", DHash.Decrypt(getCookie.Value));
                }
                if (filter.HttpContext.Session["DUserName"] == null)
                {
                    ReloadInfoUser();
                }
            }
            string DUser = Auth.GetCookie("DUser");

            /// Kiểm tra các lỗi xảy ra
            /// Nếu không đúng phân vùng Area: Thông báo lỗi 404
            if (Areas != "" && AreaName != Areas.ToLower())
            {
                filter.HttpContext.Session["ErrorHttp"] = filter.HttpContext.Request.RawUrl;
                filter.Result = new RedirectResult(DDefault.Path404);
                return;
            }

            /// Nếu chưa đăng nhập
            /// Chuyển về trang đăng nhập
            if (DUser == null)
            {
                filter.HttpContext.Session["returnUrl"] = filter.HttpContext.Request.RawUrl;
                filter.Result = new RedirectResult(DDefault.PathLogin);
                return;
            }

            /// Đặc quyền cho SUPERADMIN
            if (DUser == DDefault.SAdminID)
            {
                filter.HttpContext.Session["ErrorHttp"] = "";
                filter.HttpContext.Session["returnUrl"] = "";
                if (!IsNotLayout)
                {
                    IsNotLayout = false;
                    LoadBreadcrumb(filter);
                    LoadMenuLeft(filter);
                }
                base.OnActionExecuting(filter);
                return;
            }

            DBAdmin       db            = new DBAdmin();
            int?          GroupId       = int.Parse(Auth.GetCookie("DGroupId")) as int?;
            tbMenuInGroup tbMenuInGroup = new tbMenuInGroup(db.tbMenus.Where(p => p.IsDisable == false && p.Controller.ToLower() == ControllerName && p.Action.ToLower() == ActionName).FirstOrDefault(), GroupId.Value);

            if (tbMenuInGroup.IsIn)
            {
                /// Trường hợp thỏa các điều kiện
                filter.HttpContext.Session["ErrorHttp"] = "";
                filter.HttpContext.Session["returnUrl"] = "";
                if (!IsNotLayout)
                {
                    IsNotLayout = false;
                    LoadBreadcrumb(filter);
                    LoadMenuLeft(filter);
                }
                base.OnActionExecuting(filter);
                return;
            }
            else
            {
                /// Nếu tài khoản không đủ quyền
                /// Chuyển về trang thông báo quyền truy cập hoặc 404
                HttpContext.Current.Session["ErrorHttp"] = filter.HttpContext.Request.RawUrl;
                filter.Result = new RedirectResult(DDefault.PathAccessDenied);
                return;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Hàm check quyền của control Admin
        /// </summary>
        /// <param name="ct">Control hiện tại (this)</param>
        /// <returns>int</returns>
        public static int IsUse(Controller ct, string area = "")
        {
            string ActionName     = ct.ControllerContext.RouteData.Values["action"].ToString().ToLower();
            string ControllerName = ct.ControllerContext.RouteData.Values["controller"].ToString().ToLower();
            string AreaName       = ct.ControllerContext.RouteData.DataTokens["area"] != null ? ct.ControllerContext.RouteData.DataTokens["area"].ToString().ToLower() : "";
            string MethodName     = ct.ControllerContext.HttpContext.Request.HttpMethod;
            //string Ip = IPAddress;

            HttpCookie getCookie = HttpContext.Current.Request.Cookies[DDefault.NameCookieRemember];

            if (getCookie != null)
            {
                if (GetCookie("DUser") == null)
                {
                    SetCookie("DUser", DHash.Decrypt(getCookie.Value));
                    if (HttpContext.Current.Session["DUserName"] == null)
                    {
                        ReloadInfoUser();
                    }
                }
            }
            string DUser = GetCookie("DUser");

            /// Kiểm tra các lỗi xảy ra
            /// Nếu không đúng phân vùng Area: Thông báo lỗi 404
            if (area != "" && AreaName != area.ToLower())
            {
                HttpContext.Current.Session["ErrorHttp"] = ct.Request.RawUrl;
                _Auth = -1;
                return(-1);
            }

            /// Nếu chưa đăng nhập
            /// Chuyển về trang đăng nhập
            if (DUser == null)
            {
                HttpContext.Current.Session["returnUrl"] = ct.Request.RawUrl;
                _Auth = -2;
                return(-2);
            }

            /// Đặc quyền cho SUPERADMIN
            if (DUser == DDefault.SAdminID)
            {
                HttpContext.Current.Session["ErrorHttp"] = "";
                HttpContext.Current.Session["returnUrl"] = "";
                _Auth = 1;
                LoadBreadcrumb(ct);
                LoadMenuLeft(ct);
                return(1);
            }

            DBAdmin       db            = new DBAdmin();
            int?          GroupId       = int.Parse(GetCookie("DGroupId")) as int?;
            tbMenuInGroup tbMenuInGroup = new tbMenuInGroup(db.tbMenus.Where(p => p.IsDisable == false && p.Controller.ToLower() == ControllerName && p.Action.ToLower() == ActionName).FirstOrDefault(), GroupId.Value);

            if (tbMenuInGroup.IsIn)
            {
                /// Trường hợp thỏa các điều kiện
                HttpContext.Current.Session["ErrorHttp"] = "";
                HttpContext.Current.Session["returnUrl"] = "";
                _Auth = 1;
                LoadBreadcrumb(ct);
                LoadMenuLeft(ct);
                return(1);
            }
            else
            {
                /// Nếu tài khoản không đủ quyền
                /// Chuyển về trang thông báo quyền truy cập hoặc 404
                HttpContext.Current.Session["ErrorHttp"] = ct.Request.RawUrl;
                _Auth = 0;
                return(0);
            }
        }