Documentation ¶
Overview ¶
Package members implements helpers for accessing the currently logged in admin or moderator of an active request.
Index ¶
- Constants
- func CheckAllowed(ctx context.Context, cfg roomdb.RoomConfig, action string) (*roomdb.Member, error)
- func FromContext(ctx context.Context) *roomdb.Member
- func MiddlewareForTests(m *roomdb.Member) func(http.Handler) http.Handler
- func TemplateHelpers(roomCfg roomdb.RoomConfig) []render.Option
- type AllowedFunc
- type Middleware
Constants ¶
const ( ActionInviteMember = "invite" ActionChangeDeniedKeys = "change-denied-keys" ActionRemoveMember = "remove-member" ActionChangeNotice = "change-notice" )
member actions
Variables ¶
This section is empty.
Functions ¶
func CheckAllowed ¶
func CheckAllowed(ctx context.Context, cfg roomdb.RoomConfig, action string) (*roomdb.Member, error)
CheckAllowed retreives the member from the passed context and lookups the current privacy mode from the passed cfg to determain if the action is okay or not. If it's not it returns an error. For convenience it also returns the member if the action is okay.
func FromContext ¶
FromContext returns the member or nil if not logged in
func MiddlewareForTests ¶
MiddlewareForTests gives us a way to inject _test members_. It should not be used in production. This is part of testing.go because we need to use roomMemberContextKey, which shouldn't be exported either. TODO: could be protected with an extra build tag. (Sadly +build test does not exist https://github.com/golang/go/issues/21360 )
func TemplateHelpers ¶
func TemplateHelpers(roomCfg roomdb.RoomConfig) []render.Option
TemplateHelpers returns functions to be used with the go.mindeco.de/http/render package. Each helper 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 on 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" }} {{ member_is_elevated }} is a shortcut for {{ or member_has_role "RoleAdmin" member_has_role "RoleModerator"}} {{ member_can "action" }} returns true if a member can execute a certain action. Actions are "invite" and "remove-denied-key". See allowedActions to add more.
Types ¶
type AllowedFunc ¶
type AllowedFunc func(mode roomdb.PrivacyMode, role roomdb.Role) bool
AllowedFunc returns true if a member role is allowed to do a thing under the passed mode
func AllowedActions ¶
func AllowedActions(name string) (AllowedFunc, bool)
AllowedActions exposes check function by name. It exists to protected against changes of the map
type Middleware ¶
func AuthenticateFromContext ¶
func AuthenticateFromContext(r *render.Renderer) Middleware
AuthenticateFromContext calls the next http handler if there is a member stored in the context otherwise it will call r.Error
func ContextInjecter ¶
func ContextInjecter(mdb roomdb.MembersService, withPassword *auth.Handler, withSSB *authWithSSB.WithSSBHandler) Middleware
ContextInjecter returns middleware for injecting a member into the context of the request. Retreive it using FromContext(ctx)