-
Notifications
You must be signed in to change notification settings - Fork 640
/
Copy pathuser.rs
196 lines (175 loc) · 6.77 KB
/
user.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use chrono::NaiveDateTime;
use diesel::prelude::*;
use secrecy::SecretString;
use crate::app::App;
use crate::controllers::user::me::UserConfirmEmail;
use crate::email::Emails;
use crate::rate_limiter::RateLimiter;
use crate::util::errors::AppResult;
use crate::models::{ApiToken, Crate, CrateOwner, Email, NewEmail, Owner, OwnerKind, Rights};
use crate::schema::{crate_owners, emails, users};
use crate::sql::lower;
/// The model representing a row in the `users` database table.
#[derive(Clone, Debug, PartialEq, Eq, Queryable, Identifiable, AsChangeset)]
pub struct User {
pub id: i32,
pub gh_access_token: String,
pub gh_login: String,
pub name: Option<String>,
pub gh_avatar: Option<String>,
pub gh_id: i32,
pub account_lock_reason: Option<String>,
pub account_lock_until: Option<NaiveDateTime>,
pub is_admin: bool,
}
/// Represents a new user record insertable to the `users` table
#[derive(Insertable, Debug, Default)]
#[diesel(table_name = users, check_for_backend(diesel::pg::Pg))]
pub struct NewUser<'a> {
pub gh_id: i32,
pub gh_login: &'a str,
pub name: Option<&'a str>,
pub gh_avatar: Option<&'a str>,
pub gh_access_token: &'a str,
}
impl<'a> NewUser<'a> {
pub fn new(
gh_id: i32,
gh_login: &'a str,
name: Option<&'a str>,
gh_avatar: Option<&'a str>,
gh_access_token: &'a str,
) -> Self {
NewUser {
gh_id,
gh_login,
name,
gh_avatar,
gh_access_token,
}
}
/// Inserts the user into the database, or updates an existing one.
pub fn create_or_update(
&self,
email: Option<&'a str>,
emails: &Emails,
rate_limiter: &RateLimiter,
conn: &mut PgConnection,
) -> AppResult<User> {
use diesel::dsl::sql;
use diesel::insert_into;
use diesel::pg::upsert::excluded;
use diesel::sql_types::Integer;
conn.transaction(|conn| {
let user: User = insert_into(users::table)
.values(self)
// We need the `WHERE gh_id > 0` condition here because `gh_id` set
// to `-1` indicates that we were unable to find a GitHub ID for
// the associated GitHub login at the time that we backfilled
// GitHub IDs. Therefore, there are multiple records in production
// that have a `gh_id` of `-1` so we need to exclude those when
// considering uniqueness of `gh_id` values. The `> 0` condition isn't
// necessary for most fields in the database to be used as a conflict
// target :)
.on_conflict(sql::<Integer>("(gh_id) WHERE gh_id > 0"))
.do_update()
.set((
users::gh_login.eq(excluded(users::gh_login)),
users::name.eq(excluded(users::name)),
users::gh_avatar.eq(excluded(users::gh_avatar)),
users::gh_access_token.eq(excluded(users::gh_access_token)),
))
.get_result(conn)?;
// To send the user an account verification email
if let Some(user_email) = email {
let new_email = NewEmail {
user_id: user.id,
email: user_email,
};
let token = insert_into(emails::table)
.values(&new_email)
.on_conflict_do_nothing()
.returning(emails::token)
.get_result(conn)
.optional()?
.map(SecretString::new);
if let Some(token) = token {
let email =
UserConfirmEmail::new(rate_limiter, conn, &user, &emails.domain, token)?;
// Swallows any error. Some users might insert an invalid email address here.
let _ = emails.send(user_email, email);
}
}
Ok(user)
})
}
}
impl User {
pub fn find(conn: &mut PgConnection, id: i32) -> QueryResult<User> {
users::table.find(id).first(conn)
}
/// Queries the database for a user with a certain `api_token` value.
pub fn find_by_api_token(conn: &mut PgConnection, token: &str) -> AppResult<User> {
let api_token = ApiToken::find_by_api_token(conn, token)?;
Ok(Self::find(conn, api_token.user_id)?)
}
pub fn find_by_login(conn: &mut PgConnection, login: &str) -> QueryResult<User> {
users::table
.filter(lower(users::gh_login).eq(login.to_lowercase()))
.filter(users::gh_id.ne(-1))
.order(users::gh_id.desc())
.first(conn)
}
pub fn owning(krate: &Crate, conn: &mut PgConnection) -> QueryResult<Vec<Owner>> {
let users = CrateOwner::by_owner_kind(OwnerKind::User)
.inner_join(users::table)
.select(users::all_columns)
.filter(crate_owners::crate_id.eq(krate.id))
.load(conn)?
.into_iter()
.map(Owner::User);
Ok(users.collect())
}
/// Given this set of owners, determines the strongest rights the
/// user has.
///
/// Shortcircuits on `Full` because you can't beat it. In practice we'll always
/// see `[user, user, user, ..., team, team, team]`, so we could shortcircuit on
/// `Publish` as well, but this is a non-obvious invariant so we don't bother.
/// Sweet free optimization if teams are proving burdensome to check.
/// More than one team isn't really expected, though.
pub async fn rights(&self, app: &App, owners: &[Owner]) -> AppResult<Rights> {
let mut best = Rights::None;
for owner in owners {
match *owner {
Owner::User(ref other_user) => {
if other_user.id == self.id {
return Ok(Rights::Full);
}
}
Owner::Team(ref team) => {
if team.contains_user(app, self).await? {
best = Rights::Publish;
}
}
}
}
Ok(best)
}
/// Queries the database for the verified emails
/// belonging to a given user
pub fn verified_email(&self, conn: &mut PgConnection) -> QueryResult<Option<String>> {
Email::belonging_to(self)
.select(emails::email)
.filter(emails::verified.eq(true))
.first(conn)
.optional()
}
/// Queries for the email belonging to a particular user
pub fn email(&self, conn: &mut PgConnection) -> QueryResult<Option<String>> {
Email::belonging_to(self)
.select(emails::email)
.first(conn)
.optional()
}
}