Skip to content

Commit 0720c96

Browse files
committed
Enhance session management: add theme and timezone support, update user message handling, and refactor session context functions
1 parent 1946680 commit 0720c96

File tree

6 files changed

+77
-49
lines changed

6 files changed

+77
-49
lines changed

app/dao/sessionStore/sessionStore_model.go

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,39 @@ import (
88
"time"
99

1010
"github.com/mt1976/frantic-core/dao/audit"
11+
"github.com/mt1976/frantic-core/messageHelpers"
1112
)
1213

1314
// Session_Store represents a Session_Store entity.
1415
type Session_Store struct {
15-
ID int `storm:"id,increment=100000"` // primary key with auto increment
16-
Key string `storm:"unique"` // key, not used
17-
Raw string `storm:"index"` // raw ID before encoding
18-
Audit audit.Audit `csv:"-"` // audit data
19-
SessionID string `storm:"index"` // session key
20-
UserKey string `storm:"index"` // user key
21-
UserCode string `storm:"index"` // user code
22-
Expiry time.Time // expiry time
23-
Locale string // locale
16+
ID int `storm:"id,increment=100000"` // primary key with auto increment
17+
Key string `storm:"unique"` // key, not used
18+
Raw string `storm:"index"` // raw ID before encoding
19+
Audit audit.Audit `csv:"-"` // audit data
20+
SessionID string `storm:"index"` // session key
21+
UserKey string `storm:"index"` // user key
22+
UserCode string `storm:"index"` // user code
23+
Expiry time.Time // expiry time
24+
Locale string // locale
25+
Theme string // theme
26+
Timezone string // timezone
27+
UserMessage messageHelpers.UserMessage `csv:"-"` // user message
2428
}
2529

2630
// Define the field set as names
2731
var (
28-
FIELD_ID = "ID"
29-
FIELD_Key = "Key"
30-
FIELD_SessionID = "SessionID"
31-
FIELD_Raw = "Raw"
32-
FIELD_UserID = "UserID"
33-
FIELD_UserCode = "UserCode"
34-
FIELD_Expiry = "Expiry"
35-
FIELD_Audit = "Audit"
36-
FIELD_Locale = "Locale"
32+
FIELD_ID = "ID"
33+
FIELD_Key = "Key"
34+
FIELD_SessionID = "SessionID"
35+
FIELD_Raw = "Raw"
36+
FIELD_UserID = "UserID"
37+
FIELD_UserCode = "UserCode"
38+
FIELD_Expiry = "Expiry"
39+
FIELD_Audit = "Audit"
40+
FIELD_Locale = "Locale"
41+
FIELD_Theme = "Theme"
42+
FIELD_Timezone = "Timezone"
43+
FIELD_UserMessage = "UserMessage"
3744
)
3845

3946
var domain = "Session"

app/web/security/security_getset.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func Current_UserKey(ctx context.Context) string {
1717
return contextHandler.GetSession_UserKey(ctx)
1818
}
1919

20-
func Current_Locale(ctx context.Context) string {
20+
func Current_UserLocale(ctx context.Context) string {
2121
return contextHandler.GetSession_Locale(ctx)
2222
}
2323

@@ -27,27 +27,47 @@ func Current_SessionID(ctx context.Context) string {
2727

2828
func Current_SessionToken(ctx context.Context) sessionStore.Session_Store {
2929
return contextHandler.GetSession_Token(ctx).(sessionStore.Session_Store)
30-
// return ctx.Value(cfg.GetSecuritySessionKey_Token()).(sessionStore.Session_Store)
3130
}
3231

3332
func Current_SessionExpiry(ctx context.Context) time.Time {
3433
return contextHandler.GetSession_Expiry(ctx)
3534
}
3635

36+
func Current_SessionTheme(ctx context.Context) string {
37+
return contextHandler.GetSession_Theme(ctx)
38+
}
39+
40+
func Current_SessionTimezone(ctx context.Context) string {
41+
return contextHandler.GetSession_Timezone(ctx)
42+
}
43+
3744
func setSessionContextValues(ctx context.Context, user messageHelpers.UserMessage, sessionID string, session sessionStore.Session_Store) context.Context {
3845
ctx = contextHandler.SetSession_ID(ctx, sessionID)
3946
ctx = contextHandler.SetSession_Token(ctx, session)
4047
ctx = contextHandler.SetSession_UserKey(ctx, user.Key)
4148
ctx = contextHandler.SetSession_UserCode(ctx, user.Code)
4249
ctx = contextHandler.SetSession_Expiry(ctx, session.Expiry)
43-
ctx = contextHandler.SetSession_Locale(ctx, user.Locale)
44-
ctx = contextHandler.SetSession_Theme(ctx, "")
45-
ctx = contextHandler.SetSession_Timezone(ctx, "")
50+
51+
ctx = contextHandler.SetSession_Locale(ctx, session.Locale)
52+
if session.Locale == "" {
53+
ctx = contextHandler.SetSession_Locale(ctx, user.Locale)
54+
if user.Locale == "" {
55+
ctx = contextHandler.SetSession_Locale(ctx, cfg.GetApplication_Locale())
56+
}
57+
}
58+
ctx = contextHandler.SetSession_Theme(ctx, session.Theme)
59+
if session.Theme == "" {
60+
ctx = contextHandler.SetSession_Theme(ctx, user.Theme)
61+
if user.Theme == "" {
62+
ctx = contextHandler.SetSession_Theme(ctx, cfg.GetApplication_Theme())
63+
}
64+
}
65+
ctx = contextHandler.SetSession_Timezone(ctx, session.Timezone)
66+
if session.Timezone == "" {
67+
ctx = contextHandler.SetSession_Timezone(ctx, user.Timezone)
68+
if user.Timezone == "" {
69+
ctx = contextHandler.SetSession_Timezone(ctx, cfg.GetApplication_Timezone())
70+
}
71+
}
4672
return ctx
47-
// ctx = context.WithValue(ctx, cfg.GetSecuritySessionKey_Session(), sessionID)
48-
// ctx = context.WithValue(ctx, cfg.GetSecuritySessionKey_Token(), session)
49-
// ctx = context.WithValue(ctx, cfg.GetSecuritySessionKey_UserKey(), user.Key)
50-
// ctx = context.WithValue(ctx, cfg.GetSecuritySessionKey_UserCode(), user.Code)
51-
// ctx = context.WithValue(ctx, cfg.GetSecuritySessionKey_ExpiryPeriod(), session.Expiry)
52-
// return ctx
5373
}

app/web/security/security_routes.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ func Validate(h httprouter.Handle) httprouter.Handle {
161161
query := r.URL.Query()
162162
query.Add(sessionKey, sessionID)
163163
r.URL.RawQuery = query.Encode()
164-
logHandler.SecurityLogger.Printf("url adding [%v=%v]\n", sessionKey, sessionID)
164+
// logHandler.SecurityLogger.Printf("url adding [%v=%v]\n", sessionKey, sessionID)
165165
r.URL.RawQuery = r.URL.Query().Encode()
166-
logHandler.SecurityLogger.Printf("ps=%+v", ps)
167-
logHandler.SecurityLogger.Printf("r=%+v", r.URL.Query().Encode())
166+
// logHandler.SecurityLogger.Printf("ps=%+v", ps)
167+
// logHandler.SecurityLogger.Printf("r=%+v", r.URL.Query().Encode())
168168
h(w, r, ps)
169169
} else {
170170
// Error Response

app/web/security/security_sessions.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,17 @@ func GetSessionContext(w http.ResponseWriter, r *http.Request, ps httprouter.Par
7979

8080
logHandler.SecurityLogger.Printf("[%v] GetSessionContext: Session=[%v]", strings.ToUpper(domain), sessionID)
8181

82-
sessionToken, err := sessionStore.GetBy(sessionStore.FIELD_SessionID, sessionID)
82+
userSessionTokenRecord, err := sessionStore.GetBy(sessionStore.FIELD_SessionID, sessionID)
8383
if err != nil {
8484
logHandler.ErrorLogger.Printf("Error=[%v]", err.Error())
8585
msg, _ := trnsl8.Get("Session Not Found")
8686
Violation(w, r, msg.String())
8787
return ctx
8888
}
8989

90-
logHandler.SecurityLogger.Printf("[%v] GetSessionContext: UserKey=[%v] (%v)", strings.ToUpper(domain), sessionToken.UserKey, sessionToken.UserCode)
90+
logHandler.SecurityLogger.Printf("[%v] GetSessionContext: UserKey=[%v] (%v)", strings.ToUpper(domain), userSessionTokenRecord.UserKey, userSessionTokenRecord.UserCode)
9191
clock := timing.Start(domain, "userValidator", "")
92-
UserMessage, err := userValidator(sessionToken.UserKey)
92+
UserMessage, err := userValidator(userSessionTokenRecord.UserKey)
9393
clock.Stop(1)
9494
if err == commonErrors.ErrorUserNotFound {
9595
logHandler.ErrorLogger.Printf("Error=[%v]", err.Error())
@@ -110,18 +110,19 @@ func GetSessionContext(w http.ResponseWriter, r *http.Request, ps httprouter.Par
110110
return ctx
111111
}
112112

113-
ctx = setSessionContextValues(ctx, UserMessage, sessionID, sessionToken)
113+
ctx = setSessionContextValues(ctx, UserMessage, sessionID, userSessionTokenRecord)
114114

115-
if appModeDev {
116-
logHandler.SecurityLogger.Printf("[%v] EstablishSessionContext: [%v]=[%v]", strings.ToUpper(domain), sessionUserCodeKey, UserMessage.Code)
117-
logHandler.SecurityLogger.Printf("[%v] EstablishSessionContext: [%v]=[%v]", strings.ToUpper(domain), sessionUserKeyKey, UserMessage.Key)
118-
logHandler.SecurityLogger.Printf("[%v] EstablishSessionContext: [%v]=[%v]", strings.ToUpper(domain), sessionKey, sessionID)
119-
logHandler.SecurityLogger.Printf("[%v] EstablishSessionContext: [%v]=[%v]", strings.ToUpper(domain), sessionExpiryKey, sessionToken.Expiry)
120-
logHandler.SecurityLogger.Printf("[%v] EstablishSessionContext: [%v]=[%+v]", strings.ToUpper(domain), sessionTokenKey, sessionToken)
121-
logHandler.SecurityLogger.Printf("[%v] EstablishSessionContext: [%v]=[%+v]", strings.ToUpper(domain), sessionLocaleKey, UserMessage.Locale)
122-
logHandler.SecurityLogger.Printf("[%v] EstablishSessionContext: [%v]=[%+v]", strings.ToUpper(domain), sessionThemeKey, UserMessage.Spare1)
123-
logHandler.SecurityLogger.Printf("[%v] EstablishSessionContext: [%v]=[%+v]", strings.ToUpper(domain), sessionTimezoneKey, UserMessage.Spare2)
124-
}
115+
// if appModeDev {
116+
logHandler.SecurityLogger.Printf("[%v] SessionContext: [%v]=[%v]", strings.ToUpper(domain), sessionUserCodeKey, Current_UserCode(ctx))
117+
logHandler.SecurityLogger.Printf("[%v] SessionContext: [%v]=[%v]", strings.ToUpper(domain), sessionUserKeyKey, Current_UserKey(ctx))
118+
logHandler.SecurityLogger.Printf("[%v] SessionContext: [%v]=[%v]", strings.ToUpper(domain), sessionKey, sessionID)
119+
logHandler.SecurityLogger.Printf("[%v] SessionContext: [%v]=[%v]", strings.ToUpper(domain), sessionExpiryKey, Current_SessionExpiry(ctx))
120+
logHandler.SecurityLogger.Printf("[%v] SessionContext: [%v]=[%v]", strings.ToUpper(domain), sessionLocaleKey, Current_UserLocale(ctx))
121+
logHandler.SecurityLogger.Printf("[%v] SessionContext: [%v]=[%v]", strings.ToUpper(domain), sessionThemeKey, Current_SessionTheme(ctx))
122+
logHandler.SecurityLogger.Printf("[%v] SessionContext: [%v]=[%v]", strings.ToUpper(domain), sessionTimezoneKey, Current_SessionTimezone(ctx))
123+
logHandler.SecurityLogger.Printf("[%v] SessionContext: [%v]=[%+v]", strings.ToUpper(domain), sessionTokenKey, userSessionTokenRecord)
124+
125+
// }
125126

126127
return ctx
127128
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.24.0
55
require (
66
github.com/asdine/storm/v3 v3.2.1
77
github.com/julienschmidt/httprouter v1.3.0
8-
github.com/mt1976/frantic-core v1.2.66
8+
github.com/mt1976/frantic-core v1.2.67
99
)
1010

1111
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
4545
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
4646
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
4747
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
48-
github.com/mt1976/frantic-core v1.2.66 h1:D0IFm0WNa2flDP2SBqRXo2btmq3gByU8C+BdLz2wg1U=
49-
github.com/mt1976/frantic-core v1.2.66/go.mod h1:K7VwBS3qq6pfZojZ4fROC9yk7bFsYU+qjUMOjQ2HWwY=
48+
github.com/mt1976/frantic-core v1.2.67 h1:D0EuEQUXaJCzKO+rU4kf0Q4Des3JrOv+Dn6Y6gV13Wk=
49+
github.com/mt1976/frantic-core v1.2.67/go.mod h1:K7VwBS3qq6pfZojZ4fROC9yk7bFsYU+qjUMOjQ2HWwY=
5050
github.com/mt1976/trnsl8r_connect v1.3.2 h1:KqsipoEbi2hjOnDIyGhdMpjJPAOfyXPGnA92RAIWvxA=
5151
github.com/mt1976/trnsl8r_connect v1.3.2/go.mod h1:3eBEn9m5SZWnOzP9LXoWQQ0TgwqedzJLiVYQdlrTYc0=
5252
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

0 commit comments

Comments
 (0)