From 3fb1d874d5dbd47a04c063bf3fe0a1a1d32ceaed Mon Sep 17 00:00:00 2001 From: lishuang Date: Sun, 21 Jun 2020 00:04:11 +0800 Subject: [PATCH] Finish the delete feature. --- code/rest/bridge_dao.go | 10 +++- code/rest/download_token_dao.go | 7 +++ code/rest/footprint_dao.go | 7 +++ code/rest/image_cache_dao.go | 5 ++ code/rest/matter_dao.go | 7 +++ code/rest/matter_model.go | 8 +++ code/rest/session_dao.go | 7 +++ code/rest/share_dao.go | 11 +++- code/rest/share_service.go | 24 +++++++++ code/rest/upload_token_dao.go | 7 +++ code/rest/user_controller.go | 21 +++++++- code/rest/user_dao.go | 6 +++ code/rest/user_service.go | 91 +++++++++++++++++++++++++++++++++ code/test/main_test.go | 5 ++ 14 files changed, 212 insertions(+), 4 deletions(-) diff --git a/code/rest/bridge_dao.go b/code/rest/bridge_dao.go index 12758cb..2dda6b5 100644 --- a/code/rest/bridge_dao.go +++ b/code/rest/bridge_dao.go @@ -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 diff --git a/code/rest/download_token_dao.go b/code/rest/download_token_dao.go index e902b45..3e51d71 100644 --- a/code/rest/download_token_dao.go +++ b/code/rest/download_token_dao.go @@ -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{}) diff --git a/code/rest/footprint_dao.go b/code/rest/footprint_dao.go index bbd9af1..64ab500 100644 --- a/code/rest/footprint_dao.go +++ b/code/rest/footprint_dao.go @@ -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") diff --git a/code/rest/image_cache_dao.go b/code/rest/image_cache_dao.go index f9a44dc..a03b5c7 100644 --- a/code/rest/image_cache_dao.go +++ b/code/rest/image_cache_dao.go @@ -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}} diff --git a/code/rest/matter_dao.go b/code/rest/matter_dao.go index 4737585..b26ba10 100644 --- a/code/rest/matter_dao.go +++ b/code/rest/matter_dao.go @@ -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) diff --git a/code/rest/matter_model.go b/code/rest/matter_model.go index 9bc5ffb..8778c3b 100644 --- a/code/rest/matter_model.go +++ b/code/rest/matter_model.go @@ -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) { diff --git a/code/rest/session_dao.go b/code/rest/session_dao.go index d45ceb3..82f090f 100644 --- a/code/rest/session_dao.go +++ b/code/rest/session_dao.go @@ -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") diff --git a/code/rest/share_dao.go b/code/rest/share_dao.go index c14297c..2969c99 100644 --- a/code/rest/share_dao.go +++ b/code/rest/share_dao.go @@ -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 { diff --git a/code/rest/share_service.go b/code/rest/share_service.go index fbb70c2..7a7a7c9 100644 --- a/code/rest/share_service.go +++ b/code/rest/share_service.go @@ -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) + } + } + +} diff --git a/code/rest/upload_token_dao.go b/code/rest/upload_token_dao.go index 144300d..a3641b3 100644 --- a/code/rest/upload_token_dao.go +++ b/code/rest/upload_token_dao.go @@ -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) + +} diff --git a/code/rest/user_controller.go b/code/rest/user_controller.go index 6a440bf..9af71c9 100644 --- a/code/rest/user_controller.go +++ b/code/rest/user_controller.go @@ -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") diff --git a/code/rest/user_dao.go b/code/rest/user_dao.go index f1ec4eb..5482f49 100644 --- a/code/rest/user_dao.go +++ b/code/rest/user_dao.go @@ -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") diff --git a/code/rest/user_service.go b/code/rest/user_service.go index 00f8556..641065b 100644 --- a/code/rest/user_service.go +++ b/code/rest/user_service.go @@ -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) + +} diff --git a/code/test/main_test.go b/code/test/main_test.go index 5bc019e..3efc88b 100644 --- a/code/test/main_test.go +++ b/code/test/main_test.go @@ -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) {