properly set user guilds/permissions with first time setup

pull/12/head
Patrick Cleavelin 2024-06-09 22:26:41 -05:00
parent 66ea9ac2fa
commit c07ac7ceac
3 changed files with 46 additions and 36 deletions

View File

@ -54,6 +54,7 @@ impl Database {
// NOTE(pcleavelin): for some reason this needs to be a let-binding or else
// the compiler complains about it being dropped too early (maybe I should update the compiler version)
#[allow(clippy::useless_conversion)]
let guilds = query
.query_map([], |row| {
Ok(Guild {
@ -68,6 +69,18 @@ impl Database {
guilds
}
pub(crate) fn get_user_count(&self) -> Result<i64> {
self.conn.query_row(
"
SELECT
COUNT(username)
FROM User
",
[],
|row| row.get(0),
)
}
pub(crate) fn get_user_from_api_key(&self, api_key: &str) -> Result<User> {
self.conn.query_row(
"

View File

@ -46,12 +46,7 @@ pub(crate) async fn home(
Redirect::to(&format!("{}/login", state.origin))
})?;
tracing::info!("user name: {}", user.name);
let user_app_permissions = db.get_user_app_permissions(&user.name).map_err(|err| {
error!(?err, "failed to get user app permissions");
// TODO: change this to returning a error to the client
Redirect::to(&format!("{}/error", state.origin))
})?;
let user_app_permissions = db.get_user_app_permissions(&user.name).unwrap_or_default();
let can_add_guild = user_app_permissions.can(auth::AppPermission::AddGuild);
let client = reqwest::Client::new();

View File

@ -174,7 +174,7 @@ pub(crate) async fn v2_auth(
.map_err(|err| Error::Auth(err.to_string()))?;
let db = state.db.lock().await;
let needs_setup = db.get_guilds().map_err(Error::Database)?.is_empty();
let needs_setup = db.get_user_count().map_err(Error::Database)? == 0;
let token = if let Some(user) = db
.get_user(&user.username)
.map_err(Error::Database)?
@ -201,19 +201,21 @@ pub(crate) async fn v2_auth(
auth::AppPermissions(auth::AppPermission::all()),
)
.map_err(Error::Database)?;
} else {
let guilds = db.get_guilds().map_err(Error::Database)?;
let mut in_a_guild = false;
for guild in guilds {
let Some(discord_guild) = discord_guilds
.iter()
.find(|discord_guild| discord_guild.id == guild.id)
else {
continue;
};
}
in_a_guild = true;
let guilds = db.get_guilds().map_err(Error::Database)?;
let mut in_a_guild = false;
for guild in guilds {
let Some(discord_guild) = discord_guilds
.iter()
.find(|discord_guild| discord_guild.id == guild.id)
else {
continue;
};
in_a_guild = true;
if !needs_setup {
let now = Utc::now().naive_utc();
db.insert_user(
&user.username,
@ -223,27 +225,27 @@ pub(crate) async fn v2_auth(
now + Duration::seconds(auth.expires_in as i64),
)
.map_err(Error::Database)?;
db.insert_user_guild(&user.username, guild.id)
.map_err(Error::Database)?;
if db.get_user_permissions(&user.username, guild.id).is_err() {
db.insert_user_permission(
&user.username,
guild.id,
if discord_guild.owner {
auth::Permissions(auth::Permission::all())
} else {
Default::default()
},
)
.map_err(Error::Database)?;
}
}
if !in_a_guild {
return Err(Error::NoGuildFound);
db.insert_user_guild(&user.username, guild.id)
.map_err(Error::Database)?;
if db.get_user_permissions(&user.username, guild.id).is_err() {
db.insert_user_permission(
&user.username,
guild.id,
if discord_guild.owner {
auth::Permissions(auth::Permission::all())
} else {
Default::default()
},
)
.map_err(Error::Database)?;
}
}
if !in_a_guild {
return Err(Error::NoGuildFound);
}
// TODO: add permissions based on roles