zoukankan      html  css  js  c++  java
  • 通用权限管理系统如何进行角色判断

    面对多个子系统,每个子系统中设置的角色都不一样,如何判断某个用户具有某个角色呢?通用权限系统中提供了一个方法,下面是使用角色接口来实现的判断。

            #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 IsInRole(BaseUserInfo userInfo, string roleName)
            {
                //需要访问数据库的
                //BaseUserManager userManager = new BaseUserManager(userInfo);
                //return userManager.IsInRole(userInfo, 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

    调用方法就很简单了,IsInRole(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 ";
    
        }

    此时,角色判断调用就是这样了:IsInRole(userinfo,RoleCode.Tester)。 系统测试人员的判断,

    由于是在子系统调用角色信息,所以可能无法连接数据库,就没有使用通用权限提供的角色判断方法,而是使用了其提供的接口。

    在通用权限底层中判断角色的方法如下图

    调用的时候就是

                BaseUserManager userManager = new BaseUserManager(userInfo);
                bool result=userManager.IsInRole(userInfo, roleName);

    通用权限管理系统 访问地址:http://www.cnblogs.com/jirigala/

  • 相关阅读:
    暑假集训单切赛第二场 UVA 10982 Troublemakers
    暑假集训单切赛第一场 POJ 2309 BST(找规律的题)
    暑假集训单切赛第一场 CF 191A Dynasty Puzzles
    暑假集训单切赛第一场 CF 266E More Queries to Array(线段树+二项式展开式)
    暑假集训单切赛第一场 UVA 1737 Mnemonics and Palindromes 3
    大一暑假集训第六周第一场单切赛
    POJ 1486 Sorting Slides(寻找必须边)
    【机器学习】梯度下降法的相关介绍
    Linux下使用Tmux提高终端环境下的效率
    Fedora23安装以后要做的优化配置
  • 原文地址:https://www.cnblogs.com/hnsongbiao/p/4153000.html
Copyright © 2011-2022 走看看