92 Zeilen
1.9 KiB
Go
92 Zeilen
1.9 KiB
Go
package menus
|
|
|
|
import (
|
|
"net/url"
|
|
|
|
"go.sebtobie.de/httpserver/auth"
|
|
)
|
|
|
|
// 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",
|
|
ReqAuthRequired: "Authentication Required",
|
|
ReqAuthUnspec: "Authentication irrelevant",
|
|
}
|
|
|
|
// 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 {
|
|
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
|
|
case ReqAuthAbsent:
|
|
return true && a.Anonymous()
|
|
case ReqAuthRequired:
|
|
return true && !a.Anonymous()
|
|
}
|
|
return false
|
|
}
|
|
|
|
var _ Menu = &MenuItem{}
|