Browse Source

Disable add key button if SSH is disabled (#2873)

Michael Kuhn 1 year ago
parent
commit
420fc8efc2

+ 14 - 0
models/error.go

@@ -37,6 +37,20 @@ func (err ErrNamePatternNotAllowed) Error() string {
37 37
 	return fmt.Sprintf("name pattern is not allowed [pattern: %s]", err.Pattern)
38 38
 }
39 39
 
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
+
40 54
 //  ____ ___
41 55
 // |    |   \______ ___________
42 56
 // |    |   /  ___// __ \_  __ \

+ 1 - 1
models/ssh_key.go

@@ -260,7 +260,7 @@ func SSHNativeParsePublicKey(keyLine string) (string, int, error) {
260 260
 // It returns the actual public key line on success.
261 261
 func CheckPublicKeyString(content string) (_ string, err error) {
262 262
 	if setting.SSH.Disabled {
263
-		return "", errors.New("SSH is disabled")
263
+		return "", ErrSSHDisabled{}
264 264
 	}
265 265
 
266 266
 	content, err = parseKeyString(content)

+ 1 - 0
options/locale/locale_en-US.ini

@@ -405,6 +405,7 @@ key_state_desc = This key has been used in the last 7 days
405 405
 token_state_desc = This token has been used in the last 7 days
406 406
 show_openid = Show on profile
407 407
 hide_openid = Hide from profile
408
+ssh_disabled = SSH is disabled
408 409
 
409 410
 manage_social = Manage Associated Social Accounts
410 411
 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.

+ 3 - 1
routers/api/v1/repo/key.go

@@ -106,7 +106,9 @@ func GetDeployKey(ctx *context.APIContext) {
106 106
 
107 107
 // HandleCheckKeyStringError handle check key error
108 108
 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) {
110 112
 		ctx.Error(422, "", "Unable to verify key content")
111 113
 	} else {
112 114
 		ctx.Error(422, "", fmt.Errorf("Invalid key content: %v", err))

+ 6 - 3
routers/repo/setting.go

@@ -515,6 +515,7 @@ func GitHooksEditPost(ctx *context.Context) {
515 515
 func DeployKeys(ctx *context.Context) {
516 516
 	ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
517 517
 	ctx.Data["PageIsSettingsKeys"] = true
518
+	ctx.Data["DisableSSH"] = setting.SSH.Disabled
518 519
 
519 520
 	keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
520 521
 	if err != nil {
@@ -545,15 +546,17 @@ func DeployKeysPost(ctx *context.Context, form auth.AddKeyForm) {
545 546
 
546 547
 	content, err := models.CheckPublicKeyString(form.Content)
547 548
 	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) {
549 552
 			ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
550 553
 		} else {
551 554
 			ctx.Data["HasError"] = true
552 555
 			ctx.Data["Err_Content"] = true
553 556
 			ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
554
-			ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
555
-			return
556 557
 		}
558
+		ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
559
+		return
557 560
 	}
558 561
 
559 562
 	key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content)

+ 6 - 3
routers/user/setting.go

@@ -339,6 +339,7 @@ func DeleteEmail(ctx *context.Context) {
339 339
 func SettingsKeys(ctx *context.Context) {
340 340
 	ctx.Data["Title"] = ctx.Tr("settings")
341 341
 	ctx.Data["PageIsSettingsKeys"] = true
342
+	ctx.Data["DisableSSH"] = setting.SSH.Disabled
342 343
 
343 344
 	keys, err := models.ListPublicKeys(ctx.User.ID)
344 345
 	if err != nil {
@@ -405,13 +406,15 @@ func SettingsKeysPost(ctx *context.Context, form auth.AddKeyForm) {
405 406
 	case "ssh":
406 407
 		content, err := models.CheckPublicKeyString(form.Content)
407 408
 		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) {
409 412
 				ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
410 413
 			} else {
411 414
 				ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
412
-				ctx.Redirect(setting.AppSubURL + "/user/settings/keys")
413
-				return
414 415
 			}
416
+			ctx.Redirect(setting.AppSubURL + "/user/settings/keys")
417
+			return
415 418
 		}
416 419
 
417 420
 		if _, err = models.AddPublicKey(ctx.User.ID, form.Title, content); err != nil {

+ 4 - 0
templates/repo/settings/deploy_keys.tmpl

@@ -7,7 +7,11 @@
7 7
 		<h4 class="ui top attached header">
8 8
 			{{.i18n.Tr "repo.settings.deploy_keys"}}
9 9
 			<div class="ui right">
10
+			{{if not .DisableSSH}}
10 11
 				<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}}
11 15
 			</div>
12 16
 		</h4>
13 17
 		<div class="ui attached segment">

+ 4 - 0
templates/user/settings/keys_ssh.tmpl

@@ -1,7 +1,11 @@
1 1
 <h4 class="ui top attached header">
2 2
   {{.i18n.Tr "settings.manage_ssh_keys"}}
3 3
   <div class="ui right">
4
+  {{if not .DisableSSH}}
4 5
     <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}}
5 9
   </div>
6 10
 </h4>
7 11
 <div class="ui attached segment">