diff options
author | Unknown <joe2010xtmf@163.com> | 2014-03-10 20:48:58 -0400 |
---|---|---|
committer | Unknown <joe2010xtmf@163.com> | 2014-03-10 20:48:58 -0400 |
commit | 897329a64416dd46b9e4f5a81c3c7a1666fc1173 (patch) | |
tree | ced28c8cea44742e091a38370414453b6c172ade | |
parent | efa039a0f75ff9ba9376af1cbd3d948752c074fc (diff) | |
download | gitea-897329a64416dd46b9e4f5a81c3c7a1666fc1173.tar.gz gitea-897329a64416dd46b9e4f5a81c3c7a1666fc1173.zip |
Add add and delete SSH key
-rw-r--r-- | gogs.go | 2 | ||||
-rw-r--r-- | models/repo.go | 21 | ||||
-rw-r--r-- | modules/auth/auth.go (renamed from modules/auth/form.go) | 0 | ||||
-rw-r--r-- | modules/auth/setting.go | 55 | ||||
-rw-r--r-- | routers/repo/repo.go | 14 | ||||
-rw-r--r-- | routers/user/setting.go | 43 | ||||
-rw-r--r-- | routers/user/ssh.go | 37 | ||||
-rw-r--r-- | templates/user/publickey.tmpl | 8 | ||||
-rw-r--r-- | templates/user/publickey_add.tmpl | 4 | ||||
-rw-r--r-- | templates/user/setting.tmpl | 2 | ||||
-rw-r--r-- | web.go | 7 |
11 files changed, 116 insertions, 77 deletions
@@ -19,7 +19,7 @@ import ( // Test that go1.1 tag above is included in builds. main.go refers to this definition. const go11tag = true -const APP_VER = "0.0.2.0309" +const APP_VER = "0.0.2.0310" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/repo.go b/models/repo.go index 05612a4229..d17f32976a 100644 --- a/models/repo.go +++ b/models/repo.go @@ -6,6 +6,7 @@ package models import ( "errors" + "fmt" "os" "path/filepath" "strings" @@ -83,7 +84,8 @@ func CreateRepository(user *User, repoName, desc string, private bool) (*Reposit if _, err = session.Insert(repo); err != nil { if err2 := os.RemoveAll(f); err2 != nil { - log.Error("delete repo directory %s/%s failed", user.Name, repoName) + return nil, errors.New(fmt.Sprintf( + "delete repo directory %s/%s failed", user.Name, repoName)) } session.Rollback() return nil, err @@ -97,7 +99,8 @@ func CreateRepository(user *User, repoName, desc string, private bool) (*Reposit } if _, err = session.Insert(&access); err != nil { if err2 := os.RemoveAll(f); err2 != nil { - log.Error("delete repo directory %s/%s failed", user.Name, repoName) + return nil, errors.New(fmt.Sprintf( + "delete repo directory %s/%s failed", user.Name, repoName)) } session.Rollback() return nil, err @@ -105,7 +108,8 @@ func CreateRepository(user *User, repoName, desc string, private bool) (*Reposit if _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id); err != nil { if err2 := os.RemoveAll(f); err2 != nil { - log.Error("delete repo directory %s/%s failed", user.Name, repoName) + return nil, errors.New(fmt.Sprintf( + "delete repo directory %s/%s failed", user.Name, repoName)) } session.Rollback() return nil, err @@ -113,7 +117,8 @@ func CreateRepository(user *User, repoName, desc string, private bool) (*Reposit if err = session.Commit(); err != nil { if err2 := os.RemoveAll(f); err2 != nil { - log.Error("delete repo directory %s/%s failed", user.Name, repoName) + return nil, errors.New(fmt.Sprintf( + "delete repo directory %s/%s failed", user.Name, repoName)) } session.Rollback() return nil, err @@ -121,6 +126,14 @@ func CreateRepository(user *User, repoName, desc string, private bool) (*Reposit return repo, nil } +// InitRepository initializes README and .gitignore if needed. +func InitRepository(repo *Repository, initReadme bool, repoLang string) error { + // README. + + // .gitignore + return nil +} + // GetRepositories returns the list of repositories of given user. func GetRepositories(user *User) ([]Repository, error) { repos := make([]Repository, 0, 10) diff --git a/modules/auth/form.go b/modules/auth/auth.go index b085527554..b085527554 100644 --- a/modules/auth/form.go +++ b/modules/auth/auth.go diff --git a/modules/auth/setting.go b/modules/auth/setting.go new file mode 100644 index 0000000000..0bc6afd4a6 --- /dev/null +++ b/modules/auth/setting.go @@ -0,0 +1,55 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package auth + +import ( + "net/http" + "reflect" + "strings" + + "github.com/codegangsta/martini" + + "github.com/gogits/binding" + + "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/log" +) + +type AddSSHKeyForm struct { + KeyName string `form:"keyname" binding:"Required"` + KeyContent string `form:"key_content" binding:"Required"` +} + +func (f *AddSSHKeyForm) Name(field string) string { + names := map[string]string{ + "KeyName": "SSH key name", + "KeyContent": "SSH key content", + } + return names[field] +} + +func (f *AddSSHKeyForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { + data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) + AssignForm(f, data) + + if req.Method == "GET" || errors.Count() == 0 { + if req.Method == "POST" && + (len(f.KeyContent) < 100 || !strings.HasPrefix(f.KeyContent, "ssh-rsa")) { + data["HasError"] = true + data["ErrorMsg"] = "SSH key content is not valid" + } + return + } + + data["HasError"] = true + if len(errors.Overall) > 0 { + for _, err := range errors.Overall { + log.Error("AddSSHKeyForm.Validate: %v", err) + } + return + } + + validate(errors, data, f) +} diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 7353436a5f..6f59b408e0 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -44,16 +44,14 @@ func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data b } fmt.Println(models.RepoPath(user.Name, form.RepoName)) if err == nil { - if _, err = models.CreateRepository(user, + if repo, err := models.CreateRepository(user, form.RepoName, form.Description, form.Visibility == "private"); err == nil { - // Initialize README. - if form.InitReadme == "true" { - // TODO + err = models.InitRepository(repo, form.InitReadme == "true", form.Language) + if err == nil { + data["RepoName"] = user.Name + "/" + form.RepoName + r.HTML(200, "repo/created", data) + return } - // TODO: init .gitignore file - data["RepoName"] = user.Name + "/" + form.RepoName - r.HTML(200, "repo/created", data) - return } } diff --git a/routers/user/setting.go b/routers/user/setting.go index aabbc21293..96a02b6f46 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -5,14 +5,16 @@ package user import ( + "net/http" + "strconv" + + "github.com/martini-contrib/render" + "github.com/martini-contrib/sessions" + "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" - "github.com/martini-contrib/render" - "github.com/martini-contrib/sessions" - "net/http" - "strconv" ) func Setting(r render.Render, data base.TmplData, session sessions.Session) { @@ -21,9 +23,12 @@ func Setting(r render.Render, data base.TmplData, session sessions.Session) { r.HTML(200, "user/setting", data) } -func SettingSSHKeys(r render.Render, data base.TmplData, req *http.Request, session sessions.Session) { - // del ssh ky +func SettingSSHKeys(form auth.AddSSHKeyForm, r render.Render, data base.TmplData, req *http.Request, session sessions.Session) { + data["Title"] = "SSH Keys" + + // Delete SSH key. if req.Method == "DELETE" || req.FormValue("_method") == "DELETE" { + println(1) id, err := strconv.ParseInt(req.FormValue("id"), 10, 64) if err != nil { data["ErrorMsg"] = err @@ -38,8 +43,8 @@ func SettingSSHKeys(r render.Render, data base.TmplData, req *http.Request, sess Id: id, OwnerId: auth.SignedInId(session), } - err = models.DeletePublicKey(k) - if err != nil { + + if err = models.DeletePublicKey(k); err != nil { data["ErrorMsg"] = err log.Error("ssh.DelPublicKey: %v", err) r.JSON(200, map[string]interface{}{ @@ -51,16 +56,21 @@ func SettingSSHKeys(r render.Render, data base.TmplData, req *http.Request, sess "ok": true, }) } - return } - // add ssh key + + // Add new SSH key. if req.Method == "POST" { + if hasErr, ok := data["HasError"]; ok && hasErr.(bool) { + r.HTML(200, "user/publickey", data) + return + } + k := &models.PublicKey{OwnerId: auth.SignedInId(session), - Name: req.FormValue("keyname"), - Content: req.FormValue("key_content"), + Name: form.KeyName, + Content: form.KeyContent, } - err := models.AddPublicKey(k) - if err != nil { + + if err := models.AddPublicKey(k); err != nil { data["ErrorMsg"] = err log.Error("ssh.AddPublicKey: %v", err) r.HTML(200, "base/error", data) @@ -69,7 +79,8 @@ func SettingSSHKeys(r render.Render, data base.TmplData, req *http.Request, sess data["AddSSHKeySuccess"] = true } } - // get keys + + // List existed SSH keys. keys, err := models.ListPublicKey(auth.SignedInId(session)) if err != nil { data["ErrorMsg"] = err @@ -78,8 +89,6 @@ func SettingSSHKeys(r render.Render, data base.TmplData, req *http.Request, sess return } - // set to template - data["Title"] = "SSH Keys" data["PageIsUserSetting"] = true data["Keys"] = keys r.HTML(200, "user/publickey", data) diff --git a/routers/user/ssh.go b/routers/user/ssh.go index 0144f7a96d..1a5837dc88 100644 --- a/routers/user/ssh.go +++ b/routers/user/ssh.go @@ -17,28 +17,6 @@ import ( "github.com/gogits/gogs/modules/log" ) -func AddPublicKey(req *http.Request, data base.TmplData, r render.Render, session sessions.Session) { - data["Title"] = "Add Public Key" - - if req.Method == "GET" { - r.HTML(200, "user/publickey_add", data) - return - } - - k := &models.PublicKey{OwnerId: auth.SignedInId(session), - Name: req.FormValue("keyname"), - Content: req.FormValue("key_content"), - } - err := models.AddPublicKey(k) - if err != nil { - data["ErrorMsg"] = err - log.Error("ssh.AddPublicKey: %v", err) - r.HTML(200, "base/error", data) - } else { - r.HTML(200, "user/publickey_added", data) - } -} - func DelPublicKey(req *http.Request, data base.TmplData, r render.Render, session sessions.Session) { data["Title"] = "Del Public Key" @@ -78,18 +56,3 @@ func DelPublicKey(req *http.Request, data base.TmplData, r render.Render, sessio } } } - -func ListPublicKey(req *http.Request, data base.TmplData, r render.Render, session sessions.Session) { - data["Title"] = "Public Keys" - - keys, err := models.ListPublicKey(auth.SignedInId(session)) - if err != nil { - data["ErrorMsg"] = err - log.Error("ssh.ListPublicKey: %v", err) - r.HTML(200, "base/error", data) - return - } - - data["Keys"] = keys - r.HTML(200, "user/publickey_list", data) -} diff --git a/templates/user/publickey.tmpl b/templates/user/publickey.tmpl index 0c8b527db9..7510616d86 100644 --- a/templates/user/publickey.tmpl +++ b/templates/user/publickey.tmpl @@ -12,10 +12,11 @@ <li class="list-group-item"><a href="#">Kill Myself</a></li> </ul> </div> + <div id="gogs-user-setting-container" class="col-md-9"> <div id="gogs-ssh-keys"> <h4>SSH Keys</h4>{{if .AddSSHKeySuccess}} - <p class="alert alert-success">New SSH Key is added !</p>{{end}} + <p class="alert alert-success">New SSH Key has been added !</p>{{else if .HasError}}<p class="alert alert-danger">{{.ErrorMsg}}</p>{{end}} <ul id="gogs-ssh-keys-list" class="list-group"> <li class="list-group-item"><span class="name">SSH Key's name</span></li>{{range .Keys}} <li class="list-group-item"> @@ -34,6 +35,7 @@ <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title" id="myModalLabel">Add SSH Key</h4> </div> + <div class="modal-body"> <div class="form-group"> <label class="col-md-3 control-label">The name of key<strong class="text-danger">*</strong></label> @@ -41,6 +43,7 @@ <input name="keyname" class="form-control" placeholder="Type your preferred name" required="required"> </div> </div> + <div class="form-group"> <label class="col-md-3 control-label">SSH Key<strong class="text-danger">*</strong></label> <div class="col-md-8"> @@ -48,6 +51,7 @@ </div> </div> </div> + <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> <button type="submit" class="btn btn-primary">Save SSH Key</button> @@ -55,7 +59,7 @@ </form> </div> </div> - <p><strong>Need help?</strong> Check out our guide to <a href="https://help.github.com/articles/generating-ssh-keys" target="_blank">generating SSH keys</a> or troubleshoot <a href="https://help.github.com/ssh-issues/" target="_blank">common SSH Problems</a></p> + <p><strong>Need help?</strong> Check out the guide to <a href="https://help.github.com/articles/generating-ssh-keys" target="_blank">generating SSH keys</a> or troubleshoot <a href="https://help.github.com/ssh-issues/" target="_blank">common SSH Problems</a></p> </div> </div> </div> diff --git a/templates/user/publickey_add.tmpl b/templates/user/publickey_add.tmpl index b6757d1f63..634b859ddd 100644 --- a/templates/user/publickey_add.tmpl +++ b/templates/user/publickey_add.tmpl @@ -5,14 +5,14 @@ <div class="form-group"> <label class="col-md-4 control-label">Name of this public key: </label> <div class="col-md-3"> - <input name="keyname" class="form-control" placeholder="Type your preferred name"> + <input name="keyname" class="form-control" placeholder="Type your preferred name" value="{{.KeyName}}"> </div> </div> <div class="form-group"> <label class="col-md-4 control-label">Paste your key here: </label> <div class="col-md-3"> - <textarea name="key_content" cols="30" rows="10" class="form-control"></textarea> + <textarea name="key_content" cols="30" rows="10" class="form-control">{{.KeyContent}}</textarea> </div> </div> diff --git a/templates/user/setting.tmpl b/templates/user/setting.tmpl index b2b595ffe2..cc527a25ac 100644 --- a/templates/user/setting.tmpl +++ b/templates/user/setting.tmpl @@ -9,7 +9,7 @@ <li class="list-group-item"><a href="#">Notifications</a></li> <li class="list-group-item"><a href="/user/setting/ssh/">SSH Keys</a></li> <li class="list-group-item"><a href="#">Security</a></li> - <li class="list-group-item"><a href="#">Kill myself</a></li> + <li class="list-group-item"><a href="#">Kill Myself</a></li> </ul> </div> <div id="gogs-user-setting-container" class="col-md-9"> @@ -65,14 +65,11 @@ func runWeb(*cli.Context) { m.Any("/user/sign_up", auth.SignOutRequire(), binding.BindIgnErr(auth.RegisterForm{}), user.SignUp) m.Any("/user/delete", auth.SignInRequire(true), user.Delete) - m.Any("/user/setting",auth.SignInRequire(true),user.Setting) - m.Any("/user/setting/ssh",auth.SignInRequire(true),user.SettingSSHKeys) + m.Any("/user/setting", auth.SignInRequire(true), user.Setting) + m.Any("/user/setting/ssh", auth.SignInRequire(true), binding.BindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys) m.Get("/user/:username", auth.SignInRequire(false), user.Profile) - //m.Any("/user/publickey/add", auth.SignInRequire(true), user.AddPublicKey) - //m.Any("/user/publickey/list", auth.SignInRequire(true), user.ListPublicKey) - m.Any("/repo/create", auth.SignInRequire(true), binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create) m.Any("/repo/delete", auth.SignInRequire(true), repo.Delete) m.Any("/repo/list", auth.SignInRequire(false), repo.List) |