From d892d1cfa01319291f80a4a87f4d12b1677972da Mon Sep 17 00:00:00 2001 From: filipriec Date: Mon, 14 Apr 2025 20:50:02 +0200 Subject: [PATCH] role restricted admin panel --- client/src/components/admin/admin_panel.rs | 118 +++++++++++---------- client/src/ui/handlers/render.rs | 1 + 2 files changed, 62 insertions(+), 57 deletions(-) diff --git a/client/src/components/admin/admin_panel.rs b/client/src/components/admin/admin_panel.rs index d57d805..59fac14 100644 --- a/client/src/components/admin/admin_panel.rs +++ b/client/src/components/admin/admin_panel.rs @@ -1,24 +1,25 @@ // src/components/admin/admin_panel.rs use crate::config::colors::themes::Theme; -use crate::state::pages::admin::AdminState; // Import the persistent state +use crate::state::pages::auth::AuthState; +use crate::state::pages::admin::AdminState; use common::proto::multieko2::table_definition::ProfileTreeResponse; use ratatui::{ layout::{Alignment, Constraint, Direction, Layout, Rect}, - style::Style, // Added Modifier + style::Style, text::{Line, Span, Text}, widgets::{Block, BorderType, Borders, List, ListItem, Paragraph, Wrap}, Frame, }; -// Renamed from AdminPanelState::render and made a standalone function pub fn render_admin_panel( f: &mut Frame, - admin_state: &AdminState, // Accept the persistent state (immutable borrow is enough for rendering) + auth_state: &AuthState, + admin_state: &AdminState, area: Rect, theme: &Theme, profile_tree: &ProfileTreeResponse, - selected_profile: &Option, // The globally selected profile for the checkmark + selected_profile: &Option, ) { let block = Block::default() .borders(Borders::ALL) @@ -45,64 +46,67 @@ pub fn render_admin_panel( .constraints([Constraint::Percentage(30), Constraint::Percentage(70)]) .split(chunks[1]); - // Profile list - Use data from admin_state - let items: Vec = admin_state - .profiles // Use profiles from the persistent state - .iter() - .map(|p| { - ListItem::new(Line::from(vec![ - Span::styled( - // Check against the globally selected profile for the checkmark - if Some(p) == selected_profile.as_ref() { "✓ " } else { " " }, - Style::default().fg(theme.accent), - ), - Span::styled(p, Style::default().fg(theme.fg)), - ])) - }) - .collect(); + if auth_state.role.as_deref() != Some("admin") { + // Profile list - Use data from admin_state + let items: Vec = admin_state + .profiles + .iter() + .map(|p| { + ListItem::new(Line::from(vec![ + Span::styled( + if Some(p) == selected_profile.as_ref() { "✓ " } else { " " }, + Style::default().fg(theme.accent), + ), + Span::styled(p, Style::default().fg(theme.fg)), + ])) + }) + .collect(); - let list = List::new(items) - .block(Block::default().title("Profiles")) - .highlight_style(Style::default().bg(theme.highlight).fg(theme.bg)); + let list = List::new(items) + .block(Block::default().title("Profiles")) + .highlight_style(Style::default().bg(theme.highlight).fg(theme.bg)); - // Render statefully using a CLONE of the list_state from persistent AdminState - // Cloning is necessary because render_stateful_widget needs `&mut ListState` - // but we only have `&AdminState`. This is a common pattern in Ratatui. - let mut list_state_clone = admin_state.list_state.clone(); - f.render_stateful_widget(list, content_chunks[0], &mut list_state_clone); + let mut list_state_clone = admin_state.list_state.clone(); + f.render_stateful_widget(list, content_chunks[0], &mut list_state_clone); - // Profile details - Use selection info from admin_state - if let Some(profile) = admin_state - .get_selected_index() // Use the method from persistent state - .and_then(|i| profile_tree.profiles.get(i)) - { - let mut text = Text::default(); - text.lines.push(Line::from(vec![ - Span::styled("Profile: ", Style::default().fg(theme.accent)), - Span::styled(&profile.name, Style::default().fg(theme.highlight)), - ])); + // Profile details - Use selection info from admin_state + if let Some(profile) = admin_state + .get_selected_index() + .and_then(|i| profile_tree.profiles.get(i)) + { + let mut text = Text::default(); + text.lines.push(Line::from(vec![ + Span::styled("Profile: ", Style::default().fg(theme.accent)), + Span::styled(&profile.name, Style::default().fg(theme.highlight)), + ])); - text.lines.push(Line::from("")); - text.lines.push(Line::from(Span::styled( - "Tables:", - Style::default().fg(theme.accent), - ))); + text.lines.push(Line::from("")); + text.lines.push(Line::from(Span::styled( + "Tables:", + Style::default().fg(theme.accent), + ))); - for table in &profile.tables { - let mut line = vec![Span::styled(format!("├─ {}", table.name), theme.fg)]; - if !table.depends_on.is_empty() { - line.push(Span::styled( - format!(" → {}", table.depends_on.join(", ")), - Style::default().fg(theme.secondary), - )); + for table in &profile.tables { + let mut line = vec![Span::styled(format!("├─ {}", table.name), theme.fg)]; + if !table.depends_on.is_empty() { + line.push(Span::styled( + format!(" → {}", table.depends_on.join(", ")), + Style::default().fg(theme.secondary), + )); + } + text.lines.push(Line::from(line)); } - text.lines.push(Line::from(line)); - } - let details_widget = Paragraph::new(text) - .block(Block::default().title("Details")) - .wrap(Wrap { trim: true }); // Add wrapping - f.render_widget(details_widget, content_chunks[1]); + let details_widget = Paragraph::new(text) + .block(Block::default().title("Details")) + .wrap(Wrap { trim: true }); + f.render_widget(details_widget, content_chunks[1]); + } + } else { + // Admin-specific view + let admin_message = Paragraph::new("Admin-specific view. Profile selection not applicable.") + .style(Style::default().fg(theme.fg)) + .alignment(Alignment::Center); + f.render_widget(admin_message, content_chunks[1]); } } - diff --git a/client/src/ui/handlers/render.rs b/client/src/ui/handlers/render.rs index ed703e1..cc1f8ca 100644 --- a/client/src/ui/handlers/render.rs +++ b/client/src/ui/handlers/render.rs @@ -73,6 +73,7 @@ pub fn render_ui( } else if app_state.ui.show_admin { crate::components::admin::admin_panel::render_admin_panel( f, + auth_state, admin_state, main_content_area, theme,