public DmMenuModel(DM_Menu entity, UserInfo user) { // var allowRoles = "," + entity.ListRole + ","; Id = entity.Id; Name = entity.Name; Status = entity.Status; ParentId = entity.ParentId; Loai = entity.Loai; ListRole = entity.ListRole; Code = entity.Code; ControllerName = entity.ControllerName; ListAction = new List <DM_Menu>(); var listActionAll = StaticDataHelper.GetCacheDataMenuRole().Where(x => x.Loai.Equals("ACTION") && x.ParentId == entity.Id).ToList(); foreach (var item in listActionAll) { var allowRoles = "," + item.ListRole + ","; if (user.Roles != null) { if (user.Roles.Any(x => allowRoles.Contains("," + x.Code + ","))) { ListAction.Add(item); } } } }
private bool UserCanDoByToken(string token, string controllerName, string actionName) { controllerName = controllerName.ToLower(); actionName = actionName.ToLower(); var action = StaticDataHelper.GetCacheDataMenuRole() .FirstOrDefault(x => x.Code.ToLower().Equals(actionName) && x.ControllerName.ToLower().Equals(controllerName)); if (action == null) { var log = String.Format("UserCanDoByToken(): Token: {0}, controler {1}, action: {2}", token, controllerName, actionName); // NLog.LogManager.GetCurrentClassLogger().Debug(log); return(true); } else { if (string.IsNullOrEmpty(token)) { return(false); } var userCheck = StaticDataHelper.GetCacheDataUser().FirstOrDefault(x => x.SessionToken == token); //NLog.LogManager.GetCurrentClassLogger().Debug("UserCanDoByToken-userCheck:" + JsonHelper.SerializeObject(userCheck)); if (userCheck != null) { var allowRoles = "," + action.ListRole + ","; // NLog.LogManager.GetCurrentClassLogger().Debug("UserCanDoByToken-allowRoles" + JsonHelper.SerializeObject(allowRoles)); var rs = userCheck.Roles.Any(x => allowRoles.Contains("," + x.Code + ",")); return(rs); } } return(false); }