diff --git a/models/repo.go b/models/repo.go index 6a0071ef9f..70e2673d34 100644 --- a/models/repo.go +++ b/models/repo.go @@ -46,12 +46,12 @@ func IsRepositoryExist(user *User, reposName string) (bool, error) { // func CreateRepository(user *User, reposName string) (*Repo, error) { f := RepoPath(user.Name, reposName) - _, err := git.InitRepository(f, false) + _, err := git.InitRepository(f, true) if err != nil { return nil, err } - repo := Repo{OwnerId: user.Id, Name: reposName} + repo := Repo{OwnerId: user.Id, Name: reposName, LowerName: strings.ToLower(reposName)} session := orm.NewSession() defer session.Close() session.Begin() @@ -64,6 +64,19 @@ func CreateRepository(user *User, reposName string) (*Repo, error) { session.Rollback() return nil, err } + access := Access{UserName: user.Name, + RepoName: repo.Name, + Mode: AU_WRITABLE, + } + _, err = session.Insert(&access) + if err != nil { + err2 := os.RemoveAll(f) + if err2 != nil { + log.Error("delete repo directory %s/%s failed", user.Name, reposName) + } + session.Rollback() + return nil, err + } _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id) if err != nil { err2 := os.RemoveAll(f) diff --git a/models/user.go b/models/user.go index 4618273b3d..c5208f0313 100644 --- a/models/user.go +++ b/models/user.go @@ -161,6 +161,18 @@ func GetUserByKeyId(keyId int64) (*User, error) { return user, nil } +func GetUserById(id int64) (*User, error) { + user := new(User) + has, err := orm.Id(id).Get(user) + if err != nil { + return nil, err + } + if !has { + return nil, ErrUserNotExist + } + return user, nil +} + // LoginUserPlain validates user by raw user name and password. func LoginUserPlain(name, passwd string) (*User, error) { user := User{Name: name, Passwd: passwd} diff --git a/routers/repo/repo.go b/routers/repo/repo.go index cf79d539b6..11e9e2fb65 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -7,6 +7,7 @@ package repo import ( "fmt" "net/http" + "strconv" "github.com/martini-contrib/render" @@ -21,11 +22,32 @@ func Create(req *http.Request, r render.Render) { return } - u := &models.User{} - _, err := models.CreateRepository(u, "") - r.HTML(403, "status/403", map[string]interface{}{ - "Title": fmt.Sprintf("%v", err), - }) + // TODO: access check + fmt.Println(req.FormValue("userId"), req.FormValue("name")) + + id, err := strconv.ParseInt(req.FormValue("userId"), 10, 64) + if err == nil { + var user *models.User + user, err = models.GetUserById(id) + if user == nil { + err = models.ErrUserNotExist + } + if err == nil { + _, err = models.CreateRepository(user, req.FormValue("name")) + } + if err == nil { + r.HTML(200, "repo/created", map[string]interface{}{ + "RepoName": user.Name + "/" + req.FormValue("name"), + }) + return + } + } + + if err != nil { + r.HTML(403, "status/403", map[string]interface{}{ + "Title": fmt.Sprintf("%v", err), + }) + } } func Delete(req *http.Request, r render.Render) { diff --git a/templates/repo/create.tmpl b/templates/repo/create.tmpl index 4ec4f6f1e8..c93f9386df 100644 --- a/templates/repo/create.tmpl +++ b/templates/repo/create.tmpl @@ -3,7 +3,19 @@