Refine all the panic method.

This commit is contained in:
zicla
2019-04-26 02:50:32 +08:00
parent 8edc30babc
commit 46256439da
13 changed files with 98 additions and 100 deletions

View File

@ -1,7 +1,6 @@
package rest package rest
import ( import (
"fmt"
"net/http" "net/http"
"tank/rest/config" "tank/rest/config"
"tank/rest/logger" "tank/rest/logger"
@ -44,26 +43,6 @@ func (this *Bean) PanicError(err error) {
} }
} }
//请求参数有问题
func (this *Bean) PanicBadRequest(format string, v ...interface{}) {
panic(result.CustomWebResult(result.CODE_WRAPPER_BAD_REQUEST, fmt.Sprintf(format, v...)))
}
//没有权限
func (this *Bean) PanicUnauthorized(format string, v ...interface{}) {
panic(result.CustomWebResult(result.CODE_WRAPPER_UNAUTHORIZED, fmt.Sprintf(format, v...)))
}
//没有找到
func (this *Bean) PanicNotFound(format string, v ...interface{}) {
panic(result.CustomWebResult(result.CODE_WRAPPER_NOT_FOUND, fmt.Sprintf(format, v...)))
}
//服务器内部出问题
func (this *Bean) PanicServer(format string, v ...interface{}) {
panic(result.CustomWebResult(result.CODE_WRAPPER_SERVER, fmt.Sprintf(format, v...)))
}
//能找到一个user就找到一个 //能找到一个user就找到一个
func (this *Bean) findUser(writer http.ResponseWriter, request *http.Request) *User { func (this *Bean) findUser(writer http.ResponseWriter, request *http.Request) *User {

View File

@ -85,10 +85,10 @@ func (this *DavController) CheckCurrentUser(writer http.ResponseWriter, request
user := this.userDao.FindByUsername(username) user := this.userDao.FindByUsername(username)
if user == nil { if user == nil {
this.PanicBadRequest("邮箱或密码错误") panic(result.BadRequest("邮箱或密码错误"))
} else { } else {
if !tool.MatchBcrypt(password, user.Password) { if !tool.MatchBcrypt(password, user.Password) {
this.PanicBadRequest("邮箱或密码错误") panic(result.BadRequest("邮箱或密码错误"))
} }
} }

View File

@ -9,6 +9,7 @@ import (
"strings" "strings"
"tank/rest/dav" "tank/rest/dav"
"tank/rest/dav/xml" "tank/rest/dav/xml"
"tank/rest/result"
"tank/rest/tool" "tank/rest/tool"
) )
@ -57,7 +58,7 @@ func (this *DavService) ParseDepth(request *http.Request) int {
return 1 return 1
} }
} else { } else {
this.PanicBadRequest("必须指定Header Depth") panic(result.BadRequest("必须指定Header Depth"))
} }
return depth return depth
} }
@ -106,7 +107,7 @@ func (this *DavService) PropstatsFromXmlNames(user *User, matter *Matter, xmlNam
} }
if len(properties) == 0 { if len(properties) == 0 {
this.PanicBadRequest("请求的属性项无法解析!") panic(result.BadRequest("请求的属性项无法解析!"))
} }
okPropstat := dav.Propstat{Status: http.StatusOK, Props: properties} okPropstat := dav.Propstat{Status: http.StatusOK, Props: properties}
@ -135,7 +136,7 @@ func (this *DavService) Propstats(user *User, matter *Matter, propfind *dav.Prop
propstats := make([]dav.Propstat, 0) propstats := make([]dav.Propstat, 0)
if propfind.Propname != nil { if propfind.Propname != nil {
this.PanicBadRequest("propfind.Propname != nil 尚未处理") panic(result.BadRequest("propfind.Propname != nil 尚未处理"))
} else if propfind.Allprop != nil { } else if propfind.Allprop != nil {
//TODO: 如果include中还有内容那么包含进去。 //TODO: 如果include中还有内容那么包含进去。
@ -312,7 +313,7 @@ func (this *DavService) prepareMoveCopy(
var destinationPath string var destinationPath string
if destinationStr == "" { if destinationStr == "" {
this.PanicBadRequest("Header Destination必填") panic(result.BadRequest("Header Destination必填"))
} }
//如果是重命名那么就不是http开头了。 //如果是重命名那么就不是http开头了。
@ -322,7 +323,7 @@ func (this *DavService) prepareMoveCopy(
destinationUrl, err := url.Parse(destinationStr) destinationUrl, err := url.Parse(destinationStr)
this.PanicError(err) this.PanicError(err)
if destinationUrl.Host != request.Host { if destinationUrl.Host != request.Host {
this.PanicBadRequest("Destination Host不一致. %s %s != %s", destinationStr, destinationUrl.Host, request.Host) panic(result.BadRequest("Destination Host不一致. %s %s != %s", destinationStr, destinationUrl.Host, request.Host))
} }
fullDestinationPath = destinationUrl.Path fullDestinationPath = destinationUrl.Path
} }
@ -337,7 +338,7 @@ func (this *DavService) prepareMoveCopy(
if len(strs) == 2 { if len(strs) == 2 {
destinationPath = strs[1] destinationPath = strs[1]
} else { } else {
this.PanicBadRequest("目标前缀必须为:%s", WEBDAV_PREFFIX) panic(result.BadRequest("目标前缀必须为:%s", WEBDAV_PREFFIX))
} }
destinationName = tool.GetFilenameOfPath(destinationPath) destinationName = tool.GetFilenameOfPath(destinationPath)
@ -360,7 +361,7 @@ func (this *DavService) prepareMoveCopy(
//如果是空或者/就是请求根目录 //如果是空或者/就是请求根目录
if srcMatter.Uuid == MATTER_ROOT { if srcMatter.Uuid == MATTER_ROOT {
this.PanicBadRequest("你不能移动根目录!") panic(result.BadRequest("你不能移动根目录!"))
} }
//寻找目标文件夹matter //寻找目标文件夹matter
@ -404,19 +405,19 @@ func (this *DavService) HandleCopy(writer http.ResponseWriter, request *http.Req
//加锁 //加锁
func (this *DavService) HandleLock(writer http.ResponseWriter, request *http.Request, user *User, subPath string) { func (this *DavService) HandleLock(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {
this.PanicBadRequest("不支持LOCK方法") panic(result.BadRequest("不支持LOCK方法"))
} }
//解锁 //解锁
func (this *DavService) HandleUnlock(writer http.ResponseWriter, request *http.Request, user *User, subPath string) { func (this *DavService) HandleUnlock(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {
this.PanicBadRequest("不支持UNLOCK方法") panic(result.BadRequest("不支持UNLOCK方法"))
} }
//修改文件属性 //修改文件属性
func (this *DavService) HandleProppatch(writer http.ResponseWriter, request *http.Request, user *User, subPath string) { func (this *DavService) HandleProppatch(writer http.ResponseWriter, request *http.Request, user *User, subPath string) {
this.PanicBadRequest("不支持PROPPATCH方法") panic(result.BadRequest("不支持PROPPATCH方法"))
} }
//处理所有的请求 //处理所有的请求
@ -480,7 +481,7 @@ func (this *DavService) HandleDav(writer http.ResponseWriter, request *http.Requ
} else { } else {
this.PanicBadRequest("该方法还不支持。%s", method) panic(result.BadRequest("该方法还不支持。%s", method))
} }

View File

@ -47,7 +47,7 @@ func (this *FootprintController) Detail(writer http.ResponseWriter, request *htt
uuid := request.FormValue("uuid") uuid := request.FormValue("uuid")
if uuid == "" { if uuid == "" {
this.PanicBadRequest("图片缓存的uuid必填") panic(result.BadRequest("图片缓存的uuid必填"))
} }
footprint := this.footprintService.Detail(uuid) footprint := this.footprintService.Detail(uuid)
@ -113,7 +113,7 @@ func (this *FootprintController) Delete(writer http.ResponseWriter, request *htt
uuid := request.FormValue("uuid") uuid := request.FormValue("uuid")
if uuid == "" { if uuid == "" {
this.PanicBadRequest("uuid必填") panic(result.BadRequest("uuid必填"))
} }
footprint := this.footprintDao.FindByUuid(uuid) footprint := this.footprintDao.FindByUuid(uuid)

View File

@ -49,7 +49,7 @@ func (this *ImageCacheController) Detail(writer http.ResponseWriter, request *ht
uuid := request.FormValue("uuid") uuid := request.FormValue("uuid")
if uuid == "" { if uuid == "" {
this.PanicBadRequest("图片缓存的uuid必填") panic(result.BadRequest("图片缓存的uuid必填"))
} }
imageCache := this.imageCacheService.Detail(uuid) imageCache := this.imageCacheService.Detail(uuid)
@ -127,7 +127,7 @@ func (this *ImageCacheController) Delete(writer http.ResponseWriter, request *ht
uuid := request.FormValue("uuid") uuid := request.FormValue("uuid")
if uuid == "" { if uuid == "" {
this.PanicBadRequest("图片缓存的uuid必填") panic(result.BadRequest("图片缓存的uuid必填"))
} }
imageCache := this.imageCacheDao.FindByUuid(uuid) imageCache := this.imageCacheDao.FindByUuid(uuid)
@ -135,7 +135,8 @@ func (this *ImageCacheController) Delete(writer http.ResponseWriter, request *ht
//判断图片缓存的所属人是否正确 //判断图片缓存的所属人是否正确
user := this.checkUser(writer, request) user := this.checkUser(writer, request)
if user.Role != USER_ROLE_ADMINISTRATOR && imageCache.UserUuid != user.Uuid { if user.Role != USER_ROLE_ADMINISTRATOR && imageCache.UserUuid != user.Uuid {
this.PanicUnauthorized("没有权限")
panic(result.Unauthorized("没有权限"))
} }
this.imageCacheDao.Delete(imageCache) this.imageCacheDao.Delete(imageCache)
@ -148,7 +149,7 @@ func (this *ImageCacheController) DeleteBatch(writer http.ResponseWriter, reques
uuids := request.FormValue("uuids") uuids := request.FormValue("uuids")
if uuids == "" { if uuids == "" {
this.PanicBadRequest("图片缓存的uuids必填") panic(result.BadRequest("图片缓存的uuids必填"))
} }
uuidArray := strings.Split(uuids, ",") uuidArray := strings.Split(uuids, ",")
@ -160,7 +161,7 @@ func (this *ImageCacheController) DeleteBatch(writer http.ResponseWriter, reques
//判断图片缓存的所属人是否正确 //判断图片缓存的所属人是否正确
user := this.checkUser(writer, request) user := this.checkUser(writer, request)
if user.Role != USER_ROLE_ADMINISTRATOR && imageCache.UserUuid != user.Uuid { if user.Role != USER_ROLE_ADMINISTRATOR && imageCache.UserUuid != user.Uuid {
this.PanicUnauthorized("没有权限") panic(result.Unauthorized("没有权限"))
} }
this.imageCacheDao.Delete(imageCache) this.imageCacheDao.Delete(imageCache)

View File

@ -130,7 +130,7 @@ func (this *InstallController) getCreateSQLFromFile(tableName string) string {
filePath := homePath + "/db/" + tableName + ".sql" filePath := homePath + "/db/" + tableName + ".sql"
exists, err := tool.PathExists(filePath) exists, err := tool.PathExists(filePath)
if err != nil { if err != nil {
this.PanicServer("从安装目录判断建表语句文件是否存在时出错!") panic(result.Server("从安装目录判断建表语句文件是否存在时出错!"))
} }
//2. 从GOPATH下面去找因为可能是开发环境 //2. 从GOPATH下面去找因为可能是开发环境
@ -142,11 +142,11 @@ func (this *InstallController) getCreateSQLFromFile(tableName string) string {
filePath = build.Default.GOPATH + "/src/tank/build/db/" + tableName + ".sql" filePath = build.Default.GOPATH + "/src/tank/build/db/" + tableName + ".sql"
exists, err = tool.PathExists(filePath) exists, err = tool.PathExists(filePath)
if err != nil { if err != nil {
this.PanicServer("从GOPATH判断建表语句文件是否存在时出错") panic(result.Server("从GOPATH判断建表语句文件是否存在时出错"))
} }
if !exists { if !exists {
this.PanicServer(fmt.Sprintf("%s 或 %s 均不存在,请检查你的安装情况。", filePath1, filePath)) panic(result.Server("%s 或 %s 均不存在,请检查你的安装情况。", filePath1, filePath))
} }
} }
@ -219,10 +219,10 @@ func (this *InstallController) validateTableMetaList(tableInfoList []*InstallTab
strs = append(strs, v.DBName) strs = append(strs, v.DBName)
} }
this.PanicBadRequest(fmt.Sprintf("%s 表的以下字段缺失:%v", tableInfo.Name, strs)) panic(result.BadRequest(fmt.Sprintf("%s 表的以下字段缺失:%v", tableInfo.Name, strs)))
} }
} else { } else {
this.PanicBadRequest(tableInfo.Name + "表不存在") panic(result.BadRequest(tableInfo.Name + "表不存在"))
} }
} }
@ -309,15 +309,15 @@ func (this *InstallController) CreateAdmin(writer http.ResponseWriter, request *
//验证超级管理员的信息 //验证超级管理员的信息
if m, _ := regexp.MatchString(`^[0-9a-zA-Z_]+$`, adminUsername); !m { if m, _ := regexp.MatchString(`^[0-9a-zA-Z_]+$`, adminUsername); !m {
this.PanicBadRequest(`超级管理员用户名必填,且只能包含字母,数字和'_''`) panic(result.BadRequest(`超级管理员用户名必填,且只能包含字母,数字和'_''`))
} }
if len(adminPassword) < 6 { if len(adminPassword) < 6 {
this.PanicBadRequest(`超级管理员密码长度至少为6位`) panic(result.BadRequest(`超级管理员密码长度至少为6位`))
} }
if adminEmail == "" { if adminEmail == "" {
this.PanicBadRequest(`超级管理员邮箱必填`) panic(result.BadRequest(`超级管理员邮箱必填`))
} }
//检查是否有重复。 //检查是否有重复。
@ -325,14 +325,14 @@ func (this *InstallController) CreateAdmin(writer http.ResponseWriter, request *
db1 := db.Model(&User{}).Where("email = ?", adminEmail).Count(&count1) db1 := db.Model(&User{}).Where("email = ?", adminEmail).Count(&count1)
this.PanicError(db1.Error) this.PanicError(db1.Error)
if count1 > 0 { if count1 > 0 {
this.PanicBadRequest(`该邮箱已存在`) panic(result.BadRequest(`该邮箱已存在`))
} }
var count2 int64 var count2 int64
db2 := db.Model(&User{}).Where("username = ?", adminUsername).Count(&count2) db2 := db.Model(&User{}).Where("username = ?", adminUsername).Count(&count2)
this.PanicError(db2.Error) this.PanicError(db2.Error)
if count2 > 0 { if count2 > 0 {
this.PanicBadRequest(`该用户名已存在`) panic(result.BadRequest(`该用户名已存在`))
} }
user := &User{} user := &User{}
@ -369,24 +369,24 @@ func (this *InstallController) ValidateAdmin(writer http.ResponseWriter, request
//验证超级管理员的信息 //验证超级管理员的信息
if adminEmail == "" { if adminEmail == "" {
this.PanicBadRequest(`超级管理员邮箱必填`) panic(result.BadRequest(`超级管理员邮箱必填`))
} }
if len(adminPassword) < 6 { if len(adminPassword) < 6 {
this.PanicBadRequest(`超级管理员密码长度至少为6位`) panic(result.BadRequest(`超级管理员密码长度至少为6位`))
} }
var existEmailUser = &User{} var existEmailUser = &User{}
db = db.Where(&User{Email: adminEmail}).First(existEmailUser) db = db.Where(&User{Email: adminEmail}).First(existEmailUser)
if db.Error != nil { if db.Error != nil {
this.PanicBadRequest(fmt.Sprintf("%s对应的用户不存在", adminEmail)) panic(result.BadRequest(fmt.Sprintf("%s对应的用户不存在", adminEmail)))
} }
if !tool.MatchBcrypt(adminPassword, existEmailUser.Password) { if !tool.MatchBcrypt(adminPassword, existEmailUser.Password) {
this.PanicBadRequest("邮箱或密码错误") panic(result.BadRequest("邮箱或密码错误"))
} }
if existEmailUser.Role != USER_ROLE_ADMINISTRATOR { if existEmailUser.Role != USER_ROLE_ADMINISTRATOR {
this.PanicBadRequest("该账号不是管理员") panic(result.BadRequest("该账号不是管理员"))
} }
return this.Success("OK") return this.Success("OK")
@ -422,7 +422,7 @@ func (this *InstallController) Finish(writer http.ResponseWriter, request *http.
db1 := db.Model(&User{}).Where("role = ?", USER_ROLE_ADMINISTRATOR).Count(&count1) db1 := db.Model(&User{}).Where("role = ?", USER_ROLE_ADMINISTRATOR).Count(&count1)
this.PanicError(db1.Error) this.PanicError(db1.Error)
if count1 == 0 { if count1 == 0 {
this.PanicBadRequest(`请至少配置一名管理员`) panic(result.BadRequest(`请至少配置一名管理员`))
} }
var configItem = &config.ConfigItem{ var configItem = &config.ConfigItem{

View File

@ -72,7 +72,7 @@ func (this *MatterController) Detail(writer http.ResponseWriter, request *http.R
uuid := request.FormValue("uuid") uuid := request.FormValue("uuid")
if uuid == "" { if uuid == "" {
this.PanicBadRequest("文件的uuid必填") panic(result.BadRequest("文件的uuid必填"))
} }
matter := this.matterService.Detail(uuid) matter := this.matterService.Detail(uuid)
@ -284,7 +284,7 @@ func (this *MatterController) Delete(writer http.ResponseWriter, request *http.R
uuid := request.FormValue("uuid") uuid := request.FormValue("uuid")
if uuid == "" { if uuid == "" {
this.PanicBadRequest("文件的uuid必填") panic(result.BadRequest("文件的uuid必填"))
} }
matter := this.matterDao.CheckByUuid(uuid) matter := this.matterDao.CheckByUuid(uuid)
@ -292,7 +292,7 @@ func (this *MatterController) Delete(writer http.ResponseWriter, request *http.R
//判断文件的所属人是否正确 //判断文件的所属人是否正确
user := this.checkUser(writer, request) user := this.checkUser(writer, request)
if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid {
this.PanicUnauthorized("没有权限") panic(result.Unauthorized("没有权限"))
} }
this.matterService.AtomicDelete(matter) this.matterService.AtomicDelete(matter)
@ -305,7 +305,7 @@ func (this *MatterController) DeleteBatch(writer http.ResponseWriter, request *h
uuids := request.FormValue("uuids") uuids := request.FormValue("uuids")
if uuids == "" { if uuids == "" {
this.PanicBadRequest("文件的uuids必填") panic(result.BadRequest("文件的uuids必填"))
} }
uuidArray := strings.Split(uuids, ",") uuidArray := strings.Split(uuids, ",")
@ -323,7 +323,7 @@ func (this *MatterController) DeleteBatch(writer http.ResponseWriter, request *h
//判断文件的所属人是否正确 //判断文件的所属人是否正确
user := this.checkUser(writer, request) user := this.checkUser(writer, request)
if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid {
this.PanicUnauthorized("没有权限") panic(result.Unauthorized("没有权限"))
} }
this.matterService.AtomicDelete(matter) this.matterService.AtomicDelete(matter)
@ -345,7 +345,7 @@ func (this *MatterController) Rename(writer http.ResponseWriter, request *http.R
matter := this.matterDao.CheckByUuid(uuid) matter := this.matterDao.CheckByUuid(uuid)
if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid {
this.PanicUnauthorized("没有权限") panic(result.Unauthorized("没有权限"))
} }
this.matterService.AtomicRename(matter, name, user) this.matterService.AtomicRename(matter, name, user)
@ -371,7 +371,7 @@ func (this *MatterController) ChangePrivacy(writer http.ResponseWriter, request
//权限验证 //权限验证
user := this.checkUser(writer, request) user := this.checkUser(writer, request)
if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid { if user.Role != USER_ROLE_ADMINISTRATOR && matter.UserUuid != user.Uuid {
this.PanicUnauthorized("没有权限") panic(result.Unauthorized("没有权限"))
} }
matter.Privacy = privacy matter.Privacy = privacy
@ -390,7 +390,7 @@ func (this *MatterController) Move(writer http.ResponseWriter, request *http.Req
var srcUuids []string var srcUuids []string
//验证参数。 //验证参数。
if srcUuidsStr == "" { if srcUuidsStr == "" {
this.PanicBadRequest("srcUuids参数必填") panic(result.BadRequest("srcUuids参数必填"))
} else { } else {
srcUuids = strings.Split(srcUuidsStr, ",") srcUuids = strings.Split(srcUuidsStr, ",")
} }
@ -405,11 +405,11 @@ func (this *MatterController) Move(writer http.ResponseWriter, request *http.Req
//验证dest是否有问题 //验证dest是否有问题
var destMatter = this.matterDao.CheckWithRootByUuid(destUuid, user) var destMatter = this.matterDao.CheckWithRootByUuid(destUuid, user)
if !destMatter.Dir { if !destMatter.Dir {
this.PanicBadRequest("目标不是文件夹") panic(result.BadRequest("目标不是文件夹"))
} }
if user.Role != USER_ROLE_ADMINISTRATOR && destMatter.UserUuid != user.Uuid { if user.Role != USER_ROLE_ADMINISTRATOR && destMatter.UserUuid != user.Uuid {
this.PanicUnauthorized("没有权限") panic(result.Unauthorized("没有权限"))
} }
var srcMatters []*Matter var srcMatters []*Matter
@ -419,14 +419,14 @@ func (this *MatterController) Move(writer http.ResponseWriter, request *http.Req
srcMatter := this.matterDao.CheckByUuid(uuid) srcMatter := this.matterDao.CheckByUuid(uuid)
if srcMatter.Puuid == destMatter.Uuid { if srcMatter.Puuid == destMatter.Uuid {
this.PanicBadRequest("没有进行移动,操作无效!") panic(result.BadRequest("没有进行移动,操作无效!"))
} }
//判断同级文件夹中是否有同名的文件 //判断同级文件夹中是否有同名的文件
count := this.matterDao.CountByUserUuidAndPuuidAndDirAndName(user.Uuid, destMatter.Uuid, srcMatter.Dir, srcMatter.Name) count := this.matterDao.CountByUserUuidAndPuuidAndDirAndName(user.Uuid, destMatter.Uuid, srcMatter.Dir, srcMatter.Name)
if count > 0 { if count > 0 {
this.PanicBadRequest("【" + srcMatter.Name + "】在目标文件夹已经存在了,操作失败。") panic(result.BadRequest("【" + srcMatter.Name + "】在目标文件夹已经存在了,操作失败。"))
} }
//判断和目标文件夹是否是同一个主人。 //判断和目标文件夹是否是同一个主人。

View File

@ -46,7 +46,7 @@ func (this *MatterDao) FindByUuid(uuid string) *Matter {
func (this *MatterDao) CheckByUuid(uuid string) *Matter { func (this *MatterDao) CheckByUuid(uuid string) *Matter {
matter := this.FindByUuid(uuid) matter := this.FindByUuid(uuid)
if matter == nil { if matter == nil {
this.PanicNotFound("%s 对应的matter不存在", uuid) panic(result.NotFound("%s 对应的matter不存在", uuid))
} }
return matter return matter
} }
@ -55,13 +55,13 @@ func (this *MatterDao) CheckByUuid(uuid string) *Matter {
func (this *MatterDao) CheckWithRootByUuid(uuid string, user *User) *Matter { func (this *MatterDao) CheckWithRootByUuid(uuid string, user *User) *Matter {
if uuid == "" { if uuid == "" {
this.PanicBadRequest("uuid cannot be nil.") panic(result.BadRequest("uuid cannot be nil."))
} }
var matter *Matter var matter *Matter
if uuid == MATTER_ROOT { if uuid == MATTER_ROOT {
if user == nil { if user == nil {
this.PanicBadRequest("user cannot be nil.") panic(result.BadRequest("user cannot be nil."))
} }
matter = NewRootMatter(user) matter = NewRootMatter(user)
} else { } else {
@ -77,7 +77,7 @@ func (this *MatterDao) CheckWithRootByPath(path string, user *User) *Matter {
var matter *Matter var matter *Matter
if user == nil { if user == nil {
this.PanicBadRequest("user cannot be nil.") panic(result.BadRequest("user cannot be nil."))
} }
//目标文件夹matter //目标文件夹matter
@ -349,11 +349,11 @@ func (this *MatterDao) findByUserUuidAndPath(userUuid string, path string) *Matt
func (this *MatterDao) checkByUserUuidAndPath(userUuid string, path string) *Matter { func (this *MatterDao) checkByUserUuidAndPath(userUuid string, path string) *Matter {
if path == "" { if path == "" {
this.PanicBadRequest("path 不能为空") panic(result.BadRequest("path 不能为空"))
} }
matter := this.findByUserUuidAndPath(userUuid, path) matter := this.findByUserUuidAndPath(userUuid, path)
if matter == nil { if matter == nil {
this.PanicNotFound("path = %s 不存在", path) panic(result.NotFound("path = %s 不存在", path))
} }
return matter return matter

View File

@ -277,7 +277,7 @@ func (this *MatterService) handleOverwrite(userUuid string, destinationPath stri
//要求覆盖。那么删除。 //要求覆盖。那么删除。
this.matterDao.Delete(destMatter) this.matterDao.Delete(destMatter)
} else { } else {
this.PanicBadRequest("%s已经存在操作失败", destMatter.Path) panic(result.BadRequest("%s已经存在操作失败", destMatter.Path))
} }
} }
@ -291,7 +291,7 @@ func (this *MatterService) move(srcMatter *Matter, destDirMatter *Matter) {
} }
if !destDirMatter.Dir { if !destDirMatter.Dir {
this.PanicBadRequest("目标必须为文件夹") panic(result.BadRequest("目标必须为文件夹"))
} }
if srcMatter.Dir { if srcMatter.Dir {
@ -352,7 +352,7 @@ func (this *MatterService) AtomicMove(srcMatter *Matter, destDirMatter *Matter,
panic(result.BadRequest("destDirMatter cannot be nil.")) panic(result.BadRequest("destDirMatter cannot be nil."))
} }
if !destDirMatter.Dir { if !destDirMatter.Dir {
this.PanicBadRequest("目标必须为文件夹") panic(result.BadRequest("目标必须为文件夹"))
} }
//文件夹不能把自己移入到自己中,也不可以移入到自己的子文件夹下。 //文件夹不能把自己移入到自己中,也不可以移入到自己的子文件夹下。
@ -389,7 +389,7 @@ func (this *MatterService) AtomicMoveBatch(srcMatters []*Matter, destDirMatter *
} }
if !destDirMatter.Dir { if !destDirMatter.Dir {
this.PanicBadRequest("目标必须为文件夹") panic(result.BadRequest("目标必须为文件夹"))
} }
//文件夹不能把自己移入到自己中,也不可以移入到自己的子文件夹下。 //文件夹不能把自己移入到自己中,也不可以移入到自己的子文件夹下。
@ -477,7 +477,7 @@ func (this *MatterService) AtomicCopy(srcMatter *Matter, destDirMatter *Matter,
defer this.userService.MatterUnlock(srcMatter.UserUuid) defer this.userService.MatterUnlock(srcMatter.UserUuid)
if !destDirMatter.Dir { if !destDirMatter.Dir {
this.PanicBadRequest("目标必须为文件夹") panic(result.BadRequest("目标必须为文件夹"))
} }
destinationPath := destDirMatter.Path + "/" + name destinationPath := destDirMatter.Path + "/" + name
@ -490,7 +490,7 @@ func (this *MatterService) AtomicCopy(srcMatter *Matter, destDirMatter *Matter,
func (this *MatterService) AtomicRename(matter *Matter, name string, user *User) { func (this *MatterService) AtomicRename(matter *Matter, name string, user *User) {
if user == nil { if user == nil {
this.PanicBadRequest("user cannot be nil") panic(result.BadRequest("user cannot be nil"))
} }
//操作锁 //操作锁
@ -499,10 +499,10 @@ func (this *MatterService) AtomicRename(matter *Matter, name string, user *User)
//验证参数。 //验证参数。
if name == "" { if name == "" {
this.PanicBadRequest("name参数必填") panic(result.BadRequest("name参数必填"))
} }
if m, _ := regexp.MatchString(`[<>|*?/\\]`, name); m { if m, _ := regexp.MatchString(`[<>|*?/\\]`, name); m {
this.PanicBadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`) panic(result.BadRequest(`名称中不能包含以下特殊符号:< > | * ? / \`))
} }
if len(name) > 200 { if len(name) > 200 {
@ -510,14 +510,14 @@ func (this *MatterService) AtomicRename(matter *Matter, name string, user *User)
} }
if name == matter.Name { if name == matter.Name {
this.PanicBadRequest("新名称和旧名称一样,操作失败!") panic(result.BadRequest("新名称和旧名称一样,操作失败!"))
} }
//判断同级文件夹中是否有同名的文件 //判断同级文件夹中是否有同名的文件
count := this.matterDao.CountByUserUuidAndPuuidAndDirAndName(user.Uuid, matter.Puuid, matter.Dir, name) count := this.matterDao.CountByUserUuidAndPuuidAndDirAndName(user.Uuid, matter.Puuid, matter.Dir, name)
if count > 0 { if count > 0 {
this.PanicBadRequest("【" + name + "】已经存在了,请使用其他名称。") panic(result.BadRequest("【" + name + "】已经存在了,请使用其他名称。"))
} }
if matter.Dir { if matter.Dir {

View File

@ -92,7 +92,7 @@ func (this *PreferenceController) SystemCleanup(writer http.ResponseWriter, requ
password := request.FormValue("password") password := request.FormValue("password")
if !tool.MatchBcrypt(password, user.Password) { if !tool.MatchBcrypt(password, user.Password) {
this.PanicBadRequest("密码错误,不能执行!") panic(result.BadRequest("密码错误,不能执行!"))
} }
for _, bean := range CONTEXT.BeanMap { for _, bean := range CONTEXT.BeanMap {

View File

@ -97,6 +97,22 @@ func BadRequest(format string, v ...interface{}) *WebResult {
return CustomWebResult(CODE_WRAPPER_BAD_REQUEST, fmt.Sprintf(format, v...)) return CustomWebResult(CODE_WRAPPER_BAD_REQUEST, fmt.Sprintf(format, v...))
} }
//没有权限
func Unauthorized(format string, v ...interface{}) *WebResult {
return CustomWebResult(CODE_WRAPPER_UNAUTHORIZED, fmt.Sprintf(format, v...))
}
//没有找到
func NotFound(format string, v ...interface{}) *WebResult {
return CustomWebResult(CODE_WRAPPER_NOT_FOUND, fmt.Sprintf(format, v...))
}
//服务器内部出问题
func Server(format string, v ...interface{}) *WebResult {
return CustomWebResult(CODE_WRAPPER_SERVER, fmt.Sprintf(format, v...))
}
//所有的数据库错误情况 //所有的数据库错误情况
var ( var (
DB_ERROR_DUPLICATE_KEY = "Error 1062: Duplicate entry" DB_ERROR_DUPLICATE_KEY = "Error 1062: Duplicate entry"

View File

@ -51,18 +51,18 @@ func (this *UserController) Login(writer http.ResponseWriter, request *http.Requ
if "" == email || "" == password { if "" == email || "" == password {
this.PanicBadRequest("请输入邮箱和密码") panic(result.BadRequest("请输入邮箱和密码"))
} }
user := this.userDao.FindByEmail(email) user := this.userDao.FindByEmail(email)
if user == nil { if user == nil {
this.PanicBadRequest("邮箱或密码错误") panic(result.BadRequest("邮箱或密码错误"))
} else { } else {
if !tool.MatchBcrypt(password, user.Password) { if !tool.MatchBcrypt(password, user.Password) {
this.PanicBadRequest("邮箱或密码错误") panic(result.BadRequest("邮箱或密码错误"))
} }
} }
@ -188,7 +188,7 @@ func (this *UserController) Edit(writer http.ResponseWriter, request *http.Reque
user.SizeLimit = sizeLimit user.SizeLimit = sizeLimit
} else { } else {
if currentUser.Uuid != uuid { if currentUser.Uuid != uuid {
this.PanicUnauthorized("没有权限") panic(result.Unauthorized("没有权限"))
} }
} }
@ -310,11 +310,11 @@ func (this *UserController) Disable(writer http.ResponseWriter, request *http.Re
loginUser := this.checkUser(writer, request) loginUser := this.checkUser(writer, request)
if uuid == loginUser.Uuid { if uuid == loginUser.Uuid {
this.PanicBadRequest("你不能操作自己的状态。") panic(result.BadRequest("你不能操作自己的状态。"))
} }
if user.Status == USER_STATUS_DISABLED { if user.Status == USER_STATUS_DISABLED {
this.PanicBadRequest("用户已经被禁用,操作无效。") panic(result.BadRequest("用户已经被禁用,操作无效。"))
} }
user.Status = USER_STATUS_DISABLED user.Status = USER_STATUS_DISABLED
@ -333,11 +333,11 @@ func (this *UserController) Enable(writer http.ResponseWriter, request *http.Req
user := this.userDao.CheckByUuid(uuid) user := this.userDao.CheckByUuid(uuid)
loginUser := this.checkUser(writer, request) loginUser := this.checkUser(writer, request)
if uuid == loginUser.Uuid { if uuid == loginUser.Uuid {
this.PanicBadRequest("你不能操作自己的状态。") panic(result.BadRequest("你不能操作自己的状态。"))
} }
if user.Status == USER_STATUS_OK { if user.Status == USER_STATUS_OK {
this.PanicBadRequest("用户已经是正常状态,操作无效。") panic(result.BadRequest("用户已经是正常状态,操作无效。"))
} }
user.Status = USER_STATUS_OK user.Status = USER_STATUS_OK
@ -354,7 +354,7 @@ func (this *UserController) ChangePassword(writer http.ResponseWriter, request *
oldPassword := request.FormValue("oldPassword") oldPassword := request.FormValue("oldPassword")
newPassword := request.FormValue("newPassword") newPassword := request.FormValue("newPassword")
if oldPassword == "" || newPassword == "" { if oldPassword == "" || newPassword == "" {
this.PanicBadRequest("旧密码和新密码都不能为空") panic(result.BadRequest("旧密码和新密码都不能为空"))
} }
user := this.checkUser(writer, request) user := this.checkUser(writer, request)
@ -365,7 +365,7 @@ func (this *UserController) ChangePassword(writer http.ResponseWriter, request *
} }
if !tool.MatchBcrypt(oldPassword, user.Password) { if !tool.MatchBcrypt(oldPassword, user.Password) {
this.PanicBadRequest("旧密码不正确!") panic(result.BadRequest("旧密码不正确!"))
} }
user.Password = tool.GetBcrypt(newPassword) user.Password = tool.GetBcrypt(newPassword)
@ -381,16 +381,16 @@ func (this *UserController) ResetPassword(writer http.ResponseWriter, request *h
userUuid := request.FormValue("userUuid") userUuid := request.FormValue("userUuid")
password := request.FormValue("password") password := request.FormValue("password")
if userUuid == "" { if userUuid == "" {
this.PanicBadRequest("用户不能为空") panic(result.BadRequest("用户不能为空"))
} }
if password == "" { if password == "" {
this.PanicBadRequest("密码不能为空") panic(result.BadRequest("密码不能为空"))
} }
currentUser := this.checkUser(writer, request) currentUser := this.checkUser(writer, request)
if currentUser.Role != USER_ROLE_ADMINISTRATOR { if currentUser.Role != USER_ROLE_ADMINISTRATOR {
this.PanicUnauthorized("没有权限") panic(result.Unauthorized("没有权限"))
} }
user := this.userDao.CheckByUuid(userUuid) user := this.userDao.CheckByUuid(userUuid)

View File

@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"tank/rest/cache" "tank/rest/cache"
"tank/rest/config" "tank/rest/config"
"tank/rest/result"
"time" "time"
) )
@ -49,7 +50,7 @@ func (this *UserService) MatterLock(userUuid string) {
//当前被锁住了。 //当前被锁住了。
if cacheItem != nil && cacheItem.Data() != nil { if cacheItem != nil && cacheItem.Data() != nil {
this.PanicBadRequest("当前正在进行文件操作,请稍后再试!") panic(result.BadRequest("当前正在进行文件操作,请稍后再试!"))
} }
//添加一把新锁有效期为12小时 //添加一把新锁有效期为12小时