Skip to content

Commit 79ce0b6

Browse files
committed
Adding audit trail actions into the publish, yank and unyank transactions.
1 parent d280f13 commit 79ce0b6

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/controllers/krate/publish.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ use swirl::Job;
88
use crate::controllers::prelude::*;
99
use crate::git;
1010
use crate::models::dependency;
11-
use crate::models::{Badge, Category, Keyword, NewCrate, NewVersion, Rights, User};
11+
use crate::models::{
12+
Badge, Category, Keyword, NewCrate, NewVersion, NewVersionOwnerAction, Rights, User,
13+
VersionAction,
14+
};
15+
1216
use crate::render;
1317
use crate::util::{internal, CargoError, ChainError, Maximums};
1418
use crate::util::{read_fill, read_le_u32};
@@ -148,6 +152,14 @@ pub fn publish(req: &mut dyn Request) -> CargoResult<Response> {
148152
)?
149153
.save(&conn, &new_crate.authors, &verified_email_address)?;
150154

155+
NewVersionOwnerAction::new(
156+
version.id,
157+
user.id,
158+
req.authentication_source()?.api_token_id(),
159+
VersionAction::Publish,
160+
)
161+
.save(&conn)?;
162+
151163
// Link this new version to all dependencies
152164
let git_deps = dependency::add_dependencies(&conn, &new_crate.deps, version.id)?;
153165

src/controllers/version/yank.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use swirl::Job;
55
use super::version_and_crate;
66
use crate::controllers::prelude::*;
77
use crate::git;
8-
use crate::models::Rights;
8+
use crate::models::{NewVersionOwnerAction, Rights, VersionAction};
99
use crate::util::CargoError;
1010

1111
/// Handles the `DELETE /crates/:crate_id/:version/yank` route.
@@ -35,6 +35,14 @@ fn modify_yank(req: &mut dyn Request, yanked: bool) -> CargoResult<Response> {
3535
if user.rights(req.app(), &owners)? < Rights::Publish {
3636
return Err(human("must already be an owner to yank or unyank"));
3737
}
38+
let action = if yanked {
39+
VersionAction::Yank
40+
} else {
41+
VersionAction::Unyank
42+
};
43+
let api_token_id = req.authentication_source()?.api_token_id();
44+
45+
NewVersionOwnerAction::new(version.id, user.id, api_token_id, action).save(&conn)?;
3846

3947
git::yank(krate.name, version, yanked)
4048
.enqueue(&conn)

src/middleware/current_user.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,12 @@ impl<'a> RequestUser for dyn Request + 'a {
8686
.chain_error(|| Unauthorized)
8787
}
8888
}
89+
90+
impl AuthenticationSource {
91+
pub fn api_token_id(self) -> Option<i32> {
92+
match self {
93+
AuthenticationSource::SessionCookie => None,
94+
AuthenticationSource::ApiToken { api_token_id } => Some(api_token_id),
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)