diff options
author | jan <jan@ruken.pw> | 2015-12-29 18:57:27 (UTC) |
---|---|---|
committer | jan <jan@ruken.pw> | 2015-12-29 18:57:27 (UTC) |
commit | de1276a29e9bc6cf427a326d1bf41be22b195e7d (patch) | |
tree | 40e2baea60250b84e4c07089c12f44494d28d5be | |
parent | bd88166988bb31ff82da847d1e69c907f5f53930 (diff) |
GetUserByID implementiert.
-rw-r--r-- | charakterin.go | 27 | ||||
-rw-r--r-- | user.go | 9 |
2 files changed, 21 insertions, 15 deletions
diff --git a/charakterin.go b/charakterin.go index 142484c..253e627 100644 --- a/charakterin.go +++ b/charakterin.go | |||
@@ -13,9 +13,9 @@ import ( | |||
13 | ) | 13 | ) |
14 | 14 | ||
15 | const ( | 15 | const ( |
16 | NoSuchUser = "pq: no_such_user" | 16 | NoSuchUser = "pq: no_such_user" |
17 | InvalidPassword = "pq: invalid_password" | 17 | InvalidPassword = "pq: invalid_password" |
18 | UsernameTaken = "pq: username_taken" | 18 | UsernameTaken = "pq: username_taken" |
19 | EmailAlreadyRegistered = "pq: email_already_registered" | 19 | EmailAlreadyRegistered = "pq: email_already_registered" |
20 | ) | 20 | ) |
21 | 21 | ||
@@ -278,27 +278,22 @@ func (c *Charakterin) GetUserFromRequest(r *http.Request) (*User, error) { | |||
278 | } | 278 | } |
279 | 279 | ||
280 | var email, name string | 280 | var email, name string |
281 | var displayName *string | 281 | var displayName sql.NullString |
282 | var password []byte | 282 | var password []byte |
283 | var id int | 283 | var id int |
284 | var lastActivity time.Time | 284 | var lastActivity *time.Time |
285 | var isActive bool | 285 | var isActive bool |
286 | err = c.Database.QueryRow(`SELECT id, email, name, password, display_name, last_activity, is_active FROM login.get_user_by_session($1)`, cookie.Value).Scan(&id, &email, &name, &password, &displayName, &lastActivity, &isActive) | 286 | err = c.Database.QueryRow(`SELECT id, email, name, password, display_name, last_activity, is_active FROM login.get_user_by_session($1)`, cookie.Value).Scan(&id, &email, &name, &password, &displayName, &lastActivity, &isActive) |
287 | if err != nil { | 287 | if err != nil { |
288 | return nil, err | 288 | return nil, err |
289 | } | 289 | } |
290 | 290 | ||
291 | dspName := "" | ||
292 | if displayName != nil { | ||
293 | dspName = *displayName | ||
294 | } | ||
295 | |||
296 | user := &User{ | 291 | user := &User{ |
297 | id, | 292 | id, |
298 | name, | 293 | name, |
299 | email, | 294 | email, |
300 | password, | 295 | password, |
301 | dspName, | 296 | displayName, |
302 | lastActivity, | 297 | lastActivity, |
303 | isActive, | 298 | isActive, |
304 | } | 299 | } |
@@ -306,6 +301,16 @@ func (c *Charakterin) GetUserFromRequest(r *http.Request) (*User, error) { | |||
306 | return user, nil | 301 | return user, nil |
307 | } | 302 | } |
308 | 303 | ||
304 | // GetUserByID gibt den User mit der gegebenen ID zurück | ||
305 | func (c *Charakterin) GetUserByID(id int) (*User, error) { | ||
306 | user := &User{} | ||
307 | err := c.Database.QueryRow(`SELECT id, email, name, password, display_name, last_activity, is_active FROM login.users WHERE id = $1`, id).Scan(&user.ID, &user.EMail, &user.Name, &user.Password, &user.DisplayName, &user.LastActivity, &user.IsActive) | ||
308 | if err != nil { | ||
309 | return nil, err | ||
310 | } | ||
311 | return user, nil | ||
312 | } | ||
313 | |||
309 | // ConfirmEmail konfirmiert die email addresse mit der gegebenen confirm id. | 314 | // ConfirmEmail konfirmiert die email addresse mit der gegebenen confirm id. |
310 | func (c *Charakterin) ConfirmEmail(confirmId string) error { | 315 | func (c *Charakterin) ConfirmEmail(confirmId string) error { |
311 | var result string | 316 | var result string |
@@ -1,6 +1,7 @@ | |||
1 | package charakterin | 1 | package charakterin |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "database/sql" | ||
4 | "time" | 5 | "time" |
5 | ) | 6 | ) |
6 | 7 | ||
@@ -10,15 +11,15 @@ type User struct { | |||
10 | Name string | 11 | Name string |
11 | EMail string | 12 | EMail string |
12 | Password []byte | 13 | Password []byte |
13 | DisplayName string | 14 | DisplayName sql.NullString |
14 | LastActivity time.Time | 15 | LastActivity *time.Time |
15 | IsActive bool | 16 | IsActive bool |
16 | } | 17 | } |
17 | 18 | ||
18 | // GetName gibt den Anzeigenamen oder wenn dieser nicht gesetzt ist den Benutzernamen zurück. | 19 | // GetName gibt den Anzeigenamen oder wenn dieser nicht gesetzt ist den Benutzernamen zurück. |
19 | func (u *User) GetName() string { | 20 | func (u *User) GetName() string { |
20 | if len(u.DisplayName) > 0 { | 21 | if u.DisplayName.Valid { |
21 | return u.DisplayName | 22 | return u.DisplayName.String |
22 | } | 23 | } |
23 | return u.Name | 24 | return u.Name |
24 | } | 25 | } |