summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorUnknown <joe2010xtmf@163.com>2014-04-04 18:55:17 -0400
committerUnknown <joe2010xtmf@163.com>2014-04-04 18:55:17 -0400
commite41ab839c7dbbdffc60a4e02775f24add9d126d9 (patch)
treeac450cf0e38a6fa4819ea4701a79e7719dbfe77d /models
parenteb803ec5eb3a7e66a107873ce6ee584b8c77b131 (diff)
downloadgitea-e41ab839c7dbbdffc60a4e02775f24add9d126d9.tar.gz
gitea-e41ab839c7dbbdffc60a4e02775f24add9d126d9.zip
Use session for rolling back
Diffstat (limited to 'models')
-rw-r--r--models/access.go11
-rw-r--r--models/repo.go43
-rw-r--r--models/user.go18
3 files changed, 62 insertions, 10 deletions
diff --git a/models/access.go b/models/access.go
index 83261575e6..2c0900151f 100644
--- a/models/access.go
+++ b/models/access.go
@@ -7,6 +7,8 @@ package models
import (
"strings"
"time"
+
+ "github.com/lunny/xorm"
)
// Access types.
@@ -40,6 +42,15 @@ func UpdateAccess(access *Access) error {
return err
}
+// UpdateAccess updates access information with session for rolling back.
+func UpdateAccessWithSession(sess *xorm.Session, access *Access) error {
+ if _, err := sess.Id(access.Id).Update(access); err != nil {
+ sess.Rollback()
+ return err
+ }
+ return nil
+}
+
// HasAccess returns true if someone can read or write to given repository.
func HasAccess(userName, repoName string, mode int) (bool, error) {
return orm.Get(&Access{
diff --git a/models/repo.go b/models/repo.go
index e8ebce9250..acee6f6af6 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -381,45 +381,62 @@ func TransferOwnership(user *User, newOwner string, repo *Repository) (err error
if err = orm.Find(&accesses, &Access{RepoName: user.LowerName + "/" + repo.LowerName}); err != nil {
return err
}
+
+ sess := orm.NewSession()
+ defer sess.Close()
+ if err = sess.Begin(); err != nil {
+ return err
+ }
+
for i := range accesses {
accesses[i].RepoName = newUser.LowerName + "/" + repo.LowerName
if accesses[i].UserName == user.LowerName {
accesses[i].UserName = newUser.LowerName
}
- if err = UpdateAccess(&accesses[i]); err != nil {
+ if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil {
return err
}
}
// Update repository.
repo.OwnerId = newUser.Id
- if _, err := orm.Id(repo.Id).Update(repo); err != nil {
+ if _, err := sess.Id(repo.Id).Update(repo); err != nil {
+ sess.Rollback()
return err
}
// Update user repository number.
rawSql := "UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?"
- if _, err = orm.Exec(rawSql, newUser.Id); err != nil {
+ if _, err = sess.Exec(rawSql, newUser.Id); err != nil {
+ sess.Rollback()
return err
}
rawSql = "UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?"
- if _, err = orm.Exec(rawSql, user.Id); err != nil {
+ if _, err = sess.Exec(rawSql, user.Id); err != nil {
+ sess.Rollback()
return err
}
// Add watch of new owner to repository.
if !IsWatching(newUser.Id, repo.Id) {
if err = WatchRepo(newUser.Id, repo.Id, true); err != nil {
+ sess.Rollback()
return err
}
}
if err = TransferRepoAction(user, newUser, repo); err != nil {
+ sess.Rollback()
return err
}
// Change repository directory name.
- return os.Rename(RepoPath(user.Name, repo.Name), RepoPath(newUser.Name, repo.Name))
+ if err = os.Rename(RepoPath(user.Name, repo.Name), RepoPath(newUser.Name, repo.Name)); err != nil {
+ sess.Rollback()
+ return err
+ }
+
+ return sess.Commit()
}
// ChangeRepositoryName changes all corresponding setting from old repository name to new one.
@@ -429,15 +446,27 @@ func ChangeRepositoryName(userName, oldRepoName, newRepoName string) (err error)
if err = orm.Find(&accesses, &Access{RepoName: strings.ToLower(userName + "/" + oldRepoName)}); err != nil {
return err
}
+
+ sess := orm.NewSession()
+ defer sess.Close()
+ if err = sess.Begin(); err != nil {
+ return err
+ }
+
for i := range accesses {
accesses[i].RepoName = userName + "/" + newRepoName
- if err = UpdateAccess(&accesses[i]); err != nil {
+ if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil {
return err
}
}
// Change repository directory name.
- return os.Rename(RepoPath(userName, oldRepoName), RepoPath(userName, newRepoName))
+ if err = os.Rename(RepoPath(userName, oldRepoName), RepoPath(userName, newRepoName)); err != nil {
+ sess.Rollback()
+ return err
+ }
+
+ return sess.Commit()
}
func UpdateRepository(repo *Repository) error {
diff --git a/models/user.go b/models/user.go
index 2641a15ff2..1ec3b29520 100644
--- a/models/user.go
+++ b/models/user.go
@@ -218,11 +218,18 @@ func ChangeUserName(user *User, newUserName string) (err error) {
if err = orm.Find(&accesses, &Access{UserName: user.LowerName}); err != nil {
return err
}
+
+ sess := orm.NewSession()
+ defer sess.Close()
+ if err = sess.Begin(); err != nil {
+ return err
+ }
+
for i := range accesses {
accesses[i].UserName = newUserName
if strings.HasPrefix(accesses[i].RepoName, user.LowerName+"/") {
accesses[i].RepoName = strings.Replace(accesses[i].RepoName, user.LowerName, newUserName, 1)
- if err = UpdateAccess(&accesses[i]); err != nil {
+ if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil {
return err
}
}
@@ -241,14 +248,19 @@ func ChangeUserName(user *User, newUserName string) (err error) {
for j := range accesses {
accesses[j].RepoName = newUserName + "/" + repos[i].LowerName
- if err = UpdateAccess(&accesses[j]); err != nil {
+ if err = UpdateAccessWithSession(sess, &accesses[j]); err != nil {
return err
}
}
}
// Change user directory name.
- return os.Rename(UserPath(user.LowerName), UserPath(newUserName))
+ if err = os.Rename(UserPath(user.LowerName), UserPath(newUserName)); err != nil {
+ sess.Rollback()
+ return err
+ }
+
+ return sess.Commit()
}
// UpdateUser updates user's information.