Finish the delete feature.

This commit is contained in:
lishuang
2020-06-21 00:04:11 +08:00
parent 601c60fca7
commit 3fb1d874d5
14 changed files with 212 additions and 4 deletions

View File

@ -59,7 +59,7 @@ func (this *BridgeDao) CheckByShareUuidAndMatterUuid(shareUuid string, matterUui
}
//get pager
func (this *BridgeDao) Page(page int, pageSize int, shareUuid string, sortArray []builder.OrderPair) *Pager {
func (this *BridgeDao) PlainPage(page int, pageSize int, shareUuid string, sortArray []builder.OrderPair) (int, []*Bridge) {
var wp = &builder.WherePair{}
@ -77,6 +77,14 @@ func (this *BridgeDao) Page(page int, pageSize int, shareUuid string, sortArray
var bridges []*Bridge
db = conditionDB.Order(this.GetSortString(sortArray)).Offset(page * pageSize).Limit(pageSize).Find(&bridges)
this.PanicError(db.Error)
return count, bridges
}
//get pager
func (this *BridgeDao) Page(page int, pageSize int, shareUuid string, sortArray []builder.OrderPair) *Pager {
count, bridges := this.PlainPage(page, pageSize, shareUuid, sortArray)
pager := NewPager(page, pageSize, count, bridges)
return pager

View File

@ -58,6 +58,13 @@ func (this *DownloadTokenDao) Save(downloadToken *DownloadToken) *DownloadToken
return downloadToken
}
func (this *DownloadTokenDao) DeleteByUserUuid(userUuid string) {
db := core.CONTEXT.GetDB().Where("user_uuid = ?", userUuid).Delete(DownloadToken{})
this.PanicError(db.Error)
}
func (this *DownloadTokenDao) Cleanup() {
this.logger.Info("[DownloadTokenDao] clean up. Delete all DownloadToken")
db := core.CONTEXT.GetDB().Where("uuid is not null").Delete(DownloadToken{})

View File

@ -137,6 +137,13 @@ func (this *FootprintDao) DeleteByCreateTimeBefore(createTime time.Time) {
this.PanicError(db.Error)
}
func (this *FootprintDao) DeleteByUserUuid(userUuid string) {
db := core.CONTEXT.GetDB().Where("user_uuid = ?", userUuid).Delete(Footprint{})
this.PanicError(db.Error)
}
//System cleanup.
func (this *FootprintDao) Cleanup() {
this.logger.Info("[FootprintDao][DownloadTokenDao] clean up. Delete all Footprint")

View File

@ -184,6 +184,11 @@ func (this *ImageCacheDao) DeleteByMatterUuid(matterUuid string) {
}
func (this *ImageCacheDao) DeleteByUserUuid(userUuid string) {
db := core.CONTEXT.GetDB().Where("user_uuid = ?", userUuid).Delete(ImageCache{})
this.PanicError(db.Error)
}
func (this *ImageCacheDao) SizeBetweenTime(startTime time.Time, endTime time.Time) int64 {
var wp = &builder.WherePair{Query: "create_time >= ? AND create_time <= ?", Args: []interface{}{startTime, endTime}}

View File

@ -385,6 +385,13 @@ func (this *MatterDao) Delete(matter *Matter) {
}
}
func (this *MatterDao) DeleteByUserUuid(userUuid string) {
db := core.CONTEXT.GetDB().Where("user_uuid = ?", userUuid).Delete(Matter{})
this.PanicError(db.Error)
}
func (this *MatterDao) CountBetweenTime(startTime time.Time, endTime time.Time) int64 {
var count int64
db := core.CONTEXT.GetDB().Model(&Matter{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Count(&count)

View File

@ -73,6 +73,14 @@ func NewRootMatter(user *User) *Matter {
return matter
}
//get user's space absolute path
func GetUserSpaceRootDir(username string) (rootDirPath string) {
rootDirPath = fmt.Sprintf("%s/%s", core.CONFIG.MatterPath(), username)
return rootDirPath
}
//get user's root absolute path
func GetUserMatterRootDir(username string) (rootDirPath string) {

View File

@ -67,6 +67,13 @@ func (this *SessionDao) Delete(uuid string) {
}
func (this *SessionDao) DeleteByUserUuid(userUuid string) {
db := core.CONTEXT.GetDB().Where("user_uuid = ?", userUuid).Delete(Session{})
this.PanicError(db.Error)
}
//System cleanup.
func (this *SessionDao) Cleanup() {
this.logger.Info("[SessionDao] clean up. Delete all Session")

View File

@ -39,6 +39,14 @@ func (this *ShareDao) CheckByUuid(uuid string) *Share {
func (this *ShareDao) Page(page int, pageSize int, userUuid string, sortArray []builder.OrderPair) *Pager {
count, shares := this.PlainPage(page, pageSize, userUuid, sortArray)
pager := NewPager(page, pageSize, count, shares)
return pager
}
func (this *ShareDao) PlainPage(page int, pageSize int, userUuid string, sortArray []builder.OrderPair) (int, []*Share) {
var wp = &builder.WherePair{}
if userUuid != "" {
@ -55,9 +63,8 @@ func (this *ShareDao) Page(page int, pageSize int, userUuid string, sortArray []
var shares []*Share
db = conditionDB.Order(this.GetSortString(sortArray)).Offset(page * pageSize).Limit(pageSize).Find(&shares)
this.PanicError(db.Error)
pager := NewPager(page, pageSize, count, shares)
return pager
return count, shares
}
func (this *ShareDao) Create(share *Share) *Share {

View File

@ -2,8 +2,10 @@ package rest
import (
"github.com/eyebluecn/tank/code/core"
"github.com/eyebluecn/tank/code/tool/builder"
"github.com/eyebluecn/tank/code/tool/i18n"
"github.com/eyebluecn/tank/code/tool/result"
"math"
"net/http"
"strings"
"time"
@ -108,3 +110,25 @@ func (this *ShareService) ValidateMatter(request *http.Request, shareUuid string
}
}
//delete user's shares and corresponding bridges.
func (this *ShareService) DeleteSharesByUser(request *http.Request, currentUser *User) {
//delete share and bridges.
pageSize := 100
var sortArray []builder.OrderPair
count, _ := this.shareDao.PlainPage(0, pageSize, currentUser.Uuid, sortArray)
var totalPages = int(math.Ceil(float64(count) / float64(pageSize)))
var page int
for page = 0; page < totalPages; page++ {
_, shares := this.shareDao.PlainPage(0, pageSize, currentUser.Uuid, sortArray)
for _, share := range shares {
this.bridgeDao.DeleteByShareUuid(share.Uuid)
//delete this share
this.shareDao.Delete(share)
}
}
}

View File

@ -56,3 +56,10 @@ func (this *UploadTokenDao) Save(uploadToken *UploadToken) *UploadToken {
return uploadToken
}
func (this *UploadTokenDao) DeleteByUserUuid(userUuid string) {
db := core.CONTEXT.GetDB().Where("user_uuid = ?", userUuid).Delete(UploadToken{})
this.PanicError(db.Error)
}

View File

@ -50,6 +50,7 @@ func (this *UserController) RegisterRoutes() map[string]func(writer http.Respons
routeMap["/api/user/page"] = this.Wrap(this.Page, USER_ROLE_ADMINISTRATOR)
routeMap["/api/user/toggle/status"] = this.Wrap(this.ToggleStatus, USER_ROLE_ADMINISTRATOR)
routeMap["/api/user/transfiguration"] = this.Wrap(this.Transfiguration, USER_ROLE_ADMINISTRATOR)
routeMap["/api/user/delete"] = this.Wrap(this.Delete, USER_ROLE_ADMINISTRATOR)
return routeMap
}
@ -392,7 +393,7 @@ func (this *UserController) ToggleStatus(writer http.ResponseWriter, request *ht
currentUser := this.userDao.CheckByUuid(uuid)
user := this.checkUser(request)
if uuid == user.Uuid {
panic(result.UNAUTHORIZED)
panic(result.BadRequest("You cannot disable yourself."))
}
if currentUser.Status == USER_STATUS_OK {
@ -431,6 +432,24 @@ func (this *UserController) Transfiguration(writer http.ResponseWriter, request
return this.Success(session.Uuid)
}
func (this *UserController) Delete(writer http.ResponseWriter, request *http.Request) *result.WebResult {
uuid := request.FormValue("uuid")
currentUser := this.userDao.CheckByUuid(uuid)
user := this.checkUser(request)
if currentUser.Status != USER_STATUS_DISABLED {
panic(result.BadRequest("Only disabled user can be deleted."))
}
if currentUser.Uuid == user.Uuid {
panic(result.BadRequest("You cannot delete yourself."))
}
this.userService.DeleteUser(request, currentUser)
return this.Success("OK")
}
func (this *UserController) ChangePassword(writer http.ResponseWriter, request *http.Request) *result.WebResult {
oldPassword := request.FormValue("oldPassword")

View File

@ -141,6 +141,12 @@ func (this *UserDao) DeleteUsers20() {
this.PanicError(db.Error)
}
func (this *UserDao) Delete(user *User) {
db := core.CONTEXT.GetDB().Delete(&user)
this.PanicError(db.Error)
}
//System cleanup.
func (this *UserDao) Cleanup() {
this.logger.Info("[UserDao] clean up. Delete all User")

View File

@ -7,6 +7,7 @@ import (
"github.com/eyebluecn/tank/code/tool/util"
gouuid "github.com/nu7hatch/gouuid"
"net/http"
"os"
"time"
)
@ -18,6 +19,15 @@ type UserService struct {
//file lock
locker *cache.Table
matterDao *MatterDao
matterService *MatterService
imageCacheDao *ImageCacheDao
shareDao *ShareDao
shareService *ShareService
downloadTokenDao *DownloadTokenDao
uploadTokenDao *UploadTokenDao
footprintDao *FootprintDao
}
func (this *UserService) Init() {
@ -33,6 +43,41 @@ func (this *UserService) Init() {
this.sessionDao = b
}
b = core.CONTEXT.GetBean(this.matterDao)
if b, ok := b.(*MatterDao); ok {
this.matterDao = b
}
b = core.CONTEXT.GetBean(this.matterService)
if b, ok := b.(*MatterService); ok {
this.matterService = b
}
b = core.CONTEXT.GetBean(this.imageCacheDao)
if b, ok := b.(*ImageCacheDao); ok {
this.imageCacheDao = b
}
b = core.CONTEXT.GetBean(this.shareService)
if b, ok := b.(*ShareService); ok {
this.shareService = b
}
b = core.CONTEXT.GetBean(this.downloadTokenDao)
if b, ok := b.(*DownloadTokenDao); ok {
this.downloadTokenDao = b
}
b = core.CONTEXT.GetBean(this.uploadTokenDao)
if b, ok := b.(*UploadTokenDao); ok {
this.uploadTokenDao = b
}
b = core.CONTEXT.GetBean(this.footprintDao)
if b, ok := b.(*FootprintDao); ok {
this.footprintDao = b
}
//create a lock cache.
this.locker = cache.NewTable()
}
@ -187,3 +232,49 @@ func (this *UserService) RemoveCacheUserByUuid(userUuid string) {
}
}
}
//delete user
func (this *UserService) DeleteUser(request *http.Request, currentUser *User) {
//delete from cache
this.logger.Info("delete from cache userUuid = %s", currentUser.Uuid)
this.RemoveCacheUserByUuid(currentUser.Uuid)
//delete download tokens
this.logger.Info("delete download tokens")
this.downloadTokenDao.DeleteByUserUuid(currentUser.Uuid)
//delete upload tokens
this.logger.Info("delete upload tokens")
this.uploadTokenDao.DeleteByUserUuid(currentUser.Uuid)
//delete footprints
this.logger.Info("delete footprints")
this.footprintDao.DeleteByUserUuid(currentUser.Uuid)
//delete session
this.logger.Info("delete session")
this.sessionDao.DeleteByUserUuid(currentUser.Uuid)
//delete shares and bridges
this.logger.Info("elete shares and bridges")
this.shareService.DeleteSharesByUser(request, currentUser)
//delete caches
this.logger.Info("delete caches")
this.imageCacheDao.DeleteByUserUuid(currentUser.Uuid)
//delete matters
this.logger.Info("delete matters")
this.matterDao.DeleteByUserUuid(currentUser.Uuid)
//delete this user
this.logger.Info("delete this user.")
this.userDao.Delete(currentUser)
//delete files from disk.
this.logger.Info("delete files from disk. %s", GetUserSpaceRootDir(currentUser.Username))
err := os.RemoveAll(GetUserSpaceRootDir(currentUser.Username))
this.PanicError(err)
}

View File

@ -16,6 +16,11 @@ func TestHello(t *testing.T) {
split := strings.Split("good", "/")
fmt.Printf("%v", split)
var i int
for i = 1; i < 10; i++ {
fmt.Printf("i=%d\n", i)
}
}
func TestCron(t *testing.T) {