gowiki/pkg/core/permission.go
2018-09-17 23:54:55 +02:00

66 lines
1,006 B
Go

package core
type Permission int
const (
Invalid Permission = iota
Public
Internal
Private
)
func ParsePerm(p int) Permission {
switch p {
case 1:
return Public
case 2:
return Internal
case 3:
return Private
}
return Invalid
}
func ParsePermString(p string) Permission {
switch p {
case "1":
return Public
case "2":
return Internal
case "3":
return Private
}
return Invalid
}
func ReadPerm(username string, p *Page) bool {
switch p.Perm {
case Public:
return true
case Internal:
return username != ""
case Private:
return username == p.Owner
}
return false
}
func WritePerm(username, section string, p *Page) bool {
if section != WikiSection && section != username {
return false
}
return writePerm(username, p)
}
func writePerm(username string, p *Page) bool {
switch p.Perm {
case Public, Internal:
if p.Owner == WikiSection {
return username != ""
}
return username == p.Owner
case Private:
return username == p.Owner
}
return false
}