2021-01-19 22:44:35 +00:00
|
|
|
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.
|
2021-01-21 20:48:59 +00:00
|
|
|
type Rauth int
|
|
|
|
|
|
|
|
//
|
|
|
|
const (
|
|
|
|
ReqAuthAbsent Rauth = -1
|
|
|
|
ReqAuthUnspec Rauth = 0
|
|
|
|
ReqAuthRequired Rauth = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
var rauthmap = map[Rauth]string{
|
|
|
|
ReqAuthAbsent: "Authentication Absent",
|
|
|
|
ReqAuthRequired: "Authntication Required",
|
|
|
|
ReqAuthUnspec: "Authentication irrelevant",
|
|
|
|
}
|
|
|
|
|
|
|
|
// String is to Statisfy th Stringer interface
|
|
|
|
func (r *Rauth) String() string {
|
|
|
|
return rauthmap[*r]
|
|
|
|
}
|
|
|
|
|
2021-01-19 22:44:35 +00:00
|
|
|
// 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
|
2021-01-19 22:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MenuItem is an Implementation of the Menu Interface
|
|
|
|
// Enabled returns always true
|
|
|
|
type MenuItem struct {
|
|
|
|
ItemName string
|
|
|
|
ItemAltText string
|
|
|
|
ItemURL *url.URL
|
|
|
|
ItemChildren []Menu
|
2021-01-21 20:48:59 +00:00
|
|
|
RequireAuth Rauth
|
2021-01-19 22:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2021-01-21 20:48:59 +00:00
|
|
|
func (m *MenuItem) Enabled(a auth.Account) bool {
|
|
|
|
switch m.RequireAuth {
|
|
|
|
case ReqAuthAbsent:
|
|
|
|
return true && a.Anonymous()
|
|
|
|
case ReqAuthUnspec:
|
|
|
|
return true
|
|
|
|
case ReqAuthRequired:
|
|
|
|
return true && !a.Anonymous()
|
|
|
|
}
|
|
|
|
return false
|
2021-01-19 22:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Menu = &MenuItem{}
|