1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

Move keys to models/asymkey (#17917)

* Move keys to models/keys

* Rename models/keys -> models/asymkey

* change the missed package name

* Fix package alias

* Fix test

* Fix docs

* Fix test

* Fix test

* merge
This commit is contained in:
Lunny Xiao
2021-12-10 16:14:24 +08:00
committed by GitHub
parent 0a9fcf63a4
commit 3ca5dc7e32
75 changed files with 1001 additions and 887 deletions

View File

@@ -12,7 +12,7 @@ import (
"io"
"os"
"code.gitea.io/gitea/models"
asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
)
@@ -97,7 +97,7 @@ func readAndVerifyCommit(sha string, repo *git.Repository, env []string) error {
if err != nil {
return err
}
verification := models.ParseCommitWithSignature(commit)
verification := asymkey_model.ParseCommitWithSignature(commit)
if !verification.Verified {
cancel()
return &errUnverifiedCommit{

View File

@@ -8,7 +8,7 @@ package private
import (
"net/http"
"code.gitea.io/gitea/models"
asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/private"
"code.gitea.io/gitea/modules/timeutil"
@@ -18,16 +18,16 @@ import (
func UpdatePublicKeyInRepo(ctx *context.PrivateContext) {
keyID := ctx.ParamsInt64(":id")
repoID := ctx.ParamsInt64(":repoid")
if err := models.UpdatePublicKeyUpdated(keyID); err != nil {
if err := asymkey_model.UpdatePublicKeyUpdated(keyID); err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
return
}
deployKey, err := models.GetDeployKeyByRepo(keyID, repoID)
deployKey, err := asymkey_model.GetDeployKeyByRepo(keyID, repoID)
if err != nil {
if models.IsErrDeployKeyNotExist(err) {
if asymkey_model.IsErrDeployKeyNotExist(err) {
ctx.PlainText(http.StatusOK, []byte("success"))
return
}
@@ -37,7 +37,7 @@ func UpdatePublicKeyInRepo(ctx *context.PrivateContext) {
return
}
deployKey.UpdatedUnix = timeutil.TimeStampNow()
if err = models.UpdateDeployKeyCols(deployKey, "updated_unix"); err != nil {
if err = asymkey_model.UpdateDeployKeyCols(deployKey, "updated_unix"); err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),
})
@@ -52,7 +52,7 @@ func UpdatePublicKeyInRepo(ctx *context.PrivateContext) {
func AuthorizedPublicKeyByContent(ctx *context.PrivateContext) {
content := ctx.FormString("content")
publicKey, err := models.SearchPublicKeyByContent(content)
publicKey, err := asymkey_model.SearchPublicKeyByContent(content)
if err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(),

View File

@@ -11,6 +11,7 @@ import (
"strings"
"code.gitea.io/gitea/models"
asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/models/perm"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
@@ -34,9 +35,9 @@ func ServNoCommand(ctx *context.PrivateContext) {
}
results := private.KeyAndOwner{}
key, err := models.GetPublicKeyByID(keyID)
key, err := asymkey_model.GetPublicKeyByID(keyID)
if err != nil {
if models.IsErrKeyNotExist(err) {
if asymkey_model.IsErrKeyNotExist(err) {
ctx.JSON(http.StatusUnauthorized, private.Response{
Err: fmt.Sprintf("Cannot find key: %d", keyID),
})
@@ -50,7 +51,7 @@ func ServNoCommand(ctx *context.PrivateContext) {
}
results.Key = key
if key.Type == models.KeyTypeUser || key.Type == models.KeyTypePrincipal {
if key.Type == asymkey_model.KeyTypeUser || key.Type == asymkey_model.KeyTypePrincipal {
user, err := user_model.GetUserByID(key.OwnerID)
if err != nil {
if user_model.IsErrUserNotExist(err) {
@@ -184,9 +185,9 @@ func ServCommand(ctx *context.PrivateContext) {
}
// Get the Public Key represented by the keyID
key, err := models.GetPublicKeyByID(keyID)
key, err := asymkey_model.GetPublicKeyByID(keyID)
if err != nil {
if models.IsErrKeyNotExist(err) {
if asymkey_model.IsErrKeyNotExist(err) {
ctx.JSON(http.StatusNotFound, private.ErrServCommand{
Results: results,
Err: fmt.Sprintf("Cannot find key: %d", keyID),
@@ -205,7 +206,7 @@ func ServCommand(ctx *context.PrivateContext) {
results.UserID = key.OwnerID
// If repo doesn't exist, deploy key doesn't make sense
if !repoExist && key.Type == models.KeyTypeDeploy {
if !repoExist && key.Type == asymkey_model.KeyTypeDeploy {
ctx.JSON(http.StatusNotFound, private.ErrServCommand{
Results: results,
Err: fmt.Sprintf("Cannot find repository %s/%s", results.OwnerName, results.RepoName),
@@ -216,15 +217,15 @@ func ServCommand(ctx *context.PrivateContext) {
// Deploy Keys have ownerID set to 0 therefore we can't use the owner
// So now we need to check if the key is a deploy key
// We'll keep hold of the deploy key here for permissions checking
var deployKey *models.DeployKey
var deployKey *asymkey_model.DeployKey
var user *user_model.User
if key.Type == models.KeyTypeDeploy {
if key.Type == asymkey_model.KeyTypeDeploy {
results.IsDeployKey = true
var err error
deployKey, err = models.GetDeployKeyByRepo(key.ID, repo.ID)
deployKey, err = asymkey_model.GetDeployKeyByRepo(key.ID, repo.ID)
if err != nil {
if models.IsErrDeployKeyNotExist(err) {
if asymkey_model.IsErrDeployKeyNotExist(err) {
ctx.JSON(http.StatusNotFound, private.ErrServCommand{
Results: results,
Err: fmt.Sprintf("Public (Deploy) Key: %d:%s is not authorized to %s %s/%s.", key.ID, key.Name, modeString, results.OwnerName, results.RepoName),
@@ -297,7 +298,7 @@ func ServCommand(ctx *context.PrivateContext) {
owner.Visibility.IsPrivate() ||
(user != nil && user.IsRestricted) || // user will be nil if the key is a deploykey
setting.Service.RequireSignInView) {
if key.Type == models.KeyTypeDeploy {
if key.Type == asymkey_model.KeyTypeDeploy {
if deployKey.Mode < mode {
ctx.JSON(http.StatusUnauthorized, private.ErrServCommand{
Results: results,