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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
75 changed files with 1001 additions and 887 deletions

View file

@ -7,7 +7,7 @@ package user
import (
"net/http"
"code.gitea.io/gitea/models"
asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/models/perm"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/context"
@ -17,13 +17,14 @@ import (
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/api/v1/repo"
"code.gitea.io/gitea/routers/api/v1/utils"
asymkey_service "code.gitea.io/gitea/services/asymkey"
)
// appendPrivateInformation appends the owner and key type information to api.PublicKey
func appendPrivateInformation(apiKey *api.PublicKey, key *models.PublicKey, defaultUser *user_model.User) (*api.PublicKey, error) {
if key.Type == models.KeyTypeDeploy {
func appendPrivateInformation(apiKey *api.PublicKey, key *asymkey_model.PublicKey, defaultUser *user_model.User) (*api.PublicKey, error) {
if key.Type == asymkey_model.KeyTypeDeploy {
apiKey.KeyType = "deploy"
} else if key.Type == models.KeyTypeUser {
} else if key.Type == asymkey_model.KeyTypeUser {
apiKey.KeyType = "user"
if defaultUser.ID == key.OwnerID {
@ -47,7 +48,7 @@ func composePublicKeysAPILink() string {
}
func listPublicKeys(ctx *context.APIContext, user *user_model.User) {
var keys []*models.PublicKey
var keys []*asymkey_model.PublicKey
var err error
var count int
@ -58,14 +59,14 @@ func listPublicKeys(ctx *context.APIContext, user *user_model.User) {
// Querying not just listing
if username != "" {
// Restrict to provided uid
keys, err = models.SearchPublicKey(user.ID, fingerprint)
keys, err = asymkey_model.SearchPublicKey(user.ID, fingerprint)
} else {
// Unrestricted
keys, err = models.SearchPublicKey(0, fingerprint)
keys, err = asymkey_model.SearchPublicKey(0, fingerprint)
}
count = len(keys)
} else {
total, err2 := models.CountPublicKeys(user.ID)
total, err2 := asymkey_model.CountPublicKeys(user.ID)
if err2 != nil {
ctx.InternalServerError(err)
return
@ -73,7 +74,7 @@ func listPublicKeys(ctx *context.APIContext, user *user_model.User) {
count = int(total)
// Use ListPublicKeys
keys, err = models.ListPublicKeys(user.ID, utils.GetListOptions(ctx))
keys, err = asymkey_model.ListPublicKeys(user.ID, utils.GetListOptions(ctx))
}
if err != nil {
@ -177,9 +178,9 @@ func GetPublicKey(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
key, err := models.GetPublicKeyByID(ctx.ParamsInt64(":id"))
key, err := asymkey_model.GetPublicKeyByID(ctx.ParamsInt64(":id"))
if err != nil {
if models.IsErrKeyNotExist(err) {
if asymkey_model.IsErrKeyNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetPublicKeyByID", err)
@ -197,13 +198,13 @@ func GetPublicKey(ctx *context.APIContext) {
// CreateUserPublicKey creates new public key to given user by ID.
func CreateUserPublicKey(ctx *context.APIContext, form api.CreateKeyOption, uid int64) {
content, err := models.CheckPublicKeyString(form.Key)
content, err := asymkey_model.CheckPublicKeyString(form.Key)
if err != nil {
repo.HandleCheckKeyStringError(ctx, err)
return
}
key, err := models.AddPublicKey(uid, form.Title, content, 0)
key, err := asymkey_model.AddPublicKey(uid, form.Title, content, 0)
if err != nil {
repo.HandleAddKeyError(ctx, err)
return
@ -263,7 +264,7 @@ func DeletePublicKey(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
id := ctx.ParamsInt64(":id")
externallyManaged, err := models.PublicKeyIsExternallyManaged(id)
externallyManaged, err := asymkey_model.PublicKeyIsExternallyManaged(id)
if err != nil {
ctx.Error(http.StatusInternalServerError, "PublicKeyIsExternallyManaged", err)
}
@ -271,10 +272,10 @@ func DeletePublicKey(ctx *context.APIContext) {
ctx.Error(http.StatusForbidden, "", "SSH Key is externally managed for this user")
}
if err := models.DeletePublicKey(ctx.User, id); err != nil {
if models.IsErrKeyNotExist(err) {
if err := asymkey_service.DeletePublicKey(ctx.User, id); err != nil {
if asymkey_model.IsErrKeyNotExist(err) {
ctx.NotFound()
} else if models.IsErrKeyAccessDenied(err) {
} else if asymkey_model.IsErrKeyAccessDenied(err) {
ctx.Error(http.StatusForbidden, "", "You do not have access to this key")
} else {
ctx.Error(http.StatusInternalServerError, "DeletePublicKey", err)