Skip to content

Commit

Permalink
add sftp permssion cache
Browse files Browse the repository at this point in the history
LeeEirc committed Jul 11, 2019
1 parent 24dd3c4 commit 82dd500
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion pkg/handler/sftp.go
Original file line number Diff line number Diff line change
@@ -51,12 +51,15 @@ type sftpHandler struct {
assets model.AssetList
rootPath string // tmp || home || ~
hosts map[string]*HostNameDir

permCache map[string]bool
}

func (fs *sftpHandler) initial() {
fs.loadAssets()
fs.hosts = make(map[string]*HostNameDir)
fs.rootPath = config.GetConf().SftpRoot
fs.permCache = make(map[string]bool)
for i, item := range fs.assets {
tmpDir := &HostNameDir{
rootPath: fs.rootPath,
@@ -401,9 +404,16 @@ func (fs *sftpHandler) Close() {
}

func (fs *sftpHandler) validatePermission(aid, suid, operate string) bool {
return service.ValidateUserAssetPermission(
permKey := fmt.Sprintf("%s_%s_%s", aid, suid, operate)
permission, ok := fs.permCache[permKey]
if ok {
return permission
}
permission = service.ValidateUserAssetPermission(
fs.user.ID, aid, suid, operate,
)
fs.permCache[permKey] = permission
return permission
}

type HostNameDir struct {
12 changes: 11 additions & 1 deletion pkg/httpd/sftpvolume.go
Original file line number Diff line number Diff line change
@@ -48,13 +48,16 @@ type UserVolume struct {
hosts map[string]*hostnameVolume

localTmpPath string

permCache map[string]bool
}

func (u *UserVolume) initial() {
conf := config.GetConf()
u.loadAssets()
u.rootPath = conf.SftpRoot
u.localTmpPath = filepath.Join(conf.RootPath, "data", "tmp")
u.permCache = make(map[string]bool)
_ = common.EnsureDirExist(u.localTmpPath)
u.hosts = make(map[string]*hostnameVolume)
for i, item := range u.assets {
@@ -939,9 +942,16 @@ func (u *UserVolume) CreateFTPLog(data *model.FTPLog) {
}

func (u *UserVolume) validatePermission(aid, suid, operate string) bool {
return service.ValidateUserAssetPermission(
permKey := fmt.Sprintf("%s_%s_%s", aid, suid, operate)
permission, ok := u.permCache[permKey]
if ok {
return permission
}
permission = service.ValidateUserAssetPermission(
u.user.ID, aid, suid, operate,
)
u.permCache[permKey] = permission
return permission
}

type hostnameVolume struct {

0 comments on commit 82dd500

Please sign in to comment.