moved more of the common preliminaries for conferences into middleware
This commit is contained in:
@@ -23,52 +23,6 @@ import (
|
|||||||
"git.erbosoft.com/amy/amsterdam/ui"
|
"git.erbosoft.com/amy/amsterdam/ui"
|
||||||
)
|
)
|
||||||
|
|
||||||
// conferencesPrequel consolidates some of the basic conference checks into one function.
|
|
||||||
func conferencesPrequel(ctxt ui.AmContext) (string, any, error) {
|
|
||||||
comm := ctxt.CurrentCommunity() // set by middleware
|
|
||||||
b, err := database.AmTestService(comm, "Conference")
|
|
||||||
if err != nil {
|
|
||||||
return ui.ErrorPage(ctxt, err)
|
|
||||||
}
|
|
||||||
if !b {
|
|
||||||
ctxt.SetRC(http.StatusNotFound)
|
|
||||||
return ui.ErrorPage(ctxt, errors.New("this community does not use conferencing services"))
|
|
||||||
}
|
|
||||||
if comm.MembersOnly && !ctxt.IsMember() && !ctxt.TestPermission("Community.NoJoinRequired") {
|
|
||||||
ctxt.SetRC(http.StatusForbidden)
|
|
||||||
return ui.ErrorPage(ctxt, errors.New("you are not a member of this community"))
|
|
||||||
}
|
|
||||||
if !comm.TestPermission("Community.Read", ctxt.EffectiveLevel()) {
|
|
||||||
ctxt.SetRC(http.StatusForbidden)
|
|
||||||
return ui.ErrorPage(ctxt, errors.New("you are not authorized access to conferences"))
|
|
||||||
}
|
|
||||||
return "", nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// singleConferencePrequel consolidates some of the basic conference checks into one function.
|
|
||||||
func singleConferencePrequel(ctxt ui.AmContext) (string, any, error) {
|
|
||||||
cmd, arg, err := conferencesPrequel(ctxt)
|
|
||||||
if cmd != "" {
|
|
||||||
return cmd, arg, err
|
|
||||||
}
|
|
||||||
var conf *database.Conference
|
|
||||||
conf, err = database.AmGetConferenceByAliasInCommunity(ctxt.CurrentCommunity().Id, ctxt.URLParam("confid"))
|
|
||||||
if err != nil {
|
|
||||||
return ui.ErrorPage(ctxt, err)
|
|
||||||
}
|
|
||||||
m, lvl, err := conf.Membership(ctxt.CurrentUser())
|
|
||||||
if err != nil {
|
|
||||||
return ui.ErrorPage(ctxt, err)
|
|
||||||
}
|
|
||||||
myLevel := ctxt.EffectiveLevel()
|
|
||||||
if m && lvl > myLevel {
|
|
||||||
myLevel = lvl
|
|
||||||
}
|
|
||||||
ctxt.SetScratch("currentConference", conf)
|
|
||||||
ctxt.SetScratch("levelInConference", myLevel)
|
|
||||||
return "", nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Conferences displayes the list of conferences in a community.
|
/* Conferences displayes the list of conferences in a community.
|
||||||
* Parameters:
|
* Parameters:
|
||||||
* ctxt - The AmContext for the request.
|
* ctxt - The AmContext for the request.
|
||||||
@@ -78,10 +32,6 @@ func singleConferencePrequel(ctxt ui.AmContext) (string, any, error) {
|
|||||||
* Standard Go error status.
|
* Standard Go error status.
|
||||||
*/
|
*/
|
||||||
func Conferences(ctxt ui.AmContext) (string, any, error) {
|
func Conferences(ctxt ui.AmContext) (string, any, error) {
|
||||||
cmd, arg, err := conferencesPrequel(ctxt)
|
|
||||||
if cmd != "" {
|
|
||||||
return cmd, arg, err
|
|
||||||
}
|
|
||||||
comm := ctxt.CurrentCommunity()
|
comm := ctxt.CurrentCommunity()
|
||||||
ctxt.VarMap().Set("commName", comm.Name)
|
ctxt.VarMap().Set("commName", comm.Name)
|
||||||
ctxt.VarMap().Set("commAlias", comm.Alias)
|
ctxt.VarMap().Set("commAlias", comm.Alias)
|
||||||
@@ -104,10 +54,6 @@ func Conferences(ctxt ui.AmContext) (string, any, error) {
|
|||||||
* Standard Go error status.
|
* Standard Go error status.
|
||||||
*/
|
*/
|
||||||
func Topics(ctxt ui.AmContext) (string, any, error) {
|
func Topics(ctxt ui.AmContext) (string, any, error) {
|
||||||
cmd, arg, err := singleConferencePrequel(ctxt)
|
|
||||||
if cmd != "" {
|
|
||||||
return cmd, arg, err
|
|
||||||
}
|
|
||||||
prefs, err := ctxt.CurrentUser().Prefs()
|
prefs, err := ctxt.CurrentUser().Prefs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ui.ErrorPage(ctxt, err)
|
return ui.ErrorPage(ctxt, err)
|
||||||
@@ -176,10 +122,6 @@ func Topics(ctxt ui.AmContext) (string, any, error) {
|
|||||||
* Standard Go error status.
|
* Standard Go error status.
|
||||||
*/
|
*/
|
||||||
func NewTopicForm(ctxt ui.AmContext) (string, any, error) {
|
func NewTopicForm(ctxt ui.AmContext) (string, any, error) {
|
||||||
cmd, arg, err := singleConferencePrequel(ctxt)
|
|
||||||
if cmd != "" {
|
|
||||||
return cmd, arg, err
|
|
||||||
}
|
|
||||||
comm := ctxt.CurrentCommunity()
|
comm := ctxt.CurrentCommunity()
|
||||||
conf := ctxt.GetScratch("currentConference").(*database.Conference)
|
conf := ctxt.GetScratch("currentConference").(*database.Conference)
|
||||||
myLevel := ctxt.GetScratch("levelInConference").(uint16)
|
myLevel := ctxt.GetScratch("levelInConference").(uint16)
|
||||||
@@ -217,10 +159,6 @@ func NewTopicForm(ctxt ui.AmContext) (string, any, error) {
|
|||||||
* Standard Go error status.
|
* Standard Go error status.
|
||||||
*/
|
*/
|
||||||
func NewTopic(ctxt ui.AmContext) (string, any, error) {
|
func NewTopic(ctxt ui.AmContext) (string, any, error) {
|
||||||
cmd, arg, err := singleConferencePrequel(ctxt)
|
|
||||||
if cmd != "" {
|
|
||||||
return cmd, arg, err
|
|
||||||
}
|
|
||||||
comm := ctxt.CurrentCommunity()
|
comm := ctxt.CurrentCommunity()
|
||||||
conf := ctxt.GetScratch("currentConference").(*database.Conference)
|
conf := ctxt.GetScratch("currentConference").(*database.Conference)
|
||||||
myLevel := ctxt.GetScratch("levelInConference").(uint16)
|
myLevel := ctxt.GetScratch("levelInConference").(uint16)
|
||||||
|
|||||||
@@ -42,11 +42,8 @@ func setupEcho() *echo.Echo {
|
|||||||
} else {
|
} else {
|
||||||
log.Warn("WARNING: --debug-panic in effect - DO NOT use this in production!")
|
log.Warn("WARNING: --debug-panic in effect - DO NOT use this in production!")
|
||||||
}
|
}
|
||||||
e.Use(LogrusMiddleware)
|
e.Use(LogrusMiddleware, session.Middleware(ui.SessionStore))
|
||||||
e.Use(session.Middleware(ui.SessionStore))
|
e.Use(ui.ContextCreator, ui.IPBanTest, ui.CookieLoginTest)
|
||||||
e.Use(ui.ContextCreator)
|
|
||||||
e.Use(ui.IPBanTest)
|
|
||||||
e.Use(ui.CookieLoginTest)
|
|
||||||
|
|
||||||
fn := ui.AmWrap(NotImplPage)
|
fn := ui.AmWrap(NotImplPage)
|
||||||
e.GET("/TODO/*", fn)
|
e.GET("/TODO/*", fn)
|
||||||
@@ -79,6 +76,7 @@ func setupEcho() *echo.Echo {
|
|||||||
e.POST("/create_comm", ui.AmWrap(CreateCommunity))
|
e.POST("/create_comm", ui.AmWrap(CreateCommunity))
|
||||||
e.POST("/attachment_upload", ui.AmWrap(AttachmentUpload))
|
e.POST("/attachment_upload", ui.AmWrap(AttachmentUpload))
|
||||||
|
|
||||||
|
// community group
|
||||||
commGroup := e.Group("/comm/:cid", ui.SetCommunity)
|
commGroup := e.Group("/comm/:cid", ui.SetCommunity)
|
||||||
commGroup.GET("/profile", ui.AmWrap(ShowCommunity))
|
commGroup.GET("/profile", ui.AmWrap(ShowCommunity))
|
||||||
commGroup.GET("/join", ui.AmWrap(JoinCommunity))
|
commGroup.GET("/join", ui.AmWrap(JoinCommunity))
|
||||||
@@ -93,9 +91,10 @@ func setupEcho() *echo.Echo {
|
|||||||
commGroup.GET("/admin/logo", ui.AmWrap(CommunityLogoForm))
|
commGroup.GET("/admin/logo", ui.AmWrap(CommunityLogoForm))
|
||||||
commGroup.POST("/admin/logo", ui.AmWrap(EditCommunityLogo))
|
commGroup.POST("/admin/logo", ui.AmWrap(EditCommunityLogo))
|
||||||
|
|
||||||
commGroup.GET("/conf", ui.AmWrap(Conferences))
|
// conference group
|
||||||
confGroup := commGroup.Group("/conf/:confid")
|
commGroup.GET("/conf", ui.AmWrap(Conferences), ui.ValidateConference)
|
||||||
confGroup.GET("", ui.AmWrap(Topics))
|
confGroup := commGroup.Group("/conf/:confid", ui.ValidateConference, ui.SetConference)
|
||||||
|
confGroup.GET("/", ui.AmWrap(Topics))
|
||||||
confGroup.GET("/new_topic", ui.AmWrap(NewTopicForm))
|
confGroup.GET("/new_topic", ui.AmWrap(NewTopicForm))
|
||||||
confGroup.POST("/new_topic", ui.AmWrap(NewTopic))
|
confGroup.POST("/new_topic", ui.AmWrap(NewTopic))
|
||||||
|
|
||||||
|
|||||||
+54
-2
@@ -11,6 +11,7 @@
|
|||||||
package ui
|
package ui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
@@ -20,6 +21,12 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// middlewareErrorPage is a shortcut to displaying an ErrorPage from middleware.
|
||||||
|
func middlewareErrorPage(c echo.Context, ctxt AmContext, err error) error {
|
||||||
|
cmd, data, _ := ErrorPage(ctxt, err)
|
||||||
|
return AmSendPageData(c, ctxt, cmd, data)
|
||||||
|
}
|
||||||
|
|
||||||
// IPBanTest is middleware that handles the IP banning.
|
// IPBanTest is middleware that handles the IP banning.
|
||||||
func IPBanTest(next echo.HandlerFunc) echo.HandlerFunc {
|
func IPBanTest(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
@@ -80,10 +87,55 @@ func SetCommunity(next echo.HandlerFunc) echo.HandlerFunc {
|
|||||||
err := ctxt.SetCommunityContext(ctxt.URLParam("cid"))
|
err := ctxt.SetCommunityContext(ctxt.URLParam("cid"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctxt.SetRC(http.StatusNotFound)
|
ctxt.SetRC(http.StatusNotFound)
|
||||||
cmd, data, _ := ErrorPage(ctxt, err)
|
return middlewareErrorPage(c, ctxt, err)
|
||||||
return AmSendPageData(c, ctxt, cmd, data)
|
|
||||||
}
|
}
|
||||||
ctxt.SetLeftMenu("community")
|
ctxt.SetLeftMenu("community")
|
||||||
return next(c)
|
return next(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ValidateConference is middleware that validates the user has access to the community's conference facility.
|
||||||
|
func ValidateConference(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
|
return func(c echo.Context) error {
|
||||||
|
ctxt := AmContextFromEchoContext(c)
|
||||||
|
comm := ctxt.CurrentCommunity() // set by middleware
|
||||||
|
b, err := database.AmTestService(comm, "Conference")
|
||||||
|
if err != nil {
|
||||||
|
return middlewareErrorPage(c, ctxt, err)
|
||||||
|
}
|
||||||
|
if !b {
|
||||||
|
ctxt.SetRC(http.StatusNotFound)
|
||||||
|
return middlewareErrorPage(c, ctxt, errors.New("this community does not use conferencing services"))
|
||||||
|
}
|
||||||
|
if comm.MembersOnly && !ctxt.IsMember() && !ctxt.TestPermission("Community.NoJoinRequired") {
|
||||||
|
ctxt.SetRC(http.StatusForbidden)
|
||||||
|
return middlewareErrorPage(c, ctxt, errors.New("you are not a member of this community"))
|
||||||
|
}
|
||||||
|
if !comm.TestPermission("Community.Read", ctxt.EffectiveLevel()) {
|
||||||
|
ctxt.SetRC(http.StatusForbidden)
|
||||||
|
return middlewareErrorPage(c, ctxt, errors.New("you are not authorized access to conferences"))
|
||||||
|
}
|
||||||
|
return next(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetConference(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
|
return func(c echo.Context) error {
|
||||||
|
ctxt := AmContextFromEchoContext(c)
|
||||||
|
conf, err := database.AmGetConferenceByAliasInCommunity(ctxt.CurrentCommunity().Id, ctxt.URLParam("confid"))
|
||||||
|
if err != nil {
|
||||||
|
return middlewareErrorPage(c, ctxt, err)
|
||||||
|
}
|
||||||
|
m, lvl, err := conf.Membership(ctxt.CurrentUser())
|
||||||
|
if err != nil {
|
||||||
|
return middlewareErrorPage(c, ctxt, err)
|
||||||
|
}
|
||||||
|
myLevel := ctxt.EffectiveLevel()
|
||||||
|
if m && lvl > myLevel {
|
||||||
|
myLevel = lvl
|
||||||
|
}
|
||||||
|
ctxt.SetScratch("currentConference", conf)
|
||||||
|
ctxt.SetScratch("levelInConference", myLevel)
|
||||||
|
return next(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user