Skip to content

Commit

Permalink
Merge pull request #125 from silverbulleters/feature/RoleData
Browse files Browse the repository at this point in the history
Добавлена поддержка прав доступа хранящихся в ролях
  • Loading branch information
otymko authored Oct 14, 2020
2 parents 821490e + 80436dd commit 9ce7e42
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 2 deletions.
45 changes: 45 additions & 0 deletions src/main/java/com/github/_1c_syntax/mdclasses/mdo/ObjectRight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* This file is a part of MDClasses.
*
* Copyright © 2019 - 2020
* 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.mdclasses.mdo;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Collections;
import java.util.List;

/**
* POJO представление прав доступа объекта метаданных, содержащихся в конкретной роли
*/
@Getter
@Setter
@NoArgsConstructor
public class ObjectRight {
private String name = "";

@XStreamAlias("right")
@XStreamImplicit
List<Right> rights = Collections.emptyList();
}
37 changes: 37 additions & 0 deletions src/main/java/com/github/_1c_syntax/mdclasses/mdo/Right.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* This file is a part of MDClasses.
*
* Copyright © 2019 - 2020
* 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.mdclasses.mdo;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
* POJO представление конкретного права доступа, находящегося в роли
*/
@Getter
@Setter
@NoArgsConstructor
public class Right {
private String name = "";
private boolean value = false;
}
6 changes: 4 additions & 2 deletions src/main/java/com/github/_1c_syntax/mdclasses/mdo/Role.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@

import com.github._1c_syntax.mdclasses.mdo.wrapper.DesignerMDO;
import com.github._1c_syntax.mdclasses.metadata.additional.MDOType;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.Value;

@Value
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true, onlyExplicitlyIncluded = true)
@NoArgsConstructor
public class Role extends MDObjectBase {

private RoleData roleData;

public Role(DesignerMDO designerMDO) {
super(designerMDO);
}
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/com/github/_1c_syntax/mdclasses/mdo/RoleData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* This file is a part of MDClasses.
*
* Copyright © 2019 - 2020
* 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.mdclasses.mdo;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Collections;
import java.util.List;


/**
* POJO представление данных конкретной роли
*/
@NoArgsConstructor
@Getter
@Setter
public class RoleData {

/**
* Устанавливать права для новых объектов
*/
@XStreamAlias("setForNewObjects")
private boolean setForNewObjects = false;

/**
* Устанавливать права для реквизитов и табличных частей по умолчанию
*/
@XStreamAlias("setForAttributesByDefault")
private boolean setForAttributesByDefault = false;

/**
* Независимые права подчиненных объектов
*/
@XStreamAlias("independentRightsOfChildObjects")
private boolean independentRightsOfChildObjects = false;

@XStreamAlias("object")
@XStreamImplicit
List<ObjectRight> objectRights = Collections.emptyList();
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import com.github._1c_syntax.mdclasses.mdo.MDObjectComplex;
import com.github._1c_syntax.mdclasses.mdo.Report;
import com.github._1c_syntax.mdclasses.mdo.Role;
import com.github._1c_syntax.mdclasses.mdo.RoleData;
import com.github._1c_syntax.mdclasses.mdo.ScheduledJob;
import com.github._1c_syntax.mdclasses.mdo.Sequence;
import com.github._1c_syntax.mdclasses.mdo.SessionParameter;
Expand Down Expand Up @@ -344,6 +345,7 @@ private void addClassAliases(XStream xStream) {
xStream.alias("mdclass:Enum", MDOEnum.class);
xStream.alias("mdclass:Report", Report.class);
xStream.alias("mdclass:Role", Role.class);
xStream.alias("Rights", RoleData.class);
xStream.alias("mdclass:ScheduledJob", ScheduledJob.class);
xStream.alias("mdclass:Sequence", Sequence.class);
xStream.alias("mdclass:SessionParameter", SessionParameter.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import com.github._1c_syntax.mdclasses.mdo.MDObjectBSL;
import com.github._1c_syntax.mdclasses.mdo.MDObjectBase;
import com.github._1c_syntax.mdclasses.mdo.MDObjectComplex;
import com.github._1c_syntax.mdclasses.mdo.Role;
import com.github._1c_syntax.mdclasses.mdo.RoleData;
import com.github._1c_syntax.mdclasses.mdo.Subsystem;
import com.github._1c_syntax.mdclasses.mdo.TabularSection;
import com.github._1c_syntax.mdclasses.mdo.Template;
Expand All @@ -49,6 +51,7 @@
import io.vavr.control.Either;
import lombok.experimental.UtilityClass;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -136,6 +139,14 @@ public Optional<MDObjectBase> readMDObject(ConfigurationSource configurationSour
});
}


// загрузка данных роли
if (mdoValue instanceof Role) {
var roleDataPath = MDOPathUtils.getRoleDataPath(configurationSource,
mdoPath.getParent().toString(), mdoValue.getName());
var roleDataOptional = readRoleData(roleDataPath);
roleDataOptional.ifPresent(((Role) mdoValue)::setRoleData);
}
});

return mdo;
Expand All @@ -162,6 +173,20 @@ public Optional<FormData> readFormData(ConfigurationSource configurationSource,
return Optional.ofNullable(formData);
}

/**
* Читает данные роли из файла Rights
*
* @param roleDataPath - путь к файлу прав роли.
* @return {@code Optional} POJO представление данных ролей
*/
private static Optional<RoleData> readRoleData(Path roleDataPath) {
if (Files.notExists(roleDataPath)) {
return Optional.empty();
}

return Optional.ofNullable((RoleData) XStreamFactory.fromXML(roleDataPath.toFile()));
}

/**
* Читает объект по его файлу описания, но не выполняет чтение дочерних элементов
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,23 @@ private Path getModulePathDesigner(Path folder, String name, ModuleType moduleTy
return Paths.get(folder.toString(), subdirectory, moduleType.getFileName());
}

/**
* Получает путь к файлу прав роли для любого формата относительного базового каталога,
* и имени объекта
*
* @param configurationSource - формат данных, конфигуратор или EDT
* @param basePath - базовый каталог конфигурации
* @param mdoName - имя объекта метаданных, без расширения
* @return - путь к файлу прав конкретной роли
*/
public static Path getRoleDataPath(ConfigurationSource configurationSource, String basePath, String mdoName) {
Path path;
if (configurationSource == ConfigurationSource.EDT) {
path = Path.of(basePath, "Rights.rights");
} else {
path = Path.of(basePath, mdoName, "Ext", "Rights.xml");
}

return path;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* This file is a part of MDClasses.
*
* Copyright © 2019 - 2020
* 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.mdclasses.mdo;

import com.github._1c_syntax.mdclasses.metadata.additional.ConfigurationSource;
import com.github._1c_syntax.mdclasses.metadata.additional.MDOType;
import com.github._1c_syntax.mdclasses.utils.MDOFactory;
import org.junit.jupiter.api.Test;

import java.nio.file.Paths;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

class RoleDataTest {

private static final String SRC_EDT = "src/test/resources/metadata/edt/src";
private static final String SRC_DESIGNER = "src/test/resources/metadata/original";

@Test
void testRoleDataEdt() {
var mdo = MDOFactory.readMDObject(ConfigurationSource.EDT,
MDOType.ROLE, Paths.get(SRC_EDT, "Roles/Роль1/Роль1.mdo"));
assertThat(mdo).isPresent();
Role role = (Role) mdo.get();
testRole(role);
}

@Test
void testRoleDataDesigner() {
var mdo = MDOFactory.readMDObject(ConfigurationSource.DESIGNER,
MDOType.ROLE, Paths.get(SRC_DESIGNER, "Roles/Роль1.xml"));
assertThat(mdo).isPresent();
Role role = (Role) mdo.get();
testRole(role);
}


private void testRole(Role role) {

RoleData roleData = role.getRoleData();
assertThat(roleData).isNotNull();

List<ObjectRight> objectRights = roleData.getObjectRights();
assertThat(objectRights).hasSize(3);

ObjectRight confRights = objectRights.get(0);
assertThat(confRights.getName()).isEqualTo("Configuration.Конфигурация");
assertThat(confRights.getRights()).hasSize(18);

ObjectRight documentRights = objectRights.get(1);
assertThat(documentRights.getName()).isEqualTo("Document.Документ1");
assertThat(documentRights.getRights()).hasSize(18);

ObjectRight catalogRights = objectRights.get(2);
assertThat(catalogRights.getName()).isEqualTo("Catalog.Справочник1");
assertThat(catalogRights.getRights()).hasSize(16);
}
}

0 comments on commit 9ce7e42

Please sign in to comment.