From 913d88c11d6b5a6239bc0671d1923a7514e47ef1 Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: 星期二, 27 七月 2021 12:51:15 +0800
Subject: [PATCH] 修复任意账户越权问题

---
 src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java |  118 +++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 103 insertions(+), 15 deletions(-)

diff --git a/src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java b/src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java
index 4347993..311b306 100644
--- a/src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java
+++ b/src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java
@@ -7,8 +7,10 @@
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.velocity.Template;
 import org.apache.velocity.VelocityContext;
@@ -26,7 +28,6 @@
 import com.ruoyi.common.exception.CustomException;
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.StringUtils;
-import com.ruoyi.common.utils.file.FileUtils;
 import com.ruoyi.project.tool.gen.domain.GenTable;
 import com.ruoyi.project.tool.gen.domain.GenTableColumn;
 import com.ruoyi.project.tool.gen.mapper.GenTableColumnMapper;
@@ -99,6 +100,17 @@
     public List<GenTable> selectDbTableListByNames(String[] tableNames)
     {
         return genTableMapper.selectDbTableListByNames(tableNames);
+    }
+
+    /**
+     * 鏌ヨ鎵�鏈夎〃淇℃伅
+     * 
+     * @return 琛ㄤ俊鎭泦鍚�
+     */
+    @Override
+    public List<GenTable> selectGenTableAll()
+    {
+        return genTableMapper.selectGenTableAll();
     }
 
     /**
@@ -184,9 +196,10 @@
         Map<String, String> dataMap = new LinkedHashMap<>();
         // 鏌ヨ琛ㄤ俊鎭�
         GenTable table = genTableMapper.selectGenTableById(tableId);
-        // 鏌ヨ鍒椾俊鎭�
-        List<GenTableColumn> columns = table.getColumns();
-        setPkColumn(table, columns);
+        // 璁剧疆涓诲瓙琛ㄤ俊鎭�
+        setSubTable(table);
+        // 璁剧疆涓婚敭鍒椾俊鎭�
+        setPkColumn(table);
         VelocityInitializer.initVelocity();
 
         VelocityContext context = VelocityUtils.prepareContext(table);
@@ -224,16 +237,16 @@
      * 鐢熸垚浠g爜锛堣嚜瀹氫箟璺緞锛�
      * 
      * @param tableName 琛ㄥ悕绉�
-     * @return 鏁版嵁
      */
     @Override
     public void generatorCode(String tableName)
     {
         // 鏌ヨ琛ㄤ俊鎭�
         GenTable table = genTableMapper.selectGenTableByName(tableName);
-        // 鏌ヨ鍒椾俊鎭�
-        List<GenTableColumn> columns = table.getColumns();
-        setPkColumn(table, columns);
+        // 璁剧疆涓诲瓙琛ㄤ俊鎭�
+        setSubTable(table);
+        // 璁剧疆涓婚敭鍒椾俊鎭�
+        setPkColumn(table);
 
         VelocityInitializer.initVelocity();
 
@@ -263,6 +276,41 @@
     }
 
     /**
+     * 鍚屾鏁版嵁搴�
+     * 
+     * @param tableName 琛ㄥ悕绉�
+     */
+    @Override
+    @Transactional
+    public void synchDb(String tableName)
+    {
+        GenTable table = genTableMapper.selectGenTableByName(tableName);
+        List<GenTableColumn> tableColumns = table.getColumns();
+        List<String> tableColumnNames = tableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
+
+        List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
+        if (StringUtils.isEmpty(dbTableColumns))
+        {
+            throw new CustomException("鍚屾鏁版嵁澶辫触锛屽師琛ㄧ粨鏋勪笉瀛樺湪");
+        }
+        List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
+
+        dbTableColumns.forEach(column -> {
+            if (!tableColumnNames.contains(column.getColumnName()))
+            {
+                GenUtils.initColumnField(column, table);
+                genTableColumnMapper.insertGenTableColumn(column);
+            }
+        });
+
+        List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList());
+        if (StringUtils.isNotEmpty(delColumns))
+        {
+            genTableColumnMapper.deleteGenTableColumns(delColumns);
+        }
+    }
+
+    /**
      * 鎵归噺鐢熸垚浠g爜锛堜笅杞芥柟寮忥級
      * 
      * @param tableNames 琛ㄦ暟缁�
@@ -288,9 +336,10 @@
     {
         // 鏌ヨ琛ㄤ俊鎭�
         GenTable table = genTableMapper.selectGenTableByName(tableName);
-        // 鏌ヨ鍒椾俊鎭�
-        List<GenTableColumn> columns = table.getColumns();
-        setPkColumn(table, columns);
+        // 璁剧疆涓诲瓙琛ㄤ俊鎭�
+        setSubTable(table);
+        // 璁剧疆涓婚敭鍒椾俊鎭�
+        setPkColumn(table);
 
         VelocityInitializer.initVelocity();
 
@@ -345,17 +394,27 @@
                 throw new CustomException("鏍戝悕绉板瓧娈典笉鑳戒负绌�");
             }
         }
+        else if (GenConstants.TPL_SUB.equals(genTable.getTplCategory()))
+        {
+            if (StringUtils.isEmpty(genTable.getSubTableName()))
+            {
+                throw new CustomException("鍏宠仈瀛愯〃鐨勮〃鍚嶄笉鑳戒负绌�");
+            }
+            else if (StringUtils.isEmpty(genTable.getSubTableFkName()))
+            {
+                throw new CustomException("瀛愯〃鍏宠仈鐨勫閿悕涓嶈兘涓虹┖");
+            }
+        }
     }
 
     /**
      * 璁剧疆涓婚敭鍒椾俊鎭�
      * 
      * @param table 涓氬姟琛ㄤ俊鎭�
-     * @param columns 涓氬姟瀛楁鍒楄〃
      */
-    public void setPkColumn(GenTable table, List<GenTableColumn> columns)
+    public void setPkColumn(GenTable table)
     {
-        for (GenTableColumn column : columns)
+        for (GenTableColumn column : table.getColumns())
         {
             if (column.isPk())
             {
@@ -365,7 +424,36 @@
         }
         if (StringUtils.isNull(table.getPkColumn()))
         {
-            table.setPkColumn(columns.get(0));
+            table.setPkColumn(table.getColumns().get(0));
+        }
+        if (GenConstants.TPL_SUB.equals(table.getTplCategory()))
+        {
+            for (GenTableColumn column : table.getSubTable().getColumns())
+            {
+                if (column.isPk())
+                {
+                    table.getSubTable().setPkColumn(column);
+                    break;
+                }
+            }
+            if (StringUtils.isNull(table.getSubTable().getPkColumn()))
+            {
+                table.getSubTable().setPkColumn(table.getSubTable().getColumns().get(0));
+            }
+        }
+    }
+
+    /**
+     * 璁剧疆涓诲瓙琛ㄤ俊鎭�
+     * 
+     * @param table 涓氬姟琛ㄤ俊鎭�
+     */
+    public void setSubTable(GenTable table)
+    {
+        String subTableName = table.getSubTableName();
+        if (StringUtils.isNotEmpty(subTableName))
+        {
+            table.setSubTable(genTableMapper.selectGenTableByName(subTableName));
         }
     }
 

--
Gitblit v1.9.3