Browse Source

#1253: Avoid removing an account when the user still have repository labels

pull/5260/head
isundil 7 years ago
parent
commit
888c62687a
  1. 13
      models/error.go
  2. 10
      models/repo.go
  3. 8
      models/user.go
  4. 3
      routes/user/setting.go

13
models/error.go

@ -93,6 +93,19 @@ func (err ErrUserHasOrgs) Error() string {
return fmt.Sprintf("user still has membership of organizations [uid: %d]", err.UID) return fmt.Sprintf("user still has membership of organizations [uid: %d]", err.UID)
} }
type ErrUserHasRepoLabels struct {
UID int64
}
func IsErrUserHasRepoLabels(err error) bool {
_, ok := err.(ErrUserHasRepoLabels)
return ok
}
func (err ErrUserHasRepoLabels) Error() string {
return fmt.Sprintf("user still has repository labels [uid: %d]", err.UID)
}
// __ __.__ __ .__ // __ __.__ __ .__
// / \ / \__| | _|__| // / \ / \__| | _|__|
// \ \/\/ / | |/ / | // \ \/\/ / | |/ / |

10
models/repo.go

@ -150,8 +150,6 @@ type RepositoryLabel struct {
Color string `xorm:"VARCHAR(7)"` Color string `xorm:"VARCHAR(7)"`
IsPrivate bool IsPrivate bool
NumRepo int
Created time.Time `xorm:"-"` Created time.Time `xorm:"-"`
CreatedUnix int64 CreatedUnix int64
Updated time.Time `xorm:"-"` Updated time.Time `xorm:"-"`
@ -1869,6 +1867,14 @@ func GetRepositoryCount(u *User) (int64, error) {
return getRepositoryCount(x, u) return getRepositoryCount(x, u)
} }
func getRepositoryLabelCount(e Engine, u *User) (int64, error) {
return x.Count(&RepositoryLabel{OwnerID: u.ID})
}
func GetRepositoryLabelCount(u *User) (int64, error) {
return getRepositoryLabelCount(x, u)
}
type SearchRepoOptions struct { type SearchRepoOptions struct {
Keyword string Keyword string
OwnerID int64 OwnerID int64

8
models/user.go

@ -751,6 +751,14 @@ func deleteUser(e *xorm.Session, u *User) error {
return ErrUserHasOrgs{UID: u.ID} return ErrUserHasOrgs{UID: u.ID}
} }
// Check repository labels
count, err = GetRepositoryLabelCount(u)
if err != nil {
return fmt.Errorf("GetRepositoryLabelCount: %v", err)
} else if count > 0 {
return ErrUserHasRepoLabels{UID: u.ID}
}
// ***** START: Watch ***** // ***** START: Watch *****
watches := make([]*Watch, 0, 10) watches := make([]*Watch, 0, 10)
if err = e.Find(&watches, &Watch{UserID: u.ID}); err != nil { if err = e.Find(&watches, &Watch{UserID: u.ID}); err != nil {

3
routes/user/setting.go

@ -697,6 +697,9 @@ func SettingsDelete(c *context.Context) {
case models.IsErrUserHasOrgs(err): case models.IsErrUserHasOrgs(err):
c.Flash.Error(c.Tr("form.still_has_org")) c.Flash.Error(c.Tr("form.still_has_org"))
c.Redirect(setting.AppSubURL + "/user/settings/delete") c.Redirect(setting.AppSubURL + "/user/settings/delete")
case models.IsErrUserHasRepoLabels(err):
c.Flash.Error(c.Tr("form.still_has_repo_label"))
c.Redirect(setting.AppSubURL + "/user/settings/delete")
default: default:
c.ServerError("DeleteUser", err) c.ServerError("DeleteUser", err)
} }

Loading…
Cancel
Save