added user independent sections and started db migration work
This commit is contained in:
parent
6a2dc8146c
commit
16ea95bf09
24 changed files with 622 additions and 122 deletions
11
pkg/core/helper.go
Normal file
11
pkg/core/helper.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package core
|
||||
|
||||
// Contains checks if slice a contains string str
|
||||
func Contains(str string, a []string) (int, bool) {
|
||||
for i, s := range a {
|
||||
if s == str {
|
||||
return i, true
|
||||
}
|
||||
}
|
||||
return -1, false
|
||||
}
|
||||
|
|
@ -7,7 +7,6 @@ type Permission int
|
|||
const (
|
||||
Invalid Permission = iota
|
||||
Public
|
||||
Internal
|
||||
Private
|
||||
)
|
||||
|
||||
|
|
@ -16,37 +15,27 @@ func ParsePermString(p string) Permission {
|
|||
case "1":
|
||||
return Public
|
||||
case "2":
|
||||
return Internal
|
||||
case "3":
|
||||
return Private
|
||||
}
|
||||
return Invalid
|
||||
}
|
||||
|
||||
func ReadPerm(username string, p *Page) bool {
|
||||
switch p.Perm {
|
||||
func ReadPerm(username string, p Permission, s Section) bool {
|
||||
switch p {
|
||||
case Public:
|
||||
return true
|
||||
case Internal:
|
||||
return username != ""
|
||||
case Private:
|
||||
return username == p.Owner
|
||||
_, c := Contains(username, s.Members)
|
||||
return c
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func WritePerm(username, section string, p *Page) bool {
|
||||
if section != WikiSection && section != username {
|
||||
return false
|
||||
}
|
||||
switch p.Perm {
|
||||
case Public, Internal:
|
||||
if p.Owner == WikiSection {
|
||||
return username != ""
|
||||
}
|
||||
return username == p.Owner
|
||||
case Private:
|
||||
return username == p.Owner
|
||||
func WritePerm(username string, p Permission, s Section) bool {
|
||||
switch p {
|
||||
case Public, Private:
|
||||
_, c := Contains(username, s.Members)
|
||||
return c
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,8 +11,7 @@ func TestParsePermString(t *testing.T) {
|
|||
data := map[string]Permission{
|
||||
"": Invalid,
|
||||
"1": Public,
|
||||
"2": Internal,
|
||||
"3": Private,
|
||||
"2": Private,
|
||||
"123": Invalid,
|
||||
"test": Invalid,
|
||||
}
|
||||
|
|
@ -28,21 +27,23 @@ func TestParsePermString(t *testing.T) {
|
|||
func TestReadPerm(t *testing.T) {
|
||||
data := []struct {
|
||||
Username string
|
||||
Page *Page
|
||||
Perm Permission
|
||||
Section Section
|
||||
OK bool
|
||||
}{
|
||||
{"", &Page{Perm: Invalid, Owner: "admin"}, false},
|
||||
{"", &Page{Perm: Public, Owner: "admin"}, true},
|
||||
{"user", &Page{Perm: Public, Owner: "admin"}, true},
|
||||
{"", &Page{Perm: Internal, Owner: "admin"}, false},
|
||||
{"user", &Page{Perm: Internal, Owner: "admin"}, true},
|
||||
{"", &Page{Perm: Private, Owner: "admin"}, false},
|
||||
{"user", &Page{Perm: Private, Owner: "admin"}, false},
|
||||
{"admin", &Page{Perm: Private, Owner: "admin"}, true},
|
||||
{"", Invalid, Section{Members: []string{"admin"}}, false},
|
||||
{"", Public, Section{Members: []string{"admin"}}, true},
|
||||
{"user", Public, Section{Members: []string{"admin"}}, true},
|
||||
{"", Private, Section{Members: []string{"admin"}}, false},
|
||||
{"user", Private, Section{Members: []string{"admin"}}, false},
|
||||
{"", Private, Section{Members: []string{"user"}}, false},
|
||||
{"user", Private, Section{Members: []string{"user"}}, true},
|
||||
{"user", Private, Section{Members: []string{"user", "admin"}}, true},
|
||||
{"admin", Private, Section{Members: []string{"user"}}, false},
|
||||
}
|
||||
for i, v := range data {
|
||||
t.Run(fmt.Sprintf("Read:%d", i), func(t *testing.T) {
|
||||
if ReadPerm(v.Username, v.Page) != v.OK {
|
||||
if ReadPerm(v.Username, v.Perm, v.Section) != v.OK {
|
||||
t.Fail()
|
||||
}
|
||||
})
|
||||
|
|
@ -52,28 +53,23 @@ func TestReadPerm(t *testing.T) {
|
|||
func TestWritePerm(t *testing.T) {
|
||||
data := []struct {
|
||||
Username string
|
||||
Section string
|
||||
Page *Page
|
||||
Perm Permission
|
||||
Section Section
|
||||
OK bool
|
||||
}{
|
||||
{"user", "admin", &Page{Perm: Invalid, Owner: "admin"}, false},
|
||||
{"", "user", &Page{Perm: Invalid, Owner: "admin"}, false},
|
||||
{"user", "user", &Page{Perm: Invalid, Owner: "user"}, false},
|
||||
{"user", "user", &Page{Perm: Public, Owner: "user"}, true},
|
||||
{"user", "user", &Page{Perm: Internal, Owner: "user"}, true},
|
||||
{"user", "user", &Page{Perm: Private, Owner: "user"}, true},
|
||||
{"", WikiSection, &Page{Perm: Invalid, Owner: "admin"}, false},
|
||||
{"", WikiSection, &Page{Perm: Public, Owner: "admin"}, false},
|
||||
{"", WikiSection, &Page{Perm: Internal, Owner: "admin"}, false},
|
||||
{"user", WikiSection, &Page{Perm: Public, Owner: WikiSection}, true},
|
||||
{"user", WikiSection, &Page{Perm: Internal, Owner: WikiSection}, true},
|
||||
{"", "admin", &Page{Perm: Private, Owner: "admin"}, false},
|
||||
{"user", "admin", &Page{Perm: Private, Owner: "admin"}, false},
|
||||
{"admin", "admin", &Page{Perm: Private, Owner: "admin"}, true},
|
||||
{"", Invalid, Section{Members: []string{"user"}}, false},
|
||||
{"", Public, Section{Members: []string{"user"}}, false},
|
||||
{"user", Public, Section{Members: []string{"user"}}, true},
|
||||
{"user", Public, Section{Members: []string{"admin"}}, false},
|
||||
{"user", Private, Section{Members: []string{"user"}}, true},
|
||||
{"", Private, Section{Members: []string{"user"}}, false},
|
||||
{"", Private, Section{Members: []string{"user", "admin"}}, false},
|
||||
{"user", Private, Section{Members: []string{"user", "admin"}}, true},
|
||||
{"admin", Private, Section{Members: []string{"user", "admin"}}, true},
|
||||
}
|
||||
for i, v := range data {
|
||||
t.Run(fmt.Sprintf("Write:%d", i), func(t *testing.T) {
|
||||
if WritePerm(v.Username, v.Section, v.Page) != v.OK {
|
||||
if WritePerm(v.Username, v.Perm, v.Section) != v.OK {
|
||||
t.Fail()
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ type Page struct {
|
|||
HTML template.HTML `json:"html"`
|
||||
Created string `json:"created"`
|
||||
Updated string `json:"updated"`
|
||||
Owner string `json:"owner"`
|
||||
Perm Permission `json:"perm"`
|
||||
}
|
||||
|
||||
|
|
@ -29,15 +28,27 @@ func (p UpdatedPages) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|||
func (p UpdatedPages) Less(i, j int) bool { return p[i].Updated > p[j].Updated }
|
||||
|
||||
type Section struct {
|
||||
Section string
|
||||
Pages UpdatedPages
|
||||
Name string
|
||||
User bool
|
||||
Members []string
|
||||
}
|
||||
|
||||
type Sections []Section
|
||||
|
||||
func (s Sections) Len() int { return len(s) }
|
||||
func (s Sections) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
func (s Sections) Less(i, j int) bool { return s[i].Section < s[j].Section }
|
||||
func (s Sections) Less(i, j int) bool { return s[i].Name < s[j].Name }
|
||||
|
||||
type SectionPage struct {
|
||||
Section string
|
||||
Pages UpdatedPages
|
||||
}
|
||||
|
||||
type SectionPages []SectionPage
|
||||
|
||||
func (s SectionPages) Len() int { return len(s) }
|
||||
func (s SectionPages) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
func (s SectionPages) Less(i, j int) bool { return s[i].Section < s[j].Section }
|
||||
|
||||
type User struct {
|
||||
Username string
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue