setting.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. settingGroup{
  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. settingGroup{
  51. Name: "Devices & IoT",
  52. Group: "Device",
  53. IconPath: "SystemAO/system_setting/img/device.svg",
  54. Desc: "Connected clients and IoT devices",
  55. },
  56. settingGroup{
  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. settingGroup{
  63. Name: "Disk & Storage",
  64. Group: "Disk",
  65. IconPath: "SystemAO/system_setting/img/drive.svg",
  66. Desc: "Manage Storage Devices and Disks",
  67. },
  68. settingGroup{
  69. Name: "Network & Connection",
  70. Group: "Network",
  71. IconPath: "SystemAO/system_setting/img/network.svg",
  72. Desc: "Manage Host Network and Connections",
  73. },
  74. settingGroup{
  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. settingGroup{
  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. settingGroup{
  87. Name: "Advance Options",
  88. Group: "Advance",
  89. IconPath: "SystemAO/system_setting/img/code.svg",
  90. Desc: "Advance configs for developers",
  91. },
  92. settingGroup{
  93. Name: "About ArOZ",
  94. Group: "About",
  95. IconPath: "SystemAO/system_setting/img/info.svg",
  96. Desc: "Information of the current running ArOZ Online System",
  97. },
  98. }
  99. }
  100. func registerSetting(thismodule settingModule) {
  101. settingModules = append(settingModules, thismodule)
  102. }
  103. //List all the setting modules and output it as JSON
  104. func system_setting_handleListing(w http.ResponseWriter, r *http.Request) {
  105. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  106. if err != nil {
  107. sendErrorResponse(w, "User not logged in")
  108. return
  109. }
  110. allSettingGroups := system_setting_getSettingGroups()
  111. listGroup, _ := mv(r, "listGroup", false)
  112. if len(listGroup) > 0 {
  113. //List the given group
  114. var results []settingModule
  115. for _, setMod := range settingModules {
  116. if setMod.Group == listGroup {
  117. //Check if the module is admin only.
  118. if setMod.RequireAdmin && userinfo.IsAdmin() {
  119. //Admin module and user is admin. Append to list
  120. results = append(results, setMod)
  121. } else if setMod.RequireAdmin == false {
  122. //Normal module. Append to list
  123. results = append(results, setMod)
  124. }
  125. }
  126. }
  127. if len(results) > 0 {
  128. jsonString, _ := json.Marshal(results)
  129. sendJSONResponse(w, string(jsonString))
  130. return
  131. } else {
  132. //This group not found,
  133. sendErrorResponse(w, "Group not found")
  134. return
  135. }
  136. } else {
  137. //List all root groups
  138. jsonString, _ := json.Marshal(allSettingGroups)
  139. sendJSONResponse(w, string(jsonString))
  140. return
  141. }
  142. }