|
|
@@ -23,9 +23,10 @@ import java.util.List; |
|
|
|
import static bubble.ApiConstants.HOME_DIR; |
|
|
|
import static bubble.model.device.Device.UNINITIALIZED_DEVICE_LIKE; |
|
|
|
import static bubble.model.device.Device.newUninitializedDevice; |
|
|
|
import static org.cobbzilla.util.daemon.ZillaRuntime.background; |
|
|
|
import static org.cobbzilla.util.daemon.ZillaRuntime.*; |
|
|
|
import static org.cobbzilla.util.io.FileUtil.abs; |
|
|
|
import static org.cobbzilla.util.io.FileUtil.touch; |
|
|
|
import static org.cobbzilla.util.reflect.ReflectionUtil.copy; |
|
|
|
|
|
|
|
@Repository @Slf4j |
|
|
|
public class DeviceDAO extends AccountOwnedEntityDAO<Device> { |
|
|
@@ -58,43 +59,40 @@ public class DeviceDAO extends AccountOwnedEntityDAO<Device> { |
|
|
|
@Transactional |
|
|
|
@Override public Device create(@NonNull final Device device) { |
|
|
|
if (device.uninitialized()) return super.create(device); |
|
|
|
device.initDeviceType(); |
|
|
|
|
|
|
|
final var accountUuid = device.getAccount(); |
|
|
|
final var uninitializedDevices = findByAccountAndUninitialized(accountUuid); |
|
|
|
|
|
|
|
if (uninitializedDevices.size() < SPARE_DEVICES_PER_ACCOUNT_THRESHOLD |
|
|
|
&& !configuration.getBean(AccountDAO.class).findByUuid(accountUuid).isRoot()) { |
|
|
|
ensureAllSpareDevices(accountUuid, device.getNetwork()); |
|
|
|
} |
|
|
|
|
|
|
|
final Device result; |
|
|
|
// run the above creation of spare devices in parallel, but if there were no spare devices loaded before that, |
|
|
|
// create a brand new entry here: |
|
|
|
if (uninitializedDevices.isEmpty()) { |
|
|
|
log.info("create: no uninitialized devices for account " + accountUuid); |
|
|
|
background(() -> { |
|
|
|
if (!configuration.getBean(AccountDAO.class).findByUuid(accountUuid).isRoot()) { |
|
|
|
ensureAllSpareDevices(accountUuid, device.getNetwork()); |
|
|
|
} |
|
|
|
}); |
|
|
|
// just create the device now: |
|
|
|
device.initTotpKey(); |
|
|
|
device.initDeviceType(); |
|
|
|
final var d = super.create(device); |
|
|
|
refreshVpnUsers(); |
|
|
|
return d; |
|
|
|
result = super.create(device); |
|
|
|
} else { |
|
|
|
final var d = uninitializedDevices.get(0); |
|
|
|
d.initialize(device); |
|
|
|
return update(d); |
|
|
|
// update will do refreshVpnUsers |
|
|
|
final var uninitialized = uninitializedDevices.get(0); |
|
|
|
copy(uninitialized, device); |
|
|
|
result = super.update(uninitialized); |
|
|
|
} |
|
|
|
|
|
|
|
refreshVpnUsers(); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@Override @NonNull public Device update(@NonNull final Device updateRequest) { |
|
|
|
final var old = findByUuid(updateRequest.getUuid()); |
|
|
|
|
|
|
|
if (old != null && old.uninitialized()) updateRequest.initTotpKey(); |
|
|
|
updateRequest.initDeviceType(); |
|
|
|
|
|
|
|
final var updated = super.update(updateRequest); |
|
|
|
if (old.uninitialized() && !updated.uninitialized() |
|
|
|
&& findByAccountAndUninitialized(old.getAccount()).size() < SPARE_DEVICES_PER_ACCOUNT_THRESHOLD) { |
|
|
|
background(() -> ensureAllSpareDevices(old.getAccount(), old.getNetwork())); |
|
|
|
} |
|
|
|
final var toUpdate = (Device) readOnlySession().get(Device.class, updateRequest.getUuid()); |
|
|
|
if (toUpdate == null) die("Cannot find device to update with uuid: " + updateRequest.getUuid()); |
|
|
|
if (toUpdate.uninitialized()) die("Cannot update special devices: " + updateRequest.getName()); |
|
|
|
|
|
|
|
final var updated = super.update(toUpdate); |
|
|
|
refreshVpnUsers(); |
|
|
|
return updated; |
|
|
|
} |
|
|
@@ -102,12 +100,10 @@ public class DeviceDAO extends AccountOwnedEntityDAO<Device> { |
|
|
|
@Override public void delete(String uuid) { |
|
|
|
final Device device = findByUuid(uuid); |
|
|
|
if (device != null) { |
|
|
|
if (device.uninitialized()) die("Cannot delete special device: " + device.getName()); |
|
|
|
|
|
|
|
dataDAO.deleteDevice(uuid); |
|
|
|
super.delete(uuid); |
|
|
|
if (device.uninitialized() && findByAccountAndUninitialized(device.getAccount()).size() |
|
|
|
< SPARE_DEVICES_PER_ACCOUNT_THRESHOLD) { |
|
|
|
background(() -> ensureAllSpareDevices(device.getAccount(), device.getNetwork())); |
|
|
|
} |
|
|
|
refreshVpnUsers(); |
|
|
|
} |
|
|
|
} |
|
|
|