Add add and delete SSH key

This commit is contained in:
Unknown 2014-03-10 20:48:58 -04:00
parent efa039a0f7
commit 897329a644
11 changed files with 117 additions and 78 deletions

View File

@ -19,7 +19,7 @@ import (
// Test that go1.1 tag above is included in builds. main.go refers to this definition. // Test that go1.1 tag above is included in builds. main.go refers to this definition.
const go11tag = true const go11tag = true
const APP_VER = "0.0.2.0309" const APP_VER = "0.0.2.0310"
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

View File

@ -6,6 +6,7 @@ package models
import ( import (
"errors" "errors"
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -83,7 +84,8 @@ func CreateRepository(user *User, repoName, desc string, private bool) (*Reposit
if _, err = session.Insert(repo); err != nil { if _, err = session.Insert(repo); err != nil {
if err2 := os.RemoveAll(f); err2 != 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() session.Rollback()
return nil, err 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 _, err = session.Insert(&access); err != nil {
if err2 := os.RemoveAll(f); err2 != 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() session.Rollback()
return nil, err 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 _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id); err != nil {
if err2 := os.RemoveAll(f); err2 != 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() session.Rollback()
return nil, err return nil, err
@ -113,7 +117,8 @@ func CreateRepository(user *User, repoName, desc string, private bool) (*Reposit
if err = session.Commit(); err != nil { if err = session.Commit(); err != nil {
if err2 := os.RemoveAll(f); err2 != 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() session.Rollback()
return nil, err return nil, err
@ -121,6 +126,14 @@ func CreateRepository(user *User, repoName, desc string, private bool) (*Reposit
return repo, nil 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. // GetRepositories returns the list of repositories of given user.
func GetRepositories(user *User) ([]Repository, error) { func GetRepositories(user *User) ([]Repository, error) {
repos := make([]Repository, 0, 10) repos := make([]Repository, 0, 10)

55
modules/auth/setting.go Normal file
View File

@ -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)
}

View File

@ -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)) fmt.Println(models.RepoPath(user.Name, form.RepoName))
if err == nil { if err == nil {
if _, err = models.CreateRepository(user, if repo, err := models.CreateRepository(user,
form.RepoName, form.Description, form.Visibility == "private"); err == nil { form.RepoName, form.Description, form.Visibility == "private"); err == nil {
// Initialize README. err = models.InitRepository(repo, form.InitReadme == "true", form.Language)
if form.InitReadme == "true" { if err == nil {
// TODO 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
} }
} }

View File

@ -5,14 +5,16 @@
package user package user
import ( import (
"net/http"
"strconv"
"github.com/martini-contrib/render"
"github.com/martini-contrib/sessions"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log" "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) { 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) r.HTML(200, "user/setting", data)
} }
func SettingSSHKeys(r render.Render, data base.TmplData, req *http.Request, session sessions.Session) { func SettingSSHKeys(form auth.AddSSHKeyForm, r render.Render, data base.TmplData, req *http.Request, session sessions.Session) {
// del ssh ky data["Title"] = "SSH Keys"
// Delete SSH key.
if req.Method == "DELETE" || req.FormValue("_method") == "DELETE" { if req.Method == "DELETE" || req.FormValue("_method") == "DELETE" {
println(1)
id, err := strconv.ParseInt(req.FormValue("id"), 10, 64) id, err := strconv.ParseInt(req.FormValue("id"), 10, 64)
if err != nil { if err != nil {
data["ErrorMsg"] = err data["ErrorMsg"] = err
@ -38,8 +43,8 @@ func SettingSSHKeys(r render.Render, data base.TmplData, req *http.Request, sess
Id: id, Id: id,
OwnerId: auth.SignedInId(session), OwnerId: auth.SignedInId(session),
} }
err = models.DeletePublicKey(k)
if err != nil { if err = models.DeletePublicKey(k); err != nil {
data["ErrorMsg"] = err data["ErrorMsg"] = err
log.Error("ssh.DelPublicKey: %v", err) log.Error("ssh.DelPublicKey: %v", err)
r.JSON(200, map[string]interface{}{ r.JSON(200, map[string]interface{}{
@ -51,16 +56,21 @@ func SettingSSHKeys(r render.Render, data base.TmplData, req *http.Request, sess
"ok": true, "ok": true,
}) })
} }
return
} }
// add ssh key
// Add new SSH key.
if req.Method == "POST" { if req.Method == "POST" {
k := &models.PublicKey{OwnerId: auth.SignedInId(session), if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
Name: req.FormValue("keyname"), r.HTML(200, "user/publickey", data)
Content: req.FormValue("key_content"), return
} }
err := models.AddPublicKey(k)
if err != nil { k := &models.PublicKey{OwnerId: auth.SignedInId(session),
Name: form.KeyName,
Content: form.KeyContent,
}
if err := models.AddPublicKey(k); err != nil {
data["ErrorMsg"] = err data["ErrorMsg"] = err
log.Error("ssh.AddPublicKey: %v", err) log.Error("ssh.AddPublicKey: %v", err)
r.HTML(200, "base/error", data) 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 data["AddSSHKeySuccess"] = true
} }
} }
// get keys
// List existed SSH keys.
keys, err := models.ListPublicKey(auth.SignedInId(session)) keys, err := models.ListPublicKey(auth.SignedInId(session))
if err != nil { if err != nil {
data["ErrorMsg"] = err data["ErrorMsg"] = err
@ -78,8 +89,6 @@ func SettingSSHKeys(r render.Render, data base.TmplData, req *http.Request, sess
return return
} }
// set to template
data["Title"] = "SSH Keys"
data["PageIsUserSetting"] = true data["PageIsUserSetting"] = true
data["Keys"] = keys data["Keys"] = keys
r.HTML(200, "user/publickey", data) r.HTML(200, "user/publickey", data)

View File

@ -17,28 +17,6 @@ import (
"github.com/gogits/gogs/modules/log" "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) { func DelPublicKey(req *http.Request, data base.TmplData, r render.Render, session sessions.Session) {
data["Title"] = "Del Public Key" 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)
}

View File

@ -12,10 +12,11 @@
<li class="list-group-item"><a href="#">Kill Myself</a></li> <li class="list-group-item"><a href="#">Kill Myself</a></li>
</ul> </ul>
</div> </div>
<div id="gogs-user-setting-container" class="col-md-9"> <div id="gogs-user-setting-container" class="col-md-9">
<div id="gogs-ssh-keys"> <div id="gogs-ssh-keys">
<h4>SSH Keys</h4>{{if .AddSSHKeySuccess}} <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"> <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"><span class="name">SSH Key's name</span></li>{{range .Keys}}
<li class="list-group-item"> <li class="list-group-item">
@ -34,6 +35,7 @@
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title" id="myModalLabel">Add SSH Key</h4> <h4 class="modal-title" id="myModalLabel">Add SSH Key</h4>
</div> </div>
<div class="modal-body"> <div class="modal-body">
<div class="form-group"> <div class="form-group">
<label class="col-md-3 control-label">The name of key<strong class="text-danger">*</strong></label> <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"> <input name="keyname" class="form-control" placeholder="Type your preferred name" required="required">
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label class="col-md-3 control-label">SSH Key<strong class="text-danger">*</strong></label> <label class="col-md-3 control-label">SSH Key<strong class="text-danger">*</strong></label>
<div class="col-md-8"> <div class="col-md-8">
@ -48,6 +51,7 @@
</div> </div>
</div> </div>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Save SSH Key</button> <button type="submit" class="btn btn-primary">Save SSH Key</button>
@ -55,7 +59,7 @@
</form> </form>
</div> </div>
</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> </div>
</div> </div>

View File

@ -5,14 +5,14 @@
<div class="form-group"> <div class="form-group">
<label class="col-md-4 control-label">Name of this public key: </label> <label class="col-md-4 control-label">Name of this public key: </label>
<div class="col-md-3"> <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> </div>
<div class="form-group"> <div class="form-group">
<label class="col-md-4 control-label">Paste your key here: </label> <label class="col-md-4 control-label">Paste your key here: </label>
<div class="col-md-3"> <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>
</div> </div>

View File

@ -9,7 +9,7 @@
<li class="list-group-item"><a href="#">Notifications</a></li> <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="/user/setting/ssh/">SSH Keys</a></li>
<li class="list-group-item"><a href="#">Security</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> </ul>
</div> </div>
<div id="gogs-user-setting-container" class="col-md-9"> <div id="gogs-user-setting-container" class="col-md-9">

7
web.go
View File

@ -65,14 +65,11 @@ func runWeb(*cli.Context) {
m.Any("/user/sign_up", auth.SignOutRequire(), binding.BindIgnErr(auth.RegisterForm{}), user.SignUp) 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/delete", auth.SignInRequire(true), user.Delete)
m.Any("/user/setting",auth.SignInRequire(true),user.Setting) m.Any("/user/setting", auth.SignInRequire(true), user.Setting)
m.Any("/user/setting/ssh",auth.SignInRequire(true),user.SettingSSHKeys) m.Any("/user/setting/ssh", auth.SignInRequire(true), binding.BindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys)
m.Get("/user/:username", auth.SignInRequire(false), user.Profile) 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/create", auth.SignInRequire(true), binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create)
m.Any("/repo/delete", auth.SignInRequire(true), repo.Delete) m.Any("/repo/delete", auth.SignInRequire(true), repo.Delete)
m.Any("/repo/list", auth.SignInRequire(false), repo.List) m.Any("/repo/list", auth.SignInRequire(false), repo.List)