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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use crate::api::rcos::users::create::CreateOneUser;
use crate::api::rcos::users::{UserAccountType, UserRole};
use crate::error::TelescopeError;
use crate::templates::page::Page;
use crate::templates::{auth, Template};
use crate::web::services::auth::identity::{AuthenticationCookie, RootIdentity};
use crate::web::services::auth::rpi_cas::RpiCasIdentity;
use actix_web::http::header::LOCATION;
use actix_web::web::Form;
use actix_web::{HttpRequest, HttpResponse, Responder};
use uuid::Uuid;
const TEMPLATE_PATH: &'static str = "user/register";
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RegistrationFormInput {
first_name: String,
last_name: String,
}
impl RegistrationFormInput {
fn is_valid(&self) -> bool {
!self.first_name.is_empty() && !self.last_name.is_empty()
}
}
async fn empty_registration_form(id: &RootIdentity) -> Result<Template, TelescopeError> {
let mut template = Template::new(TEMPLATE_PATH);
match id {
RootIdentity::Discord(d) => {
template.fields = d.get_authenticated_user().await.map(|discord_user| {
json!({
"icon": UserAccountType::Discord,
"info": {
"username": discord_user.tag(),
"avatar_url": discord_user.face(),
}
})
})?;
}
RootIdentity::GitHub(g) => {
template.fields = g
.get_authenticated_user()
.await
.map(|gh_user| {
json!({
"icon": UserAccountType::GitHub,
"info": {
"username": gh_user.login,
"avatar_url": gh_user.avatar_url,
"profile_url": gh_user.url
}
})
})?;
}
RootIdentity::RpiCas(r) => {
template.fields = json!({
"info": {
"username": format!("{}@rpi.edu", r.rcs_id),
}
});
}
}
return Ok(template);
}
async fn form_with_input(
id: &RootIdentity,
input: &RegistrationFormInput,
) -> Result<Template, TelescopeError> {
let mut template = empty_registration_form(id).await?;
let fields = template
.fields
.as_object_mut()
.expect("The previous function should return a JSON object.");
fields.insert(
"first_name".into(),
json!({
"value": input.first_name,
"error": input.first_name
.is_empty()
.then(|| "Your first name cannot be empty.")
.unwrap_or("")
}),
);
fields.insert(
"last_name".into(),
json!({
"value": input.last_name,
"error": input.last_name
.is_empty()
.then(|| "Your last name cannot be empty.")
.unwrap_or("")
}),
);
return Ok(template);
}
#[get("/register")]
pub async fn register_page(req: HttpRequest) -> Result<Page, TelescopeError> {
auth::register().in_page(&req, "Create RCOS Account").await
}
#[get("/register/finish")]
pub async fn finish_registration(
req: HttpRequest,
identity_cookie: AuthenticationCookie,
) -> Result<HttpResponse, actix_web::Error> {
if let Some(user_id) = identity_cookie.get_user_id().await? {
return Ok(HttpResponse::Found()
.header(LOCATION, format!("/user/{}", user_id))
.finish());
} else {
return empty_registration_form(&identity_cookie.root)
.await?
.in_page(&req, "Create Account")
.await
.respond_to(&req)
.await;
}
}
#[post("/register/finish")]
pub async fn submit_registration(
req: HttpRequest,
identity_cookie: AuthenticationCookie,
form_input: Form<RegistrationFormInput>,
) -> Result<HttpResponse, TelescopeError> {
if !form_input.is_valid() {
let invalid_form = form_with_input(&identity_cookie.root, &form_input)
.await?
.in_page(&req, "Create Account")
.await?;
return Err(TelescopeError::InvalidForm(invalid_form));
}
let RegistrationFormInput {
first_name,
last_name,
} = form_input.0;
let platform = identity_cookie.root.get_user_account_type();
let platform_id: String = match &identity_cookie.root {
RootIdentity::GitHub(gh) => gh.get_github_id().await?,
RootIdentity::Discord(d) => d.get_discord_id().await?,
RootIdentity::RpiCas(RpiCasIdentity { rcs_id }) => rcs_id.clone(),
};
let created_user_id: Uuid = CreateOneUser::execute(
first_name,
last_name,
(platform == UserAccountType::Rpi)
.then(|| UserRole::Student)
.unwrap_or(UserRole::External),
platform,
platform_id,
)
.await
.map_err(|_| TelescopeError::BadRequest {
header: "Could Not Create Account".into(),
message: format!(
"We could not create an account. This likely (although not always) \
means that your {0} account is already linked to an existing user's account. Please \
try to login to that account. If you continue having issues or are sure that your {0} \
account is not already linked to an existing user, please contact a coordinator and \
file an issue on the Telescope GitHub.",
platform
),
show_status_code: false,
})?
.ok_or(TelescopeError::ise(
"Create User mutation did not return user ID",
))?;
Ok(HttpResponse::Found()
.header(LOCATION, format!("/user/{}", created_user_id))
.finish())
}