|
1 |
| -use diesel::dsl::now; |
2 | 1 | use diesel::prelude::*;
|
3 | 2 | use std::borrow::Cow;
|
4 | 3 |
|
5 | 4 | use crate::app::App;
|
6 | 5 | use crate::util::CargoResult;
|
7 | 6 |
|
8 |
| -use crate::models::{Crate, CrateOwner, Email, NewEmail, Owner, OwnerKind, Rights}; |
| 7 | +use crate::models::{ApiToken, Crate, CrateOwner, Email, NewEmail, Owner, OwnerKind, Rights}; |
9 | 8 | use crate::schema::{crate_owners, emails, users};
|
10 | 9 | use crate::views::{EncodablePrivateUser, EncodablePublicUser};
|
11 | 10 |
|
@@ -105,27 +104,14 @@ impl<'a> NewUser<'a> {
|
105 | 104 | }
|
106 | 105 |
|
107 | 106 | impl User {
|
108 |
| - /// Queries the database for a user with a certain `api_token` value. |
109 |
| - pub fn find_by_api_token(conn: &PgConnection, token_: &str) -> QueryResult<User> { |
110 |
| - use crate::schema::api_tokens::dsl::{api_tokens, last_used_at, revoked, token, user_id}; |
111 |
| - use diesel::update; |
112 |
| - |
113 |
| - let tokens = api_tokens |
114 |
| - .filter(token.eq(token_)) |
115 |
| - .filter(revoked.eq(false)); |
116 |
| - |
117 |
| - // If the database is in read only mode, we can't update last_used_at. |
118 |
| - // Try updating in a new transaction, if that fails, fall back to reading |
119 |
| - let user_id_ = conn |
120 |
| - .transaction(|| { |
121 |
| - update(tokens) |
122 |
| - .set(last_used_at.eq(now.nullable())) |
123 |
| - .returning(user_id) |
124 |
| - .get_result::<i32>(conn) |
125 |
| - }) |
126 |
| - .or_else(|_| tokens.select(user_id).first(conn))?; |
127 |
| - |
128 |
| - users::table.find(user_id_).first(conn) |
| 107 | + pub fn find(conn: &PgConnection, id: i32) -> QueryResult<User> { |
| 108 | + users::table.find(id).first(conn) |
| 109 | + } |
| 110 | + |
| 111 | + pub fn find_by_api_token(conn: &PgConnection, token: &str) -> QueryResult<User> { |
| 112 | + let api_token = ApiToken::find_by_api_token_and_revoked(conn, token, false)?; |
| 113 | + |
| 114 | + Self::find(conn, api_token.user_id) |
129 | 115 | }
|
130 | 116 |
|
131 | 117 | pub fn owning(krate: &Crate, conn: &PgConnection) -> CargoResult<Vec<Owner>> {
|
|
0 commit comments