batch.go 3.8 KB

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