batch.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package auth
  2. /*
  3. Auth batch operation handler
  4. author: tobychui
  5. This handler handles batch operations related to authentications
  6. Allowing easy management of the user lists
  7. */
  8. import (
  9. "encoding/json"
  10. "log"
  11. "net/http"
  12. "strings"
  13. "imuslab.com/arozos/mod/utils"
  14. )
  15. /*
  16. CreateUserAccountsFromCSV
  17. This function allow mass import of user accounts for organization purpses.
  18. Must be in the format of:{ username, default password, default group } format.
  19. Each user occupied one new line
  20. */
  21. func (a *AuthAgent) HandleCreateUserAccountsFromCSV(w http.ResponseWriter, r *http.Request) {
  22. csvContent, err := utils.Mv(r, "csv", true)
  23. if err != nil {
  24. sendErrorResponse(w, "Invalid csv")
  25. return
  26. }
  27. //Process the csv and create user accounts
  28. newusers := [][]string{}
  29. csvContent = strings.ReplaceAll(csvContent, "\r\n", "\n")
  30. lines := strings.Split(csvContent, "\n")
  31. for _, line := range lines {
  32. data := strings.Split(line, ",")
  33. if len(data) >= 3 {
  34. newusers = append(newusers, data)
  35. }
  36. }
  37. errors := []string{}
  38. //Ok. Add the valid users to the system
  39. for _, userCreationSetting := range newusers {
  40. //Check if this user already exists
  41. if a.UserExists(userCreationSetting[0]) {
  42. errors = append(errors, "User "+userCreationSetting[0]+" already exists! Skipping.")
  43. continue
  44. }
  45. a.CreateUserAccount(userCreationSetting[0], userCreationSetting[1], []string{userCreationSetting[2]})
  46. }
  47. js, _ := json.Marshal(errors)
  48. sendJSONResponse(w, string(js))
  49. }
  50. /*
  51. HandleUserDeleteByGroup handles user batch delete request by group name
  52. Set exact = true will only delete users which the user is
  53. 1. inside the given group and
  54. 2. that group is his / her only group
  55. Require paramter: group, exact
  56. */
  57. func (a *AuthAgent) HandleUserDeleteByGroup(w http.ResponseWriter, r *http.Request) {
  58. group, err := utils.Mv(r, "group", true)
  59. if err != nil {
  60. sendErrorResponse(w, "Invalid group")
  61. }
  62. requireExact := true //Default true
  63. exact, _ := utils.Mv(r, "exact", true)
  64. if exact == "false" {
  65. requireExact = false
  66. }
  67. entries, _ := a.Database.ListTable("auth")
  68. deletePendingUsernames := []string{}
  69. for _, keypairs := range entries {
  70. if strings.Contains(string(keypairs[0]), "group/") {
  71. //Get username
  72. username := strings.Split(string(keypairs[0]), "/")[1]
  73. usergroup := []string{}
  74. a.Database.Read("auth", "group/"+username, &usergroup)
  75. if requireExact {
  76. if len(usergroup) == 1 && usergroup[0] == group {
  77. deletePendingUsernames = append(deletePendingUsernames, username)
  78. }
  79. } else {
  80. if inSlice(usergroup, group) {
  81. deletePendingUsernames = append(deletePendingUsernames, username)
  82. }
  83. }
  84. }
  85. }
  86. for _, username := range deletePendingUsernames {
  87. a.UnregisterUser(username)
  88. }
  89. sendOK(w)
  90. }
  91. /*
  92. Export all the users into a csv file. Should only be usable via command line as a form of db backup.
  93. DO NOT EXPOSE THIS TO HTTP SERVER
  94. */
  95. func (a *AuthAgent) ExportUserListAsCSV() string {
  96. entries, _ := a.Database.ListTable("auth")
  97. results := [][]string{}
  98. for _, keypairs := range entries {
  99. if strings.Contains(string(keypairs[0]), "passhash/") {
  100. //This is a user registry
  101. key := string(keypairs[0])
  102. //Get username
  103. username := strings.Split(key, "/")[1]
  104. //Get usergroup
  105. usergroup := []string{}
  106. a.Database.Read("auth", "group/"+username, &usergroup)
  107. log.Println(usergroup)
  108. //Get user password hash
  109. passhash := string(keypairs[1])
  110. results = append(results, []string{username, passhash, strings.Join(usergroup, ",")})
  111. }
  112. }
  113. //Parse the results as csv
  114. csv := "Username,Password Hash,Group(s)\n"
  115. for _, line := range results {
  116. csv += line[0] + "," + line[1] + "," + line[2] + "\n"
  117. }
  118. csv = strings.TrimSpace(csv)
  119. return csv
  120. }