setting.go 4.4 KB

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