Auth: Refactor user model and path validation #98

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2023-03-13 17:52:48 +01:00
parent 36d62b5506
commit 88367b4ff7
2 changed files with 23 additions and 3 deletions

View file

@ -427,8 +427,21 @@ func (m *User) CanUpload() bool {
}
}
// DefaultBasePath returns the default base path of the user based on the user name.
func (m *User) DefaultBasePath() string {
if m.UserName == "" {
return ""
}
return fmt.Sprintf("users/%s", m.UserName)
}
// GetBasePath returns the user's relative base path.
func (m *User) GetBasePath() string {
if m.BasePath == "" && m.HasRole("contributor") {
m.BasePath = m.DefaultBasePath()
}
return m.BasePath
}
@ -437,7 +450,7 @@ func (m *User) SetBasePath(dir string) *User {
if list.Contains(list.List{"", ".", "./", "/", "\\"}, dir) {
m.BasePath = ""
} else if dir == "~" && m.UserName != "" {
m.BasePath = fmt.Sprintf("users/%s", m.UserName)
m.BasePath = m.DefaultBasePath()
} else {
m.BasePath = clean.UserPath(dir)
}
@ -454,7 +467,7 @@ func (m *User) GetUploadPath() string {
} else if basePath != "" && strings.HasPrefix(m.UploadPath, basePath+"/") {
return m.UploadPath
} else if basePath == "" && m.UploadPath == "~" && m.UserName != "" {
return fmt.Sprintf("users/%s", m.UserName)
return m.DefaultBasePath()
}
return path.Join(basePath, m.UploadPath)
@ -467,7 +480,7 @@ func (m *User) SetUploadPath(dir string) *User {
if list.Contains(list.List{"", ".", "./", "/", "\\"}, dir) {
m.UploadPath = ""
} else if basePath == "" && dir == "~" && m.UserName != "" {
m.UploadPath = fmt.Sprintf("users/%s", m.UserName)
m.UploadPath = m.DefaultBasePath()
} else {
m.UploadPath = clean.UserPath(dir)
}
@ -609,6 +622,11 @@ func (m *User) SetRole(role string) *User {
return m
}
// HasRole checks the user role specified as string.
func (m *User) HasRole(role string) bool {
return m.AclRole().String() == acl.ValidRoles[clean.Role(role)].String()
}
// AclRole returns the user role for ACL permission checks.
func (m *User) AclRole() acl.Role {
role := clean.Role(m.UserRole)

View file

@ -1039,6 +1039,7 @@ func TestUser_SetBasePath(t *testing.T) {
assert.Equal(t, "base", u.SetBasePath("base").GetBasePath())
assert.Equal(t, "users/test", u.SetBasePath("~").GetBasePath())
assert.Equal(t, "users/test", u.DefaultBasePath())
assert.Equal(t, "users/test", u.GetUploadPath())
})
}
@ -1072,6 +1073,7 @@ func TestUser_SetUploadPath(t *testing.T) {
assert.Equal(t, "upload", u.SetUploadPath("upload").GetUploadPath())
assert.Equal(t, "base/upload", u.SetBasePath("base").GetUploadPath())
assert.Equal(t, "base", u.SetUploadPath("~").GetUploadPath())
assert.Equal(t, "users/test", u.DefaultBasePath())
assert.Equal(t, "users/test", u.SetBasePath("~").GetUploadPath())
})
}