/// <summary>
        /// 用户从角色移除
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="roleName">角色名</param>
        /// <param name="permissionItemCode">权限编号</param>
        /// <returns>主键</returns>
        public int RemoveUserFromRole(BaseUserInfo userInfo, string userName, string roleName)
        {
            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
            LogOnService.UserIsLogOn(userInfo);
            #endif

            int returnValue = 0;
            using (IDbHelper dbHelper = DbHelperFactory.GetHelper(BaseSystemInfo.UserCenterDbType))
            {
                try
                {
                    dbHelper.Open(UserCenterDbConnection);
                    BaseUserManager userManager = new BaseUserManager(dbHelper, userInfo);
                    string          userId      = userManager.GetId(new KeyValuePair <string, object>(BaseUserEntity.FieldUserName, userName));
                    BaseRoleManager roleManager = new BaseRoleManager(dbHelper, userInfo);
                    string          roleId      = roleManager.GetId(new KeyValuePair <string, object>(BaseRoleEntity.FieldRealName, roleName));
                    if (!String.IsNullOrEmpty(userId) && !String.IsNullOrEmpty(roleId))
                    {
                        returnValue = userManager.RemoveFormRole(userId, roleId);
                    }
                }
                catch (Exception ex)
                {
                    BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbHelper.Close();
                }
            }

            return(returnValue);
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="role">角色</param>
        /// <returns>影响行数</returns>
        public int DeleteRole(BaseUserInfo userInfo, string role)
        {
            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
            LogOnService.UserIsLogOn(userInfo);
            #endif

            int returnValue = 0;
            using (IDbHelper dbHelper = DbHelperFactory.GetHelper(BaseSystemInfo.UserCenterDbType))
            {
                try
                {
                    dbHelper.Open(UserCenterDbConnection);
                    BaseRoleManager roleManager = new BaseRoleManager(dbHelper, userInfo);
                    string          id          = roleManager.GetId(new KeyValuePair <string, object>(BaseRoleEntity.FieldRealName, role));
                    if (!String.IsNullOrEmpty(id))
                    {
                        // 在删除时,可能会把相关的其他配置角色会删除掉,所以需要调用这个方法。
                        returnValue = roleManager.Delete(id);
                    }
                }
                catch (Exception ex)
                {
                    BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbHelper.Close();
                }
            }

            return(returnValue);
        }
        /// <summary>
        /// 用户是否在某个角色中
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="roleCode">角色编号</param>
        /// <returns>存在</returns>
        public bool IsInRoleByCode(string userId, string code)
        {
            bool returnValue = false;

            if (string.IsNullOrEmpty(code))
            {
                return(false);
            }

            string tableName = BaseRoleEntity.TableName;

            if (!string.IsNullOrEmpty(BaseSystemInfo.SystemCode))
            {
                tableName = BaseSystemInfo.SystemCode + "Role";
            }
            BaseRoleManager roleManager = new BaseRoleManager(this.DbHelper, this.UserInfo, tableName);
            string          roleId      = roleManager.GetId(
                new KeyValuePair <string, object>(BaseRoleEntity.FieldDeletionStateCode, 0)
                , new KeyValuePair <string, object>(BaseRoleEntity.FieldCode, code));

            if (string.IsNullOrEmpty(roleId))
            {
                return(false);
            }
            string[] roleIds = GetAllRoleIds(userId);
            returnValue = StringUtil.Exists(roleIds, roleId);
            return(returnValue);
        }
示例#4
0
        /// <summary>
        /// 获取有岗位的组织机构主键
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="dutyName">岗位名称</param>
        /// <returns>主键数组</returns>
        public string[] GetOrganizeIdsByDutyName(string userId = null, string dutyName = "部门主管")
        {
            // 这里需要一个转换的过程,先找到系统角色里这个角色是什么编号
            string          dutyCode    = "Manager";
            BaseRoleManager roleManager = new BaseRoleManager(this.UserInfo);
            List <KeyValuePair <string, object> > parameters = new List <KeyValuePair <string, object> >();

            parameters.Add(new KeyValuePair <string, object>(BaseRoleEntity.FieldRealName, dutyName));
            parameters.Add(new KeyValuePair <string, object>(BaseRoleEntity.FieldCategoryCode, "SystemRole"));
            parameters.Add(new KeyValuePair <string, object>(BaseRoleEntity.FieldEnabled, 1));
            parameters.Add(new KeyValuePair <string, object>(BaseRoleEntity.FieldDeletionStateCode, 0));
            roleManager.GetId(parameters);
            // 这里需要返回公司的主键数组
            return(GetOrganizeIdsByDuty(userId, dutyCode));
        }
        /// <summary>
        /// 用户是否在某个角色中
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="realName">角色</param>
        /// <returns>存在</returns>
        public bool IsInRole(string userId, string realName)
        {
            bool returnValue = false;

            if (string.IsNullOrEmpty(realName))
            {
                return(false);
            }
            BaseRoleManager roleManager = new BaseRoleManager(this.DbHelper, this.UserInfo);
            string          roleId      = roleManager.GetId(new KeyValuePair <string, object>(BaseRoleEntity.FieldDeletionStateCode, 0)
                                                            , new KeyValuePair <string, object>(BaseRoleEntity.FieldRealName, realName));

            if (string.IsNullOrEmpty(roleId))
            {
                return(false);
            }
            string[] roleIds = GetAllRoleIds(userId);
            returnValue = StringUtil.Exists(roleIds, roleId);
            return(returnValue);
        }
        /// <summary>
        /// 给角色权限
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="roleName">角色名</param>
        /// <param name="permissionItemCode">权限编号</param>
        /// <returns>主键</returns>
        public string GrantRolePermission(BaseUserInfo userInfo, string roleName, string permissionItemCode)
        {
            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
            LogOnService.UserIsLogOn(userInfo);
            #endif

            string returnValue = string.Empty;
            using (IDbHelper dbHelper = DbHelperFactory.GetHelper(BaseSystemInfo.UserCenterDbType))
            {
                try
                {
                    dbHelper.Open(UserCenterDbConnection);
                    BaseRoleManager           roleManager           = new BaseRoleManager(dbHelper, userInfo);
                    string                    roleId                = roleManager.GetId(new KeyValuePair <string, object>(BaseRoleEntity.FieldRealName, roleName));
                    BasePermissionItemManager permissionItemManager = new BasePermissionItemManager(dbHelper, userInfo);
                    string                    permissionItemId      = permissionItemManager.GetId(new KeyValuePair <string, object>(BasePermissionItemEntity.FieldCode, permissionItemCode));
                    if (!String.IsNullOrEmpty(roleId) && !String.IsNullOrEmpty(permissionItemId))
                    {
                        BaseRolePermissionManager rolePermissionManager = new BaseRolePermissionManager(dbHelper, userInfo);
                        returnValue = rolePermissionManager.Grant(roleId, permissionItemId);
                    }
                }
                catch (Exception ex)
                {
                    BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbHelper.Close();
                }
            }

            return(returnValue);
        }