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

            string returnValue = string.Empty;
            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.AddToRole(userId, roleId);
                    }
                }
                catch (Exception ex)
                {
                    BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbHelper.Close();
                }
            }

            return(returnValue);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 添加角色(同时添加用户,一个数据库事务里进行处理)
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="entity">实体</param>
        /// <param name="userIds">用户主键数组</param>
        /// <param name="status">状态</param>
        /// <param name="statusMessage">返回状态信息</param>
        /// <returns>主键</returns>
        public string AddWithUser(BaseUserInfo userInfo, BaseRoleEntity entity, string[] userIds, out Status status, out string statusMessage)
        {
            var result = string.Empty;

            var returnCode    = Status.Ok;
            var returnMessage = string.Empty;

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterWriteDbWithTransaction(userInfo, parameter, (dbHelper) =>
            {
                // 这里是判断已经登录的用户是否有调用当前函数的权限,加强服务层被远程调用的安全性的
                var tableName = userInfo.SystemCode + "Role";
                var manager   = new BaseRoleManager(dbHelper, userInfo, tableName);
                result        = manager.Add(entity, out returnCode);
                if (!string.IsNullOrEmpty(result) && userIds != null && userIds.Length > 0)
                {
                    tableName       = userInfo.SystemCode + "UserRole";
                    var userManager = new BaseUserManager(dbHelper, userInfo, tableName);
                    // 小心异常,检查一下参数的有效性
                    if (userIds != null)
                    {
                        userManager.AddToRole(userInfo.SystemCode, userIds, new string[] { result });
                    }
                }
                returnMessage = manager.GetStateMessage(returnCode);
            });
            status        = returnCode;
            statusMessage = returnMessage;

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 用户添加到角色
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="roleId">角色主键</param>
        /// <param name="addUserIds">用户主键</param>
        /// <returns>影响行数</returns>
        public int AddUserToRole(BaseUserInfo userInfo, string roleId, string[] addUserIds)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
            LogOnService.UserIsLogOn(userInfo);
            #endif

            int returnValue = 0;
            using (IDbHelper dbHelper = DbHelperFactory.GetHelper(BaseSystemInfo.UserCenterDbType))
            {
                try
                {
                    dbHelper.Open(UserCenterDbConnection);
                    string tableName = BaseUserRoleEntity.TableName;
                    if (!string.IsNullOrEmpty(BaseSystemInfo.SystemCode))
                    {
                        tableName = BaseSystemInfo.SystemCode + "UserRole";
                    }
                    BaseUserManager userManager = new BaseUserManager(dbHelper, userInfo, tableName);
                    // 小心异常,检查一下参数的有效性
                    if (addUserIds != null)
                    {
                        returnValue += userManager.AddToRole(addUserIds, roleId);
                    }
                    BaseLogManager.Instance.Add(dbHelper, userInfo, serviceName, AppMessage.RoleService_AddUserToRole, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbHelper.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return(returnValue);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 用户添加到角色
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="roleId">角色主键</param>
        /// <param name="addUserIds">用户主键</param>
        /// <returns>影响行数</returns>
        public int AddUserToRole(BaseUserInfo userInfo, string roleId, string[] addUserIds)
        {
            var result = 0;

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterWriteDb(userInfo, parameter, (dbHelper) =>
            {
                var manager = new BaseUserManager(dbHelper, userInfo);
                // 小心异常,检查一下参数的有效性
                if (addUserIds != null)
                {
                    result += manager.AddToRole(userInfo.SystemCode, addUserIds, new string[] { roleId });
                }
            });

            return(result);
        }
        /// <summary>
        /// 用户添加到角色
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userName">用户名</param>
        /// <param name="roleName">角色名</param>
        /// <returns>主键</returns>
        public string AddUserToRole(BaseUserInfo userInfo, string userName, string roleName)
        {
            var result = string.Empty;

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterWriteDb(userInfo, parameter, (dbHelper) =>
            {
                var userManager = new BaseUserManager(dbHelper, userInfo);
                var userId      = userManager.GetId(new KeyValuePair <string, object>(BaseUserEntity.FieldUserName, userName));
                var roleManager = new BaseRoleManager(dbHelper, userInfo);
                var roleId      = roleManager.GetId(new KeyValuePair <string, object>(BaseRoleEntity.FieldName, roleName));
                if (!string.IsNullOrEmpty(userId) && !string.IsNullOrEmpty(roleId))
                {
                    userManager.AddToRole(userInfo.SystemCode, new string[] { userId }, new string[] { roleId });
                }
            });
            return(result);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 批量设置用户的角色
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userId">用户主键</param>
        /// <param name="roleIds">角色数组</param>
        /// <returns>影响的行数</returns>
        public int SetUserRoles(BaseUserInfo userInfo, string userId, string[] roleIds)
        {
            var result = 0;

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterWriteDb(userInfo, parameter, (dbHelper) =>
            {
                var userManager = new BaseUserManager(dbHelper, userInfo);
                // 小心异常,检查一下参数的有效性
                if (roleIds != null)
                {
                    userManager.ClearRole(userInfo.SystemCode, userId);
                    result += userManager.AddToRole(userInfo.SystemCode, new string[] { userId.ToString() }, roleIds);
                }
            });

            return(result);
        }