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
use crate::api::rcos::meetings::{MeetingType, ALL_MEETING_TYPES};
use crate::api::rcos::prelude::*;
use crate::api::rcos::send_query;
use crate::api::rcos::users::UserRole;
use crate::error::TelescopeError;
use chrono::Local;
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "graphql/rcos/schema.json",
query_path = "graphql/rcos/meetings/authorization_for.graphql"
)]
pub struct AuthorizationFor;
use crate::api::rcos::meetings::get_host::MeetingHost;
use authorization_for::{ResponseData, Variables};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct UserMeetingAuthorization {
pub user_id: Option<uuid>,
role: UserRole,
is_current_coordinator: bool,
is_current_mentor: bool,
}
impl Default for UserMeetingAuthorization {
fn default() -> Self {
UserMeetingAuthorization {
user_id: None,
role: UserRole::External,
is_current_coordinator: false,
is_current_mentor: false,
}
}
}
impl UserMeetingAuthorization {
fn faculty_advisor(user_id: uuid) -> Self {
Self {
user_id: Some(user_id),
role: UserRole::FacultyAdvisor,
is_current_mentor: false,
is_current_coordinator: false,
}
}
pub fn can_view_drafts(&self) -> bool {
self.is_current_coordinator
|| self.role == UserRole::FacultyAdvisor
|| self.role == UserRole::Sysadmin
}
pub fn can_view(&self, meeting_type: MeetingType) -> bool {
match meeting_type {
MeetingType::Coordinators => self.can_view_drafts(),
MeetingType::Mentors | MeetingType::Grading => {
self.is_current_mentor || self.can_view_drafts()
}
_ => true,
}
}
pub fn can_edit(&self, host_user_id: Option<uuid>) -> bool {
if let (Some(host), Some(viewer)) = (host_user_id, self.user_id) {
host == viewer || self.can_view_drafts()
} else {
self.can_view_drafts()
}
}
pub async fn can_edit_by_id(&self, meeting_id: i64) -> Result<bool, TelescopeError> {
if self.can_view_drafts() {
Ok(true)
} else {
let meeting_host: Option<uuid> = MeetingHost::get(meeting_id).await?;
match (meeting_host, self.user_id) {
(Some(host), Some(viewer)) => Ok(host == viewer),
_ => Ok(false),
}
}
}
pub fn can_delete_meetings(&self) -> bool {
self.can_view_drafts()
}
pub fn can_create_meetings(&self) -> bool {
self.can_view_drafts()
}
pub fn viewable_types(&self) -> Vec<MeetingType> {
let mut visible_types: Vec<MeetingType> = Vec::with_capacity(ALL_MEETING_TYPES.len());
for ty in ALL_MEETING_TYPES.iter() {
if self.can_view(*ty) {
visible_types.push(*ty);
}
}
return visible_types;
}
}
impl AuthorizationFor {
pub async fn get(user_id: Option<uuid>) -> Result<UserMeetingAuthorization, TelescopeError> {
if user_id.is_none() {
return Ok(UserMeetingAuthorization::default());
}
let user_id = user_id.unwrap();
let query_vars: Variables = Variables {
now: Local::today().naive_local(),
user_id,
};
let api_response: ResponseData = send_query::<Self>(query_vars).await?;
let user_role: UserRole = api_response
.users_by_pk
.map(|user| user.role)
.unwrap_or(UserRole::External);
if user_role == UserRole::FacultyAdvisor {
return Ok(UserMeetingAuthorization::faculty_advisor(user_id));
}
let is_current_coordinator: bool = api_response
.current_semesters
.iter()
.map(|semester| semester.enrollments.as_slice())
.flatten()
.map(|enrollment| enrollment.is_coordinator)
.any(|is_coordinator| is_coordinator);
let is_current_mentor: bool = api_response
.current_semesters
.iter()
.map(|semester| semester.small_groups.as_slice())
.flatten()
.map(|small_group| small_group.small_group_id)
.count()
>= 1;
return Ok(UserMeetingAuthorization {
user_id: Some(user_id),
role: user_role,
is_current_coordinator,
is_current_mentor,
});
}
}