Skip to content

Commit d03686b

Browse files
committed
Adding audit trail actions into the publish, yank and unyank transactions.
1 parent 5ed2929 commit d03686b

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/controllers/krate/publish.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ use crate::util::{read_fill, read_le_u32};
1212

1313
use crate::controllers::prelude::*;
1414
use crate::models::dependency;
15-
use crate::models::{Badge, Category, Keyword, NewCrate, NewVersion, Rights, User};
15+
use crate::models::{
16+
Badge, Category, Keyword, NewCrate, NewVersion, NewVersionOwnerAction, Rights, User,
17+
VersionAction,
18+
};
1619
use crate::views::{EncodableCrateUpload, GoodCrate, PublishWarnings};
1720

1821
/// Handles the `PUT /crates/new` route.
@@ -149,6 +152,14 @@ pub fn publish(req: &mut dyn Request) -> CargoResult<Response> {
149152
)?
150153
.save(&conn, &new_crate.authors, &verified_email_address)?;
151154

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

src/controllers/version/yank.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::controllers::prelude::*;
44

55
use crate::git;
66

7-
use crate::models::Rights;
7+
use crate::models::{NewVersionOwnerAction, Rights, VersionAction};
88

99
use super::version_and_crate;
1010

@@ -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(&conn, krate.name, version, yanked)?;
4048

src/middleware/current_user.rs

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

0 commit comments

Comments
 (0)