summaryrefslogtreecommitdiffstats
path: root/models/publickey.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2014-02-17 23:57:23 +0800
committerLunny Xiao <xiaolunwen@gmail.com>2014-02-17 23:57:23 +0800
commitee9bcf4d9ff41fb0cc02e53c2da2b9bfc520d54b (patch)
tree93d530cc5112efc23e5fd313e6d2cad3f9aed334 /models/publickey.go
parentb1c5adc2f206b855ccf19eaf492fd86a86e2befc (diff)
downloadgitea-ee9bcf4d9ff41fb0cc02e53c2da2b9bfc520d54b.tar.gz
gitea-ee9bcf4d9ff41fb0cc02e53c2da2b9bfc520d54b.zip
add publickey & access
Diffstat (limited to 'models/publickey.go')
-rw-r--r--models/publickey.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/models/publickey.go b/models/publickey.go
new file mode 100644
index 0000000000..bc9fbb33bd
--- /dev/null
+++ b/models/publickey.go
@@ -0,0 +1,55 @@
+package models
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "time"
+)
+
+var (
+ publicKeyRootPath string
+ tmplPublicKey = "### autogenerated by gitgos, DO NOT EDIT\n" +
+ "command=\"gitosis-serve %s\",no-port-forwarding," +
+ "no-X11-forwarding,no-agent-forwarding,no-pty %s"
+)
+
+type PublicKey struct {
+ Id int64
+ OwnerId int64 `xorm:"index"`
+ Name string `xorm:"unique not null"`
+ Content string `xorm:"text not null"`
+ Created time.Time `xorm:"created"`
+ Updated time.Time `xorm:"updated"`
+}
+
+func GenAuthorizedKey(user, key string) string {
+ return fmt.Sprintf(tmplPublicKey, user, key)
+}
+
+func AddPublicKey(key *PublicKey, user string) error {
+ _, err := orm.Insert(key)
+ if err != nil {
+ return err
+ }
+
+ err = SaveAuthorizedKeyFile(user, key.Content)
+ if err != nil {
+ _, err2 := orm.Delete(key)
+ if err2 != nil {
+ // TODO: logo the error
+ }
+ return err
+ }
+
+ return nil
+}
+
+func SaveAuthorizedKeyFile(user, key string) error {
+ f, err := os.Create(filepath.Join(publicKeyRootPath, user+".pub"))
+ if err != nil {
+ return err
+ }
+ _, err = f.WriteString(GenAuthorizedKey(user, key))
+ return err
+}