admin_user.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package gogs
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "fmt"
  9. )
  10. type CreateUserOption struct {
  11. SourceID int64 `json:"source_id"`
  12. LoginName string `json:"login_name"`
  13. Username string `json:"username" binding:"Required;AlphaDashDot;MaxSize(35)"`
  14. FullName string `json:"full_name" binding:"MaxSize(100)"`
  15. Email string `json:"email" binding:"Required;Email;MaxSize(254)"`
  16. Password string `json:"password" binding:"MaxSize(255)"`
  17. SendNotify bool `json:"send_notify"`
  18. }
  19. func (c *Client) AdminCreateUser(opt CreateUserOption) (*User, error) {
  20. body, err := json.Marshal(&opt)
  21. if err != nil {
  22. return nil, err
  23. }
  24. user := new(User)
  25. return user, c.getParsedResponse("POST", "/admin/users", jsonHeader, bytes.NewReader(body), user)
  26. }
  27. type EditUserOption struct {
  28. SourceID int64 `json:"source_id"`
  29. LoginName string `json:"login_name"`
  30. FullName string `json:"full_name" binding:"MaxSize(100)"`
  31. Email string `json:"email" binding:"Required;Email;MaxSize(254)"`
  32. Password string `json:"password" binding:"MaxSize(255)"`
  33. Website string `json:"website" binding:"MaxSize(50)"`
  34. Location string `json:"location" binding:"MaxSize(50)"`
  35. Active *bool `json:"active"`
  36. Admin *bool `json:"admin"`
  37. AllowGitHook *bool `json:"allow_git_hook"`
  38. AllowImportLocal *bool `json:"allow_import_local"`
  39. MaxRepoCreation *int `json:"max_repo_creation"`
  40. }
  41. func (c *Client) AdminEditUser(user string, opt EditUserOption) error {
  42. body, err := json.Marshal(&opt)
  43. if err != nil {
  44. return err
  45. }
  46. _, err = c.getResponse("PATCH", fmt.Sprintf("/admin/users/%s", user), jsonHeader, bytes.NewReader(body))
  47. return err
  48. }
  49. func (c *Client) AdminDeleteUser(user string) error {
  50. _, err := c.getResponse("DELETE", fmt.Sprintf("/admin/users/%s", user), nil, nil)
  51. return err
  52. }
  53. func (c *Client) AdminCreateUserPublicKey(user string, opt CreateKeyOption) (*PublicKey, error) {
  54. body, err := json.Marshal(&opt)
  55. if err != nil {
  56. return nil, err
  57. }
  58. key := new(PublicKey)
  59. return key, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/keys", user), jsonHeader, bytes.NewReader(body), key)
  60. }