Skip to content

Commit 420fc8e

Browse files
michaelkuhnlunny
authored andcommitted
Disable add key button if SSH is disabled (#2873)
1 parent 1f7aab6 commit 420fc8e

File tree

8 files changed

+39
-8
lines changed

8 files changed

+39
-8
lines changed

models/error.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ func (err ErrNamePatternNotAllowed) Error() string {
3737
return fmt.Sprintf("name pattern is not allowed [pattern: %s]", err.Pattern)
3838
}
3939

40+
// ErrSSHDisabled represents an "SSH disabled" error.
41+
type ErrSSHDisabled struct {
42+
}
43+
44+
// IsErrSSHDisabled checks if an error is a ErrSSHDisabled.
45+
func IsErrSSHDisabled(err error) bool {
46+
_, ok := err.(ErrSSHDisabled)
47+
return ok
48+
}
49+
50+
func (err ErrSSHDisabled) Error() string {
51+
return "SSH is disabled"
52+
}
53+
4054
// ____ ___
4155
// | | \______ ___________
4256
// | | / ___// __ \_ __ \

models/ssh_key.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func SSHNativeParsePublicKey(keyLine string) (string, int, error) {
260260
// It returns the actual public key line on success.
261261
func CheckPublicKeyString(content string) (_ string, err error) {
262262
if setting.SSH.Disabled {
263-
return "", errors.New("SSH is disabled")
263+
return "", ErrSSHDisabled{}
264264
}
265265

266266
content, err = parseKeyString(content)

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ key_state_desc = This key has been used in the last 7 days
405405
token_state_desc = This token has been used in the last 7 days
406406
show_openid = Show on profile
407407
hide_openid = Hide from profile
408+
ssh_disabled = SSH is disabled
408409
409410
manage_social = Manage Associated Social Accounts
410411
social_desc = This is a list of associated social accounts. For security reasons, please make sure you recognize all of these entries, as they can be used to log in to your account.

routers/api/v1/repo/key.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ func GetDeployKey(ctx *context.APIContext) {
106106

107107
// HandleCheckKeyStringError handle check key error
108108
func HandleCheckKeyStringError(ctx *context.APIContext, err error) {
109-
if models.IsErrKeyUnableVerify(err) {
109+
if models.IsErrSSHDisabled(err) {
110+
ctx.Error(422, "", "SSH is disabled")
111+
} else if models.IsErrKeyUnableVerify(err) {
110112
ctx.Error(422, "", "Unable to verify key content")
111113
} else {
112114
ctx.Error(422, "", fmt.Errorf("Invalid key content: %v", err))

routers/repo/setting.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ func GitHooksEditPost(ctx *context.Context) {
515515
func DeployKeys(ctx *context.Context) {
516516
ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
517517
ctx.Data["PageIsSettingsKeys"] = true
518+
ctx.Data["DisableSSH"] = setting.SSH.Disabled
518519

519520
keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
520521
if err != nil {
@@ -545,15 +546,17 @@ func DeployKeysPost(ctx *context.Context, form auth.AddKeyForm) {
545546

546547
content, err := models.CheckPublicKeyString(form.Content)
547548
if err != nil {
548-
if models.IsErrKeyUnableVerify(err) {
549+
if models.IsErrSSHDisabled(err) {
550+
ctx.Flash.Info(ctx.Tr("settings.ssh_disabled"))
551+
} else if models.IsErrKeyUnableVerify(err) {
549552
ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
550553
} else {
551554
ctx.Data["HasError"] = true
552555
ctx.Data["Err_Content"] = true
553556
ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
554-
ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
555-
return
556557
}
558+
ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
559+
return
557560
}
558561

559562
key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content)

routers/user/setting.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ func DeleteEmail(ctx *context.Context) {
339339
func SettingsKeys(ctx *context.Context) {
340340
ctx.Data["Title"] = ctx.Tr("settings")
341341
ctx.Data["PageIsSettingsKeys"] = true
342+
ctx.Data["DisableSSH"] = setting.SSH.Disabled
342343

343344
keys, err := models.ListPublicKeys(ctx.User.ID)
344345
if err != nil {
@@ -405,13 +406,15 @@ func SettingsKeysPost(ctx *context.Context, form auth.AddKeyForm) {
405406
case "ssh":
406407
content, err := models.CheckPublicKeyString(form.Content)
407408
if err != nil {
408-
if models.IsErrKeyUnableVerify(err) {
409+
if models.IsErrSSHDisabled(err) {
410+
ctx.Flash.Info(ctx.Tr("settings.ssh_disabled"))
411+
} else if models.IsErrKeyUnableVerify(err) {
409412
ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
410413
} else {
411414
ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
412-
ctx.Redirect(setting.AppSubURL + "/user/settings/keys")
413-
return
414415
}
416+
ctx.Redirect(setting.AppSubURL + "/user/settings/keys")
417+
return
415418
}
416419

417420
if _, err = models.AddPublicKey(ctx.User.ID, form.Title, content); err != nil {

templates/repo/settings/deploy_keys.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
<h4 class="ui top attached header">
88
{{.i18n.Tr "repo.settings.deploy_keys"}}
99
<div class="ui right">
10+
{{if not .DisableSSH}}
1011
<div class="ui blue tiny show-panel button" data-panel="#add-deploy-key-panel">{{.i18n.Tr "repo.settings.add_deploy_key"}}</div>
12+
{{else}}
13+
<div class="ui blue tiny button disabled">{{.i18n.Tr "settings.ssh_disabled"}}</div>
14+
{{end}}
1115
</div>
1216
</h4>
1317
<div class="ui attached segment">

templates/user/settings/keys_ssh.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<h4 class="ui top attached header">
22
{{.i18n.Tr "settings.manage_ssh_keys"}}
33
<div class="ui right">
4+
{{if not .DisableSSH}}
45
<div class="ui blue tiny show-panel button" data-panel="#add-ssh-key-panel">{{.i18n.Tr "settings.add_key"}}</div>
6+
{{else}}
7+
<div class="ui blue tiny button disabled">{{.i18n.Tr "settings.ssh_disabled"}}</div>
8+
{{end}}
59
</div>
610
</h4>
711
<div class="ui attached segment">

0 commit comments

Comments
 (0)