/// <summary> /// 设置服务路由。 /// </summary> /// <param name="routes">服务路由集合。</param> /// <returns>一个任务。</returns> protected override async Task SetRoutesAsync(IEnumerable <ServiceRouteDescriptor> routes) { using (var locker = await _lockerProvider.CreateLockAsync($"set_routes_async")) { await locker.Lock(async() => { var zooKeeperClients = await _zookeeperClientProvider.GetZooKeeperClients(); foreach (var zooKeeperClient in zooKeeperClients) { _logger.LogInformation($"准备向服务注册中心{zooKeeperClient.Options.ConnectionString}注册路由信息"); await CreateSubdirectory(zooKeeperClient, _configInfo.RoutePath); var path = _configInfo.RoutePath; if (!path.EndsWith("/")) { path += "/"; } routes = routes.ToArray(); var registerCount = 0; foreach (var serviceRoute in routes) { if (await SetRouteAsync(serviceRoute, zooKeeperClient)) { registerCount++; } } _logger.LogInformation($"成功向服务注册中心注册{registerCount}个服务路由"); } }); } }
/// <summary> /// 设置mqtt服务路由。 /// </summary> /// <param name="routes">服务路由集合。</param> /// <returns>一个任务。</returns> protected override async Task SetRoutesAsync(IEnumerable <MqttServiceDescriptor> routes) { if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation("准备添加mqtt服务路由。"); } var zooKeeperClients = await _zookeeperClientProvider.GetZooKeeperClients(); foreach (var zooKeeperClient in zooKeeperClients) { await CreateSubdirectory(zooKeeperClient, _configInfo.MqttRoutePath); var path = _configInfo.MqttRoutePath; routes = routes.ToArray(); foreach (var serviceRoute in routes) { var nodePath = $"{path}{serviceRoute.MqttDescriptor.Topic}"; var nodeData = _serializer.Serialize(serviceRoute); using (var locker = await _lockerProvider.CreateLockAsync(nodePath)) { await locker.Lock(async() => { if (!nodeWatchers.ContainsKey(nodePath)) { var nodeWathcher = nodeWatchers.GetOrAdd(nodePath, f => new NodeMonitorWatcher(path, async(oldData, newData) => await NodeChange(oldData, newData))); await zooKeeperClient.SubscribeDataChange(nodePath, nodeWathcher.HandleNodeDataChange); } if (!await zooKeeperClient.ExistsAsync(nodePath)) { if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"节点:{nodePath}不存在将进行创建。"); } await zooKeeperClient.CreateAsync(nodePath, nodeData, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } else { if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"将更新节点:{nodePath}的数据。"); } var onlineData = (await zooKeeperClient.GetDataAsync(nodePath)).ToArray(); if (!DataEquals(nodeData, onlineData)) { await zooKeeperClient.SetDataAsync(nodePath, nodeData); } } }); } } if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation("mqtt服务路由添加成功。"); } } }
public async Task <long> Create(CreateUserInput input, long?tenanId = null) { var userInfo = await CheckUserInfo(input, tenanId); userInfo.Password = _passwordHelper.EncryptPassword(userInfo.UserName, userInfo.Password); using (var locker = await _lockerProvider.CreateLockAsync("CreateUser")) { return(await locker.Lock(async() => { await UnitOfWorkAsync(async(conn, trans) => { var userId = await _userRepository.InsertAndGetIdAsync(userInfo, conn, trans); foreach (var roleId in input.RoleIds) { var role = await _roleRepository.SingleOrDefaultAsync(p => p.Id == roleId, conn, trans); if (role == null) { throw new BusinessException($"系统中不存在Id为{roleId}的角色信息"); } await _userRoleRepository.InsertAsync(new UserRole { UserId = userId, RoleId = roleId, TenantId = userInfo.TenantId }, conn, trans); } foreach (var userGroupId in input.UserGroupIds) { var userGroup = await _userGroupRepository.SingleOrDefaultAsync(p => p.Id == userGroupId); if (userGroup == null) { throw new BusinessException($"系统中不存在Id为{userGroupId}的用户组信息"); } await _userUserGroupRelationRepository.InsertAsync( new UserUserGroupRelation { UserId = userId, UserGroupId = userGroupId, TenantId = userInfo.TenantId }, conn, trans); } }, Connection); return userInfo.Id; })); } }
public async Task <long> Create(CreateRoleInput input, long?tenantId = null) { using (var locker = await _lockerProvider.CreateLockAsync("CreateRole")) { return(await locker.Lock(async() => { var exsitRole = await _roleRepository.FirstOrDefaultAsync(p => p.Identification == input.Identification, false); if (exsitRole != null) { throw new BusinessException($"系统中已经存在{input.Identification}的角色"); } CheckUserDefinedDataPermission(input.DataPermissionType, input.DataPermissionOrgIds); var role = input.MapTo <Role>(); role.TenantId = _session.TenantId; if (tenantId.HasValue) { role.TenantId = tenantId.Value; } await UnitOfWorkAsync(async(conn, trans) => { var roleId = await _roleRepository.InsertAndGetIdAsync(role, conn, trans); await _rolePermissionRepository.DeleteAsync(p => p.RoleId == role.Id, conn, trans); var insertSql = "INSERT INTO RolePermission(PermissionId,RoleId,CreateTime,CreateBy,TenantId) VALUES(@PermissionId,@RoleId,@CreationTime,@CreatorUserId,@TenantId)"; var rolePermissions = new List <RolePermission>(); foreach (var permissionId in input.PermissionIds) { rolePermissions.Add(new RolePermission { PermissionId = permissionId, RoleId = roleId, CreationTime = DateTime.Now, CreatorUserId = _session.UserId, TenantId = role.TenantId }); } await conn.ExecuteAsync(insertSql, rolePermissions, trans); if (!input.IsAllOrg) { foreach (var orgId in input.OrgIds) { var roleOrg = new RoleOrganization() { RoleId = roleId, OrgId = orgId, TenantId = role.TenantId }; await _roleOrganizationRepository.InsertAsync(roleOrg, conn, trans); } } if (input.DataPermissionType == DataPermissionType.UserDefined) { var insertDataPermissionOrgSql = "INSERT INTO RoleDataPermissionOrgRelation(RoleId,OrgId,CreateTime,CreateBy,TenantId) VALUES(@RoleId,@OrgId,@CreationTime,@CreatorUserId,@TenantId)"; var dataPermissionOrgDatas = new List <RoleDataPermissionOrgRelation>(); foreach (var orgId in input.DataPermissionOrgIds) { dataPermissionOrgDatas.Add(new RoleDataPermissionOrgRelation() { RoleId = roleId, OrgId = orgId, CreationTime = DateTime.Now, CreatorUserId = _session.UserId, TenantId = role.TenantId }); } await conn.ExecuteAsync(insertDataPermissionOrgSql, dataPermissionOrgDatas, trans); } }, Connection); return role.Id; })); } }
public async Task Create(CreateUserInput input) { var userInfo = input.MapTo <UserInfo>(); var departAppServiceProxy = GetService <IDepartmentAppService>(); if (userInfo.OrgId.HasValue) { if (!await departAppServiceProxy.Check(userInfo.OrgId.Value)) { throw new BusinessException($"不存在Id为{userInfo.OrgId}的部门信息"); } } var positionAppServiceProxy = GetService <IPositionAppService>(); if (userInfo.PositionId.HasValue) { if (!await positionAppServiceProxy.CheckExsit(userInfo.PositionId.Value)) { throw new BusinessException($"不存在Id为{userInfo.PositionId}的职位信息"); } } userInfo.Password = _passwordHelper.EncryptPassword(userInfo.UserName, userInfo.Password); using (var locker = await _lockerProvider.CreateLockAsync("CreateUser")) { await locker.Lock(async() => { await UnitOfWorkAsync(async(conn, trans) => { var userId = await _userRepository.InsertAndGetIdAsync(userInfo, conn, trans); foreach (var roleId in input.RoleIds) { var role = await _roleRepository.SingleOrDefaultAsync(p => p.Id == roleId); if (role == null) { throw new BusinessException($"系统中不存在Id为{roleId}的角色信息"); } await _userRoleRepository.InsertAsync(new UserRole { UserId = userId, RoleId = roleId }, conn, trans); } foreach (var userGroupId in input.UserGroupIds) { var userGroup = await _userGroupRepository.SingleOrDefaultAsync(p => p.Id == userGroupId); if (userGroup == null) { throw new BusinessException($"系统中不存在Id为{userGroupId}的用户组信息"); } await _userUserGroupRelationRepository.InsertAsync( new UserUserGroupRelation { UserId = userId, UserGroupId = userGroupId }, conn, trans); } }, Connection); }); } }
protected async Task RegisterRouteWithLockAsync(ServiceRouteDescriptor serviceRouteDescriptor) { using var locker = await _lockerProvider.CreateLockAsync(serviceRouteDescriptor.ServiceDescriptor.Id); await locker.Lock(async() => { await RegisterRouteAsync(serviceRouteDescriptor); }); }
public async Task <CreateMenuOutput> Create(CreateMenuInput input) { var menu = input.MapTo <Menu>(); long menuParentId = 0; var exsitMenu = await _menuRepository.FirstOrDefaultAsync(p => p.Name == input.Name); if (exsitMenu != null) { throw new BusinessException($"系统中已经存在{input.Name}的菜单,请勿重复添加"); } var exsitPermission = await _permissionRepository.FirstOrDefaultAsync(p => p.Name == input.Name); if (exsitPermission != null) { throw new BusinessException($"系统中已经存在{input.Name}的权限,请勿重复添加"); } if (menu.Mold == MenuMold.Top) { var thisLevelMenuCount = await _menuRepository.GetCountAsync(p => p.Mold == MenuMold.Top); menu.Code = (thisLevelMenuCount + 1).ToString().PadRight(HeroConstants.CodeRuleRestrain.CodeCoverBit, HeroConstants.CodeRuleRestrain.CodeCoverSymbol); menu.Level = 1; } else { var parentMenu = await _menuRepository.SingleOrDefaultAsync(p => p.PermissionId == input.ParentPermissionId); if (parentMenu == null) { throw new BusinessException($"不存在PermissionId为{input.ParentPermissionId}的菜单信息"); } menuParentId = parentMenu.Id; var thisLevelMenuCount = await _menuRepository.GetCountAsync(p => p.Mold == MenuMold.SubMenu && p.ParentId == parentMenu.Id); menu.Code = parentMenu.Code + HeroConstants.CodeRuleRestrain.CodeSeparator + (thisLevelMenuCount + 1) .ToString().PadLeft(HeroConstants.CodeRuleRestrain.CodeCoverBit, HeroConstants.CodeRuleRestrain.CodeCoverSymbol); menu.Level = parentMenu.Level + 1; } var permission = input.MapTo <Permission>(); using (var locker = await _lockerProvider.CreateLockAsync("CreateMenu")) { return(await locker.Lock(async() => { await UnitOfWorkAsync(async(conn, trans) => { var permissionId = await _permissionRepository.InsertAndGetIdAsync(permission, conn, trans); menu.PermissionId = permissionId; menu.ParentId = menuParentId; await _menuRepository.InsertAsync(menu, conn, trans); }, Connection); return new CreateMenuOutput { Id = menu.Id, PermissionId = menu.PermissionId, Tips = "新增菜单成功" }; })); } }
public async Task Create(CreateUserGroupInput input) { CheckUserDefinedDataPermission(input.DataPermissionType, input.DataPermissionOrgIds); using (var locker = await _lockerProvider.CreateLockAsync("CreateUserGroup")) { await locker.Lock(async() => { var exsitUserGroup = await _userGroupRepository.FirstOrDefaultAsync(p => p.Identification == input.Identification, false); if (exsitUserGroup != null) { throw new BusinessException($"系统中已经存在{input.Identification}的用户组"); } var userGroup = input.MapTo <UserGroup>(); await UnitOfWorkAsync(async(conn, trans) => { var userGroupId = await _userGroupRepository.InsertAndGetIdAsync(userGroup, conn, trans); foreach (var roleId in input.RoleIds) { await _userGroupRoleRepository.InsertAsync( new UserGroupRole { UserGroupId = userGroupId, RoleId = roleId }, conn, trans); } if (!input.IsAllOrg) { foreach (var orgId in input.OrgIds) { var userGroupOrg = new UserGroupOrganization() { UserGroupId = userGroupId, OrgId = orgId }; await _userGroupOrganizationRepository.InsertAsync(userGroupOrg, conn, trans); } } var insertSql = "INSERT INTO UserGroupPermission(PermissionId,UserGroupId,CreateTime,CreateBy,TenantId) VALUES(@PermissionId,@UserGroupId,@CreationTime,@CreatorUserId,@TenantId)"; var userGroupPermissions = new List <UserGroupPermission>(); foreach (var permissionId in input.PermissionIds) { userGroupPermissions.Add(new UserGroupPermission { PermissionId = permissionId, UserGroupId = userGroupId, CreationTime = DateTime.Now, CreatorUserId = _session.UserId, TenantId = _session.TenantId }); } await conn.ExecuteAsync(insertSql, userGroupPermissions, trans); if (input.DataPermissionType == DataPermissionType.UserDefined) { var insertDataPermissionOrgSql = "INSERT INTO UserGroupDataPermissionOrgRelation(UserGroupId,OrgId,CreateTime,CreateBy,TenantId) VALUES(@UserGroupId,@OrgId,@CreationTime,@CreatorUserId,@TenantId)"; var dataPermissionOrgDatas = new List <UserGroupDataPermissionOrgRelation>(); foreach (var orgId in input.DataPermissionOrgIds) { dataPermissionOrgDatas.Add(new UserGroupDataPermissionOrgRelation() { UserGroupId = userGroupId, OrgId = orgId, CreationTime = DateTime.Now, CreatorUserId = _session.UserId, TenantId = _session.TenantId }); } await conn.ExecuteAsync(insertDataPermissionOrgSql, dataPermissionOrgDatas, trans); } }, Connection); }); } }
public async Task InitActions(ICollection <InitActionActionInput> actions) { using (var locker = await _lockerProvider.CreateLockAsync("InitActions")) { await locker.Lock(async() => { foreach (var action in actions) { await UnitOfWorkAsync(async(conn, trans) => { try { var actionEntity = await _actionRepository.SingleOrDefaultAsync(p => p.ServiceId == action.ServiceId); if (actionEntity == null) { actionEntity = action.MapTo <Action>(); var actionId = await _actionRepository.InsertAndGetIdAsync(actionEntity, conn, trans); var operationActionRelations = await _operationActionRelationRepository.GetAllAsync( p => p.ServiceId == action.ServiceId, conn, trans); if (operationActionRelations.Any()) { foreach (var operationActionRelation in operationActionRelations) { operationActionRelation.ActionId = actionId; await _operationActionRelationRepository.UpdateAsync( operationActionRelation, conn, trans); } } } else { actionEntity = action.MapTo(actionEntity); await _actionRepository.UpdateAsync(actionEntity, conn, trans); var operationActionRelations = await _operationActionRelationRepository.GetAllAsync(p => p.ServiceId == action.ServiceId); if (operationActionRelations.Any()) { foreach (var operationActionRelation in operationActionRelations) { if (operationActionRelation.ActionId != actionEntity.Id) { operationActionRelation.ActionId = actionEntity.Id; await _operationActionRelationRepository.UpdateAsync( operationActionRelation, conn, trans); } } } } } catch (Exception e) { trans.Rollback(); throw e; } }, Connection); } }); } }
public async Task <CreateOperationOutput> Create(CreateOperationInput input) { var exsitOperation = await _operationRepository.FirstOrDefaultAsync(p => p.Name == input.Name); if (exsitOperation != null) { throw new BusinessException($"系统中已经存在{input.Name}的操作,请勿重复添加"); } var exsitPermission = await _permissionRepository.FirstOrDefaultAsync(p => p.Name == input.Name); if (exsitPermission != null) { throw new BusinessException($"系统中已经存在{input.Name}的权限,请勿重复添加"); } var menu = await _menuRepository.SingleOrDefaultAsync(p => p.PermissionId == input.PermissionId); if (menu == null) { throw new BusinessException($"不存在PermissionId为{input.PermissionId}的菜单信息"); } var operation = input.MapTo <Operation>(); var permission = input.MapTo <Permission>(); var operationCount = await _operationRepository.GetCountAsync(p => p.MenuId == input.PermissionId); operation.Code = menu.Code + HeroConstants.CodeRuleRestrain.CodeSeparator + (operationCount + 1).ToString() .PadLeft(HeroConstants.CodeRuleRestrain.CodeCoverBit, HeroConstants.CodeRuleRestrain.CodeCoverSymbol); operation.Level = menu.Level + 1; using (var locker = await _lockerProvider.CreateLockAsync("CreateOperation")) { return(await locker.Lock(async() => { await UnitOfWorkAsync(async(conn, trans) => { var permissionId = await _permissionRepository.InsertAndGetIdAsync(permission, conn, trans); operation.PermissionId = permissionId; operation.MenuId = menu.Id; var operationId = await _operationRepository.InsertAndGetIdAsync(operation, conn, trans); if (input.ActionIds != null && input.ActionIds.Any()) { foreach (var actionId in input.ActionIds) { var action = await _actionRepository.SingleOrDefaultAsync(p => p.Id == actionId); if (action == null) { throw new BusinessException($"系统中不存在Id为{actionId}的方法"); } if (operation.Mold == OperationMold.Look || operation.Mold == OperationMold.Query) { if (await _operationActionRepository.GetCountAsync(p => p.ServiceId == action.ServiceId, conn, trans) > 1) { throw new BusinessException($"一个查询接口仅被允许分配给一个操作"); } } var operationAction = new OperationActionRelation { ActionId = actionId, OperationId = operationId, ServiceId = action.ServiceId }; await _operationActionRepository.InsertAsync(operationAction, conn, trans); } } }, Connection); return new CreateOperationOutput { Id = operation.Id, PermissionId = operation.PermissionId, Tips = "新增操作成功" }; })); } }