2017-12-16 05:38:28 +01:00
|
|
|
package mvc
|
2017-12-04 04:08:05 +01:00
|
|
|
|
|
|
|
import (
|
2017-12-10 06:00:51 +01:00
|
|
|
"github.com/kataras/iris/context"
|
|
|
|
"github.com/kataras/iris/sessions"
|
2017-12-04 04:08:05 +01:00
|
|
|
)
|
|
|
|
|
2017-12-14 04:56:23 +01:00
|
|
|
var defaultSessionManager = sessions.New(sessions.Config{})
|
2017-12-04 04:08:05 +01:00
|
|
|
|
|
|
|
// SessionController is a simple `Controller` implementation
|
|
|
|
// which requires a binded session manager in order to give
|
|
|
|
// direct access to the current client's session via its `Session` field.
|
2017-12-20 07:33:53 +01:00
|
|
|
//
|
2017-12-25 19:57:04 +01:00
|
|
|
// SessionController is deprecated please use the new dependency injection's methods instead,
|
2017-12-27 03:15:41 +01:00
|
|
|
// i.e `mvcApp.Register(sessions.New(sessions.Config{}).Start)`.
|
2017-12-25 19:57:04 +01:00
|
|
|
// It's more controlled by you,
|
2017-12-20 07:33:53 +01:00
|
|
|
// also *sessions.Session type can now `Destroy` itself without the need of the manager, embrace it.
|
2017-12-04 04:08:05 +01:00
|
|
|
type SessionController struct {
|
|
|
|
Manager *sessions.Sessions
|
|
|
|
Session *sessions.Session
|
|
|
|
}
|
|
|
|
|
2017-12-17 23:16:10 +01:00
|
|
|
// BeforeActivation called, once per application lifecycle NOT request,
|
2017-12-04 04:08:05 +01:00
|
|
|
// every single time the dev registers a specific SessionController-based controller.
|
|
|
|
// It makes sure that its "Manager" field is filled
|
2017-12-27 03:15:41 +01:00
|
|
|
// even if the caller didn't provide any sessions manager via the MVC's Application's `Handle` function.
|
2017-12-17 23:16:10 +01:00
|
|
|
func (s *SessionController) BeforeActivation(b BeforeActivation) {
|
|
|
|
if didntBindManually := b.Dependencies().AddOnce(defaultSessionManager); didntBindManually {
|
|
|
|
b.Router().GetReporter().Add(
|
2017-12-13 05:17:28 +01:00
|
|
|
`MVC SessionController: couldn't find any "*sessions.Sessions" bindable value to fill the "Manager" field,
|
|
|
|
therefore this controller is using the default sessions manager instead.
|
|
|
|
Please refer to the documentation to learn how you can provide the session manager`)
|
2017-12-04 04:08:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 05:38:28 +01:00
|
|
|
// BeginRequest initializes the current user's Session.
|
2017-12-04 04:08:05 +01:00
|
|
|
func (s *SessionController) BeginRequest(ctx context.Context) {
|
|
|
|
if s.Manager == nil {
|
|
|
|
ctx.Application().Logger().Errorf(`MVC SessionController: sessions manager is nil, report this as a bug
|
|
|
|
because the SessionController should predict this on its activation state and use a default one automatically`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Session = s.Manager.Start(ctx)
|
|
|
|
}
|
2017-12-16 05:38:28 +01:00
|
|
|
|
|
|
|
// EndRequest is here to complete the `BaseController`.
|
|
|
|
func (s *SessionController) EndRequest(ctx context.Context) {}
|