Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Защищенные модули #437

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/com/github/_1c_syntax/bsl/mdo/CommonModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public class CommonModule implements MDObject, Module, ModuleOwner {

URI uri;

boolean isProtected;

/*
* Свое
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/github/_1c_syntax/bsl/mdo/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public interface Module {
* Вариант поддержки родительской конфигурации
*/
SupportVariant getSupportVariant();

/**
* Признак "защищенности" модуля, т.е. наличия пароля и отсутствие исходного кода
*/
boolean isProtected();
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ public class ObjectModule implements Module {
MdoReference owner = MdoReference.EMPTY;
@Default
SupportVariant supportVariant = SupportVariant.NONE;

boolean isProtected;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* This file is a part of MDClasses.
*
* Copyright (c) 2019 - 2023
* Tymko Oleg <[email protected]>, Maximov Valery <[email protected]> and contributors
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* MDClasses is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* MDClasses is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with MDClasses.
*/
package com.github._1c_syntax.bsl.reader.common;

import lombok.Getter;
import lombok.experimental.UtilityClass;
import org.apache.commons.io.FilenameUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

/**
* Набор вспомогательных методов, используемых при чтении данных
*/
@UtilityClass
public class ReaderUtils {
private static final byte[] PROTECTED_FILE_HEADER = new byte[]{-1, -1, -1, 127};

/**
* Читает информацию о "защищенности" модуля
*
* @param modulePath Путь к файлу модуля
* @return Параметы "защищенности"
*/
public ProtectedModuleInfo readProtectedModuleInfo(Path modulePath) {
return new ProtectedModuleInfo(modulePath);
}

/**
* Описание "защищенности" модуля
*/
public static class ProtectedModuleInfo {
@Getter
private boolean isProtected;
@Getter
private Path modulePath;

private ProtectedModuleInfo(Path path) {
modulePath = path;
isProtected = false;

if (!modulePath.toFile().exists()) {
var prtModulePath = Paths.get(FilenameUtils.removeExtension(modulePath.toFile().getPath()) + ".bin");
if (prtModulePath.toFile().exists()) {
isProtected = true;
modulePath = prtModulePath;
}
} else {
var bytes = new byte[PROTECTED_FILE_HEADER.length];
try (var fis = new FileInputStream(modulePath.toFile())) {
isProtected = (fis.read(bytes) == PROTECTED_FILE_HEADER.length
&& Arrays.equals(bytes, PROTECTED_FILE_HEADER));
} catch (IOException e) {
// ошибка чтения в данном случае неважна
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,14 @@ private void addModules() {
modulePath = EDTPaths.modulePath(folder, name, moduleType);
}

if (modulePath.toFile().exists()) {
var protectedModuleInfo = ReaderUtils.readProtectedModuleInfo(modulePath);
if (protectedModuleInfo.getModulePath().toFile().exists()) {
modules.add(ObjectModule.builder()
.moduleType(moduleType)
.uri(modulePath.toUri())
.uri(protectedModuleInfo.getModulePath().toUri())
.owner(mdoReference)
.supportVariant(supportVariant)
.isProtected(protectedModuleInfo.isProtected())
.build());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,34 @@
import com.github._1c_syntax.bsl.types.ModuleType;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import org.apache.commons.io.FilenameUtils;

import java.nio.file.Paths;

@DesignerConverter
public class CommonModuleConverter extends AbstractReadConverter {

private static final String URI_FIELD = "uri";
private static final String IS_PROTECTED_FIELD = "isProtected";

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
var readerContext = super.read(reader, context);

var folder = DesignerPaths.moduleFolder(currentPath, MDOType.COMMON_MODULE);
var modulePath = DesignerPaths.modulePath(folder, readerContext.getName(), ModuleType.CommonModule);

var isProtected = false;
if (!modulePath.toFile().exists()) {
// возможно модуль защищен
var prtModulePath = Paths.get(FilenameUtils.removeExtension(modulePath.toFile().getPath()) + ".bin");
if (prtModulePath.toFile().exists()) {
isProtected = true;
modulePath = prtModulePath;
}
}

readerContext.setValue(URI_FIELD, modulePath.toUri());
readerContext.setValue(IS_PROTECTED_FIELD, isProtected);

return readerContext.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package com.github._1c_syntax.bsl.reader.edt.converter;

import com.github._1c_syntax.bsl.mdo.CommonModule;
import com.github._1c_syntax.bsl.reader.common.ReaderUtils;
import com.github._1c_syntax.bsl.reader.edt.EDTPaths;
import com.github._1c_syntax.bsl.types.MDOType;
import com.github._1c_syntax.bsl.types.ModuleType;
Expand All @@ -32,15 +33,19 @@
public class CommonModuleConverter extends AbstractReadConverter {

private static final String URI_FIELD = "uri";
private static final String IS_PROTECTED_FIELD = "isProtected";

private static final byte[] PROTECTED_FILE_HEADER = new byte[]{-1, -1, -1, 127};
theshadowco marked this conversation as resolved.
Show resolved Hide resolved

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
var readerContext = super.read(reader, context);

var folder = EDTPaths.moduleFolder(currentPath, MDOType.COMMON_MODULE);
var modulePath = EDTPaths.modulePath(folder, readerContext.getName(), ModuleType.CommonModule);
readerContext.setValue(URI_FIELD, modulePath.toUri());

var protectedModuleInfo = ReaderUtils.readProtectedModuleInfo(modulePath);
readerContext.setValue(URI_FIELD, protectedModuleInfo.getModulePath().toUri());
readerContext.setValue(IS_PROTECTED_FIELD, protectedModuleInfo.isProtected());
return readerContext.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,10 @@ void test(ArgumentsAccessor argumentsAccessor) {
assertThat(calculationRegister.getDimensions()).hasSize(1);
assertThat(calculationRegister.getAttributes()).isEmpty();
assertThat(calculationRegister.getRecalculations()).hasSize(1);
var recalc = calculationRegister.getRecalculations().get(0);
assertThat(recalc.getModules())
.hasSize(1)
.allMatch(Module::isProtected)
;
}
}
5 changes: 4 additions & 1 deletion src/test/java/com/github/_1c_syntax/bsl/mdo/CatalogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ void test(ArgumentsAccessor argumentsAccessor) {
assertThat(catalog.getForms()).hasSize(3);
assertThat(catalog.getTemplates()).hasSize(1);
assertThat(catalog.getCommands()).hasSize(1);
assertThat(catalog.getModules()).hasSize(2);
assertThat(catalog.getModules())
.hasSize(2)
.anyMatch(Module::isProtected)
;
assertThat(catalog.getAllModules()).hasSize(6);
assertThat(catalog.getMDOChildren())
.hasSize(4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ class DocumentTest {
void test(ArgumentsAccessor argumentsAccessor) {
var mdo = MDTestUtils.getMDWithSimpleTest(argumentsAccessor);
}

}
}

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"mdoRef": "BusinessProcess.БизнесПроцесс1",
"mdoRefRu": "БизнесПроцесс.БизнесПроцесс1"
},
"supportVariant": "NONE"
"supportVariant": "NONE",
"isProtected": false
}
],
"name": "БизнесПроцесс1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"mdoRef": "BusinessProcess.БизнесПроцесс1",
"mdoRefRu": "БизнесПроцесс.БизнесПроцесс1"
},
"supportVariant": "NONE"
"supportVariant": "NONE",
"isProtected": false
}
],
"name": "БизнесПроцесс1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@
"modules": [
{
"moduleType": "RecalculationModule",
"uri": "src/test/resources/ext/designer/mdclasses/src/cf/CalculationRegisters/РегистрРасчета1/Recalculations/Перерасчет/Ext/RecordSetModule.bsl",
"uri": "src/test/resources/ext/designer/mdclasses/src/cf/CalculationRegisters/РегистрРасчета1/Recalculations/Перерасчет/Ext/RecordSetModule.bin",
"owner": {
"type": "RECALCULATION",
"mdoRef": "CalculationRegister.РегистрРасчета1.Recalculation.Перерасчет",
"mdoRefRu": "РегистрРасчета.РегистрРасчета1.Перерасчет.Перерасчет"
},
"supportVariant": "NONE"
"supportVariant": "NONE",
"isProtected": true
}
]
}
Expand Down Expand Up @@ -155,13 +156,14 @@
"modules": [
{
"moduleType": "RecalculationModule",
"uri": "src/test/resources/ext/designer/mdclasses/src/cf/CalculationRegisters/РегистрРасчета1/Recalculations/Перерасчет/Ext/RecordSetModule.bsl",
"uri": "src/test/resources/ext/designer/mdclasses/src/cf/CalculationRegisters/РегистрРасчета1/Recalculations/Перерасчет/Ext/RecordSetModule.bin",
"owner": {
"type": "RECALCULATION",
"mdoRef": "CalculationRegister.РегистрРасчета1.Recalculation.Перерасчет",
"mdoRefRu": "РегистрРасчета.РегистрРасчета1.Перерасчет.Перерасчет"
},
"supportVariant": "NONE"
"supportVariant": "NONE",
"isProtected": true
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
"mdoRef": "CalculationRegister.РегистрРасчета1.Recalculation.Перерасчет",
"mdoRefRu": "РегистрРасчета.РегистрРасчета1.Перерасчет.Перерасчет"
},
"supportVariant": "NONE"
"supportVariant": "NONE",
"isProtected": true
}
]
}
Expand Down Expand Up @@ -161,7 +162,8 @@
"mdoRef": "CalculationRegister.РегистрРасчета1.Recalculation.Перерасчет",
"mdoRefRu": "РегистрРасчета.РегистрРасчета1.Перерасчет.Перерасчет"
},
"supportVariant": "NONE"
"supportVariant": "NONE",
"isProtected": true
}
]
}
Expand Down
Loading