medal/
db_conn.rs

1/*  medal                                                                                                            *\
2 *  Copyright (C) 2022  Bundesweite Informatikwettbewerbe, Robert Czechowski                                                            *
3 *                                                                                                                   *
4 *  This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero        *
5 *  General Public License as published  by the Free Software Foundation, either version 3 of the License, or (at    *
6 *  your option) any later version.                                                                                  *
7 *                                                                                                                   *
8 *  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the       *
9 *  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public      *
10 *  License for more details.                                                                                        *
11 *                                                                                                                   *
12 *  You should have received a copy of the GNU Affero General Public License along with this program.  If not, see   *
13\*  <http://www.gnu.org/licenses/>.                                                                                  */
14
15use config;
16use db_objects::*;
17
18#[derive(Debug)]
19pub enum SignupResult {
20    SignedUp,
21    EmailTaken,
22    UsernameTaken,
23    UserLoggedIn,
24    EmptyFields,
25}
26
27/// This trait abstracts the database connection and provides function for all actions to be performed on the database
28/// in the medal platform.
29pub trait MedalConnection {
30    fn reconnect(config: &config::Config) -> Self;
31
32    fn dbtype(&self) -> &'static str;
33
34    fn migration_already_applied(&self, name: &str) -> bool;
35    fn apply_migration(&mut self, name: &str, contents: &str);
36
37    fn code_exists(&self, code: &str) -> bool;
38
39    /// Try to get session associated to the session token `key`.
40    ///
41    /// Returns an `Option` that can contain the `SessionUser` of the session if the session exists and is not expired or
42    /// `None` otherwise.
43    fn get_session(&self, key: &str) -> Option<SessionUser>;
44
45    /// Create a new anonymous session with the session token `key`.
46    ///
47    /// Returns the `SessionUser` of the session.
48    fn new_session(&self, key: &str) -> SessionUser;
49    /// Set activity date (for testing purposes)
50    fn session_set_activity_dates(&self, session_id: i32, account_created: Option<time::Timespec>,
51                                  last_login: Option<time::Timespec>, last_activity: Option<time::Timespec>);
52    /// Saves the session data of `session` in the database.
53    fn save_session(&self, session: SessionUser);
54    /// Combination of [`get_session`](#tymethod.get_session) and  [`new_session`](#tymethod.new_session).
55    ///
56    /// This method can still fail in case of database error in order to bubble them up to the webframework
57    fn get_session_or_new(&self, key: &str) -> Result<SessionUser, ()>;
58
59    /// Try to get session associated to the id `user_id`.
60    ///
61    /// Returns an `Option` that can contain the `SessionUser` of the session if the session exists or `None` otherwise.
62    fn get_user_by_id(&self, user_id: i32) -> Option<SessionUser>;
63
64    /// Try to get session and user group associated to the id `user_id`.
65    ///
66    /// Returns an `Option` that can contain a pair of `SessionUser` and `Option<Group>` of the session and optionally
67    /// the group if the session exists or `None` otherwise.
68    fn get_user_and_group_by_id(&self, user_id: i32) -> Option<(SessionUser, Option<Group>)>;
69    fn get_user_and_group_by_logincode(&self, logincode: &str) -> Option<(SessionUser, Option<Group>)>;
70
71    fn set_webauthn_passkey_registration(&self, user_id: i32, passkey_registration: &str);
72    fn get_webauthn_passkey_registration(&self, user_id: i32) -> Option<String>;
73    fn check_webauthn_cred_id_in_use(&self, cred_id: &str) -> bool;
74    fn add_webauthn_passkey(&self, user_id: i32, cred_id: &str, passkey: &str, name: &str) -> i32;
75    fn delete_webauthn_passkey(&self, session_id: i32, passkey_id: i32);
76
77    fn get_webauthn_passkey_names_for_user(&self, user_id: i32) -> Vec<(i32, String)>;
78    fn get_all_webauthn_passkeys(&self) -> Vec<String>;
79    fn get_all_webauthn_credentials(&self) -> Vec<String>;
80    fn store_webauthn_auth_challenge(&self, challenge: &str, authentication: &str) -> i32;
81    fn get_webauthn_auth_challenge_by_id(&self, auth_id: i32) -> Option<String>;
82
83    /// Try to login in the user with `username` and `password`.
84    ///
85    /// Returns a `Result` that either contains the new session token for the user if the login was successfull or no
86    /// value if the login was not successfull.
87    fn login(&self, session: Option<&str>, username: &str, password: &str) -> Result<String, ()>;
88    fn login_with_code(&self, session: Option<&str>, logincode: &str) -> Result<String, ()>;
89    fn login_with_key(&self, session: Option<&str>, cred_id: &str) -> Result<String, ()>;
90    fn login_foreign(&self, session: Option<&str>, provider_id: &str, foreign_id: &str,
91                     _: (bool, bool, &str, &str, Option<i32>, &Option<String>))
92                     -> Result<(String, Option<time::Timespec>), ()>;
93    fn create_user_with_groupcode(&self, session: Option<&str>, groupcode: &str) -> Result<String, ()>;
94    fn update_or_create_group_with_users(&self, group: Group, admin: i32);
95    fn add_admin_to_group(&self, group: &mut Group, admin: i32);
96
97    /// Logs out the user identified by session token `session` by resetting the uesr's session token in the database
98    /// to `NULL`.
99    fn logout(&self, session: &str);
100
101    fn signup(&self, session_token: &str, username: &str, email: &str, password_hash: String, salt: &str)
102              -> SignupResult;
103
104    fn load_submission(&self, session: &SessionUser, task: i32, subtask: Option<&str>) -> Option<Submission>;
105    fn get_all_submissions(&self, session_id: i32, task: i32, subtask: Option<&str>) -> Vec<Submission>;
106    fn submit_submission(&self, submission: Submission);
107    fn get_grade_by_submission(&self, submission_id: i32) -> Grade;
108    fn get_contest_groups_grades(&self, session_id: i32, contest_id: i32)
109                                 -> (Vec<String>, Vec<(Group, Vec<(UserInfo, Vec<Grade>)>)>);
110    fn get_taskgroup_user_grade(&self, session_id: i32, taskgroup_id: i32) -> Grade;
111    fn get_contest_user_grades(&self, session_id: i32, contest_id: i32) -> Vec<Grade>;
112    fn export_contest_results_to_file(&self, contest_id: i32, taskgroups_ids: &[(i32, String)], filename: &str);
113
114    fn insert_contest_annotations(&self, contest_id: i32, annotations: Vec<(i32, Option<String>)>) -> i32;
115
116    /// Returns the submission identified by `submission_id`, together with its grade, task, taskgroup and contest.
117    fn get_submission_by_id_complete_shallow_contest(&self, submission_id: i32)
118                                                     -> Option<(Submission, Task, Taskgroup, Contest)>;
119
120    /// Returns a `Vec` of /all/ contests ever defined.
121    fn get_contest_list(&self) -> Vec<Contest>;
122
123    /// Returns a `Vec` of contests where participations exist.
124    fn get_contest_list_with_group_member_participations(&self, session_id: i32) -> Vec<Contest>;
125
126    /// Returns the contest identified by `contest_id` without any associated taskgroups. Panics if the contest does not
127    /// exist.
128    fn get_contest_by_id(&self, contest_id: i32) -> Option<Contest>;
129
130    /// Returns the contest identified by `contest_id` with associated taskgroups but without any associated tasks of
131    /// the taskgroups. Panics if the contest does not exist.
132    fn get_contest_by_id_partial(&self, contest_id: i32) -> Option<Contest>;
133
134    /// Returns the contest identified by `contest_id` with associated taskgroups and all associated tasks of the
135    /// taskgroups. Panics if the contest does not exist.
136    fn get_contest_by_id_complete(&self, contest_id: i32) -> Option<Contest>;
137
138    /// Try to get the participation associated to the session id `session_id` and the contest id `contest_id`.
139    ///
140    /// Returns an `Option` that can contain the `Participation` if it exists or `None` otherwise.
141    fn get_participation(&self, session_id: i32, contest_id: i32) -> Option<Participation>;
142
143    /// Try to get the participation associated to the session token `session` and the contest id `contest_id`.
144    ///
145    /// Returns an `Option` that can contain the `Participation` if it exists or `None` otherwise.
146    fn get_own_participation(&self, session_id: i32, contest_id: i32) -> Option<Participation>;
147
148    /// Collect all the participation associated to the session token `session`.
149    ///
150    /// Returns an `Vec` that contains pairs of all participations with their associated contests.
151    fn get_all_participations_complete(&self, session_id: i32) -> Vec<(Participation, Contest)>;
152
153    fn count_all_stars(&self, session_id: i32) -> i32;
154    fn count_all_stars_by_contest(&self, session_id: i32) -> Vec<(i32, i32)>;
155
156    fn has_participation_by_contest_file(&self, session_id: i32, location: &str, filename: &str) -> bool;
157
158    /// Start a new participation of the session identified by the session token `session` for the contest with the
159    /// contest id `contest_id`. It checks whether the session is allowed to start the participation.
160    ///
161    /// Returns an `Result` that either contains the new `Participation` if the checks succeded or no value if the
162    /// checks failed.
163    fn new_participation(&self, session_id: i32, contest_id: i32, team: Option<i32>) -> Result<Participation, ()>;
164    fn get_task_by_id(&self, task_id: i32) -> Option<Task>;
165    fn get_task_by_id_complete(&self, task_id: i32) -> Option<(Task, Taskgroup, Contest)>;
166
167    fn get_submission_to_validate(&self, tasklocation: &str, subtask: Option<&str>) -> i32;
168    fn find_next_submission_to_validate(&self, userid: i32, taskgroupid: i32);
169
170    fn add_group(&self, group: &mut Group);
171    fn save_group(&self, group: &mut Group);
172    fn get_groups(&self, session_id: i32) -> Vec<Group>;
173    fn get_groups_complete(&self, session_id: i32) -> Vec<Group>;
174    fn get_group(&self, session_id: i32) -> Option<Group>;
175    fn group_has_protected_participations(&self, session_id: i32) -> bool;
176    fn get_group_complete(&self, group_id: i32) -> Option<Group>;
177
178    fn delete_user(&self, user_id: i32);
179    fn delete_all_users_for_group(&self, group_id: i32);
180    fn delete_group(&self, group_id: i32);
181    fn delete_participation(&self, user_id: i32, contest_id: i32);
182    fn remove_old_users_and_groups(&self, maxstudentage: time::Timespec, maxteacherage: Option<time::Timespec>,
183                                   maxage: Option<time::Timespec>)
184                                   -> Result<(i32, i32, i32, i32), ()>;
185    fn count_temporary_sessions(&self, maxage: time::Timespec) -> i32;
186    fn remove_temporary_sessions(&self, maxage: time::Timespec, limit: Option<u32>);
187
188    fn move_task_location(&self, old_location: &str, new_location: &str, contest: Option<i32>) -> i32;
189
190    fn get_search_users(
191        &self, _: (Option<i32>, Option<String>, Option<String>, Option<String>, Option<String>, Option<String>))
192        -> Result<Vec<(i32, Option<String>, Option<String>, Option<String>, Option<String>, Option<String>)>,
193                  Vec<(i32, String, String, String)>>;
194
195    fn get_debug_information(&self) -> String;
196
197    fn reset_all_contest_visibilities(&self);
198    fn reset_all_taskgroup_visibilities(&self);
199}
200
201pub trait MedalObject<T: MedalConnection> {
202    fn save(&mut self, conn: &T);
203}