Добавил security-util с UserRoleVerification для проверки прав.

This commit is contained in:
AKurakin 2023-03-31 15:44:59 +03:00
parent d4d544a086
commit 9abd6c818b
3 changed files with 229 additions and 0 deletions

View file

@ -20,6 +20,7 @@
<module>classes</module>
<module>backend-api</module>
<module>imdg</module>
<module>security-util</module>
<module>db-scripts</module>
<module>dbf-importer</module>
<module>dbf-exporter</module>

View file

@ -0,0 +1,142 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>security-util</artifactId>
<name>Clearing misc util for verify user roles</name>
<packaging>jar</packaging>
<version>SPCEX-1.0.0.0</version>
<parent>
<artifactId>clearing-parent</artifactId>
<groupId>ru.spcex.clearing</groupId>
<version>SPCEX-1.0.0.0</version>
</parent>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ru.spcex.clearing</groupId>
<artifactId>classes</artifactId>
<version>SPCEX-1.0.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ru.spcex.clearing</groupId>
<artifactId>dictionary</artifactId>
<version>SPCEX-1.0.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ru.spcex.platform</groupId>
<artifactId>platform-enum</artifactId>
</dependency>
<!-- IMDG -->
<dependency>
<groupId>ru.spcex.platform</groupId>
<artifactId>platform-imdg-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.33</version>
</dependency>
<!-- TEST -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application.properties</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>ru.spcex.clearing.imdg.IMDGApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<addResources>true</addResources>
<finalName>${project.artifactId}</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.2.0-M1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0-M1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View file

@ -0,0 +1,86 @@
package ru.spcex.clearing.util.security;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ru.clearing.classes.objects.BusinessObject;
import ru.clearing.classes.statics.data.company.CompanyRoleSet;
import ru.clearing.classes.statics.data.user.User;
import ru.clearing.classes.statics.data.user.UserRoleSession;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
import ru.spcex.platform.enumeration.Status;
import ru.spcex.platform.imdg.api.Imdg;
import ru.spcex.platform.imdg.api.ImdgProvider;
import ru.spcex.platform.utils.enumeration.IEnumKey;
import java.util.Collection;
/**
* Проверка дополнительных прав пользователя.
* <p>
* Основная проверка прав осуществляется на frontend-api, и использовать этот компонент дополнительно не требуется.
*/
@Service
public class UserRoleVerification {
protected Logger log = LoggerFactory.getLogger(getClass());
protected Imdg<User> users;
protected Imdg<UserRoleSession> userRoleSessions;
protected Imdg<CompanyRoleSet> companyRoleSet;
@Autowired
public UserRoleVerification(ImdgProvider imdg) {
userRoleSessions = imdg.getImdg(IMDGDistributedNames.Map_UserRoleSession, UserRoleSession.class);
companyRoleSet = imdg.getImdg(IMDGDistributedNames.Map_CompanyRoleSet, CompanyRoleSet.class);
}
public boolean userHasRole(Long userId, String userRole) {
String query = String.format("userId=%s and userRole=%s and status=%s",
userId, userRole, Status.Active.getKey());
Collection<Long> roles = userRoleSessions.getCollectionIdsBySQL(query);
log.trace("userHasRole: found {} roles by query: {}", roles.size(), query);
if (roles.isEmpty())
return false;
//todo impl, use userRoleSessions
return true;
}
public boolean userHasRole(Long userId, Long companyId, String userRole) {
String query = String.format("userId=%s and companyId=%s and userRole=%s and status=%s",
userId, companyId, userRole, Status.Active.getKey());
Collection<Long> roles = userRoleSessions.getCollectionIdsBySQL(query);
log.trace("userHasRole: found {} roles by query: {}", roles.size(), query);
if (roles.isEmpty())
return false;
//todo impl, use userRoleSessions
return true;
}
boolean userHasRole(Long userId, IEnumKey role) {
return userHasRole(userId, role == null ? null : role.getKey());
}
boolean userHasRole(Long userId, Long companyId, IEnumKey role) {
return userHasRole(userId, companyId, role == null ? null : role.getKey());
}
boolean userHasRoleOnObject(Long userId, BusinessObject /*todo WithUserId / WithCompany */ bObject) {
//todo impl
return true;
}
public boolean companyHasRole(Long companyId, String companyRole) {
String query = String.format("companyId=%s and companyRole=%s and workflowStatus=%s",
companyId, companyRole, Status.Active.getKey());
Collection<Long> roles = companyRoleSet.getCollectionIdsBySQL(query);
log.trace("companyHasRole: found {} roles by query: {}", roles.size(), query);
// if (roles.isEmpty()) return false;
//todo impl
return true;
}
// userIsEnabled & exist - frontendapi validation
}