| 48 | } |
| 49 | |
| 50 | func CanAccess(user *model.User, meta *model.Meta, reqPath string, password string) bool { |
| 51 | // if the reqPath is in hide (only can check the nearest meta) and user can't see hides, can't access |
| 52 | if meta != nil && !user.CanSeeHides() && meta.Hide != "" && |
| 53 | MetaCoversPath(meta.Path, path.Dir(reqPath), meta.HSub) { // the meta should apply to the parent of current path |
| 54 | for hide := range strings.SplitSeq(meta.Hide, "\n") { |
| 55 | re := regexp2.MustCompile(hide, regexp2.None) |
| 56 | if isMatch, _ := re.MatchString(path.Base(reqPath)); isMatch { |
| 57 | return false |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | if !CanRead(user, meta, reqPath) { |
| 62 | return false |
| 63 | } |
| 64 | // if is not guest and can access without password |
| 65 | if user.CanAccessWithoutPassword() { |
| 66 | return true |
| 67 | } |
| 68 | // if meta is nil or password is empty, can access |
| 69 | if meta == nil || meta.Password == "" { |
| 70 | return true |
| 71 | } |
| 72 | // if meta doesn't apply to sub_folder, can access |
| 73 | if !MetaCoversPath(meta.Path, reqPath, meta.PSub) { |
| 74 | return true |
| 75 | } |
| 76 | // validate password |
| 77 | return meta.Password == password |
| 78 | } |
| 79 | |
| 80 | func MetaCoversPath(metaPath, reqPath string, applyToSubFolder bool) bool { |
| 81 | if utils.PathEqual(metaPath, reqPath) { |