1
0
Fork 0
httpserver/menus/menus.go

92 Zeilen
1.9 KiB
Go

package menus
import (
"net/url"
"go.sebtobie.de/httpserver/auth"
)
2021-01-23 10:15:47 +00:00
// Rauth is an type to idenitfy the need of the handlers.
type Rauth int
//
const (
ReqAuthAbsent Rauth = -1
ReqAuthUnspec Rauth = 0
ReqAuthRequired Rauth = 1
)
var rauthmap = map[Rauth]string{
ReqAuthAbsent: "Authentication Absent",
2021-10-25 20:28:39 +00:00
ReqAuthRequired: "Authentication Required",
ReqAuthUnspec: "Authentication irrelevant",
}
2021-10-25 20:28:39 +00:00
// String is to Statisfy the Stringer interface
func (r *Rauth) String() string {
return rauthmap[*r]
}
// Menu is an Interface to give the server infos about menuitems
type Menu interface {
Name() string
AltText() string
URL() *url.URL
Children() []Menu
Enabled(auth.Account) bool
}
// MenuSite is an interface that is fullfilled by sites to return an type of menu.
type MenuSite interface {
2021-01-23 10:14:33 +00:00
Menu(string) []Menu
}
// MenuItem is an Implementation of the Menu Interface
// Enabled returns always true
type MenuItem struct {
ItemName string
ItemAltText string
ItemURL *url.URL
ItemChildren []Menu
RequireAuth Rauth
}
// Name returns the name of the Item.
func (m *MenuItem) Name() string {
return m.ItemName
}
// AltText is for hovertext or Descriptions. Its can return "".
// to make templatemaking easier i decided to make an return required.
func (m *MenuItem) AltText() string {
return m.ItemAltText
}
// URL returns the full URL of the item.
func (m *MenuItem) URL() *url.URL {
return m.ItemURL
}
// Children returns the ItemChildren or an empty Menu-Arry if ItemChildren is nil
func (m *MenuItem) Children() []Menu {
if m.ItemChildren == nil {
return []Menu{}
}
return m.ItemChildren
}
// Enabled returns always true
func (m *MenuItem) Enabled(a auth.Account) bool {
switch m.RequireAuth {
case ReqAuthUnspec:
return true
2021-10-25 20:28:39 +00:00
case ReqAuthAbsent:
return true && a.Anonymous()
case ReqAuthRequired:
return true && !a.Anonymous()
}
return false
}
var _ Menu = &MenuItem{}