diff --git a/backend/src/data/model.rs b/backend/src/data/model.rs index 087bac0..01aae36 100644 --- a/backend/src/data/model.rs +++ b/backend/src/data/model.rs @@ -16,6 +16,12 @@ pub struct User { pub is_admin: bool, } +impl User { + pub fn can_edit_recipe(&self, recipe: &Recipe) -> bool { + self.is_admin || recipe.user_id == self.id + } +} + #[derive(Debug, FromRow)] pub struct UserLoginInfo { pub last_login_datetime: DateTime, @@ -45,10 +51,6 @@ pub struct Recipe { pub groups: Vec, } -pub fn can_user_edit_recipe(user: &User, recipe: &Recipe) -> bool { - user.is_admin || recipe.user_id == user.id -} - #[derive(Debug, FromRow)] pub struct Group { pub id: i64, diff --git a/backend/src/services/mod.rs b/backend/src/services/mod.rs index 6d3980e..6fdbbc9 100644 --- a/backend/src/services/mod.rs +++ b/backend/src/services/mod.rs @@ -104,7 +104,7 @@ pub async fn dev_panel( } } -///// DEV_PANEL ///// +///// LOGS ///// #[derive(Deserialize)] pub struct LogFile { diff --git a/backend/src/services/recipe.rs b/backend/src/services/recipe.rs index 4310b18..0a09171 100644 --- a/backend/src/services/recipe.rs +++ b/backend/src/services/recipe.rs @@ -41,7 +41,7 @@ pub async fn edit( ) -> Result { if let Some(ref user) = context.user { if let Some(recipe) = connection.get_recipe(recipe_id, false).await? { - if model::can_user_edit_recipe(user, &recipe) { + if user.can_edit_recipe(&recipe) { Ok(Html( RecipeEditTemplate { recipes: Recipes::new( diff --git a/backend/templates/recipe_view.html b/backend/templates/recipe_view.html index 6d7c64b..285fdb5 100644 --- a/backend/templates/recipe_view.html +++ b/backend/templates/recipe_view.html @@ -6,7 +6,7 @@

{{ recipe.title }}

{% if let Some(user) = context.user %} - {% if crate::data::model::can_user_edit_recipe(user, recipe) %} + {% if user.can_edit_recipe(recipe) %} Edit {% endif %} {% endif %}