You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.7 KiB

  1. // Copyright 2017 The Gitea 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 private
  5. import (
  6. "crypto/tls"
  7. "encoding/json"
  8. "fmt"
  9. "net"
  10. "net/http"
  11. "net/url"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/httplib"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/setting"
  16. )
  17. func newRequest(url, method string) *httplib.Request {
  18. return httplib.NewRequest(url, method).Header("Authorization",
  19. fmt.Sprintf("Bearer %s", setting.InternalToken))
  20. }
  21. // Response internal request response
  22. type Response struct {
  23. Err string `json:"err"`
  24. }
  25. func decodeJSONError(resp *http.Response) *Response {
  26. var res Response
  27. err := json.NewDecoder(resp.Body).Decode(&res)
  28. if err != nil {
  29. res.Err = err.Error()
  30. }
  31. return &res
  32. }
  33. func newInternalRequest(url, method string) *httplib.Request {
  34. req := newRequest(url, method).SetTLSClientConfig(&tls.Config{
  35. InsecureSkipVerify: true,
  36. ServerName: setting.Domain,
  37. })
  38. if setting.Protocol == setting.UnixSocket {
  39. req.SetTransport(&http.Transport{
  40. Dial: func(_, _ string) (net.Conn, error) {
  41. return net.Dial("unix", setting.HTTPAddr)
  42. },
  43. })
  44. }
  45. return req
  46. }
  47. // CheckUnitUser check whether user could visit the unit of this repository
  48. func CheckUnitUser(userID, repoID int64, isAdmin bool, unitType models.UnitType) (*models.AccessMode, error) {
  49. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/repositories/%d/user/%d/checkunituser?isAdmin=%t&unitType=%d", repoID, userID, isAdmin, unitType)
  50. log.GitLogger.Trace("CheckUnitUser: %s", reqURL)
  51. resp, err := newInternalRequest(reqURL, "GET").Response()
  52. if err != nil {
  53. return nil, err
  54. }
  55. defer resp.Body.Close()
  56. if resp.StatusCode != 200 {
  57. return nil, fmt.Errorf("Failed to CheckUnitUser: %s", decodeJSONError(resp).Err)
  58. }
  59. var a models.AccessMode
  60. if err := json.NewDecoder(resp.Body).Decode(&a); err != nil {
  61. return nil, err
  62. }
  63. return &a, nil
  64. }
  65. // GetRepositoryByOwnerAndName returns the repository by given ownername and reponame.
  66. func GetRepositoryByOwnerAndName(ownerName, repoName string) (*models.Repository, error) {
  67. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/repo/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName))
  68. log.GitLogger.Trace("GetRepositoryByOwnerAndName: %s", reqURL)
  69. resp, err := newInternalRequest(reqURL, "GET").Response()
  70. if err != nil {
  71. return nil, err
  72. }
  73. defer resp.Body.Close()
  74. if resp.StatusCode != 200 {
  75. return nil, fmt.Errorf("Failed to get repository: %s", decodeJSONError(resp).Err)
  76. }
  77. var repo models.Repository
  78. if err := json.NewDecoder(resp.Body).Decode(&repo); err != nil {
  79. return nil, err
  80. }
  81. return &repo, nil
  82. }