Beispiel #1
0
        public async Task HandleEventAsync(CreateEventData eventData)
        {
            using (var unitOfWork = UnitOfWorkManager.Begin())
            {
                // 订阅租户新增事件,置入管理员角色所有权限
                using (CurrentTenant.Change(eventData.Id, eventData.Name))
                {
                    var definitionPermissions = PermissionDefinitionManager.GetPermissions();
                    var grantPermissions      = definitionPermissions
                                                .Where(p => p.MultiTenancySide.HasFlag(MultiTenancySides.Tenant))
                                                .Select(p => p.Name).ToArray();
                    //var grantPermissions = new List<PermissionGrant>();
                    //foreach (var permission in definitionPermissions)
                    //{
                    //    var permissionGrant = new PermissionGrant(GuidGenerator.Create(),
                    //            permission.Name, "R", "admin", eventData.Id);
                    //    grantPermissions.Add(permissionGrant);
                    //}
                    // TODO: MySql 批量新增还是一条一条的语句?
                    // await PermissionGrantRepository.GetDbSet().AddRangeAsync(grantPermissions);

                    var permissionEntityType = PermissionGrantRepository.GetDbContext()
                                               .Model.FindEntityType(typeof(PermissionGrant));
                    var permissionTableName      = permissionEntityType.GetTableName();
                    var batchInsertPermissionSql = string.Empty;
                    if (PermissionGrantRepository.GetDbContext().Database.IsMySql())
                    {
                        batchInsertPermissionSql = BuildMySqlBatchInsertScript(permissionTableName, eventData.Id, grantPermissions);
                    }
                    else
                    {
                        batchInsertPermissionSql = BuildSqlServerBatchInsertScript(permissionTableName, eventData.Id, grantPermissions);
                    }
                    await PermissionGrantRepository.GetDbContext().Database.ExecuteSqlRawAsync(batchInsertPermissionSql);

                    await unitOfWork.SaveChangesAsync();
                }
            }
        }