/// <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; }
/// <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; }