aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorBo-Yi Wu <appleboy.tw@gmail.com>2017-01-11 21:10:43 +0800
committerLunny Xiao <xiaolunwen@gmail.com>2017-01-11 21:10:43 +0800
commitcbf2a967c58780ee23ff66fff1b6699f9f765294 (patch)
treeedc7bcedfb3d6d60c379d00826be522dfb425669 /models
parent0af9a24087c2705d470c9b393e711784fb3c38e7 (diff)
downloadgitea-cbf2a967c58780ee23ff66fff1b6699f9f765294.tar.gz
gitea-cbf2a967c58780ee23ff66fff1b6699f9f765294.zip
refactor: Add new deleteOrg func. (#633)
Diffstat (limited to 'models')
-rw-r--r--models/org.go59
1 files changed, 50 insertions, 9 deletions
diff --git a/models/org.go b/models/org.go
index 7d64d5a917..a293f58216 100644
--- a/models/org.go
+++ b/models/org.go
@@ -10,6 +10,7 @@ import (
"os"
"strings"
+ "github.com/Unknwon/com"
"github.com/go-xorm/builder"
"github.com/go-xorm/xorm"
)
@@ -224,23 +225,63 @@ func DeleteOrganization(org *User) (err error) {
return err
}
- if err = deleteBeans(sess,
- &Team{OrgID: org.ID},
- &OrgUser{OrgID: org.ID},
- &TeamUser{OrgID: org.ID},
+ if err = deleteOrg(sess, org); err != nil {
+ if IsErrUserOwnRepos(err) {
+ return err
+ } else if err != nil {
+ return fmt.Errorf("deleteOrg: %v", err)
+ }
+ }
+
+ if err = sess.Commit(); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func deleteOrg(e *xorm.Session, u *User) error {
+ if !u.IsOrganization() {
+ return fmt.Errorf("You can't delete none organization user: %s", u.Name)
+ }
+
+ // Check ownership of repository.
+ count, err := getRepositoryCount(e, u)
+ if err != nil {
+ return fmt.Errorf("GetRepositoryCount: %v", err)
+ } else if count > 0 {
+ return ErrUserOwnRepos{UID: u.ID}
+ }
+
+ if err := deleteBeans(e,
+ &Team{OrgID: u.ID},
+ &OrgUser{OrgID: u.ID},
+ &TeamUser{OrgID: u.ID},
); err != nil {
return fmt.Errorf("deleteBeans: %v", err)
}
- if err = deleteUser(sess, org); err != nil {
- return fmt.Errorf("deleteUser: %v", err)
+ if _, err = e.Id(u.ID).Delete(new(User)); err != nil {
+ return fmt.Errorf("Delete: %v", err)
}
- if err = sess.Commit(); err != nil {
- return err
+ // FIXME: system notice
+ // Note: There are something just cannot be roll back,
+ // so just keep error logs of those operations.
+ path := UserPath(u.Name)
+
+ if err := os.RemoveAll(path); err != nil {
+ return fmt.Errorf("Fail to RemoveAll %s: %v", path, err)
+ }
+
+ avatarPath := u.CustomAvatarPath()
+ if com.IsExist(avatarPath) {
+ if err := os.Remove(avatarPath); err != nil {
+ return fmt.Errorf("Fail to remove %s: %v", avatarPath, err)
+ }
}
- return RewriteAllPublicKeys()
+ return nil
}
// ________ ____ ___