forked from forgejo/forgejo
Disable SSH key deletion of externally managed Keys (#13985)
* Disable SSH key addition and deletion when externally managed When a user has a login source which has SSH key management key addition and deletion using the UI should be disabled. Fix #13983 Signed-off-by: Andrew Thornton <art27@cantab.net> * Make only externally managed keys disabled Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
parent
a19447aed1
commit
ad1164f73b
5 changed files with 125 additions and 21 deletions
|
@ -267,7 +267,16 @@ func DeletePublicKey(ctx *context.APIContext) {
|
|||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
|
||||
if err := models.DeletePublicKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
|
||||
id := ctx.ParamsInt64(":id")
|
||||
externallyManaged, err := models.PublicKeyIsExternallyManaged(id)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "PublicKeyIsExternallyManaged", err)
|
||||
}
|
||||
if externallyManaged {
|
||||
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) {
|
||||
ctx.NotFound()
|
||||
} else if models.IsErrKeyAccessDenied(err) {
|
||||
|
|
|
@ -160,7 +160,18 @@ func DeleteKey(ctx *context.Context) {
|
|||
ctx.Flash.Success(ctx.Tr("settings.gpg_key_deletion_success"))
|
||||
}
|
||||
case "ssh":
|
||||
if err := models.DeletePublicKey(ctx.User, ctx.QueryInt64("id")); err != nil {
|
||||
keyID := ctx.QueryInt64("id")
|
||||
external, err := models.PublicKeyIsExternallyManaged(keyID)
|
||||
if err != nil {
|
||||
ctx.ServerError("sshKeysExternalManaged", err)
|
||||
return
|
||||
}
|
||||
if external {
|
||||
ctx.Flash.Error(ctx.Tr("setting.ssh_externally_managed"))
|
||||
ctx.Redirect(setting.AppSubURL + "/user/settings/keys")
|
||||
return
|
||||
}
|
||||
if err := models.DeletePublicKey(ctx.User, keyID); err != nil {
|
||||
ctx.Flash.Error("DeletePublicKey: " + err.Error())
|
||||
} else {
|
||||
ctx.Flash.Success(ctx.Tr("settings.ssh_key_deletion_success"))
|
||||
|
@ -188,6 +199,13 @@ func loadKeysData(ctx *context.Context) {
|
|||
}
|
||||
ctx.Data["Keys"] = keys
|
||||
|
||||
externalKeys, err := models.PublicKeysAreExternallyManaged(keys)
|
||||
if err != nil {
|
||||
ctx.ServerError("ListPublicKeys", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["ExternalKeys"] = externalKeys
|
||||
|
||||
gpgkeys, err := models.ListGPGKeys(ctx.User.ID, models.ListOptions{})
|
||||
if err != nil {
|
||||
ctx.ServerError("ListGPGKeys", err)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue