MDM.svelte 3.1 KB

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