batch.go 3.6 KB

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