-
Notifications
You must be signed in to change notification settings - Fork 649
Add Crate Owner Auditing #2025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add Crate Owner Auditing #2025
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
migrations/2019-12-14-005407_create_crate_owner_actions/down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE crate_owner_actions; |
8 changes: 8 additions & 0 deletions
8
migrations/2019-12-14-005407_create_crate_owner_actions/up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CREATE TABLE crate_owner_actions ( | ||
id SERIAL PRIMARY KEY, | ||
crate_id INTEGER NOT NULL REFERENCES crates(id) ON DELETE CASCADE, | ||
user_id INTEGER NOT NULL REFERENCES users(id), | ||
api_token_id INTEGER REFERENCES api_tokens(id), | ||
action INTEGER NOT NULL, | ||
time TIMESTAMP NOT NULL DEFAULT now() | ||
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use chrono::NaiveDateTime; | ||
use diesel::prelude::*; | ||
use diesel::{ | ||
deserialize::{self, FromSql}, | ||
pg::Pg, | ||
serialize::{self, Output, ToSql}, | ||
sql_types::Integer, | ||
}; | ||
use std::io::Write; | ||
|
||
use crate::models::{ApiToken, Crate, User}; | ||
use crate::schema::*; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, FromSqlRow, AsExpression)] | ||
#[repr(i32)] | ||
#[sql_type = "Integer"] | ||
pub enum CrateAction { | ||
InviteUser = 0, | ||
RemoveUser = 1, | ||
} | ||
|
||
impl Into<&'static str> for CrateAction { | ||
fn into(self) -> &'static str { | ||
match self { | ||
CrateAction::InviteUser => "invite_user", | ||
CrateAction::RemoveUser => "remove_user", | ||
} | ||
} | ||
} | ||
|
||
impl Into<String> for CrateAction { | ||
fn into(self) -> String { | ||
let string: &'static str = self.into(); | ||
|
||
string.into() | ||
} | ||
} | ||
|
||
impl FromSql<Integer, Pg> for CrateAction { | ||
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> { | ||
match <i32 as FromSql<Integer, Pg>>::from_sql(bytes)? { | ||
0 => Ok(CrateAction::InviteUser), | ||
1 => Ok(CrateAction::RemoveUser), | ||
n => Err(format!("unknown crate action: {}", n).into()), | ||
} | ||
} | ||
} | ||
|
||
impl ToSql<Integer, Pg> for CrateAction { | ||
fn to_sql<W: Write>(&self, out: &mut Output<'_, W, Pg>) -> serialize::Result { | ||
ToSql::<Integer, Pg>::to_sql(&(*self as i32), out) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, Queryable, Identifiable, Associations)] | ||
#[belongs_to(Crate)] | ||
#[belongs_to(User)] | ||
#[belongs_to(ApiToken)] | ||
pub struct CrateOwnerAction { | ||
pub id: i32, | ||
pub crate_id: i32, | ||
pub user_id: i32, | ||
pub api_token_id: Option<i32>, | ||
pub action: CrateAction, | ||
pub time: NaiveDateTime, | ||
} | ||
|
||
impl CrateOwnerAction { | ||
pub fn all(conn: &PgConnection) -> QueryResult<Vec<Self>> { | ||
crate_owner_actions::table.load(conn) | ||
} | ||
|
||
pub fn by_crate(conn: &PgConnection, krate: &Crate) -> QueryResult<Vec<(Self, User)>> { | ||
use crate_owner_actions::dsl::crate_id; | ||
|
||
crate_owner_actions::table | ||
.filter(crate_id.eq(krate.id)) | ||
.inner_join(users::table) | ||
.order(crate_owner_actions::dsl::id) | ||
.load(conn) | ||
} | ||
} | ||
|
||
pub fn insert_crate_owner_action( | ||
conn: &PgConnection, | ||
crate_id_: i32, | ||
user_id_: i32, | ||
api_token_id_: Option<i32>, | ||
action_: CrateAction, | ||
) -> QueryResult<CrateOwnerAction> { | ||
use crate_owner_actions::dsl::{action, api_token_id, crate_id, user_id}; | ||
|
||
diesel::insert_into(crate_owner_actions::table) | ||
.values(( | ||
crate_id.eq(crate_id_), | ||
user_id.eq(user_id_), | ||
api_token_id.eq(api_token_id_), | ||
action.eq(action_), | ||
)) | ||
.get_result(conn) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer a name like
ActionKind
orActionType
here, or evenCrateOwnerActionKind
, though this is kind of unwieldy.We have the
CrateOwnerAction
andCrateAction
types in this module, and while these types have quite different nature, the nature of their difference isn't reflected in the current naming.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the suggestion. I'll have a look.