Skip to content
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
63 changes: 25 additions & 38 deletions src/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,45 @@ impl fmt::Display for PermissionType {
}
}

pub struct TeamDirectory {
usernames: HashSet<String>,
teams: HashSet<String>,
}
// Separate module to ensure that the struct cannot be constructed without invoking `new`
mod directory {
use std::collections::HashSet;

impl TeamDirectory {
pub fn new(usernames: HashSet<String>, teams: HashSet<String>) -> Self {
Self { usernames, teams }
pub(super) struct TeamDirectory {
usernames: HashSet<String>,
teams: HashSet<String>,
}

fn user_exists(&self, username: &str) -> bool {
self.usernames.contains(&username.to_lowercase())
}
impl TeamDirectory {
pub(super) fn new(usernames: HashSet<String>, teams: HashSet<String>) -> Self {
let usernames: HashSet<String> = usernames
.into_iter()
.map(|name| name.to_lowercase())
.collect();
let teams: HashSet<String> =
teams.into_iter().map(|name| name.to_lowercase()).collect();
Self { usernames, teams }
}

fn team_exists(&self, team: &str) -> bool {
self.teams.contains(&team.to_lowercase())
pub(super) fn user_exists(&self, username: &str) -> bool {
self.usernames.contains(&username.to_lowercase())
}

pub(super) fn team_exists(&self, team: &str) -> bool {
self.teams.contains(&team.to_lowercase())
}
}
}

use directory::TeamDirectory;

pub struct UserPermissions {
review_users: HashSet<UserId>,
try_users: HashSet<UserId>,
directory: TeamDirectory,
}

impl UserPermissions {
pub fn new(
review_users: HashSet<UserId>,
try_users: HashSet<UserId>,
directory: TeamDirectory,
) -> Self {
let team_names: HashSet<String> = directory
.teams
.iter()
.map(|name| name.to_lowercase())
.collect();
let people_names: HashSet<String> = directory
.usernames
.iter()
.map(|name| name.to_lowercase())
.collect();
let directory = TeamDirectory {
teams: team_names,
usernames: people_names,
};
Self {
review_users,
try_users,
directory,
}
}

pub fn has_permission(&self, user_id: UserId, permission: PermissionType) -> bool {
match permission {
PermissionType::Review => self.review_users.contains(&user_id),
Expand Down