2018-04-30 01:35:04 +02:00
|
|
|
// Copyright 2018 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
package session // import "miniflux.app/ui/session"
|
2018-04-30 01:35:04 +02:00
|
|
|
|
|
|
|
import (
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/crypto"
|
|
|
|
"miniflux.app/storage"
|
2018-04-30 01:35:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Session handles session data.
|
|
|
|
type Session struct {
|
2018-09-03 23:26:40 +02:00
|
|
|
store *storage.Storage
|
|
|
|
sessionID string
|
2018-04-30 01:35:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOAuth2State generates a new OAuth2 state and stores the value into the database.
|
|
|
|
func (s *Session) NewOAuth2State() string {
|
|
|
|
state := crypto.GenerateRandomString(32)
|
2019-01-22 05:21:05 +01:00
|
|
|
s.store.UpdateAppSessionField(s.sessionID, "oauth2_state", state)
|
2018-04-30 01:35:04 +02:00
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFlashMessage creates a new flash message.
|
|
|
|
func (s *Session) NewFlashMessage(message string) {
|
2019-01-22 05:21:05 +01:00
|
|
|
s.store.UpdateAppSessionField(s.sessionID, "flash_message", message)
|
2018-04-30 01:35:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FlashMessage returns the current flash message if any.
|
2018-09-03 23:26:40 +02:00
|
|
|
func (s *Session) FlashMessage(message string) string {
|
2018-04-30 01:35:04 +02:00
|
|
|
if message != "" {
|
2019-01-22 05:21:05 +01:00
|
|
|
s.store.UpdateAppSessionField(s.sessionID, "flash_message", "")
|
2018-04-30 01:35:04 +02:00
|
|
|
}
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFlashErrorMessage creates a new flash error message.
|
|
|
|
func (s *Session) NewFlashErrorMessage(message string) {
|
2019-01-22 05:21:05 +01:00
|
|
|
s.store.UpdateAppSessionField(s.sessionID, "flash_error_message", message)
|
2018-04-30 01:35:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FlashErrorMessage returns the last flash error message if any.
|
2018-09-03 23:26:40 +02:00
|
|
|
func (s *Session) FlashErrorMessage(message string) string {
|
2018-04-30 01:35:04 +02:00
|
|
|
if message != "" {
|
2019-01-22 05:21:05 +01:00
|
|
|
s.store.UpdateAppSessionField(s.sessionID, "flash_error_message", "")
|
2018-04-30 01:35:04 +02:00
|
|
|
}
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
2018-07-19 07:30:05 +02:00
|
|
|
// SetLanguage updates the language field in session.
|
2018-04-30 01:35:04 +02:00
|
|
|
func (s *Session) SetLanguage(language string) {
|
2019-01-22 05:21:05 +01:00
|
|
|
s.store.UpdateAppSessionField(s.sessionID, "language", language)
|
2018-04-30 01:35:04 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 07:30:05 +02:00
|
|
|
// SetTheme updates the theme field in session.
|
|
|
|
func (s *Session) SetTheme(theme string) {
|
2019-01-22 05:21:05 +01:00
|
|
|
s.store.UpdateAppSessionField(s.sessionID, "theme", theme)
|
2018-07-19 07:30:05 +02:00
|
|
|
}
|
|
|
|
|
2018-05-21 00:29:14 +02:00
|
|
|
// SetPocketRequestToken updates Pocket Request Token.
|
|
|
|
func (s *Session) SetPocketRequestToken(requestToken string) {
|
2019-01-22 05:21:05 +01:00
|
|
|
s.store.UpdateAppSessionField(s.sessionID, "pocket_request_token", requestToken)
|
2018-05-21 00:29:14 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 01:35:04 +02:00
|
|
|
// New returns a new session handler.
|
2018-09-03 23:26:40 +02:00
|
|
|
func New(store *storage.Storage, sessionID string) *Session {
|
|
|
|
return &Session{store, sessionID}
|
2018-04-30 01:35:04 +02:00
|
|
|
}
|