2022-11-22 08:14:01 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2022-11-30 14:01:33 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2022-11-22 08:14:01 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CanWrite(meta *model.Meta, path string) bool {
|
|
|
|
if meta == nil || !meta.Write {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return meta.WSub || meta.Path == path
|
|
|
|
}
|
|
|
|
|
2022-11-30 14:01:33 +00:00
|
|
|
func CanAccess(user *model.User, meta *model.Meta, reqPath string, password string) bool {
|
|
|
|
// if the reqPath is in hide (only can check the nearest meta) and user can't see hides, can't access
|
2023-01-17 13:54:25 +00:00
|
|
|
if meta != nil && !user.CanSeeHides() && meta.Hide != "" && !utils.PathEqual(meta.Path, reqPath) {
|
2022-11-30 14:01:33 +00:00
|
|
|
for _, hide := range strings.Split(meta.Hide, "\n") {
|
|
|
|
re := regexp.MustCompile(hide)
|
2023-01-17 13:54:25 +00:00
|
|
|
if re.MatchString(reqPath[len(meta.Path)+1:]) {
|
2022-11-30 14:01:33 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if is not guest and can access without password
|
2022-11-22 08:14:01 +00:00
|
|
|
if user.CanAccessWithoutPassword() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// if meta is nil or password is empty, can access
|
|
|
|
if meta == nil || meta.Password == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// if meta doesn't apply to sub_folder, can access
|
2022-11-30 14:01:33 +00:00
|
|
|
if !utils.PathEqual(meta.Path, reqPath) && !meta.PSub {
|
2022-11-22 08:14:01 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
// validate password
|
|
|
|
return meta.Password == password
|
|
|
|
}
|