/// <summary> /// 加入角色 /// 日期:2017年8月19日 /// </summary> /// <param name="userId"></param> /// <param name="roletype"></param> /// <returns></returns> public bool JoinRole(string userId, EnumRoleType roletype) { if (string.IsNullOrEmpty(userId)) { return(false); } using (var db = new DBEntities()) { var ret = false; var cacheKey = new XuHos.Common.Cache.Keys.EntityListCacheKey <EnumRoleType>(StringCacheKeyType.User_RoleList, userId); var rolem = (from roleMap in db.UserRoleMaps join role in db.UserRoles on roleMap.RoleID equals role.RoleID where roleMap.UserID == userId && role.RoleType == roletype select roleMap).FirstOrDefault(); //已经存在 if (rolem != null) { #region 修改 if (rolem.IsDeleted) { rolem.IsDeleted = false; ret = db.SaveChanges() > 0; } else { ret = true; } #endregion } else { #region 添加记录 var roleid = (from role in db.UserRoles where role.RoleType == roletype select role.RoleID).FirstOrDefault(); if (!string.IsNullOrEmpty(roleid)) { rolem = new Entity.UserRoleMap(); rolem.UserRoleMapID = Guid.NewGuid().ToString("N"); rolem.RoleID = roleid; rolem.UserID = userId; db.UserRoleMaps.Add(rolem); ret = db.SaveChanges() > 0; } #endregion } #region 除缓存 if (ret) { cacheKey.RemoveCache(); } #endregion return(ret); } }
/// <summary> /// 总店选择框(后台用) /// </summary> /// <returns></returns> public List <ResponseTextAndValue> GetRoleList(EnumRoleType RoleType) { using (var db = new DBEntities()) { var query = from item in db.UserRoles orderby item.ModifyTime descending where item.RoleType == RoleType && item.IsDeleted == false select new ResponseTextAndValue() { Text = item.RoleName, Value = item.RoleID }; return(query.ToList()); } }
/// <summary> /// 是否有角色权限 /// </summary> /// <param name="userId"></param> /// <param name="roletype"></param> /// <returns></returns> public bool HasRolePermission(string userId, EnumRoleType roletype) { var roles = GetUserRoles(userId); return(roles.Any(a => a == roletype)); }