setting.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. module "imuslab.com/arozos/mod/modules"
  6. )
  7. type settingModule struct {
  8. Name string //Name of the setting module.
  9. Desc string //Description of the setting module
  10. IconPath string //Icon path for the setting module
  11. Group string //Accept {}
  12. StartDir string //Startup Directory / path
  13. RequireAdmin bool //If the setting require admin access.
  14. //^ Enable this to hide this setting from non-admin users, but for API call, module has to handle admin check themselves.
  15. }
  16. type settingGroup struct {
  17. Name string
  18. Group string
  19. IconPath string
  20. Desc string
  21. }
  22. var (
  23. settingModules []settingModule
  24. )
  25. func SystemSettingInit() {
  26. http.HandleFunc("/system/setting/list", system_setting_handleListing)
  27. //Register the module
  28. moduleHandler.RegisterModule(module.ModuleInfo{
  29. Name: "System Setting",
  30. Desc: "Cutomize your systems to fit your needs",
  31. Group: "System Settings",
  32. IconPath: "SystemAO/system_setting/img/small_icon.png",
  33. Version: "1.0",
  34. StartDir: "SystemAO/system_setting/index.html",
  35. SupportFW: true,
  36. InitFWSize: []int{1080, 580},
  37. LaunchFWDir: "SystemAO/system_setting/index.html",
  38. SupportEmb: false,
  39. })
  40. }
  41. //Setting group defination. Your setting module defination must match the group in-order to be shown
  42. func system_setting_getSettingGroups() []settingGroup {
  43. return []settingGroup{
  44. {
  45. Name: "Host Information",
  46. Group: "Info",
  47. IconPath: "SystemAO/system_setting/img/server.svg",
  48. Desc: "Config and info about the Server Host",
  49. },
  50. {
  51. Name: "Devices & IoT",
  52. Group: "Device",
  53. IconPath: "SystemAO/system_setting/img/device.svg",
  54. Desc: "Connected clients and IoT devices",
  55. },
  56. {
  57. Name: "Module Management",
  58. Group: "Module",
  59. IconPath: "SystemAO/system_setting/img/module.svg",
  60. Desc: "List of modules loaded in the system",
  61. },
  62. {
  63. Name: "Disk & Storage",
  64. Group: "Disk",
  65. IconPath: "SystemAO/system_setting/img/drive.svg",
  66. Desc: "Manage Storage Devices and Disks",
  67. },
  68. {
  69. Name: "Network & Connection",
  70. Group: "Network",
  71. IconPath: "SystemAO/system_setting/img/network.svg",
  72. Desc: "Manage Host Network and Connections",
  73. },
  74. {
  75. Name: "Users & Groups",
  76. Group: "Users",
  77. IconPath: "SystemAO/system_setting/img/users.svg",
  78. Desc: "Add, removed or edit users and groups",
  79. },
  80. {
  81. Name: "Clusters & Scheduling",
  82. Group: "Cluster",
  83. IconPath: "SystemAO/system_setting/img/cluster.svg",
  84. Desc: "System Functions related to Time and Dates",
  85. },
  86. {
  87. Name: "Security & Keys",
  88. Group: "Security",
  89. IconPath: "SystemAO/system_setting/img/security.svg",
  90. Desc: "System Security and Keypairs",
  91. },
  92. {
  93. Name: "Advance Options",
  94. Group: "Advance",
  95. IconPath: "SystemAO/system_setting/img/code.svg",
  96. Desc: "Advance configs for developers",
  97. },
  98. {
  99. Name: "About ArOZ",
  100. Group: "About",
  101. IconPath: "SystemAO/system_setting/img/info.svg",
  102. Desc: "Information of the current running ArOZ Online System",
  103. },
  104. }
  105. }
  106. func registerSetting(thismodule settingModule) {
  107. settingModules = append(settingModules, thismodule)
  108. }
  109. //List all the setting modules and output it as JSON
  110. func system_setting_handleListing(w http.ResponseWriter, r *http.Request) {
  111. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  112. if err != nil {
  113. sendErrorResponse(w, "User not logged in")
  114. return
  115. }
  116. allSettingGroups := system_setting_getSettingGroups()
  117. listGroup, _ := mv(r, "listGroup", false)
  118. if len(listGroup) > 0 {
  119. //List the given group
  120. var results []settingModule
  121. for _, setMod := range settingModules {
  122. if setMod.Group == listGroup {
  123. //Check if the module is admin only.
  124. if setMod.RequireAdmin && userinfo.IsAdmin() {
  125. //Admin module and user is admin. Append to list
  126. results = append(results, setMod)
  127. } else if setMod.RequireAdmin == false {
  128. //Normal module. Append to list
  129. results = append(results, setMod)
  130. }
  131. }
  132. }
  133. if len(results) > 0 {
  134. jsonString, _ := json.Marshal(results)
  135. sendJSONResponse(w, string(jsonString))
  136. return
  137. } else {
  138. //This group not found,
  139. sendErrorResponse(w, "Group not found")
  140. return
  141. }
  142. } else {
  143. //List all root groups
  144. jsonString, _ := json.Marshal(allSettingGroups)
  145. sendJSONResponse(w, string(jsonString))
  146. return
  147. }
  148. }