From 52984f85d5afc2a74bb3ca04487da7fb277437d6 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 25 Feb 2014 18:30:48 +0800 Subject: [PATCH] add publickey --- models/publickey.go | 6 +++--- routers/user/ssh.go | 17 ++++++++++++----- templates/user/publickey_add.tmpl | 2 +- templates/user/publickey_added.tmpl | 8 ++++++++ 4 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 templates/user/publickey_added.tmpl diff --git a/models/publickey.go b/models/publickey.go index 1e58ba057..2fd1895d6 100644 --- a/models/publickey.go +++ b/models/publickey.go @@ -47,7 +47,7 @@ func GenAuthorizedKey(keyId int64, key string) string { return fmt.Sprintf(tmplPublicKey, appPath, keyId, key) } -func AddPublicKey(key *PublicKey, user string) error { +func AddPublicKey(key *PublicKey) error { _, err := orm.Insert(key) if err != nil { return err @@ -67,11 +67,11 @@ func AddPublicKey(key *PublicKey, user string) error { func SaveAuthorizedKeyFile(key *PublicKey) error { p := filepath.Join(sshPath, "authorized_keys") - f, err := os.Create(p) + f, err := os.OpenFile(p, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600) if err != nil { return err } - os.Chmod(p, 0600) + //os.Chmod(p, 0600) _, err = f.WriteString(GenAuthorizedKey(key.Id, key.Content)) return err } diff --git a/routers/user/ssh.go b/routers/user/ssh.go index 84f41150f..c7fae4b4b 100644 --- a/routers/user/ssh.go +++ b/routers/user/ssh.go @@ -21,9 +21,16 @@ func AddPublickKey(req *http.Request, r render.Render) { return } - k := &models.PublicKey{} - err := models.AddPublicKey(k, "") - r.HTML(403, "status/403", map[string]interface{}{ - "Title": fmt.Sprintf("%v", err), - }) + k := &models.PublicKey{OwnerId: 1, + Name: req.FormValue("keyname"), + Content: req.FormValue("key_content"), + } + err := models.AddPublicKey(k) + if err != nil { + r.HTML(403, "status/403", map[string]interface{}{ + "Title": fmt.Sprintf("%v", err), + }) + } else { + r.HTML(200, "user/publickey_added", map[string]interface{}{}) + } } diff --git a/templates/user/publickey_add.tmpl b/templates/user/publickey_add.tmpl index 2d70d82a3..5ab25b723 100644 --- a/templates/user/publickey_add.tmpl +++ b/templates/user/publickey_add.tmpl @@ -1,7 +1,7 @@ {{template "base/head" .}} {{template "base/navbar" .}}
-
+
diff --git a/templates/user/publickey_added.tmpl b/templates/user/publickey_added.tmpl new file mode 100644 index 000000000..f67da9ed8 --- /dev/null +++ b/templates/user/publickey_added.tmpl @@ -0,0 +1,8 @@ +{{template "base/head" .}} +{{template "base/navbar" .}} +
+
+ publickey added +
+
+{{template "base/footer" .}} \ No newline at end of file