Merge pull request #157 from ssb-ngi-pointer/role-template-helper

add member_is_admin and member_has_role template helpers
This commit is contained in:
Henry 2021-04-16 10:11:57 +02:00 committed by GitHub
commit 26ce19f1eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 16 deletions

View File

@ -125,17 +125,24 @@ const (
RoleAdmin
)
var (
roleAdminString = RoleAdmin.String()
roleModString = RoleModerator.String()
roleMemberString = RoleMember.String()
)
// UnmarshalText checks if a string is a valid role
func (r *Role) UnmarshalText(text []byte) error {
roleStr := string(text)
switch roleStr {
case RoleAdmin.String():
case roleAdminString:
*r = RoleAdmin
case RoleModerator.String():
case roleModString:
*r = RoleModerator
case RoleMember.String():
case roleMemberString:
*r = RoleMember
default:

View File

@ -147,10 +147,10 @@ func New(
return u
}
}),
render.InjectTemplateFunc("is_logged_in", members.TemplateHelper()),
}
renderOpts = append(renderOpts, locHelper.GetRenderFuncs()...)
renderOpts = append(renderOpts, members.TemplateHelpers()...)
r, err := render.New(web.Templates, renderOpts...)
if err != nil {

View File

@ -91,10 +91,18 @@ func ContextInjecter(mdb roomdb.MembersService, withPassword *auth.Handler, with
}
}
// TemplateHelper returns a function to be used with the http/render package.
// It has to return a function twice because the first is evaluated with the request before it gets passed onto html/template's FuncMap.
func TemplateHelper() func(*http.Request) interface{} {
return func(r *http.Request) interface{} {
// TemplateHelpers returns functions to be used with the go.mindeco.de/http/render package.
// Each has to return a function twice because the first is evaluated with the request before it gets passed onto html/template's FuncMap.
//
// {{ is_logged_in }} returns true or false depending if the user is logged in
//
// {{ member_has_role "string" }} returns a boolean which confrms wether the member has a certain role (RoleMemeber, RoleAdmin, etc)
//
// {{ member_is_admin }} is a shortcut for {{ member_has_role "RoleAdmin" }}
func TemplateHelpers() []render.Option {
return []render.Option{
render.InjectTemplateFunc("is_logged_in", func(r *http.Request) interface{} {
no := func() *roomdb.Member { return nil }
member := FromContext(r.Context())
@ -104,5 +112,36 @@ func TemplateHelper() func(*http.Request) interface{} {
yes := func() *roomdb.Member { return member }
return yes
}),
render.InjectTemplateFunc("member_has_role", func(r *http.Request) interface{} {
no := func(_ string) bool { return false }
member := FromContext(r.Context())
if member == nil {
return no
}
return func(has string) bool {
var r roomdb.Role
if err := r.UnmarshalText([]byte(has)); err != nil {
return false
}
return member.Role == r
}
}),
render.InjectTemplateFunc("member_is_admin", func(r *http.Request) interface{} {
no := func() bool { return false }
member := FromContext(r.Context())
if member == nil {
return no
}
return func() bool {
return member.Role == roomdb.RoleAdmin
}
}),
}
}