permission.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. permission "imuslab.com/arozos/mod/permission"
  6. prout "imuslab.com/arozos/mod/prouter"
  7. "imuslab.com/arozos/mod/utils"
  8. )
  9. func permissionNewHandler() {
  10. ph, err := permission.NewPermissionHandler(sysdb)
  11. if err != nil {
  12. systemWideLogger.PrintAndLog("Permission", "Permission Handler creation failed.", err)
  13. panic(err)
  14. }
  15. permissionHandler = ph
  16. permissionHandler.LoadPermissionGroupsFromDatabase()
  17. }
  18. func permissionInit() {
  19. //Register the permission handler, require authentication except listgroup
  20. adminRouter := prout.NewModuleRouter(prout.RouterOption{
  21. ModuleName: "System Setting",
  22. AdminOnly: true,
  23. UserHandler: userHandler,
  24. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  25. utils.SendErrorResponse(w, "Permission Denied")
  26. },
  27. })
  28. //Must be handled by default router
  29. http.HandleFunc("/system/permission/listgroup", func(w http.ResponseWriter, r *http.Request) {
  30. if authAgent.GetUserCounts() == 0 {
  31. //There is no user within the system. Only allow register of admin account
  32. js, _ := json.Marshal([]string{"administrator"})
  33. utils.SendJSONResponse(w, string(js))
  34. //permissionHandler.HandleListGroup(w, r)
  35. } else {
  36. //There are already users in the system. Only allow authorized users
  37. if authAgent.CheckAuth(r) {
  38. requestingUser, _ := userHandler.GetUserInfoFromRequest(w, r)
  39. if requestingUser != nil && requestingUser.IsAdmin() {
  40. permissionHandler.HandleListGroup(w, r)
  41. } else {
  42. errorHandlePermissionDenied(w, r)
  43. }
  44. } else {
  45. errorHandlePermissionDenied(w, r)
  46. return
  47. }
  48. }
  49. })
  50. adminRouter.HandleFunc("/system/permission/newgroup", permissionHandler.HandleGroupCreate)
  51. adminRouter.HandleFunc("/system/permission/editgroup", permissionHandler.HandleGroupEdit)
  52. adminRouter.HandleFunc("/system/permission/delgroup", permissionHandler.HandleGroupRemove)
  53. registerSetting(settingModule{
  54. Name: "Permission Groups",
  55. Desc: "Handle the permission of access in groups",
  56. IconPath: "SystemAO/users/img/small_icon.png",
  57. Group: "Users",
  58. StartDir: "SystemAO/users/group.html",
  59. RequireAdmin: true,
  60. })
  61. }