|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (C) 2023, 1C-Soft LLC and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made |
| 5 | + * available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * 1C-Soft LLC - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | + |
| 14 | +package com.e1c.ssl.bsl.check; |
| 15 | + |
| 16 | +import static com._1c.g5.v8.dt.bsl.model.BslPackage.Literals.INVOCATION; |
| 17 | +import static com._1c.g5.v8.dt.bsl.model.BslPackage.Literals.STRING_LITERAL__LINES; |
| 18 | +import static com._1c.g5.v8.dt.metadata.mdclass.MdClassPackage.Literals.CONFIGURATION__ROLES; |
| 19 | +import static com._1c.g5.v8.dt.metadata.mdclass.MdClassPackage.Literals.ROLE; |
| 20 | + |
| 21 | +import java.text.MessageFormat; |
| 22 | + |
| 23 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 24 | +import org.eclipse.emf.common.util.EList; |
| 25 | +import org.eclipse.xtext.naming.IQualifiedNameConverter; |
| 26 | +import org.eclipse.xtext.scoping.IScope; |
| 27 | +import org.eclipse.xtext.scoping.IScopeProvider; |
| 28 | + |
| 29 | +import com._1c.g5.v8.dt.bsl.model.DynamicFeatureAccess; |
| 30 | +import com._1c.g5.v8.dt.bsl.model.Expression; |
| 31 | +import com._1c.g5.v8.dt.bsl.model.Invocation; |
| 32 | +import com._1c.g5.v8.dt.bsl.model.StaticFeatureAccess; |
| 33 | +import com._1c.g5.v8.dt.bsl.model.StringLiteral; |
| 34 | +import com._1c.g5.v8.dt.core.naming.ITopObjectFqnGenerator; |
| 35 | +import com.e1c.g5.v8.dt.check.CheckComplexity; |
| 36 | +import com.e1c.g5.v8.dt.check.ICheckParameters; |
| 37 | +import com.e1c.g5.v8.dt.check.components.BasicCheck; |
| 38 | +import com.e1c.g5.v8.dt.check.settings.IssueSeverity; |
| 39 | +import com.e1c.g5.v8.dt.check.settings.IssueType; |
| 40 | +import com.google.inject.Inject; |
| 41 | + |
| 42 | +public class UsersRolesAvailableRolesExistCheck |
| 43 | + extends BasicCheck |
| 44 | +{ |
| 45 | + |
| 46 | + private static final String CHECK_ID = "users-roles-available-role-exist"; |
| 47 | + |
| 48 | + private static final String COMMONMODULE_USERS_NAME = "Users"; //$NON-NLS-1$ |
| 49 | + |
| 50 | + private static final String COMMONMODULE_USERS_NAME_RU = "Пользователи"; //$NON-NLS-1$ |
| 51 | + |
| 52 | + private static final String METHOD_ISINROLES_NAME = "RolesAvailable"; //$NON-NLS-1$ |
| 53 | + |
| 54 | + private static final String METHOD_ISINROLES_NAME_RU = "РолиДоступны"; //$NON-NLS-1$ |
| 55 | + |
| 56 | + private final IQualifiedNameConverter qualifiedNameConverter; |
| 57 | + |
| 58 | + private final ITopObjectFqnGenerator topObjectFqnGenerator; |
| 59 | + |
| 60 | + private final IScopeProvider scopeProvider; |
| 61 | + |
| 62 | + @Inject |
| 63 | + public UsersRolesAvailableRolesExistCheck(IScopeProvider scopeProvider, |
| 64 | + IQualifiedNameConverter qualifiedNameConverter, ITopObjectFqnGenerator topObjectFqnGenerator) |
| 65 | + { |
| 66 | + super(); |
| 67 | + this.qualifiedNameConverter = qualifiedNameConverter; |
| 68 | + this.topObjectFqnGenerator = topObjectFqnGenerator; |
| 69 | + this.scopeProvider = scopeProvider; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public String getCheckId() |
| 74 | + { |
| 75 | + return CHECK_ID; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + protected void configureCheck(CheckConfigurer builder) |
| 80 | + { |
| 81 | + //TODO добавить extension CommonSenseCheckExtension |
| 82 | + builder.title("Users.RolesAvailabel() role exist") //TODO add NLS String |
| 83 | + .description("Users.RolesAvailabel() role exist") //TODO add NLS String |
| 84 | + .complexity(CheckComplexity.NORMAL) |
| 85 | + .severity(IssueSeverity.MAJOR) |
| 86 | + .issueType(IssueType.WARNING) |
| 87 | + .module() |
| 88 | + .checkedObjectType(INVOCATION); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + protected void check(Object object, ResultAcceptor resultAcceptor, ICheckParameters parameters, |
| 93 | + IProgressMonitor monitor) |
| 94 | + { |
| 95 | + Invocation inv = (Invocation)object; |
| 96 | + if (monitor.isCanceled() || !isValidInvocation(inv)) |
| 97 | + { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + EList<Expression> params = inv.getParams(); |
| 102 | + if (monitor.isCanceled() || params.isEmpty() || !(params.get(0) instanceof StringLiteral)) |
| 103 | + { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + StringLiteral literal = (StringLiteral)params.get(0); |
| 108 | + String content = String.join("", literal.lines(true)); //$NON-NLS-1$ |
| 109 | + if (monitor.isCanceled() || content.isBlank()) |
| 110 | + { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + String[] roles = content.replace(" ", "").split(","); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 115 | + IScope scope = scopeProvider.getScope(inv, CONFIGURATION__ROLES); |
| 116 | + |
| 117 | + for (String role : roles) |
| 118 | + { |
| 119 | + if (monitor.isCanceled()) |
| 120 | + { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + String fqn = topObjectFqnGenerator.generateStandaloneObjectFqn(ROLE, role); |
| 125 | + if (scope.getSingleElement(qualifiedNameConverter.toQualifiedName(fqn)) == null) |
| 126 | + { |
| 127 | + //TODO add NLS String |
| 128 | + String message = MessageFormat.format("Role {0} not exist", role); |
| 129 | + resultAcceptor.addIssue(message, literal, STRING_LITERAL__LINES); |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | + private boolean isValidInvocation(Invocation invocation) |
| 137 | + { |
| 138 | + |
| 139 | + if (invocation.getMethodAccess() instanceof DynamicFeatureAccess) |
| 140 | + { |
| 141 | + DynamicFeatureAccess dfa = (DynamicFeatureAccess)invocation.getMethodAccess(); |
| 142 | + Expression source = dfa.getSource(); |
| 143 | + if (source instanceof StaticFeatureAccess && isSslUsersMethod((StaticFeatureAccess)source, dfa)) |
| 144 | + { |
| 145 | + return true; |
| 146 | + } |
| 147 | + } |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + private boolean isSslUsersMethod(StaticFeatureAccess sfa, DynamicFeatureAccess dfa) |
| 152 | + { |
| 153 | + return (sfa.getName().equalsIgnoreCase(COMMONMODULE_USERS_NAME) |
| 154 | + || sfa.getName().equalsIgnoreCase(COMMONMODULE_USERS_NAME_RU)) |
| 155 | + && (dfa.getName().equalsIgnoreCase(METHOD_ISINROLES_NAME) |
| 156 | + || dfa.getName().equalsIgnoreCase(METHOD_ISINROLES_NAME_RU)); |
| 157 | + } |
| 158 | +} |
0 commit comments