[FEATURE] Расширенная имплементация UserHWID

This commit is contained in:
Gravit 2019-06-07 12:30:31 +07:00
parent 765d47deec
commit cbfbc3c63b
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
4 changed files with 75 additions and 7 deletions

View file

@ -1,5 +1,7 @@
package pro.gravit.launchserver.dao;
import pro.gravit.launcher.OshiHWID;
import java.util.List;
import java.util.UUID;
@ -7,6 +9,7 @@ public interface UserDAO {
User findById(int id);
User findByUsername(String username);
User findByUUID(UUID uuid);
List<UserHWID> findHWID(OshiHWID hwid);
void save(User user);
void update(User user);
void delete(User user);

View file

@ -1,23 +1,58 @@
package pro.gravit.launchserver.dao;
import pro.gravit.launcher.LauncherAPI;
import pro.gravit.launcher.HWID;
import pro.gravit.launcher.OshiHWID;
import javax.persistence.*;
import java.util.function.Supplier;
@Entity
@Table(name = "users_hwids")
public class UserHWID {
public class UserHWID implements HWID {
private transient Supplier<OshiHWID> oshiSupp = () -> {
OshiHWID hwid = new OshiHWID();
hwid.HWDiskSerial = this.HWDiskSerial;
hwid.macAddr = this.macAddr;
hwid.processorID = this.processorID;
hwid.serialNumber = this.serialNumber;
hwid.totalMemory = this.totalMemory;
return hwid;
};
private transient OshiHWID oshi = null;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@LauncherAPI
public long totalMemory = 0;
@LauncherAPI
public String serialNumber;
@LauncherAPI
public String HWDiskSerial;
@LauncherAPI
public String processorID;
@LauncherAPI
public String macAddr;
public boolean banned;
public OshiHWID toHWID()
{
if(oshi == null) oshi = oshiSupp.get();
return oshi;
}
@Override
public String getSerializeString() {
return toHWID().getSerializeString();
}
@Override
public int getLevel() {
return toHWID().getLevel();
}
@Override
public int compare(HWID hwid) {
return toHWID().compare(hwid);
}
@Override
public boolean isNull() {
return toHWID().isNull();
}
}

View file

@ -4,9 +4,11 @@
import java.util.List;
import java.util.UUID;
import pro.gravit.launcher.OshiHWID;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.dao.User;
import pro.gravit.launchserver.dao.UserDAO;
import pro.gravit.launchserver.dao.UserHWID;
public class DefaultUserDAOImpl implements UserDAO {
public DefaultUserDAOImpl(LaunchServer srv) { }
@ -25,6 +27,11 @@ public User findByUUID(UUID uuid) {
return null;
}
@Override
public List<UserHWID> findHWID(OshiHWID hwid) {
return new ArrayList<>();
}
@Override
public void save(User user) {
throw new UnsupportedOperationException();

View file

@ -11,9 +11,11 @@
import org.hibernate.Session;
import org.hibernate.Transaction;
import pro.gravit.launcher.OshiHWID;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.dao.User;
import pro.gravit.launchserver.dao.UserDAO;
import pro.gravit.launchserver.dao.UserHWID;
import pro.gravit.launchserver.hibernate.SessionFactoryManager;
public class HibernateUserDAOImpl implements UserDAO {
@ -53,6 +55,27 @@ public User findByUUID(UUID uuid) {
return ret.size() == 0 ? null : ret.get(0);
}
@Override
public List<UserHWID> findHWID(OshiHWID hwid) {
EntityManager em = manager.fact.createEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<UserHWID> personCriteria = cb.createQuery(UserHWID.class);
Root<UserHWID> rootUser = personCriteria.from(UserHWID.class);
personCriteria.select(rootUser).where(
cb.or(
cb.equal(rootUser.get("totalMemory"), hwid.totalMemory),
cb.equal(rootUser.get("HWDiskSerial"), hwid.HWDiskSerial),
cb.equal(rootUser.get("serialNumber"), hwid.serialNumber),
cb.equal(rootUser.get("processorID"), hwid.processorID),
cb.equal(rootUser.get("macAddr"), hwid.macAddr)
)
);
List<UserHWID> ret = em.createQuery(personCriteria).getResultList();
em.close();
return ret;
}
public void save(User user) {
try (Session session = manager.fact.openSession()) {
Transaction tx1 = session.beginTransaction();