Skip to content

Allow existing users to sign in while READ_ONLY_MODE=1 #1690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/controllers/user/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};

use crate::models::{NewUser, User};
use crate::schema::users;
use crate::util::errors::{CargoError, ReadOnlyMode};

/// Handles the `GET /authorize_url` route.
///
Expand Down Expand Up @@ -111,16 +113,30 @@ struct GithubUser {
}

impl GithubUser {
fn save_to_database(&self, access_token: &str, conn: &PgConnection) -> QueryResult<User> {
Ok(NewUser::new(
fn save_to_database(&self, access_token: &str, conn: &PgConnection) -> CargoResult<User> {
NewUser::new(
self.id,
&self.login,
self.email.as_ref().map(|s| &s[..]),
self.name.as_ref().map(|s| &s[..]),
self.avatar_url.as_ref().map(|s| &s[..]),
access_token,
)
.create_or_update(conn)?)
.create_or_update(conn)
.map_err(Into::into)
.or_else(|e: Box<dyn CargoError>| {
// If we're in read only mode, we can't update their details
// just look for an existing user
if e.is::<ReadOnlyMode>() {
users::table
.filter(users::gh_id.eq(self.id))
.first(conn)
.optional()?
.ok_or(e)
} else {
Err(e)
}
})
}
}

Expand Down