Browse Source

Load user data from session and database

Vova Tkach 6 years ago
parent
commit
f10c27806e
2 changed files with 26 additions and 0 deletions
  1. 6 0
      engine/engine.go
  2. 20 0
      engine/wrapper/wrapper.go

+ 6 - 0
engine/engine.go

@@ -91,6 +91,12 @@ func (this *Engine) Process() bool {
 		return true
 	}
 
+	// Try load current user data
+	if !this.Wrap.LoadSessionUser() {
+		http.Redirect(this.Wrap.W, this.Wrap.R, "/", 302)
+		return true
+	}
+
 	// Render backend
 	return this.Mods.XXXBackEnd(this.Wrap)
 }

+ 20 - 0
engine/wrapper/wrapper.go

@@ -88,6 +88,26 @@ func (this *Wrapper) UseDatabase() error {
 	return nil
 }
 
+func (this *Wrapper) LoadSessionUser() bool {
+	if this.S.GetInt("UserId", 0) <= 0 {
+		return false
+	}
+	if this.DB == nil {
+		return false
+	}
+	user := &utils.MySql_user{}
+	err := this.DB.QueryRow("SELECT `id`, `first_name`, `last_name`, `email`, `password` FROM `users` WHERE `id` = ? LIMIT 1;", this.S.GetInt("UserId", 0)).Scan(
+		&user.A_id, &user.A_first_name, &user.A_last_name, &user.A_email, &user.A_password)
+	if err != nil {
+		return false
+	}
+	if user.A_id != this.S.GetInt("UserId", 0) {
+		return false
+	}
+	this.User = user
+	return true
+}
+
 func (this *Wrapper) Write(data string) {
 	this.W.Write([]byte(data))
 }