diff options
author | Antoine GIRARD <sapk@users.noreply.github.com> | 2018-10-30 07:20:13 +0100 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2018-10-30 14:20:13 +0800 |
commit | 617a2433a3dd2e32188574ac7b3765bec5082123 (patch) | |
tree | 10e669a14d127e68b6a3a7e681da5da6057ac16f /modules/private | |
parent | aefeb8c46587819942125a4bbba328d203e3275a (diff) | |
download | gitea-617a2433a3dd2e32188574ac7b3765bec5082123.tar.gz gitea-617a2433a3dd2e32188574ac7b3765bec5082123.zip |
Make gitea serv use api/internal (#4886)
* Start to move to internal/private
* Add GetPublicKeyByID
* Add HasDeployKey
* Add private.UpdateDeployKeyUpdated
* Add private.GetUserByKeyID
* Add private.AccessLevel
* Add private.CheckUnitUser
* Fix mistakes I made
* Some cleaning + moving code to separate files
* Fix error handling
* Remove useless error handling for setup
* lint: fix comment on exported func
* fix copyright header
* Fix order of args
Diffstat (limited to 'modules/private')
-rw-r--r-- | modules/private/branch.go | 2 | ||||
-rw-r--r-- | modules/private/internal.go | 67 | ||||
-rw-r--r-- | modules/private/key.go | 116 |
3 files changed, 173 insertions, 12 deletions
diff --git a/modules/private/branch.go b/modules/private/branch.go index fed66d29ff..cadbf6c88c 100644 --- a/modules/private/branch.go +++ b/modules/private/branch.go @@ -33,7 +33,7 @@ func GetProtectedBranchBy(repoID int64, branchName string) (*models.ProtectedBra // All 2XX status codes are accepted and others will return an error if resp.StatusCode/100 != 2 { - return nil, fmt.Errorf("Failed to update public key: %s", decodeJSONError(resp).Err) + return nil, fmt.Errorf("Failed to get protected branch: %s", decodeJSONError(resp).Err) } return &branch, nil diff --git a/modules/private/internal.go b/modules/private/internal.go index ac2fe56b87..f4ac1c515a 100644 --- a/modules/private/internal.go +++ b/modules/private/internal.go @@ -11,6 +11,7 @@ import ( "net" "net/http" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/httplib" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -49,22 +50,66 @@ func newInternalRequest(url, method string) *httplib.Request { return req } -// UpdatePublicKeyUpdated update publick key updates -func UpdatePublicKeyUpdated(keyID int64) error { - // Ask for running deliver hook and test pull request tasks. - reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/update", keyID) - log.GitLogger.Trace("UpdatePublicKeyUpdated: %s", reqURL) +// CheckUnitUser check whether user could visit the unit of this repository +func CheckUnitUser(userID, repoID int64, isAdmin bool, unitType models.UnitType) (bool, error) { + reqURL := setting.LocalURL + fmt.Sprintf("api/internal/repositories/%d/user/%d/checkunituser?isAdmin=%t&unitType=%d", repoID, userID, isAdmin, unitType) + log.GitLogger.Trace("AccessLevel: %s", reqURL) - resp, err := newInternalRequest(reqURL, "POST").Response() + resp, err := newInternalRequest(reqURL, "GET").Response() if err != nil { - return err + return false, err } + defer resp.Body.Close() + + if resp.StatusCode == 200 { + return true, nil + } + return false, nil +} + +// AccessLevel returns the Access a user has to a repository. Will return NoneAccess if the +// user does not have access. +func AccessLevel(userID, repoID int64) (*models.AccessMode, error) { + reqURL := setting.LocalURL + fmt.Sprintf("api/internal/repositories/%d/user/%d/accesslevel", repoID, userID) + log.GitLogger.Trace("AccessLevel: %s", reqURL) + resp, err := newInternalRequest(reqURL, "GET").Response() + if err != nil { + return nil, err + } defer resp.Body.Close() - // All 2XX status codes are accepted and others will return an error - if resp.StatusCode/100 != 2 { - return fmt.Errorf("Failed to update public key: %s", decodeJSONError(resp).Err) + if resp.StatusCode != 200 { + return nil, fmt.Errorf("Failed to get user access level: %s", decodeJSONError(resp).Err) + } + + var a models.AccessMode + if err := json.NewDecoder(resp.Body).Decode(&a); err != nil { + return nil, err } - return nil + + return &a, nil +} + +// GetRepositoryByOwnerAndName returns the repository by given ownername and reponame. +func GetRepositoryByOwnerAndName(ownerName, repoName string) (*models.Repository, error) { + reqURL := setting.LocalURL + fmt.Sprintf("api/internal/repo/%s/%s", ownerName, repoName) + log.GitLogger.Trace("GetRepositoryByOwnerAndName: %s", reqURL) + + resp, err := newInternalRequest(reqURL, "GET").Response() + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode != 200 { + return nil, fmt.Errorf("Failed to get repository: %s", decodeJSONError(resp).Err) + } + + var repo models.Repository + if err := json.NewDecoder(resp.Body).Decode(&repo); err != nil { + return nil, err + } + + return &repo, nil } diff --git a/modules/private/key.go b/modules/private/key.go new file mode 100644 index 0000000000..86d0a730d1 --- /dev/null +++ b/modules/private/key.go @@ -0,0 +1,116 @@ +// Copyright 2018 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package private + +import ( + "encoding/json" + "fmt" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting" +) + +// UpdateDeployKeyUpdated update deploy key updates +func UpdateDeployKeyUpdated(keyID int64, repoID int64) error { + reqURL := setting.LocalURL + fmt.Sprintf("api/internal/repositories/%d/keys/%d/update", repoID, keyID) + log.GitLogger.Trace("UpdateDeployKeyUpdated: %s", reqURL) + + resp, err := newInternalRequest(reqURL, "POST").Response() + if err != nil { + return err + } + + defer resp.Body.Close() + + // All 2XX status codes are accepted and others will return an error + if resp.StatusCode/100 != 2 { + return fmt.Errorf("Failed to update deploy key: %s", decodeJSONError(resp).Err) + } + return nil +} + +// HasDeployKey check if repo has deploy key +func HasDeployKey(keyID, repoID int64) (bool, error) { + reqURL := setting.LocalURL + fmt.Sprintf("api/internal/repositories/%d/has-keys/%d", repoID, keyID) + log.GitLogger.Trace("HasDeployKey: %s", reqURL) + + resp, err := newInternalRequest(reqURL, "GET").Response() + if err != nil { + return false, err + } + defer resp.Body.Close() + + if resp.StatusCode == 200 { + return true, nil + } + return false, nil +} + +// GetPublicKeyByID get public ssh key by his ID +func GetPublicKeyByID(keyID int64) (*models.PublicKey, error) { + reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d", keyID) + log.GitLogger.Trace("GetPublicKeyByID: %s", reqURL) + + resp, err := newInternalRequest(reqURL, "GET").Response() + if err != nil { + return nil, err + } + + defer resp.Body.Close() + + if resp.StatusCode != 200 { + return nil, fmt.Errorf("Failed to get repository: %s", decodeJSONError(resp).Err) + } + + var pKey models.PublicKey + if err := json.NewDecoder(resp.Body).Decode(&pKey); err != nil { + return nil, err + } + return &pKey, nil +} + +// GetUserByKeyID get user attached to key +func GetUserByKeyID(keyID int64) (*models.User, error) { + reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/user", keyID) + log.GitLogger.Trace("GetUserByKeyID: %s", reqURL) + + resp, err := newInternalRequest(reqURL, "GET").Response() + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode != 200 { + return nil, fmt.Errorf("Failed to get user: %s", decodeJSONError(resp).Err) + } + + var user models.User + if err := json.NewDecoder(resp.Body).Decode(&user); err != nil { + return nil, err + } + + return &user, nil +} + +// UpdatePublicKeyUpdated update public key updates +func UpdatePublicKeyUpdated(keyID int64) error { + // Ask for running deliver hook and test pull request tasks. + reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/update", keyID) + log.GitLogger.Trace("UpdatePublicKeyUpdated: %s", reqURL) + + resp, err := newInternalRequest(reqURL, "POST").Response() + if err != nil { + return err + } + + defer resp.Body.Close() + + // All 2XX status codes are accepted and others will return an error + if resp.StatusCode/100 != 2 { + return fmt.Errorf("Failed to update public key: %s", decodeJSONError(resp).Err) + } + return nil +} |