user_handlers.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package handlers
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/gin-gonic/gin"
  6. "github.com/seaweedfs/seaweedfs/weed/admin/dash"
  7. "github.com/seaweedfs/seaweedfs/weed/admin/view/app"
  8. "github.com/seaweedfs/seaweedfs/weed/admin/view/layout"
  9. "github.com/seaweedfs/seaweedfs/weed/glog"
  10. )
  11. // UserHandlers contains all the HTTP handlers for user management
  12. type UserHandlers struct {
  13. adminServer *dash.AdminServer
  14. }
  15. // NewUserHandlers creates a new instance of UserHandlers
  16. func NewUserHandlers(adminServer *dash.AdminServer) *UserHandlers {
  17. return &UserHandlers{
  18. adminServer: adminServer,
  19. }
  20. }
  21. // ShowObjectStoreUsers renders the object store users management page
  22. func (h *UserHandlers) ShowObjectStoreUsers(c *gin.Context) {
  23. // Get object store users data from the server
  24. usersData := h.getObjectStoreUsersData(c)
  25. // Render HTML template
  26. c.Header("Content-Type", "text/html")
  27. usersComponent := app.ObjectStoreUsers(usersData)
  28. layoutComponent := layout.Layout(c, usersComponent)
  29. err := layoutComponent.Render(c.Request.Context(), c.Writer)
  30. if err != nil {
  31. c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to render template: " + err.Error()})
  32. return
  33. }
  34. }
  35. // GetUsers returns the list of users as JSON
  36. func (h *UserHandlers) GetUsers(c *gin.Context) {
  37. users, err := h.adminServer.GetObjectStoreUsers()
  38. if err != nil {
  39. c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get users: " + err.Error()})
  40. return
  41. }
  42. c.JSON(http.StatusOK, gin.H{"users": users})
  43. }
  44. // CreateUser handles user creation
  45. func (h *UserHandlers) CreateUser(c *gin.Context) {
  46. var req dash.CreateUserRequest
  47. if err := c.ShouldBindJSON(&req); err != nil {
  48. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request: " + err.Error()})
  49. return
  50. }
  51. // Validate required fields
  52. if req.Username == "" {
  53. c.JSON(http.StatusBadRequest, gin.H{"error": "Username is required"})
  54. return
  55. }
  56. user, err := h.adminServer.CreateObjectStoreUser(req)
  57. if err != nil {
  58. glog.Errorf("Failed to create user %s: %v", req.Username, err)
  59. c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create user: " + err.Error()})
  60. return
  61. }
  62. c.JSON(http.StatusCreated, gin.H{
  63. "message": "User created successfully",
  64. "user": user,
  65. })
  66. }
  67. // UpdateUser handles user updates
  68. func (h *UserHandlers) UpdateUser(c *gin.Context) {
  69. username := c.Param("username")
  70. if username == "" {
  71. c.JSON(http.StatusBadRequest, gin.H{"error": "Username is required"})
  72. return
  73. }
  74. var req dash.UpdateUserRequest
  75. if err := c.ShouldBindJSON(&req); err != nil {
  76. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request: " + err.Error()})
  77. return
  78. }
  79. user, err := h.adminServer.UpdateObjectStoreUser(username, req)
  80. if err != nil {
  81. glog.Errorf("Failed to update user %s: %v", username, err)
  82. c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update user: " + err.Error()})
  83. return
  84. }
  85. c.JSON(http.StatusOK, gin.H{
  86. "message": "User updated successfully",
  87. "user": user,
  88. })
  89. }
  90. // DeleteUser handles user deletion
  91. func (h *UserHandlers) DeleteUser(c *gin.Context) {
  92. username := c.Param("username")
  93. if username == "" {
  94. c.JSON(http.StatusBadRequest, gin.H{"error": "Username is required"})
  95. return
  96. }
  97. err := h.adminServer.DeleteObjectStoreUser(username)
  98. if err != nil {
  99. glog.Errorf("Failed to delete user %s: %v", username, err)
  100. c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete user: " + err.Error()})
  101. return
  102. }
  103. c.JSON(http.StatusOK, gin.H{
  104. "message": "User deleted successfully",
  105. })
  106. }
  107. // GetUserDetails returns detailed information about a specific user
  108. func (h *UserHandlers) GetUserDetails(c *gin.Context) {
  109. username := c.Param("username")
  110. if username == "" {
  111. c.JSON(http.StatusBadRequest, gin.H{"error": "Username is required"})
  112. return
  113. }
  114. user, err := h.adminServer.GetObjectStoreUserDetails(username)
  115. if err != nil {
  116. c.JSON(http.StatusNotFound, gin.H{"error": "User not found: " + err.Error()})
  117. return
  118. }
  119. c.JSON(http.StatusOK, user)
  120. }
  121. // CreateAccessKey creates a new access key for a user
  122. func (h *UserHandlers) CreateAccessKey(c *gin.Context) {
  123. username := c.Param("username")
  124. if username == "" {
  125. c.JSON(http.StatusBadRequest, gin.H{"error": "Username is required"})
  126. return
  127. }
  128. accessKey, err := h.adminServer.CreateAccessKey(username)
  129. if err != nil {
  130. glog.Errorf("Failed to create access key for user %s: %v", username, err)
  131. c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create access key: " + err.Error()})
  132. return
  133. }
  134. c.JSON(http.StatusCreated, gin.H{
  135. "message": "Access key created successfully",
  136. "access_key": accessKey,
  137. })
  138. }
  139. // DeleteAccessKey deletes an access key for a user
  140. func (h *UserHandlers) DeleteAccessKey(c *gin.Context) {
  141. username := c.Param("username")
  142. accessKeyId := c.Param("accessKeyId")
  143. if username == "" || accessKeyId == "" {
  144. c.JSON(http.StatusBadRequest, gin.H{"error": "Username and access key ID are required"})
  145. return
  146. }
  147. err := h.adminServer.DeleteAccessKey(username, accessKeyId)
  148. if err != nil {
  149. glog.Errorf("Failed to delete access key %s for user %s: %v", accessKeyId, username, err)
  150. c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete access key: " + err.Error()})
  151. return
  152. }
  153. c.JSON(http.StatusOK, gin.H{
  154. "message": "Access key deleted successfully",
  155. })
  156. }
  157. // GetUserPolicies returns the policies for a user
  158. func (h *UserHandlers) GetUserPolicies(c *gin.Context) {
  159. username := c.Param("username")
  160. if username == "" {
  161. c.JSON(http.StatusBadRequest, gin.H{"error": "Username is required"})
  162. return
  163. }
  164. policies, err := h.adminServer.GetUserPolicies(username)
  165. if err != nil {
  166. c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get user policies: " + err.Error()})
  167. return
  168. }
  169. c.JSON(http.StatusOK, gin.H{"policies": policies})
  170. }
  171. // UpdateUserPolicies updates the policies for a user
  172. func (h *UserHandlers) UpdateUserPolicies(c *gin.Context) {
  173. username := c.Param("username")
  174. if username == "" {
  175. c.JSON(http.StatusBadRequest, gin.H{"error": "Username is required"})
  176. return
  177. }
  178. var req dash.UpdateUserPoliciesRequest
  179. if err := c.ShouldBindJSON(&req); err != nil {
  180. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request: " + err.Error()})
  181. return
  182. }
  183. err := h.adminServer.UpdateUserPolicies(username, req.Actions)
  184. if err != nil {
  185. glog.Errorf("Failed to update policies for user %s: %v", username, err)
  186. c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update user policies: " + err.Error()})
  187. return
  188. }
  189. c.JSON(http.StatusOK, gin.H{
  190. "message": "User policies updated successfully",
  191. })
  192. }
  193. // getObjectStoreUsersData retrieves object store users data from the server
  194. func (h *UserHandlers) getObjectStoreUsersData(c *gin.Context) dash.ObjectStoreUsersData {
  195. username := c.GetString("username")
  196. if username == "" {
  197. username = "admin"
  198. }
  199. // Get object store users
  200. users, err := h.adminServer.GetObjectStoreUsers()
  201. if err != nil {
  202. glog.Errorf("Failed to get object store users: %v", err)
  203. // Return empty data on error
  204. return dash.ObjectStoreUsersData{
  205. Username: username,
  206. Users: []dash.ObjectStoreUser{},
  207. TotalUsers: 0,
  208. LastUpdated: time.Now(),
  209. }
  210. }
  211. return dash.ObjectStoreUsersData{
  212. Username: username,
  213. Users: users,
  214. TotalUsers: len(users),
  215. LastUpdated: time.Now(),
  216. }
  217. }