Skip to content

Commit

Permalink
Merge pull request #157 from ssb-ngi-pointer/role-template-helper
Browse files Browse the repository at this point in the history
add member_is_admin and member_has_role template helpers
  • Loading branch information
cryptix authored Apr 16, 2021
2 parents b411692 + 04fb06d commit 26ce19f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 18 deletions.
13 changes: 10 additions & 3 deletions roomdb/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions web/handlers/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
65 changes: 52 additions & 13 deletions web/members/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,57 @@ 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{} {
no := func() *roomdb.Member { return nil }

member := FromContext(r.Context())
if member == nil {
return no
}

yes := func() *roomdb.Member { return member }
return yes
// 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())
if member == nil {
return no
}

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
}
}),
}
}

0 comments on commit 26ce19f

Please sign in to comment.