From 6b7d35eade921058654207abf30bd718389d11f1 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 13 Feb 2015 02:14:57 -0500 Subject: [PATCH] fix mirror issues uncaught #941 --- gogs.go | 2 +- models/repo.go | 115 +++++++++++++----------------------- modules/middleware/repo.go | 7 ++- templates/.VERSION | 2 +- templates/org/home.tmpl | 2 +- templates/user/profile.tmpl | 2 +- 6 files changed, 50 insertions(+), 80 deletions(-) diff --git a/gogs.go b/gogs.go index eac593621..247bfbfbb 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.5.13.0211 Beta" +const APP_VER = "0.5.14.0213 Beta" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/repo.go b/models/repo.go index 75e29b093..fd00823a2 100644 --- a/models/repo.go +++ b/models/repo.go @@ -206,6 +206,11 @@ func (repo *Repository) RepoLink() (string, error) { return setting.AppSubUrl + "/" + repo.Owner.Name + "/" + repo.Name, nil } +func (repo *Repository) HasAccess(u *User) bool { + has, _ := HasAccess(u, repo, ACCESS_MODE_READ) + return has +} + func (repo *Repository) IsOwnedBy(u *User) bool { return repo.OwnerId == u.Id } @@ -539,10 +544,10 @@ func CreateRepository(u *User, name, desc, lang, license string, private, mirror if _, err = sess.Insert(repo); err != nil { return nil, err + } else if _, err = sess.Exec("UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?", u.Id); err != nil { + return nil, err } - var t *Team // Owner team. - // TODO fix code for mirrors? // Give access to all members in owner team. @@ -550,22 +555,8 @@ func CreateRepository(u *User, name, desc, lang, license string, private, mirror if err = repo.recalculateAccesses(sess); err != nil { return nil, err } - } - if _, err = sess.Exec("UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?", u.Id); err != nil { - return nil, err - } - - // Update owner team info and count. - if u.IsOrganization() { - t.RepoIds += "$" + com.ToStr(repo.Id) + "|" - t.NumRepos++ - if _, err = sess.Id(t.Id).AllCols().Update(t); err != nil { - return nil, err - } - } - - if u.IsOrganization() { + // Update owner team info and count. t, err := u.getOwnerTeam(sess) if err != nil { return nil, fmt.Errorf("get owner team: %v", err) @@ -578,6 +569,12 @@ func CreateRepository(u *User, name, desc, lang, license string, private, mirror return nil, fmt.Errorf("watch repository: %v", err) } } + + t.RepoIds += "$" + com.ToStr(repo.Id) + "|" + t.NumRepos++ + if _, err = sess.Id(t.Id).AllCols().Update(t); err != nil { + return nil, err + } } else { if err = watchRepo(sess, u.Id, repo.Id, true); err != nil { return nil, fmt.Errorf("watch repository 2: %v", err) @@ -746,21 +743,11 @@ func DeleteRepository(uid, repoId int64, userName string) error { } sess := x.NewSession() - defer sess.Close() + defer sessionRelease(sess) if err = sess.Begin(); err != nil { return err } - if _, err = sess.Delete(&Repository{Id: repoId}); err != nil { - sess.Rollback() - return err - } - - // Delete all access. - if _, err := sess.Delete(&Access{RepoID: repo.Id}); err != nil { - sess.Rollback() - return err - } if org.IsOrganization() { idStr := "$" + com.ToStr(repoId) + "|" for _, t := range org.Teams { @@ -770,34 +757,26 @@ func DeleteRepository(uid, repoId int64, userName string) error { t.NumRepos-- t.RepoIds = strings.Replace(t.RepoIds, idStr, "", 1) if _, err = sess.Id(t.Id).AllCols().Update(t); err != nil { - sess.Rollback() return err } } } - if _, err := sess.Delete(&Action{RepoId: repo.Id}); err != nil { - sess.Rollback() + if _, err = sess.Delete(&Repository{Id: repoId}); err != nil { return err - } - if _, err = sess.Delete(&Watch{RepoId: repoId}); err != nil { - sess.Rollback() + } else if _, err := sess.Delete(&Access{RepoID: repo.Id}); err != nil { return err - } - if _, err = sess.Delete(&Mirror{RepoId: repoId}); err != nil { - sess.Rollback() + } else if _, err := sess.Delete(&Action{RepoId: repo.Id}); err != nil { return err - } - if _, err = sess.Delete(&IssueUser{RepoId: repoId}); err != nil { - sess.Rollback() + } else if _, err = sess.Delete(&Watch{RepoId: repoId}); err != nil { return err - } - if _, err = sess.Delete(&Milestone{RepoId: repoId}); err != nil { - sess.Rollback() + } else if _, err = sess.Delete(&Mirror{RepoId: repoId}); err != nil { return err - } - if _, err = sess.Delete(&Release{RepoId: repoId}); err != nil { - sess.Rollback() + } else if _, err = sess.Delete(&IssueUser{RepoId: repoId}); err != nil { + return err + } else if _, err = sess.Delete(&Milestone{RepoId: repoId}); err != nil { + return err + } else if _, err = sess.Delete(&Release{RepoId: repoId}); err != nil { return err } @@ -805,29 +784,24 @@ func DeleteRepository(uid, repoId int64, userName string) error { if err = x.Iterate(&Issue{RepoId: repoId}, func(idx int, bean interface{}) error { issue := bean.(*Issue) if _, err = sess.Delete(&Comment{IssueId: issue.Id}); err != nil { - sess.Rollback() return err } return nil }); err != nil { - sess.Rollback() return err } if _, err = sess.Delete(&Issue{RepoId: repoId}); err != nil { - sess.Rollback() return err } if repo.IsFork { if _, err = sess.Exec("UPDATE `repository` SET num_forks = num_forks - 1 WHERE id = ?", repo.ForkId); err != nil { - sess.Rollback() return err } } if _, err = sess.Exec("UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?", uid); err != nil { - sess.Rollback() return err } @@ -839,6 +813,7 @@ func DeleteRepository(uid, repoId int64, userName string) error { log.Error(4, "Fail to add notice: %v", err) } } + return sess.Commit() } @@ -1331,38 +1306,30 @@ func ForkRepository(u *User, oldRepo *Repository, name, desc string) (*Repositor if err = repo.recalculateAccesses(sess); err != nil { return nil, err - } - - var t *Team // Owner team. - - if _, err = sess.Exec("UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?", u.Id); err != nil { + } else if _, err = sess.Exec("UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?", u.Id); err != nil { return nil, err } - // Update owner team info and count. if u.IsOrganization() { + // Update owner team info and count. + t, err := u.getOwnerTeam(sess) + if err != nil { + return nil, fmt.Errorf("get owner team: %v", err) + } else if err = t.getMembers(sess); err != nil { + return nil, fmt.Errorf("get team members: %v", err) + } + + for _, u := range t.Members { + if err = watchRepo(sess, u.Id, repo.Id, true); err != nil { + return nil, fmt.Errorf("watch repository: %v", err) + } + } + t.RepoIds += "$" + com.ToStr(repo.Id) + "|" t.NumRepos++ if _, err = sess.Id(t.Id).AllCols().Update(t); err != nil { return nil, err } - } - - if u.IsOrganization() { - t, err := u.getOwnerTeam(sess) - if err != nil { - return nil, fmt.Errorf("get owner team: %v", err) - } else { - if err = t.getMembers(sess); err != nil { - return nil, fmt.Errorf("get team members: %v", err) - } else { - for _, u := range t.Members { - if err = watchRepo(sess, u.Id, repo.Id, true); err != nil { - return nil, fmt.Errorf("watch repository: %v", err) - } - } - } - } } else { if err = watchRepo(sess, u.Id, repo.Id, true); err != nil { return nil, fmt.Errorf("watch repository 2: %v", err) diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go index ea41b0ddd..bd298819d 100644 --- a/modules/middleware/repo.go +++ b/modules/middleware/repo.go @@ -29,7 +29,7 @@ func ApiRepoAssignment() macaron.Handler { ) // Check if the user is the same as the repository owner. - if ctx.IsSigned && u.LowerName == strings.ToLower(userName) { + if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) { u = ctx.User } else { u, err = models.GetUserByName(userName) @@ -210,7 +210,7 @@ func RepoAssignment(redirect bool, args ...bool) macaron.Handler { } // Check if the user is the same as the repository owner - if ctx.IsSigned && u.LowerName == strings.ToLower(userName) { + if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) { u = ctx.User } else { u, err = models.GetUserByName(userName) @@ -248,6 +248,9 @@ func RepoAssignment(redirect bool, args ...bool) macaron.Handler { ctx.Repo.IsOwner = mode >= models.ACCESS_MODE_WRITE ctx.Repo.IsAdmin = mode >= models.ACCESS_MODE_READ ctx.Repo.IsTrueOwner = mode >= models.ACCESS_MODE_OWNER + if !ctx.Repo.IsTrueOwner && ctx.Repo.Owner.IsOrganization() { + ctx.Repo.IsTrueOwner = ctx.Repo.Owner.IsOwnedBy(ctx.User.Id) + } } // Check access. diff --git a/templates/.VERSION b/templates/.VERSION index 2aad3653c..3e22111f8 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.5.13.0211 Beta \ No newline at end of file +0.5.14.0213 Beta \ No newline at end of file diff --git a/templates/org/home.tmpl b/templates/org/home.tmpl index bb160b57b..4929a8819 100644 --- a/templates/org/home.tmpl +++ b/templates/org/home.tmpl @@ -27,7 +27,7 @@
{{range .Repos}} - {{if .HasAccess $.SignedUser.Name}} + {{if .HasAccess $.SignedUser}}
  • {{.NumStars}}
  • diff --git a/templates/user/profile.tmpl b/templates/user/profile.tmpl index 44c221238..fd33a4069 100644 --- a/templates/user/profile.tmpl +++ b/templates/user/profile.tmpl @@ -74,7 +74,7 @@
    {{range .Repos}} - {{if or (not .IsPrivate) (.HasAccess $.SignedUserName)}} + {{if or (not .IsPrivate) (.HasAccess $.SignedUser)}}
    • {{.NumStars}}