Skip to content

Commit

Permalink
Refer to the internal platform as ac_platform
Browse files Browse the repository at this point in the history
- As that is really a database backend, but masqueraded as ACPlatform.
- Also adjust the helpers for test setup.
  • Loading branch information
metatoaster committed Sep 25, 2024
1 parent 2fc95cb commit c865153
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
45 changes: 24 additions & 21 deletions pmrac/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ use crate::{
#[derive(Clone, Default)]
pub struct Builder {
// platform
platform: Option<Arc<dyn ACPlatform>>,
ac_platform: Option<Arc<dyn ACPlatform>>,
// automatically purges all but the most recent passwords
password_autopurge: bool,
}

pub struct Platform {
platform: Arc<dyn ACPlatform>,
ac_platform: Arc<dyn ACPlatform>,
password_autopurge: bool,
}

Expand All @@ -37,8 +37,8 @@ impl Builder {
Self::default()
}

pub fn platform(mut self, val: impl ACPlatform + 'static) -> Self {
self.platform = Some(Arc::new(val));
pub fn ac_platform(mut self, val: impl ACPlatform + 'static) -> Self {
self.ac_platform = Some(Arc::new(val));
self
}

Expand All @@ -49,19 +49,22 @@ impl Builder {

pub fn build(self) -> Platform {
Platform {
platform: self.platform.expect("missing required argument platform"),
ac_platform: self.ac_platform.expect("missing required argument ac_platform"),
password_autopurge: self.password_autopurge,
}
}
}

impl Platform {
pub fn new(
platform: impl ACPlatform + 'static,
ac_platform: impl ACPlatform + 'static,
password_autopurge: bool,
) -> Self {
let platform = Arc::new(platform);
Self { platform, password_autopurge }
let ac_platform = Arc::new(ac_platform);
Self {
ac_platform,
password_autopurge,
}
}
}

Expand All @@ -71,7 +74,7 @@ impl<'a> Platform {
&'a self,
name: &str,
) -> Result<User, Error> {
let id = self.platform.add_user(name).await?;
let id = self.ac_platform.add_user(name).await?;
self.force_user_id_password(id, Password::New).await?;
self.get_user(id).await
}
Expand All @@ -84,7 +87,7 @@ impl<'a> Platform {
&'a self,
id: i64,
) -> Result<User, Error> {
let user = self.platform.get_user_by_id(id).await?;
let user = self.ac_platform.get_user_by_id(id).await?;
Ok(User::new(self, user))
}

Expand All @@ -94,7 +97,7 @@ impl<'a> Platform {
password: &str,
) -> Result<User<'a>, Error> {
// TODO login can be email also
let user = self.platform.get_user_by_name(login).await?;
let user = self.ac_platform.get_user_by_name(login).await?;
self.verify_user_id_password(user.id, password).await?;
Ok(User::new(self, user))
}
Expand All @@ -111,7 +114,7 @@ impl Platform {
id: i64,
password: &str,
) -> Result<(), Error> {
let result = self.platform.get_user_password(id).await;
let result = self.ac_platform.get_user_password(id).await;
let stored_password = result
.as_deref()
.map(Password::from_database)
Expand All @@ -135,7 +138,7 @@ impl Platform {
id: i64,
password: &str,
) -> Result<(), Error> {
let result = self.platform.get_user_password(id).await;
let result = self.ac_platform.get_user_password(id).await;
let stored_password = result
.as_deref()
.map(Password::from_database)
Expand All @@ -152,9 +155,9 @@ impl Platform {
) -> Result<(), Error> {
let password_hash = password.to_database()?;
if self.password_autopurge {
self.platform.purge_user_passwords(id).await?;
self.ac_platform.purge_user_passwords(id).await?;
}
self.platform.store_user_password(id, &password_hash).await?;
self.ac_platform.store_user_password(id, &password_hash).await?;
Ok(())
}
}
Expand All @@ -168,7 +171,7 @@ impl Platform {
agent: impl Into<Agent>,
role: Role,
) -> Result<(), Error> {
Ok(self.platform.grant_role_to_agent(
Ok(self.ac_platform.grant_role_to_agent(
res,
&agent.into(),
role
Expand All @@ -181,7 +184,7 @@ impl Platform {
agent: impl Into<Agent>,
role: Role,
) -> Result<(), Error> {
Ok(self.platform.revoke_role_from_agent(
Ok(self.ac_platform.revoke_role_from_agent(
res,
&agent.into(),
role,
Expand All @@ -195,7 +198,7 @@ impl Platform {
endpoint_group: &str,
method: &str,
) -> Result<(), Error> {
Ok(self.platform.assign_policy_to_wf_state(
Ok(self.ac_platform.assign_policy_to_wf_state(
wf_state,
role,
endpoint_group,
Expand All @@ -210,7 +213,7 @@ impl Platform {
endpoint_group: &str,
method: &str,
) -> Result<(), Error> {
Ok(self.platform.remove_policy_from_wf_state(
Ok(self.ac_platform.remove_policy_from_wf_state(
wf_state,
role,
endpoint_group,
Expand All @@ -227,7 +230,7 @@ impl Platform {
res: &str,
wf_state: State,
) -> Result<(), Error> {
Ok(self.platform.set_wf_state_for_res(
Ok(self.ac_platform.set_wf_state_for_res(
res,
wf_state,
).await?)
Expand All @@ -237,7 +240,7 @@ impl Platform {
&self,
res: String,
) -> Result<ResourcePolicy, Error> {
Ok(self.platform.generate_policy_for_res(
Ok(self.ac_platform.generate_policy_for_res(
res
).await?)
}
Expand Down
12 changes: 8 additions & 4 deletions testing/src/ac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ use pmrmodel::backend::db::{
SqliteBackend,
};

pub async fn create_sqlite_backend() -> anyhow::Result<SqliteBackend> {
Ok(SqliteBackend::from_url("sqlite::memory:")
.await?
.run_migration_profile(MigrationProfile::Pmrac)
.await?)
}

pub async fn create_sqlite_platform(purge: bool) -> anyhow::Result<Platform> {
let platform = Builder::new()
.platform(SqliteBackend::from_url("sqlite::memory:")
.await?
.run_migration_profile(MigrationProfile::Pmrac)
.await?)
.ac_platform(create_sqlite_backend().await?)
.password_autopurge(purge)
.build();
Ok(platform)
Expand Down

0 comments on commit c865153

Please sign in to comment.