Skip to content

Commit

Permalink
Save last login timestamp for users and show it on admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
janost committed Nov 30, 2020
1 parent 9824d94 commit 5904a77
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 0 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE users ADD COLUMN last_login DATETIME DEFAULT NULL;
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE users ADD COLUMN last_login TIMESTAMP DEFAULT NULL;
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE users ADD COLUMN last_login DATETIME DEFAULT NULL;
5 changes: 5 additions & 0 deletions src/api/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ fn users_overview(_token: AdminToken, conn: DbConn) -> ApiResult<Html<String>> {
usr["cipher_count"] = json!(Cipher::count_owned_by_user(&u.uuid, &conn));
usr["attachment_count"] = json!(Attachment::count_by_user(&u.uuid, &conn));
usr["attachment_size"] = json!(get_display_size(Attachment::size_by_user(&u.uuid, &conn) as i32));
usr["created_at"] = json!(&u.created_at.format("%Y-%m-%d %H:%M:%S").to_string());
usr["last_login"] = match &u.last_login {
Some(val) => json!(val.format("%Y-%m-%d %H:%M:%S").to_string()),
None => json!("Never")
};
usr
}).collect();

Expand Down
6 changes: 6 additions & 0 deletions src/api/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ fn _password_login(data: ConnectData, conn: DbConn, ip: &ClientIp) -> JsonResult
result["TwoFactorToken"] = Value::String(token);
}

let mut user = user;
user.last_login = Some(now.naive_utc());
if let Err(e) = user.save(&conn) {
error!("Error updating user: {:#?}", e);
}

info!("User {} logged in successfully. IP: {}", username, ip.ip);
Ok(Json(result))
}
Expand Down
2 changes: 2 additions & 0 deletions src/db/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ db_object! {
pub updated_at: NaiveDateTime,
pub verified_at: Option<NaiveDateTime>,
pub last_verifying_at: Option<NaiveDateTime>,
pub last_login: Option<NaiveDateTime>,
pub login_verify_count: i32,

pub email: String,
Expand Down Expand Up @@ -74,6 +75,7 @@ impl User {
updated_at: now,
verified_at: None,
last_verifying_at: None,
last_login: None,
login_verify_count: 0,
name: email.clone(),
email,
Expand Down
1 change: 1 addition & 0 deletions src/db/schemas/mysql/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ table! {
updated_at -> Datetime,
verified_at -> Nullable<Datetime>,
last_verifying_at -> Nullable<Datetime>,
last_login -> Nullable<Datetime>,
login_verify_count -> Integer,
email -> Text,
email_new -> Nullable<Text>,
Expand Down
1 change: 1 addition & 0 deletions src/db/schemas/postgresql/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ table! {
updated_at -> Timestamp,
verified_at -> Nullable<Timestamp>,
last_verifying_at -> Nullable<Timestamp>,
last_login -> Nullable<Timestamp>,
login_verify_count -> Integer,
email -> Text,
email_new -> Nullable<Text>,
Expand Down
1 change: 1 addition & 0 deletions src/db/schemas/sqlite/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ table! {
updated_at -> Timestamp,
verified_at -> Nullable<Timestamp>,
last_verifying_at -> Nullable<Timestamp>,
last_login -> Nullable<Timestamp>,
login_verify_count -> Integer,
email -> Text,
email_new -> Nullable<Text>,
Expand Down
2 changes: 2 additions & 0 deletions src/static/templates/admin/users.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<div class="float-left">
<strong>{{Name}}</strong>
<span class="d-block">{{Email}}</span>
<span class="d-block">Created at: {{created_at}}</span>
<span class="d-block">Last login: {{last_login}}</span>
<span class="d-block">
{{#if TwoFactorEnabled}}
<span class="badge badge-success mr-2" title="2FA is enabled">2FA</span>
Expand Down

0 comments on commit 5904a77

Please sign in to comment.