| 1 | // Copyright (c) 2020-2021 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by a GPL license that can be found in the LICENSE file. |
| 3 | module main |
| 4 | |
| 5 | enum SecurityLogKind { |
| 6 | registered |
| 7 | logged_in |
| 8 | registered_via_github |
| 9 | logged_in_via_github |
| 10 | wrong_password |
| 11 | wrong_oauth_state |
| 12 | empty_oauth_code |
| 13 | empty_oauth_email |
| 14 | } |
| 15 | |
| 16 | struct SecurityLog { |
| 17 | id int @[primary; sql: serial] |
| 18 | user_id int |
| 19 | kind_id int |
| 20 | ip string |
| 21 | arg1 string |
| 22 | arg2 string |
| 23 | created_at int |
| 24 | mut: |
| 25 | kind SecurityLogKind @[skip] |
| 26 | } |
| 27 | |
| 28 | fn (mut app App) add_security_log(log SecurityLog) ! { |
| 29 | new_log := SecurityLog{ |
| 30 | ...log |
| 31 | kind_id: int(log.kind) |
| 32 | // ip: ip // ctx.ip() XTODO |
| 33 | } |
| 34 | |
| 35 | sql app.db { |
| 36 | insert new_log into SecurityLog |
| 37 | }! |
| 38 | } |
| 39 | |
| 40 | fn (app &App) get_all_user_security_logs(user_id int) []SecurityLog { |
| 41 | mut logs := sql app.db { |
| 42 | select from SecurityLog where user_id == user_id order by id desc |
| 43 | } or { []SecurityLog{} } |
| 44 | |
| 45 | for i, log in logs { |
| 46 | logs[i].kind = unsafe { SecurityLogKind(log.kind_id) } |
| 47 | } |
| 48 | |
| 49 | return logs |
| 50 | } |
| 51 | |