request.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package permission
  2. /*
  3. This is the handler to handle the permission request endpoints
  4. Group information are stored in database as follows
  5. group/{groupname} = module permissions
  6. isadmin/{groupname} = isAdmin
  7. quota/{groupname} = default quota in bytes
  8. */
  9. import (
  10. "encoding/json"
  11. "log"
  12. "net/http"
  13. "strconv"
  14. )
  15. //Handle group editing operations
  16. func (h *PermissionHandler) HandleListGroup(w http.ResponseWriter, r *http.Request) {
  17. listPermission, _ := mv(r, "showper", false)
  18. if listPermission == "" {
  19. //Only show the user group name
  20. results := []string{}
  21. for _, gp := range h.PermissionGroups {
  22. results = append(results, gp.Name)
  23. }
  24. jsonString, _ := json.Marshal(results)
  25. sendJSONResponse(w, string(jsonString))
  26. } else {
  27. //Show user group and its module permissions
  28. results := make(map[string][]interface{})
  29. for _, gp := range h.PermissionGroups {
  30. var thisGroupInfo []interface{}
  31. thisGroupInfo = append(thisGroupInfo, gp.AccessibleModules)
  32. thisGroupInfo = append(thisGroupInfo, gp.IsAdmin)
  33. thisGroupInfo = append(thisGroupInfo, gp.DefaultStorageQuota)
  34. results[gp.Name] = thisGroupInfo
  35. }
  36. jsonString, _ := json.Marshal(results)
  37. sendJSONResponse(w, string(jsonString))
  38. }
  39. }
  40. //Listing a group's detail for editing or updating the group content
  41. func (h *PermissionHandler) HandleGroupEdit(w http.ResponseWriter, r *http.Request) {
  42. groupname, err := mv(r, "groupname", true)
  43. if err != nil {
  44. sendErrorResponse(w, "Group name not defined")
  45. return
  46. }
  47. listmode, _ := mv(r, "list", false)
  48. if listmode == "" {
  49. //Edit update mode
  50. permission, err := mv(r, "permission", true)
  51. if err != nil {
  52. sendErrorResponse(w, "Group name not defined")
  53. return
  54. }
  55. permissionSlice := []string{}
  56. err = json.Unmarshal([]byte(permission), &permissionSlice)
  57. if err != nil {
  58. sendErrorResponse(w, "Failed to parse module list")
  59. return
  60. }
  61. isAdmin, err := mv(r, "isAdmin", true)
  62. if err != nil {
  63. sendErrorResponse(w, "Admin permission not defined")
  64. return
  65. }
  66. quota, err := mv(r, "defaultQuota", true)
  67. if err != nil {
  68. sendErrorResponse(w, "Default Quota not defined")
  69. return
  70. }
  71. interfaceModule, err := mv(r, "interfaceModule", true)
  72. if err != nil {
  73. sendErrorResponse(w, "Default Interface Module not defined")
  74. return
  75. }
  76. //Check if the group name already exists
  77. if !h.GroupExists(groupname) {
  78. sendErrorResponse(w, "Group not exists")
  79. return
  80. }
  81. quotaInt, err := strconv.Atoi(quota)
  82. if err != nil {
  83. sendErrorResponse(w, "Invalid Quota.")
  84. return
  85. }
  86. h.UpdatePermissionGroup(groupname, isAdmin == "true", int64(quotaInt), permissionSlice, interfaceModule)
  87. sendOK(w)
  88. } else {
  89. //Listing mode
  90. //Check if the group exists
  91. if !h.GroupExists(groupname) {
  92. sendErrorResponse(w, "Group not exists")
  93. return
  94. }
  95. //OK. Get the group information
  96. pg := h.GetPermissionGroupByName(groupname)
  97. //pg will not be nil because group exists has checked it availbilty
  98. jsonString, _ := json.Marshal(pg)
  99. sendJSONResponse(w, string(jsonString))
  100. }
  101. }
  102. func (h *PermissionHandler) HandleGroupCreate(w http.ResponseWriter, r *http.Request) {
  103. groupname, err := mv(r, "groupname", true)
  104. if err != nil {
  105. sendErrorResponse(w, "Group name not defined")
  106. return
  107. }
  108. permission, err := mv(r, "permission", true)
  109. if err != nil {
  110. sendErrorResponse(w, "Group name not defined")
  111. return
  112. }
  113. permissionSlice := []string{}
  114. err = json.Unmarshal([]byte(permission), &permissionSlice)
  115. if err != nil {
  116. sendErrorResponse(w, "Failed to parse module list")
  117. return
  118. }
  119. isAdmin, err := mv(r, "isAdmin", true)
  120. if err != nil {
  121. sendErrorResponse(w, "Admin permission not defined")
  122. return
  123. }
  124. quota, err := mv(r, "defaultQuota", true)
  125. if err != nil {
  126. sendErrorResponse(w, "Default Quota not defined")
  127. return
  128. }
  129. interfaceModule, err := mv(r, "interfaceModule", true)
  130. if err != nil {
  131. sendErrorResponse(w, "Default Interface Module not defined")
  132. return
  133. }
  134. //Check if the group name already exists
  135. if h.GroupExists(groupname) {
  136. sendErrorResponse(w, "Group already exists")
  137. return
  138. }
  139. quotaInt, err := strconv.Atoi(quota)
  140. if err != nil {
  141. sendErrorResponse(w, "Invalid Quota.")
  142. return
  143. }
  144. if quotaInt < -1 {
  145. sendErrorResponse(w, "Quota cannot be smaller than -1. (Set to -1 for unlimited quota)")
  146. return
  147. }
  148. //Migrated the creation process to a seperated function
  149. h.NewPermissionGroup(groupname, isAdmin == "true", int64(quotaInt), permissionSlice, interfaceModule)
  150. /*
  151. //OK. Write the results into database
  152. h.database.Write("permission", "group/" + groupname, permission)
  153. h.database.Write("permission", "isadmin/" + groupname, isAdmin)
  154. h.database.Write("permission", "quota/" + groupname, int64(quotaInt))
  155. h.database.Write("permission", "interfaceModule/" + groupname, interfaceModule)
  156. //Update the current cached permission group table
  157. h.LoadPermissionGroupsFromDatabase()
  158. */
  159. sendOK(w)
  160. log.Println("Creating New Permission Group:", groupname, permission, isAdmin, quota)
  161. }
  162. func (h *PermissionHandler) HandleGroupRemove(w http.ResponseWriter, r *http.Request) {
  163. groupname, err := mv(r, "groupname", true)
  164. if err != nil {
  165. sendErrorResponse(w, "Group name not defined")
  166. return
  167. }
  168. //Check if the group name exists
  169. if !h.GroupExists(groupname) {
  170. sendErrorResponse(w, "Group not exists")
  171. return
  172. }
  173. //Check if this is administrator group
  174. if groupname == "administrator" {
  175. sendErrorResponse(w, "You cannot remove Administrator group.")
  176. return
  177. }
  178. //Get the group by its name
  179. group := h.GetPermissionGroupByName(groupname)
  180. //Remove the group
  181. group.Remove()
  182. //Update the current cached permission group table
  183. newGroupList := []*PermissionGroup{}
  184. for _, pg := range h.PermissionGroups {
  185. if pg.Name != groupname {
  186. newGroupList = append(newGroupList, pg)
  187. }
  188. }
  189. h.PermissionGroups = newGroupList
  190. //Update 27-12-2020: Replaced database reload with new group list creation
  191. //h.LoadPermissionGroupsFromDatabase()
  192. sendOK(w)
  193. }