From 52a7ca0fadf937d17d9d7dc44d8bc2ba7b8e174f Mon Sep 17 00:00:00 2001 From: Jan Christophersen Date: Sat, 19 Dec 2015 23:00:02 +0100 Subject: =?UTF-8?q?implementierung=20von=20logout-funktionalit=C3=A4t.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit diff --git a/charakterin.go b/charakterin.go index 3573e8b..127e362 100644 --- a/charakterin.go +++ b/charakterin.go @@ -2,6 +2,7 @@ package charakterin import ( "database/sql" + "errors" "io/ioutil" "log" "net/http" @@ -116,6 +117,43 @@ func (c *Charakterin) Login(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, c.FallbackRoute, 302) } +// Logout loggt einen Charakter aus. Wird direkt über den Request gehandlet. +func (c *Charakterin) Logout(w http.ResponseWriter, r *http.Request) { + cookie, err := r.Cookie("session") + if err != nil { + http.Redirect(w, r, c.FallbackRoute, 302) + return + } + + stmt, err := c.Database.Prepare("DELETE FROM login.sessions WHERE id=$1") + if err != nil { + http.Redirect(w, r, c.FallbackRoute, 302) + return + } + + result, err := stmt.Exec(cookie.Value) + if err != nil { + log.Println(err) + http.Redirect(w, r, c.FallbackRoute, 302) + return + } + + if val, err := result.RowsAffected(); err != nil || val == 0 { + log.Println("could not remove session",cookie.Value,err) + http.Redirect(w, r, c.FallbackRoute, 302) + return + } + + http.SetCookie(w, &http.Cookie{ + Name: "session", + Value: "benis", + Expires: time.Now(), + MaxAge: 0, + }) + + http.Redirect(w, r, c.FallbackRoute, 302) +} + // IsLoggedIn überprüft anhand eines Request, ob der User eingeloggt ist. func (c *Charakterin) IsLoggedIn(r *http.Request) bool { cookie, err := r.Cookie("session") @@ -123,14 +161,45 @@ func (c *Charakterin) IsLoggedIn(r *http.Request) bool { return false } - rows, err := c.Database.Query(`SELECT * FROM login.sessions WHERE id = $1`, cookie.Value) + var result string + err = c.Database.QueryRow(`SELECT login.get_user_by_session($1)`, cookie.Value).Scan(&result) if err != nil { log.Println(err) return false } - if rows.Next() { - return true + return true +} + +func (c *Charakterin) GetUserFromRequest(r *http.Request) (*User, error) { + cookie, err := r.Cookie("session") + if err != nil { + return nil, errors.New("not logged in") } - return false + + var email, name string + var displayName *string + var password []byte + var id int + var lastActivity time.Time + err = c.Database.QueryRow(`SELECT * FROM login.get_user_by_session($1)`, cookie.Value).Scan(&id, &email, &name, &password, &displayName, &lastActivity) + if err != nil { + return nil, err + } + + dspName := "" + if displayName != nil { + dspName = *displayName + } + + user := &User{ + id, + name, + email, + password, + dspName, + lastActivity, + } + + return user, nil } diff --git a/user.go b/user.go new file mode 100644 index 0000000..4fd536c --- /dev/null +++ b/user.go @@ -0,0 +1,23 @@ +package charakterin + +import ( + "time" +) + +// Ein User ist ein ganz toller Benutzer. +type User struct { + ID int + Name string + EMail string + Password []byte + DisplayName string + LastActivity time.Time +} + +// GetName gibt den Anzeigenamen oder wenn dieser nicht gesetzt ist den Benutzernamen zurück. +func (u *User) GetName() string { + if len(u.DisplayName) > 0 { + return u.DisplayName + } + return u.Name +} -- cgit v0.10.1