Browse Source

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

tags/v1.4.0-rc1
Michael Kuhn 6 years ago
parent
commit
420fc8efc2

+ 14
- 0
models/error.go View File

return fmt.Sprintf("name pattern is not allowed [pattern: %s]", err.Pattern) return fmt.Sprintf("name pattern is not allowed [pattern: %s]", err.Pattern)
} }


// ErrSSHDisabled represents an "SSH disabled" error.
type ErrSSHDisabled struct {
}

// IsErrSSHDisabled checks if an error is a ErrSSHDisabled.
func IsErrSSHDisabled(err error) bool {
_, ok := err.(ErrSSHDisabled)
return ok
}

func (err ErrSSHDisabled) Error() string {
return "SSH is disabled"
}

// ____ ___ // ____ ___
// | | \______ ___________ // | | \______ ___________
// | | / ___// __ \_ __ \ // | | / ___// __ \_ __ \

+ 1
- 1
models/ssh_key.go View File

// It returns the actual public key line on success. // It returns the actual public key line on success.
func CheckPublicKeyString(content string) (_ string, err error) { func CheckPublicKeyString(content string) (_ string, err error) {
if setting.SSH.Disabled { if setting.SSH.Disabled {
return "", errors.New("SSH is disabled")
return "", ErrSSHDisabled{}
} }


content, err = parseKeyString(content) content, err = parseKeyString(content)

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

token_state_desc = This token has been used in the last 7 days token_state_desc = This token has been used in the last 7 days
show_openid = Show on profile show_openid = Show on profile
hide_openid = Hide from profile hide_openid = Hide from profile
ssh_disabled = SSH is disabled


manage_social = Manage Associated Social Accounts manage_social = Manage Associated Social Accounts
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. 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 View File



// HandleCheckKeyStringError handle check key error // HandleCheckKeyStringError handle check key error
func HandleCheckKeyStringError(ctx *context.APIContext, err error) { func HandleCheckKeyStringError(ctx *context.APIContext, err error) {
if models.IsErrKeyUnableVerify(err) {
if models.IsErrSSHDisabled(err) {
ctx.Error(422, "", "SSH is disabled")
} else if models.IsErrKeyUnableVerify(err) {
ctx.Error(422, "", "Unable to verify key content") ctx.Error(422, "", "Unable to verify key content")
} else { } else {
ctx.Error(422, "", fmt.Errorf("Invalid key content: %v", err)) ctx.Error(422, "", fmt.Errorf("Invalid key content: %v", err))

+ 6
- 3
routers/repo/setting.go View File

func DeployKeys(ctx *context.Context) { func DeployKeys(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys") ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
ctx.Data["PageIsSettingsKeys"] = true ctx.Data["PageIsSettingsKeys"] = true
ctx.Data["DisableSSH"] = setting.SSH.Disabled


keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID) keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
if err != nil { if err != nil {


content, err := models.CheckPublicKeyString(form.Content) content, err := models.CheckPublicKeyString(form.Content)
if err != nil { if err != nil {
if models.IsErrKeyUnableVerify(err) {
if models.IsErrSSHDisabled(err) {
ctx.Flash.Info(ctx.Tr("settings.ssh_disabled"))
} else if models.IsErrKeyUnableVerify(err) {
ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key")) ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
} else { } else {
ctx.Data["HasError"] = true ctx.Data["HasError"] = true
ctx.Data["Err_Content"] = true ctx.Data["Err_Content"] = true
ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error())) ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
return
} }
ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
return
} }


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

+ 6
- 3
routers/user/setting.go View File

func SettingsKeys(ctx *context.Context) { func SettingsKeys(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsKeys"] = true ctx.Data["PageIsSettingsKeys"] = true
ctx.Data["DisableSSH"] = setting.SSH.Disabled


keys, err := models.ListPublicKeys(ctx.User.ID) keys, err := models.ListPublicKeys(ctx.User.ID)
if err != nil { if err != nil {
case "ssh": case "ssh":
content, err := models.CheckPublicKeyString(form.Content) content, err := models.CheckPublicKeyString(form.Content)
if err != nil { if err != nil {
if models.IsErrKeyUnableVerify(err) {
if models.IsErrSSHDisabled(err) {
ctx.Flash.Info(ctx.Tr("settings.ssh_disabled"))
} else if models.IsErrKeyUnableVerify(err) {
ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key")) ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
} else { } else {
ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error())) ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
ctx.Redirect(setting.AppSubURL + "/user/settings/keys")
return
} }
ctx.Redirect(setting.AppSubURL + "/user/settings/keys")
return
} }


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

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

<h4 class="ui top attached header"> <h4 class="ui top attached header">
{{.i18n.Tr "repo.settings.deploy_keys"}} {{.i18n.Tr "repo.settings.deploy_keys"}}
<div class="ui right"> <div class="ui right">
{{if not .DisableSSH}}
<div class="ui blue tiny show-panel button" data-panel="#add-deploy-key-panel">{{.i18n.Tr "repo.settings.add_deploy_key"}}</div> <div class="ui blue tiny show-panel button" data-panel="#add-deploy-key-panel">{{.i18n.Tr "repo.settings.add_deploy_key"}}</div>
{{else}}
<div class="ui blue tiny button disabled">{{.i18n.Tr "settings.ssh_disabled"}}</div>
{{end}}
</div> </div>
</h4> </h4>
<div class="ui attached segment"> <div class="ui attached segment">

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

<h4 class="ui top attached header"> <h4 class="ui top attached header">
{{.i18n.Tr "settings.manage_ssh_keys"}} {{.i18n.Tr "settings.manage_ssh_keys"}}
<div class="ui right"> <div class="ui right">
{{if not .DisableSSH}}
<div class="ui blue tiny show-panel button" data-panel="#add-ssh-key-panel">{{.i18n.Tr "settings.add_key"}}</div> <div class="ui blue tiny show-panel button" data-panel="#add-ssh-key-panel">{{.i18n.Tr "settings.add_key"}}</div>
{{else}}
<div class="ui blue tiny button disabled">{{.i18n.Tr "settings.ssh_disabled"}}</div>
{{end}}
</div> </div>
</h4> </h4>
<div class="ui attached segment"> <div class="ui attached segment">

Loading…
Cancel
Save