public override void OnAuthorization(AuthorizationContext filterContext) { controller = filterContext.RouteData.Values["controller"].ToString(); action = filterContext.RouteData.Values["action"].ToString(); if (((Controller)filterContext.Controller).GetNhaThuoc() != null) { nhaThuoc = ((Controller)filterContext.Controller).GetNhaThuoc(); } base.OnAuthorization(filterContext); }
public static void SetNhaThuoc(this Controller controller, NhaThuoc nhaThuoc) { var nhathuocSession = new NhaThuocSessionModel(nhaThuoc); var loggedUser = WebSecurity.GetCurrentUser(); WebSessionManager.Instance.CurrentDrugStoreCode = nhaThuoc.MaNhaThuoc; var dsSession = new DrugStoreSession() { DrugStoreCode = nhaThuoc.MaNhaThuoc, ParentDrugStoreCode = nhaThuoc.MaNhaThuocCha, DrugStoreID = nhaThuoc.ID }; var service = IoC.Container.Resolve <IUtilitiesService>(); dsSession.Settings = service.GetDrugStoreSetting(dsSession.DrugStoreCode); WebSessionManager.Instance.CommonSessionData = dsSession; WebSessionManager.Instance.CurrentUserId = loggedUser.UserId; if (Roles.Provider.IsUserInRole(loggedUser.UserName, Constants.Security.Roles.SuperUser.Value)) { nhathuocSession.Role = Constants.Security.Roles.SuperUser.Value; } else { var nhanVien = nhaThuoc.Nhanviens.FirstOrDefault(e => e.User.UserId == loggedUser.UserId); if (nhanVien == null) { if (nhaThuoc.NhaThuocCha != null) { nhanVien = nhaThuoc.NhaThuocCha.Nhanviens.FirstOrDefault(e => e.User.UserId == loggedUser.UserId); } } if (nhanVien != null) { nhathuocSession.Role = nhanVien.Role; } } controller.Session["nhathuoc"] = JsonConvert.SerializeObject(nhathuocSession); }
public static bool Authorize(string controller, string action, NhaThuocSessionModel nhaThuoc, string[] checkRoles = null) { if (nhaThuoc == null || string.IsNullOrEmpty(nhaThuoc.Role)) { return(false); } if (HttpContext.Current.User.IsInRole(Constants.Security.Roles.SuperUser.Value)) { return(true); } if (nhaThuoc.Role == Constants.Security.Roles.Admin.Value) { return(true); } if (checkRoles != null && checkRoles.Contains(nhaThuoc.Role)) { return(true); } // kiem tra co quyen tren tung trang khong? var uow = new UnitOfWork(); if (controller.ToLower() == "inventory") { controller = "Phieukiemkes"; } var permission = uow.UserPermissionsRespository.Get( e => e.Controller.ToLower() == controller.ToLower() && e.Action.ToLower() == action.ToLower() && e.NhaThuoc.MaNhaThuoc == nhaThuoc.MaNhaThuoc && e.User.UserId == WebSecurity.GetCurrentUserId); if (permission.Any()) { return(true); } return(false); }