@ -8,32 +8,41 @@ import app.utils.CryptedCookie;
import jakarta.persistence.EntityManager ;
import jakarta.persistence.EntityManager ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Qualifier ;
import org.springframework.beans.factory.annotation.Qualifier ;
import org.springframework.jdbc.core.JdbcTemplate ;
import org.springframework.jdbc.core.RowMapper ;
import org.springframework.stereotype.Service ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.transaction.annotation.Transactional ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.util.List ;
import java.util.List ;
@Service
@Service
@Transactional ( value = "RwTransactionManager" )
public class BanService {
public class BanService {
EntityManager entityManager ;
@Autowired
@Qualifier ( "jt_rw" )
JdbcTemplate jdbcTemplate ;
ServerService serverService ;
ServerService serverService ;
CryptedCookie cryptedCookie ;
CryptedCookie cryptedCookie ;
PermitionService permitionService ;
PermitionService permitionService ;
@Autowired
@Autowired
public BanService ( ServerService serverService , @Qualifier ( value = "RwEntityManager" ) EntityManager entityManager , CryptedCookie cryptedCookie , PermitionService permitionService ) {
public BanService ( ServerService serverService , CryptedCookie cryptedCookie , PermitionService permitionService ) {
this . entityManager = entityManager ;
this . serverService = serverService ;
this . serverService = serverService ;
this . cryptedCookie = cryptedCookie ;
this . cryptedCookie = cryptedCookie ;
this . permitionService = permitionService ;
this . permitionService = permitionService ;
}
}
public Ban getBan ( SteamID steamID ) {
public Ban getBan ( SteamID steamID ) {
List < Object [ ] > result = entityManager . createNativeQuery ( "SELECT * FROM light_bans WHERE account_id = ?1 AND active = 1" )
return jdbcTemplate . query ( "SELECT * FROM light_bans WHERE account_id = ? AND active = 1" ,
. setParameter ( 1 , steamID . account_id )
new Object [ ] { steamID . account_id } ,
. getResultList ( ) ;
new RowMapper < Ban > ( ) {
return result . stream ( ) . map ( Ban : : new )
@Override
public Ban mapRow ( ResultSet resultSet , int num ) throws SQLException {
return new Ban ( resultSet ) ;
}
} ) . stream ( )
. peek ( ban - > ban . cryptIP ( cryptedCookie ) )
. peek ( ban - > ban . cryptIP ( cryptedCookie ) )
. peek ( ban - > ban . fillAdmins ( permitionService ) )
. peek ( ban - > ban . fillAdmins ( permitionService ) )
. findFirst ( )
. findFirst ( )
@ -41,10 +50,14 @@ public class BanService {
}
}
public Ban getBan ( int ban_id ) {
public Ban getBan ( int ban_id ) {
List < Object [ ] > result = entityManager . createNativeQuery ( "SELECT * FROM light_bans WHERE id = ?1" )
return jdbcTemplate . query ( "SELECT * FROM light_bans WHERE id = ?" ,
. setParameter ( 1 , ban_id )
new Object [ ] { ban_id } ,
. getResultList ( ) ;
new RowMapper < Ban > ( ) {
return result . stream ( ) . map ( Ban : : new )
@Override
public Ban mapRow ( ResultSet resultSet , int num ) throws SQLException {
return new Ban ( resultSet ) ;
}
} ) . stream ( )
. peek ( ban - > ban . cryptIP ( cryptedCookie ) )
. peek ( ban - > ban . cryptIP ( cryptedCookie ) )
. peek ( ban - > ban . fillAdmins ( permitionService ) )
. peek ( ban - > ban . fillAdmins ( permitionService ) )
. findFirst ( )
. findFirst ( )
@ -52,65 +65,71 @@ public class BanService {
}
}
public List < Ban > getBans ( SteamID steamID ) {
public List < Ban > getBans ( SteamID steamID ) {
List < Object [ ] > result = entityManager . createNativeQuery ( "SELECT * FROM light_bans WHERE account_id = ?1 AND active = 0" )
return jdbcTemplate . query ( "SELECT * FROM light_bans WHERE account_id = ? AND active = 0" ,
. setParameter ( 1 , steamID . account_id )
new Object [ ] { steamID . account_id } ,
. getResultList ( ) ;
new RowMapper < Ban > ( ) {
return result . stream ( ) . map ( Ban : : new )
@Override
public Ban mapRow ( ResultSet rs , int rowNum ) throws SQLException {
return new Ban ( rs ) ;
}
} ) . stream ( )
. peek ( ban - > ban . cryptIP ( cryptedCookie ) )
. peek ( ban - > ban . cryptIP ( cryptedCookie ) )
. peek ( ban - > ban . fillAdmins ( permitionService ) )
. peek ( ban - > ban . fillAdmins ( permitionService ) )
. toList ( ) ;
. toList ( ) ;
}
}
public List < Ban > getLastBans ( Integer limit , Integer offset ) {
public List < Ban > getLastBans ( Integer limit , Integer offset ) {
return ( ( List < Ban > ) entityManager . createNativeQuery ( "SELECT * FROM `light_bans` WHERE `active` = ?1 ORDER BY `light_bans`.`id` DESC LIMIT ?2 OFFSET ?3" )
return jdbcTemplate . query ( "SELECT * FROM `light_bans` WHERE `active` = ? ORDER BY `light_bans`.`id` DESC LIMIT ? OFFSET ?" ,
. setParameter ( 1 , 1 )
new Object [ ] { 1 , limit , offset } ,
. setParameter ( 2 , limit )
new RowMapper < Ban > ( ) {
. setParameter ( 3 , offset )
@Override
. getResultStream ( ) . map ( obj - > new Ban ( ( Object [ ] ) obj ) ) . toList ( ) ) . stream ( )
public Ban mapRow ( ResultSet rs , int rowNum ) throws SQLException {
return new Ban ( rs ) ;
}
} ) . stream ( )
. peek ( ban - > ban . cryptIP ( cryptedCookie ) )
. peek ( ban - > ban . cryptIP ( cryptedCookie ) )
. peek ( ban - > ban . fillAdmins ( permitionService ) )
. peek ( ban - > ban . fillAdmins ( permitionService ) )
. toList ( ) ;
. toList ( ) ;
//этот каст конечно пиздец, но он работает
}
}
public Long getBansCount ( ) {
public Long getBansCount ( ) {
return ( Long ) entityManager . createNativeQuery ( "SELECT COUNT(*) as count FROM `light_bans` WHERE active = 1" ) . getSingleResult ( ) ;
return jdbcTemplate . query ( "SELECT COUNT(*) as c FROM `light_bans` WHERE active = 1" ,
new RowMapper < Long > ( ) {
@Override
public Long mapRow ( ResultSet rs , int rowNum ) throws SQLException {
return rs . getLong ( "c" ) ;
}
} ) . stream ( ) . findFirst ( ) . orElse ( 0L ) ;
}
}
@Transactional ( value = "RwTransactionManager" )
public boolean removeBan ( PlayerProfile user , PlayerProfile admin ) {
public boolean removeBan ( PlayerProfile user , PlayerProfile admin ) {
Ban ban = getBan ( user . getSteamids ( ) ) ;
Ban ban = getBan ( user . getSteamids ( ) ) ;
if ( ban = = null ) return false ;
if ( ban = = null ) return false ;
return entityManager . createNativeQuery ( "UPDATE light_bans SET active=?1, unbanned_by_id = ?2, unbanned_timestamp = CURRENT_TIMESTAMP WHERE id = ?3" )
return jdbcTemplate . update ( "UPDATE light_bans SET active = ?, unbanned_by_id = ?, unbanned_timestamp = CURRENT_TIMESTAMP WHERE id = ?" ,
. setParameter ( 1 , 0 )
0 , admin . getSteamids ( ) . steam2 , ban . getId ( ) ) > 0 ;
. setParameter ( 2 , admin . getSteamids ( ) . steam2 )
. setParameter ( 3 , ban . getId ( ) )
. executeUpdate ( ) > 0 ;
}
}
@Transactional ( value = "RwTransactionManager" )
public int addBan ( PlayerProfile user , PlayerProfile admin , int ban_length , String ban_reason ) {
public int addBan ( PlayerProfile user , PlayerProfile admin , int ban_length , String ban_reason ) {
Ban ban = getBan ( user . getSteamids ( ) ) ;
Ban ban = getBan ( user . getSteamids ( ) ) ;
if ( ban ! = null ) return - 1 * ban . getId ( ) ;
if ( ban ! = null ) return - 1 * ban . getId ( ) ;
int result = entityManager . createNativeQuery ( "INSERT INTO light_bans (steam_id, account_id, ban_length, ban_reason, banned_by_id, active, ip, player_name, banned_by) VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9)" )
jdbcTemplate . update ( "INSERT INTO light_bans (steam_id, account_id, ban_length, ban_reason, banned_by_id, active, ip, player_name, banned_by) VALUES (?,?,?,?,?,?,?,?,?)" ,
. setParameter ( 1 , user . getSteamids ( ) . steam2 )
user . getSteamids ( ) . steam2 , user . getSteamids ( ) . account_id , ban_length , ban_reason , admin . getSteamids ( ) . steam2 , 1 ,
. setParameter ( 2 , user . getSteamids ( ) . account_id )
user . getPlay_on ( ) ! = null ? user . getPlay_on ( ) . getIp ( cryptedCookie ) : "" ,
. setParameter ( 3 , ban_length )
user . getPlay_on ( ) ! = null ? user . getPlay_on ( ) . getName ( ) : user . getSteam_data ( ) . getNickname ( ) ,
. setParameter ( 4 , ban_reason )
admin . getSteam_data ( ) . getNickname ( ) ) ;
. setParameter ( 5 , admin . getSteamids ( ) . steam2 )
. setParameter ( 6 , 1 )
. setParameter ( 7 , user . getPlay_on ( ) ! = null ? user . getPlay_on ( ) . getIp ( cryptedCookie ) : "" )
. setParameter ( 8 , user . getPlay_on ( ) ! = null ? user . getPlay_on ( ) . getName ( ) : user . getSteam_data ( ) . getNickname ( ) )
. setParameter ( 9 , admin . getSteam_data ( ) . getNickname ( ) )
. executeUpdate ( ) ;
serverService . kickPlayer ( user , "banned" ) ;
serverService . kickPlayer ( user , "banned" ) ;
return getBan ( user . getSteamids ( ) ) . getId ( ) ;
return getBan ( user . getSteamids ( ) ) . getId ( ) ;
}
}
public List < Long > getUsersDiscordWithBanOnServers ( ) {
public List < Long > getUsersDiscordWithBanOnServers ( ) {
return entityManager . createNativeQuery ( "SELECT `discord_id` FROM `light_bans` INNER JOIN `steam2discord` ON `light_bans`.`active` = ?1 AND `light_bans`.`steam_id` COLLATE utf8mb4_unicode_ci LIKE `steam2discord`.`steam_id`" )
return jdbcTemplate . query ( "SELECT `discord_id` FROM `light_bans` INNER JOIN `steam2discord` ON `light_bans`.`active` = ?1 AND `light_bans`.`steam_id` COLLATE utf8mb4_unicode_ci LIKE `steam2discord`.`steam_id`" ,
. setParameter ( 1 , 1 )
new Object [ ] { 1 } ,
. getResultList ( ) ;
new RowMapper < Long > ( ) {
@Override
public Long mapRow ( ResultSet rs , int rowNum ) throws SQLException {
return rs . getLong ( "discord_id" ) ;
}
} ) ;
}
}
public String deCrypt ( String crypted ) {
public String deCrypt ( String crypted ) {