Merge pull request 'use-proper-role' (#1065) from use-proper-role into staging
Reviewed-on: #1065
This commit is contained in:
commit
7935d1837f
@ -12,11 +12,11 @@ use super::{
|
|||||||
notification::Notification,
|
notification::Notification,
|
||||||
role::Role,
|
role::Role,
|
||||||
rower::Rower,
|
rower::Rower,
|
||||||
user::User,
|
user::{User, VorstandUser},
|
||||||
};
|
};
|
||||||
use crate::model::user::VecUser;
|
use crate::model::user::VecUser;
|
||||||
|
|
||||||
#[derive(FromRow, Serialize, Deserialize, Clone, Debug)]
|
#[derive(FromRow, Serialize, Deserialize, Clone, Debug, Difference)]
|
||||||
pub struct Logbook {
|
pub struct Logbook {
|
||||||
pub id: i64,
|
pub id: i64,
|
||||||
pub boat_id: i64,
|
pub boat_id: i64,
|
||||||
@ -193,11 +193,6 @@ impl LogbookWithBoatAndRowers {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
|
||||||
pub enum LogbookAdminUpdateError {
|
|
||||||
NotAllowed,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum LogbookUpdateError {
|
pub enum LogbookUpdateError {
|
||||||
NotYourEntry,
|
NotYourEntry,
|
||||||
@ -634,16 +629,7 @@ ORDER BY departure DESC
|
|||||||
Ok(ret)
|
Ok(ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update(
|
pub async fn update(&self, db: &SqlitePool, data: LogToUpdate, changed_by: &VorstandUser) {
|
||||||
&self,
|
|
||||||
db: &SqlitePool,
|
|
||||||
data: LogToUpdate,
|
|
||||||
user: &User,
|
|
||||||
) -> Result<(), LogbookAdminUpdateError> {
|
|
||||||
if !user.has_role(db, "Vorstand").await {
|
|
||||||
return Err(LogbookAdminUpdateError::NotAllowed);
|
|
||||||
}
|
|
||||||
|
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
"UPDATE logbook SET boat_id=?, shipmaster=?, steering_person=?, shipmaster_only_steering=?, departure=?, arrival=?, destination=?, distance_in_km=?, comments=?, logtype=? WHERE id=?",
|
"UPDATE logbook SET boat_id=?, shipmaster=?, steering_person=?, shipmaster_only_steering=?, departure=?, arrival=?, destination=?, distance_in_km=?, comments=?, logtype=? WHERE id=?",
|
||||||
data.boat_id,
|
data.boat_id,
|
||||||
@ -660,7 +646,12 @@ ORDER BY departure DESC
|
|||||||
)
|
)
|
||||||
.execute(db)
|
.execute(db)
|
||||||
.await.unwrap();
|
.await.unwrap();
|
||||||
Ok(())
|
|
||||||
|
Log::create(
|
||||||
|
db,
|
||||||
|
format!("{changed_by} updated log entry={:?} to {:?}", self, data),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn remove_rowers(&self, db: &mut Transaction<'_, Sqlite>) {
|
async fn remove_rowers(&self, db: &mut Transaction<'_, Sqlite>) {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
|
|
||||||
use rocket::{
|
use rocket::{
|
||||||
Request, Route, State,
|
|
||||||
form::Form,
|
form::Form,
|
||||||
get,
|
get,
|
||||||
http::{Cookie, CookieJar},
|
http::{Cookie, CookieJar},
|
||||||
@ -10,8 +9,9 @@ use rocket::{
|
|||||||
response::{Flash, Redirect},
|
response::{Flash, Redirect},
|
||||||
routes,
|
routes,
|
||||||
time::{Duration, OffsetDateTime},
|
time::{Duration, OffsetDateTime},
|
||||||
|
Request, Route, State,
|
||||||
};
|
};
|
||||||
use rocket_dyn_templates::{Template, context};
|
use rocket_dyn_templates::{context, Template};
|
||||||
use sqlx::SqlitePool;
|
use sqlx::SqlitePool;
|
||||||
use tera::Context;
|
use tera::Context;
|
||||||
|
|
||||||
@ -22,8 +22,8 @@ use crate::{
|
|||||||
distance::Distance,
|
distance::Distance,
|
||||||
log::Log,
|
log::Log,
|
||||||
logbook::{
|
logbook::{
|
||||||
LogToAdd, LogToFinalize, LogToUpdate, Logbook, LogbookAdminUpdateError,
|
LogToAdd, LogToFinalize, LogToUpdate, Logbook, LogbookCreateError, LogbookDeleteError,
|
||||||
LogbookCreateError, LogbookDeleteError, LogbookUpdateError,
|
LogbookUpdateError,
|
||||||
},
|
},
|
||||||
logtype::LogType,
|
logtype::LogType,
|
||||||
planned::trip::Trip,
|
planned::trip::Trip,
|
||||||
@ -394,27 +394,12 @@ async fn update(
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
match logbook.update(db, data.clone(), &user.user).await {
|
logbook.update(db, data.clone(), &user).await;
|
||||||
Ok(()) => {
|
|
||||||
Log::create(
|
|
||||||
db,
|
|
||||||
format!(
|
|
||||||
"User {} updated log entry={:?} to {:?}",
|
|
||||||
&user.name, logbook, data
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
Flash::success(
|
Flash::success(
|
||||||
Redirect::to("/log/show"),
|
Redirect::to("/log/show"),
|
||||||
"Logbucheintrag erfolgreich bearbeitet".to_string(),
|
"Logbucheintrag erfolgreich bearbeitet".to_string(),
|
||||||
)
|
)
|
||||||
}
|
|
||||||
Err(LogbookAdminUpdateError::NotAllowed) => Flash::error(
|
|
||||||
Redirect::to("/log/show"),
|
|
||||||
"Du hast keine Erlaubnis, diesen Logbucheintrag zu bearbeiten!".to_string(),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn home_logbook(
|
async fn home_logbook(
|
||||||
@ -606,7 +591,7 @@ mod test {
|
|||||||
use sqlx::SqlitePool;
|
use sqlx::SqlitePool;
|
||||||
|
|
||||||
use crate::model::logbook::Logbook;
|
use crate::model::logbook::Logbook;
|
||||||
use crate::tera::{User, log::Boat};
|
use crate::tera::{log::Boat, User};
|
||||||
use crate::testdb;
|
use crate::testdb;
|
||||||
|
|
||||||
#[sqlx::test]
|
#[sqlx::test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user