标签:des style blog http io ar color os 使用
面对多个子系统,每个子系统中设置的角色都不一样,如何判断某个用户具有某个角色呢?通用权限系统中提供了一个方法,下面是使用角色接口来实现的判断。
#region public static List<BaseRoleEntity> GetCacheRoleList(BaseUserInfo userInfo, bool refreshFlag = false) 获取用户角色 缓存 /// <summary> /// 获取用户角色 缓存 /// </summary> /// <param name="userInfo"></param> /// <param name="refreshFlag"></param> /// <returns></returns> public static List<BaseRoleEntity> GetCacheRoleList(BaseUserInfo userInfo, bool refreshFlag = false) { string cacheKey = "GetRoleList" + userInfo.Id; if (refreshFlag) { DataCacheHelper.RemoveCache(cacheKey); } if (null == DataCacheHelper.GetCache(cacheKey)) { lock (objLock) { if (null == DataCacheHelper.GetCache(cacheKey)) { try { WebClient webClient = new WebClient(); NameValueCollection postValues = new NameValueCollection(); postValues.Add("Function", "GetUserRoleList"); postValues.Add("UserInfo", userInfo.Serialize());//BaseSystemInfo.UserInfo.Serialize() postValues.Add("userId", userInfo.Id);//BaseSystemInfo.UserInfo.Serialize() // 向服务器发送POST数据 byte[] responseArray = webClient.UploadValues(userRoleServiceURL, postValues); string response = Encoding.UTF8.GetString(responseArray); List<BaseRoleEntity> list; if (!string.IsNullOrEmpty(response)) { list = javaScriptSerializer.Deserialize<List<BaseRoleEntity>>(response); DataCacheHelper.SetCache(cacheKey, list, null, DateTime.Now.AddMinutes(120), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null); } } catch (Exception ex) { LogHelper.WriteSmtp(userInfo.UserName + "获取用户角色时出错", ex); LogHelper.OracleWarn(userInfo, "获取用户角色时出错", userInfo.UserName + "获取用户角色时出错", "GetCacheRoleList(BaseUserInfo userInfo, bool refreshFlag = false)", typeof(UserHelp), ex); } } } } return DataCacheHelper.GetCache(cacheKey) as List<BaseRoleEntity>; } #endregion #region public static bool HasRole(BaseUserInfo userInfo, string roleName) 判断某个用户是否有某个角色 /// <summary> /// 判断某个用户是否有某个角色 /// </summary> /// <param name="userInfo"></param> /// <param name="roleName"></param> /// <returns></returns> public static bool HasRole(BaseUserInfo userInfo, string roleName) { bool hasRole = false; List<BaseRoleEntity> roleEntits = GetCacheRoleList(userInfo); for (int i = 0; i < roleEntits.Count; i++) { if (roleEntits[i].RealName == roleName) { hasRole = true; break; } } return hasRole; } #endregion
调用方法就很简单了,HasRole(userinfo,"角色名字")。
另外,为了方便调用,建议每个子系统中定义好角色常量,角色常量值与在管理系统中配置的保持一样
public class RoleCode { /// <summary> /// 系统管理员 /// </summary> public const string Admin = "Admin"; /// <summary> /// 系统测试人员 /// </summary> public const string Tester = "Tester"; /// <summary> /// 业务主管 /// </summary> public const string BusinessAdmin = "BusinessAdmin "; }
此时,角色判断调用就是这样了:HasRole(userinfo,RoleCode.Tester)。 系统测试人员的判断
通用权限管理系统 访问地址:http://www.cnblogs.com/jirigala/
标签:des style blog http io ar color os 使用
原文地址:http://www.cnblogs.com/hnsongbiao/p/4153000.html