setting.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package auth
  5. import (
  6. "net/http"
  7. "reflect"
  8. "strings"
  9. "github.com/go-martini/martini"
  10. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/log"
  12. )
  13. type AddSSHKeyForm struct {
  14. KeyName string `form:"keyname" binding:"Required"`
  15. KeyContent string `form:"key_content" binding:"Required"`
  16. }
  17. func (f *AddSSHKeyForm) Name(field string) string {
  18. names := map[string]string{
  19. "KeyName": "SSH key name",
  20. "KeyContent": "SSH key content",
  21. }
  22. return names[field]
  23. }
  24. func (f *AddSSHKeyForm) Validate(errors *base.BindingErrors, req *http.Request, context martini.Context) {
  25. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  26. AssignForm(f, data)
  27. if req.Method == "GET" || errors.Count() == 0 {
  28. if req.Method == "POST" &&
  29. (len(f.KeyContent) < 100 || !strings.HasPrefix(f.KeyContent, "ssh-rsa")) {
  30. data["HasError"] = true
  31. data["ErrorMsg"] = "SSH key content is not valid"
  32. }
  33. return
  34. }
  35. data["HasError"] = true
  36. if len(errors.Overall) > 0 {
  37. for _, err := range errors.Overall {
  38. log.Error("AddSSHKeyForm.Validate: %v", err)
  39. }
  40. return
  41. }
  42. validate(errors, data, f)
  43. }