MDM.svelte 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <script>
  2. const https = require("https");
  3. let id = "";
  4. let devices;
  5. let policy, profile;
  6. export let privat;
  7. function getter (url) {
  8. const options = { headers: { "X-User-Access-Token": privat.MDMKEY } };
  9. return new Promise((resolve, reject) => {
  10. https.get(url, options, async (res) => {
  11. try {
  12. let body = "";
  13. res.setEncoding("utf-8");
  14. for await (const chunk of res) {
  15. body += chunk;
  16. }
  17. resolve(JSON.parse(body));
  18. } catch (e) {
  19. reject(e);
  20. }
  21. });
  22. });
  23. }
  24. async function fetch_json(id) {
  25. const url = encodeURI(
  26. `https://${privat.MDMSERVER}/relution/api/v1/autoenrollments?getNonpagedCount=true&limit=1&filter={"type":"containsString","fieldName":"serialNumber","contains":"${id}"}&field=uuid`
  27. );
  28. devices = await getter(url);
  29. }
  30. function get_profiles() {
  31. const url = encodeURI(
  32. `https://${privat.MDMSERVER}/relution/api/v1/devices/deps/profiles?getItems=true&getNonpagedCount=false`
  33. );
  34. return getter(url)
  35. }
  36. function get_policies() {
  37. const url = encodeURI(
  38. `https://${privat.MDMSERVER}/relution/api/v1/policies?getNonpagedCount=true&limit=100&offset=0&sortOrder=-modificationDate&filter={"type":"logOp","operation":"AND","filters":[{"type":"stringEnum","fieldName":"platform","values":["IOS"]},{"type":"boolean","fieldName":"versions.published","value":true},{"type":"string","fieldName":"deletionDate","value":"NULL"}]}`
  39. );
  40. return getter(url)
  41. }
  42. function set_policy(device) {
  43. if (devices.total !== 1 && !policy) return;
  44. device.defaultPolicy = { uuid: policy };
  45. if (profile) device.profileUuid = profile
  46. const data = JSON.stringify(device);
  47. console.log(data);
  48. const options = {
  49. hostname: privat.MDMSERVER,
  50. port: 443,
  51. path: `/relution/api/v1/autoenrollments/${device.uuid}`,
  52. method: "PUT",
  53. headers: {
  54. "Content-Type": "application/json",
  55. "Content-Length": data.length,
  56. "X-User-Access-Token": privat.MDMKEY,
  57. },
  58. };
  59. const req = https.request(options, (res) => {
  60. console.log(`statusCode: ${res.statusCode}`);
  61. if (res.statusCode === 200) {
  62. id = "";
  63. devices = undefined;
  64. }
  65. });
  66. req.on("error", (error) => {
  67. console.error(error);
  68. });
  69. req.write(data);
  70. req.end();
  71. }
  72. $: console.log(profile)
  73. </script>
  74. Verfügbare DEP-Profile:
  75. {#await get_profiles() then profiles}
  76. {#each profiles.results as p}
  77. <br /><input type="radio" bind:group={profile} value={p.uuid} />{p.profileName}
  78. {/each}
  79. <br />ausgewählt {profiles.results.find((p) => p.uuid === profile)?.profileName ||
  80. "keins, bzw vorausgewählt"}
  81. {/await}
  82. <br />
  83. Verfügbare Policies:
  84. {#await get_policies() then policies}
  85. {#each policies.results as p}
  86. <br /><input type="radio" bind:group={policy} value={p.uuid} />{p.name}
  87. {/each}
  88. <br />ausgewählt {policies.results.find((p) => p.uuid === policy)?.name ||
  89. "keine"}
  90. {/await}
  91. <br />
  92. <br /><input
  93. type="text"
  94. bind:value={id}
  95. on:keyup={(e) =>
  96. e.code === "Enter" ? set_policy(devices.results[0]) : fetch_json(id)}
  97. />
  98. {#if devices}
  99. {#if devices.total > 1}
  100. {devices.total} Ergebnisse
  101. {:else if devices.total === 0}
  102. keine Ergebnisse
  103. {:else if devices.total === 1}
  104. <br />{devices.results[0].uuid}
  105. <br />{devices.results[0].serialNumber}
  106. <br />{devices.results[0].description}
  107. {:else}Fehler
  108. {/if}
  109. {/if}