feat(events): implement role-based access control

- Add Role enum to participants schema to distinguish between Admin and Member
- Grant Admin role to event creators automatically
- Restrict sensitive actions (drawing names, editing budget, managing participants) to admins
- Add UI controls for promoting members to admins and removing participants
- Clean up unused Tailwind CSS variables and utility classes
This commit is contained in:
mlackner 2025-11-20 23:06:25 +01:00
parent 776f5e8ae5
commit 7ef4ff4221
6 changed files with 411 additions and 265 deletions

View File

@ -15,7 +15,6 @@
--color-red-500: oklch(63.7% 0.237 25.331);
--color-red-600: oklch(57.7% 0.245 27.325);
--color-red-700: oklch(50.5% 0.213 27.518);
--color-red-800: oklch(44.4% 0.177 26.899);
--color-red-900: oklch(39.6% 0.141 25.723);
--color-green-400: oklch(79.2% 0.209 151.711);
--color-green-500: oklch(72.3% 0.219 149.579);
@ -25,12 +24,6 @@
--color-emerald-500: oklch(69.6% 0.17 162.48);
--color-emerald-600: oklch(59.6% 0.145 163.225);
--color-cyan-50: oklch(98.4% 0.019 200.873);
--color-blue-50: oklch(97% 0.014 254.604);
--color-blue-200: oklch(88.2% 0.059 254.128);
--color-blue-500: oklch(62.3% 0.214 259.815);
--color-blue-600: oklch(54.6% 0.245 262.881);
--color-blue-700: oklch(48.8% 0.243 264.376);
--color-blue-900: oklch(37.9% 0.146 265.522);
--color-indigo-50: oklch(96.2% 0.018 272.314);
--color-indigo-100: oklch(93% 0.034 272.788);
--color-indigo-300: oklch(78.5% 0.115 274.713);
@ -54,10 +47,8 @@
--color-slate-800: oklch(27.9% 0.041 260.031);
--color-slate-900: oklch(20.8% 0.042 265.755);
--color-gray-50: oklch(98.5% 0.002 247.839);
--color-gray-100: oklch(96.7% 0.003 264.542);
--color-gray-200: oklch(92.8% 0.006 264.531);
--color-gray-300: oklch(87.2% 0.01 258.338);
--color-gray-400: oklch(70.7% 0.022 261.325);
--color-gray-500: oklch(55.1% 0.027 264.364);
--color-gray-600: oklch(44.6% 0.03 256.802);
--color-gray-700: oklch(37.3% 0.034 259.733);
@ -70,7 +61,6 @@
--container-2xl: 42rem;
--container-3xl: 48rem;
--container-4xl: 56rem;
--container-5xl: 64rem;
--container-7xl: 80rem;
--text-xs: 0.75rem;
--text-xs--line-height: calc(1 / 0.75);
@ -84,14 +74,10 @@
--text-xl--line-height: calc(1.75 / 1.25);
--text-2xl: 1.5rem;
--text-2xl--line-height: calc(2 / 1.5);
--text-3xl: 1.875rem;
--text-3xl--line-height: calc(2.25 / 1.875);
--text-4xl: 2.25rem;
--text-4xl--line-height: calc(2.5 / 2.25);
--text-5xl: 3rem;
--text-5xl--line-height: 1;
--text-6xl: 3.75rem;
--text-6xl--line-height: 1;
--text-7xl: 4.5rem;
--text-7xl--line-height: 1;
--font-weight-medium: 500;
@ -323,9 +309,6 @@
.mx-auto {
margin-inline: auto;
}
.mt-1 {
margin-top: calc(var(--spacing) * 1);
}
.mt-2 {
margin-top: calc(var(--spacing) * 2);
}
@ -338,9 +321,6 @@
.mt-10 {
margin-top: calc(var(--spacing) * 10);
}
.mt-20 {
margin-top: calc(var(--spacing) * 20);
}
.mr-2 {
margin-right: calc(var(--spacing) * 2);
}
@ -368,8 +348,8 @@
.mb-12 {
margin-bottom: calc(var(--spacing) * 12);
}
.ml-3 {
margin-left: calc(var(--spacing) * 3);
.ml-2 {
margin-left: calc(var(--spacing) * 2);
}
.block {
display: block;
@ -389,9 +369,6 @@
.inline-flex {
display: inline-flex;
}
.table {
display: table;
}
.h-4 {
height: calc(var(--spacing) * 4);
}
@ -452,9 +429,6 @@
.w-24 {
width: calc(var(--spacing) * 24);
}
.w-96 {
width: calc(var(--spacing) * 96);
}
.w-\[800px\] {
width: 800px;
}
@ -476,9 +450,6 @@
.max-w-4xl {
max-width: var(--container-4xl);
}
.max-w-5xl {
max-width: var(--container-5xl);
}
.max-w-7xl {
max-width: var(--container-7xl);
}
@ -491,15 +462,9 @@
.flex-1 {
flex: 1;
}
.flex-shrink {
flex-shrink: 1;
}
.flex-shrink-0 {
flex-shrink: 0;
}
.border-collapse {
border-collapse: collapse;
}
.-translate-x-1\/2 {
--tw-translate-x: calc(calc(1/2 * 100%) * -1);
translate: var(--tw-translate-x) var(--tw-translate-y);
@ -510,9 +475,6 @@
.animate-spin {
animation: var(--animate-spin);
}
.resize {
resize: both;
}
.appearance-none {
appearance: none;
}
@ -540,9 +502,6 @@
.gap-6 {
gap: calc(var(--spacing) * 6);
}
.gap-8 {
gap: calc(var(--spacing) * 8);
}
.space-y-1 {
:where(& > :not(:last-child)) {
--tw-space-y-reverse: 0;
@ -630,10 +589,6 @@
border-style: var(--tw-border-style);
border-width: 1px;
}
.border-2 {
border-style: var(--tw-border-style);
border-width: 2px;
}
.border-t {
border-top-style: var(--tw-border-style);
border-top-width: 1px;
@ -646,22 +601,12 @@
border-bottom-style: var(--tw-border-style);
border-bottom-width: 2px;
}
.border-l-4 {
border-left-style: var(--tw-border-style);
border-left-width: 4px;
}
.border-blue-200 {
border-color: var(--color-blue-200);
}
.border-gray-200 {
border-color: var(--color-gray-200);
}
.border-gray-300 {
border-color: var(--color-gray-300);
}
.border-green-500 {
border-color: var(--color-green-500);
}
.border-indigo-500 {
border-color: var(--color-indigo-500);
}
@ -710,15 +655,6 @@
background-color: color-mix(in oklab, var(--color-black) 30%, transparent);
}
}
.bg-blue-50 {
background-color: var(--color-blue-50);
}
.bg-blue-500 {
background-color: var(--color-blue-500);
}
.bg-blue-600 {
background-color: var(--color-blue-600);
}
.bg-emerald-500\/20 {
background-color: color-mix(in srgb, oklch(69.6% 0.17 162.48) 20%, transparent);
@supports (color: color-mix(in lab, red, red)) {
@ -731,12 +667,6 @@
.bg-gray-50 {
background-color: var(--color-gray-50);
}
.bg-gray-100 {
background-color: var(--color-gray-100);
}
.bg-gray-500 {
background-color: var(--color-gray-500);
}
.bg-green-500\/20 {
background-color: color-mix(in srgb, oklch(72.3% 0.219 149.579) 20%, transparent);
@supports (color: color-mix(in lab, red, red)) {
@ -770,9 +700,6 @@
background-color: color-mix(in oklab, var(--color-indigo-900) 10%, transparent);
}
}
.bg-purple-500 {
background-color: var(--color-purple-500);
}
.bg-purple-600\/10 {
background-color: color-mix(in srgb, oklch(55.8% 0.288 302.321) 10%, transparent);
@supports (color: color-mix(in lab, red, red)) {
@ -803,6 +730,9 @@
.bg-slate-50 {
background-color: var(--color-slate-50);
}
.bg-slate-600 {
background-color: var(--color-slate-600);
}
.bg-slate-700\/30 {
background-color: color-mix(in srgb, oklch(37.2% 0.044 257.287) 30%, transparent);
@supports (color: color-mix(in lab, red, red)) {
@ -966,15 +896,9 @@
.py-12 {
padding-block: calc(var(--spacing) * 12);
}
.py-16 {
padding-block: calc(var(--spacing) * 16);
}
.py-24 {
padding-block: calc(var(--spacing) * 24);
}
.pt-4 {
padding-top: calc(var(--spacing) * 4);
}
.pt-6 {
padding-top: calc(var(--spacing) * 6);
}
@ -994,10 +918,6 @@
font-size: var(--text-2xl);
line-height: var(--tw-leading, var(--text-2xl--line-height));
}
.text-3xl {
font-size: var(--text-3xl);
line-height: var(--tw-leading, var(--text-3xl--line-height));
}
.text-4xl {
font-size: var(--text-4xl);
line-height: var(--tw-leading, var(--text-4xl--line-height));
@ -1056,15 +976,9 @@
.whitespace-nowrap {
white-space: nowrap;
}
.text-blue-900 {
color: var(--color-blue-900);
}
.text-emerald-400 {
color: var(--color-emerald-400);
}
.text-gray-400 {
color: var(--color-gray-400);
}
.text-gray-500 {
color: var(--color-gray-500);
}
@ -1080,9 +994,6 @@
.text-green-400 {
color: var(--color-green-400);
}
.text-green-500 {
color: var(--color-green-500);
}
.text-green-600 {
color: var(--color-green-600);
}
@ -1107,18 +1018,12 @@
.text-red-400 {
color: var(--color-red-400);
}
.text-red-500 {
color: var(--color-red-500);
}
.text-red-600 {
color: var(--color-red-600);
}
.text-red-700 {
color: var(--color-red-700);
}
.text-red-800 {
color: var(--color-red-800);
}
.text-slate-50 {
color: var(--color-slate-50);
}
@ -1143,9 +1048,6 @@
.uppercase {
text-transform: uppercase;
}
.underline {
text-decoration-line: underline;
}
.placeholder-gray-500 {
&::placeholder {
color: var(--color-gray-500);
@ -1160,10 +1062,6 @@
--tw-shadow: 0 10px 15px -3px var(--tw-shadow-color, rgb(0 0 0 / 0.1)), 0 4px 6px -4px var(--tw-shadow-color, rgb(0 0 0 / 0.1));
box-shadow: var(--tw-inset-shadow), var(--tw-inset-ring-shadow), var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow);
}
.shadow-md {
--tw-shadow: 0 4px 6px -1px var(--tw-shadow-color, rgb(0 0 0 / 0.1)), 0 2px 4px -2px var(--tw-shadow-color, rgb(0 0 0 / 0.1));
box-shadow: var(--tw-inset-shadow), var(--tw-inset-ring-shadow), var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow);
}
.shadow-sm {
--tw-shadow: 0 1px 3px 0 var(--tw-shadow-color, rgb(0 0 0 / 0.1)), 0 1px 2px -1px var(--tw-shadow-color, rgb(0 0 0 / 0.1));
box-shadow: var(--tw-inset-shadow), var(--tw-inset-ring-shadow), var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow);
@ -1197,10 +1095,6 @@
.ring-slate-700 {
--tw-ring-color: var(--color-slate-700);
}
.outline {
outline-style: var(--tw-outline-style);
outline-width: 1px;
}
.blur-3xl {
--tw-blur: blur(var(--blur-3xl));
filter: var(--tw-blur,) var(--tw-brightness,) var(--tw-contrast,) var(--tw-grayscale,) var(--tw-hue-rotate,) var(--tw-invert,) var(--tw-saturate,) var(--tw-sepia,) var(--tw-drop-shadow,);
@ -1215,11 +1109,6 @@
-webkit-backdrop-filter: var(--tw-backdrop-blur,) var(--tw-backdrop-brightness,) var(--tw-backdrop-contrast,) var(--tw-backdrop-grayscale,) var(--tw-backdrop-hue-rotate,) var(--tw-backdrop-invert,) var(--tw-backdrop-opacity,) var(--tw-backdrop-saturate,) var(--tw-backdrop-sepia,);
backdrop-filter: var(--tw-backdrop-blur,) var(--tw-backdrop-brightness,) var(--tw-backdrop-contrast,) var(--tw-backdrop-grayscale,) var(--tw-backdrop-hue-rotate,) var(--tw-backdrop-invert,) var(--tw-backdrop-opacity,) var(--tw-backdrop-saturate,) var(--tw-backdrop-sepia,);
}
.transition {
transition-property: color, background-color, border-color, outline-color, text-decoration-color, fill, stroke, --tw-gradient-from, --tw-gradient-via, --tw-gradient-to, opacity, box-shadow, transform, translate, scale, rotate, filter, -webkit-backdrop-filter, backdrop-filter, display, visibility, content-visibility, overlay, pointer-events;
transition-timing-function: var(--tw-ease, var(--default-transition-timing-function));
transition-duration: var(--tw-duration, var(--default-transition-duration));
}
.transition-all {
transition-property: all;
transition-timing-function: var(--tw-ease, var(--default-transition-timing-function));
@ -1230,11 +1119,6 @@
transition-timing-function: var(--tw-ease, var(--default-transition-timing-function));
transition-duration: var(--tw-duration, var(--default-transition-duration));
}
.transition-shadow {
transition-property: box-shadow;
transition-timing-function: var(--tw-ease, var(--default-transition-timing-function));
transition-duration: var(--tw-duration, var(--default-transition-duration));
}
.outline-none {
--tw-outline-style: none;
outline-style: none;
@ -1282,13 +1166,6 @@
}
}
}
.hover\:bg-blue-700 {
&:hover {
@media (hover: hover) {
background-color: var(--color-blue-700);
}
}
}
.hover\:bg-emerald-500 {
&:hover {
@media (hover: hover) {
@ -1296,20 +1173,6 @@
}
}
}
.hover\:bg-gray-50 {
&:hover {
@media (hover: hover) {
background-color: var(--color-gray-50);
}
}
}
.hover\:bg-gray-600 {
&:hover {
@media (hover: hover) {
background-color: var(--color-gray-600);
}
}
}
.hover\:bg-green-500\/30 {
&:hover {
@media (hover: hover) {
@ -1327,13 +1190,6 @@
}
}
}
.hover\:bg-indigo-50 {
&:hover {
@media (hover: hover) {
background-color: var(--color-indigo-50);
}
}
}
.hover\:bg-indigo-500 {
&:hover {
@media (hover: hover) {
@ -1348,10 +1204,10 @@
}
}
}
.hover\:bg-purple-600 {
.hover\:bg-red-500 {
&:hover {
@media (hover: hover) {
background-color: var(--color-purple-600);
background-color: var(--color-red-500);
}
}
}
@ -1365,13 +1221,6 @@
}
}
}
.hover\:bg-red-700 {
&:hover {
@media (hover: hover) {
background-color: var(--color-red-700);
}
}
}
.hover\:bg-slate-200 {
&:hover {
@media (hover: hover) {
@ -1448,20 +1297,6 @@
}
}
}
.hover\:text-gray-700 {
&:hover {
@media (hover: hover) {
color: var(--color-gray-700);
}
}
}
.hover\:text-gray-900 {
&:hover {
@media (hover: hover) {
color: var(--color-gray-900);
}
}
}
.hover\:text-indigo-300 {
&:hover {
@media (hover: hover) {
@ -1476,13 +1311,6 @@
}
}
}
.hover\:text-indigo-500 {
&:hover {
@media (hover: hover) {
color: var(--color-indigo-500);
}
}
}
.hover\:text-red-300 {
&:hover {
@media (hover: hover) {
@ -1511,14 +1339,6 @@
}
}
}
.hover\:shadow-lg {
&:hover {
@media (hover: hover) {
--tw-shadow: 0 10px 15px -3px var(--tw-shadow-color, rgb(0 0 0 / 0.1)), 0 4px 6px -4px var(--tw-shadow-color, rgb(0 0 0 / 0.1));
box-shadow: var(--tw-inset-shadow), var(--tw-inset-ring-shadow), var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow);
}
}
}
.hover\:shadow-xl {
&:hover {
@media (hover: hover) {
@ -1654,11 +1474,6 @@
display: flex;
}
}
.sm\:flex-row {
@media (width >= 40rem) {
flex-direction: row;
}
}
.sm\:space-x-8 {
@media (width >= 40rem) {
:where(& > :not(:last-child)) {
@ -1695,11 +1510,6 @@
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
.md\:grid-cols-3 {
@media (width >= 48rem) {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
}
.md\:flex-row {
@media (width >= 48rem) {
flex-direction: row;
@ -1715,12 +1525,6 @@
justify-content: space-between;
}
}
.md\:text-6xl {
@media (width >= 48rem) {
font-size: var(--text-6xl);
line-height: var(--tw-leading, var(--text-6xl--line-height));
}
}
.lg\:grid-cols-3 {
@media (width >= 64rem) {
grid-template-columns: repeat(3, minmax(0, 1fr));
@ -1902,11 +1706,6 @@
inherits: false;
initial-value: 0 0 #0000;
}
@property --tw-outline-style {
syntax: "*";
inherits: false;
initial-value: solid;
}
@property --tw-blur {
syntax: "*";
inherits: false;
@ -1996,6 +1795,11 @@
syntax: "*";
inherits: false;
}
@property --tw-outline-style {
syntax: "*";
inherits: false;
initial-value: solid;
}
@keyframes spin {
to {
transform: rotate(360deg);
@ -2040,7 +1844,6 @@
--tw-ring-offset-width: 0px;
--tw-ring-offset-color: #fff;
--tw-ring-offset-shadow: 0 0 #0000;
--tw-outline-style: solid;
--tw-blur: initial;
--tw-brightness: initial;
--tw-contrast: initial;
@ -2063,6 +1866,7 @@
--tw-backdrop-opacity: initial;
--tw-backdrop-saturate: initial;
--tw-backdrop-sepia: initial;
--tw-outline-style: solid;
}
}
}

View File

@ -1,6 +1,21 @@
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
#[sea_orm(rs_type = "String", db_type = "String(StringLen::None)")]
pub enum Role {
#[sea_orm(string_value = "admin")]
Admin,
#[sea_orm(string_value = "member")]
Member,
}
impl Default for Role {
fn default() -> Self {
Role::Member
}
}
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "participants")]
pub struct Model {
@ -10,6 +25,7 @@ pub struct Model {
pub name: String,
pub email: Option<String>,
pub user_id: Option<i32>,
pub role: Role,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
@ -45,3 +61,27 @@ impl Related<super::users::Entity> for Entity {
}
impl ActiveModelBehavior for ActiveModel {}
// Helper functions for role management
/// Check if a user is an admin for a specific event
pub async fn is_event_admin(user_id: i32, event_id: i32) -> Result<bool, DbErr> {
let participant = Entity::find()
.filter(Column::EventId.eq(event_id))
.filter(Column::UserId.eq(user_id))
.one(&crate::db::establish_connection().await?)
.await?;
Ok(participant.map_or(false, |p| p.role == Role::Admin))
}
/// Get a user's role in an event
pub async fn get_user_role_in_event(user_id: i32, event_id: i32) -> Result<Option<Role>, DbErr> {
let participant = Entity::find()
.filter(Column::EventId.eq(event_id))
.filter(Column::UserId.eq(user_id))
.one(&crate::db::establish_connection().await?)
.await?;
Ok(participant.map(|p| p.role))
}

View File

@ -6,7 +6,7 @@ mod server_fns;
mod views;
#[cfg(feature = "server")]
mod db;
pub mod db;
use components::navbar::Navbar;
use views::{

View File

@ -1,5 +1,6 @@
#[cfg(feature = "server")]
use crate::db::{self, assignments, events, exclusions, participants};
#[cfg(feature = "server")]
use dioxus::prelude::dioxus_fullstack;
use dioxus::prelude::server;
use dioxus::prelude::ServerFnError;
@ -44,6 +45,12 @@ pub async fn create_event(
.map(char::from)
.collect();
// Start a transaction to ensure both event and participant are created together
let txn = db
.begin()
.await
.map_err(|e| ServerFnError::new(e.to_string()))?;
let new_event = events::ActiveModel {
name: Set(name),
date: Set(date),
@ -53,10 +60,41 @@ pub async fn create_event(
..Default::default()
};
let event = new_event
.insert(&db)
.await
.map_err(|e| ServerFnError::new(e.to_string()))?;
let event = match new_event.insert(&txn).await {
Ok(e) => e,
Err(e) => {
return Err(ServerFnError::new(e.to_string()));
}
};
// Get the current user's name to create the participant record
let user = match db::users::Entity::find_by_id(user_id).one(&txn).await {
Ok(Some(u)) => u,
Ok(None) => {
return Err(ServerFnError::new("User not found"));
}
Err(e) => {
return Err(ServerFnError::new(e.to_string()));
}
};
// Add the creator as an admin participant
let new_participant = participants::ActiveModel {
event_id: Set(event.id),
name: Set(user.username),
user_id: Set(Some(user_id)),
role: Set(participants::Role::Admin),
..Default::default()
};
if let Err(e) = new_participant.insert(&txn).await {
return Err(ServerFnError::new(e.to_string()));
}
// Commit the transaction
if let Err(e) = txn.commit().await {
return Err(ServerFnError::new(e.to_string()));
}
Ok(EventSafe {
id: event.id,
@ -125,6 +163,7 @@ pub async fn join_event(token: String, name: String) -> Result<(), ServerFnError
event_id: Set(event.id),
name: Set(name),
user_id: Set(Some(user_id)),
role: Set(participants::Role::Member),
..Default::default()
};
@ -206,6 +245,10 @@ pub struct ParticipantSafe {
pub id: i32,
pub name: String,
pub email: Option<String>,
#[cfg(feature = "server")]
pub role: participants::Role,
#[cfg(not(feature = "server"))]
pub role: String,
}
#[server]
@ -232,14 +275,18 @@ pub async fn add_participant(
.map_err(|e| ServerFnError::new(e.to_string()))?
.ok_or(ServerFnError::new("Event not found"))?;
if event.creator_id != user_id {
return Err(ServerFnError::new("Not authorized"));
if !db::participants::is_event_admin(user_id, event_id)
.await
.map_err(|e| ServerFnError::new(e.to_string()))?
{
return Err(ServerFnError::new("Only event admins can add participants"));
}
let new_participant = participants::ActiveModel {
event_id: Set(event_id),
name: Set(name),
email: Set(email),
role: Set(participants::Role::Member),
..Default::default()
};
@ -252,6 +299,10 @@ pub async fn add_participant(
id: participant.id,
name: participant.name,
email: participant.email,
#[cfg(feature = "server")]
role: participant.role,
#[cfg(not(feature = "server"))]
role: format!("{:?}", participant.role),
})
}
@ -300,6 +351,10 @@ pub async fn get_participants(event_id: i32) -> Result<Vec<ParticipantSafe>, Ser
id: p.id,
name: p.name,
email: p.email,
#[cfg(feature = "server")]
role: p.role,
#[cfg(not(feature = "server"))]
role: format!("{:?}", p.role),
})
.collect())
}
@ -345,8 +400,11 @@ pub async fn draw_names(event_id: i32) -> Result<(), ServerFnError> {
.map_err(|e| ServerFnError::new(e.to_string()))?
.ok_or(ServerFnError::new("Event not found"))?;
if event.creator_id != user_id {
return Err(ServerFnError::new("Not authorized"));
if !db::participants::is_event_admin(user_id, event_id)
.await
.map_err(|e| ServerFnError::new(e.to_string()))?
{
return Err(ServerFnError::new("Only event admins can draw names"));
}
let participants = participants::Entity::find()
@ -466,8 +524,13 @@ pub async fn remove_participant(participant_id: i32) -> Result<(), ServerFnError
.map_err(|e| ServerFnError::new(e.to_string()))?
.ok_or(ServerFnError::new("Event not found"))?;
if event.creator_id != user_id {
return Err(ServerFnError::new("Not authorized"));
if !db::participants::is_event_admin(user_id, participant.event_id)
.await
.map_err(|e| ServerFnError::new(e.to_string()))?
{
return Err(ServerFnError::new(
"Only event admins can remove participants",
));
}
// Delete the participant
@ -479,6 +542,140 @@ pub async fn remove_participant(participant_id: i32) -> Result<(), ServerFnError
Ok(())
}
#[server]
pub async fn get_current_user_id() -> Result<i32, ServerFnError> {
let session: tower_sessions::Session = FullstackContext::extract().await?;
let user_id: i32 = session
.get("user_id")
.await
.map_err(|e| ServerFnError::new(e.to_string()))?
.ok_or(ServerFnError::new("Not logged in"))?;
Ok(user_id)
}
#[server]
pub async fn get_user_role_in_event(event_id: i32) -> Result<Option<String>, ServerFnError> {
let session: tower_sessions::Session = FullstackContext::extract().await?;
let user_id: i32 = session
.get("user_id")
.await
.map_err(|e| ServerFnError::new(e.to_string()))?
.ok_or(ServerFnError::new("Not logged in"))?;
let db = db::establish_connection()
.await
.map_err(|e| ServerFnError::new(e.to_string()))?;
let role = db::participants::get_user_role_in_event(user_id, event_id)
.await
.map_err(|e| ServerFnError::new(e.to_string()))?;
Ok(role.map(|r| match r {
db::participants::Role::Admin => "Admin".to_string(),
db::participants::Role::Member => "Member".to_string(),
}))
}
#[server]
pub async fn change_participant_role(
participant_id: i32,
event_id: i32,
) -> Result<(), ServerFnError> {
let session: tower_sessions::Session = FullstackContext::extract().await?;
let user_id: i32 = session
.get("user_id")
.await
.map_err(|e| ServerFnError::new(e.to_string()))?
.ok_or(ServerFnError::new("Not logged in"))?;
let db = db::establish_connection()
.await
.map_err(|e| ServerFnError::new(e.to_string()))?;
// Check if user is an event admin
if !db::participants::is_event_admin(user_id, event_id)
.await
.map_err(|e| ServerFnError::new(e.to_string()))?
{
return Err(ServerFnError::new(
"Only event admins can change participant roles",
));
}
// Find the participant
let participant = db::participants::Entity::find_by_id(participant_id)
.one(&db)
.await
.map_err(|e| ServerFnError::new(e.to_string()))?
.ok_or(ServerFnError::new("Participant not found"))?;
// Verify the participant belongs to the specified event
if participant.event_id != event_id {
return Err(ServerFnError::new(
"Participant does not belong to this event",
));
}
// Update the participant's role to Admin
let mut participant: db::participants::ActiveModel = participant.into();
participant.role = Set(db::participants::Role::Admin);
participant
.update(&db)
.await
.map_err(|e| ServerFnError::new(e.to_string()))?;
Ok(())
}
#[server]
pub async fn remove_admin_role(participant_id: i32, event_id: i32) -> Result<(), ServerFnError> {
let session: tower_sessions::Session = FullstackContext::extract().await?;
let user_id: i32 = session
.get("user_id")
.await
.map_err(|e| ServerFnError::new(e.to_string()))?
.ok_or(ServerFnError::new("Not logged in"))?;
let db = db::establish_connection()
.await
.map_err(|e| ServerFnError::new(e.to_string()))?;
// Check if user is an event admin
if !db::participants::is_event_admin(user_id, event_id)
.await
.map_err(|e| ServerFnError::new(e.to_string()))?
{
return Err(ServerFnError::new(
"Only event admins can change participant roles",
));
}
// Find the participant
let participant = db::participants::Entity::find_by_id(participant_id)
.one(&db)
.await
.map_err(|e| ServerFnError::new(e.to_string()))?
.ok_or(ServerFnError::new("Participant not found"))?;
// Verify the participant belongs to the specified event
if participant.event_id != event_id {
return Err(ServerFnError::new(
"Participant does not belong to this event",
));
}
// Update the participant's role to Member
let mut participant: db::participants::ActiveModel = participant.into();
participant.role = Set(db::participants::Role::Member);
participant
.update(&db)
.await
.map_err(|e| ServerFnError::new(e.to_string()))?;
Ok(())
}
#[server]
pub async fn update_event_budget(event_id: i32, new_budget: String) -> Result<(), ServerFnError> {
let session: tower_sessions::Session = FullstackContext::extract().await?;
@ -499,8 +696,13 @@ pub async fn update_event_budget(event_id: i32, new_budget: String) -> Result<()
.map_err(|e| ServerFnError::new(e.to_string()))?
.ok_or(ServerFnError::new("Event not found"))?;
if event.creator_id != user_id {
return Err(ServerFnError::new("Not authorized"));
if !db::participants::is_event_admin(user_id, event_id)
.await
.map_err(|e| ServerFnError::new(e.to_string()))?
{
return Err(ServerFnError::new(
"Only event admins can update event budget",
));
}
// Update the budget

View File

@ -1,3 +1,5 @@
#[cfg(feature = "server")]
use crate::db;
use crate::server_fns;
use crate::Route;
use dioxus::prelude::*;
@ -8,17 +10,11 @@ pub fn EventPage(id: i32) -> Element {
use_resource(move || async move { server_fns::get_participants(id).await });
let mut new_participant_name = use_signal(|| String::new());
let mut new_participant_email = use_signal(|| String::new());
let mut event_details = use_resource(move || async move {
// We need a way to get event details by ID, reusing get_my_events for now is inefficient but works if creator
// Better: add get_event(id) server fn. For now, let's just show the ID or fetch via participants if needed.
// Actually, let's just display the invite link.
// We can construct the invite link from the ID if we had the token.
// Let's fetch the event details to get the token.
// Wait, we don't have a get_event(id) that returns the token.
// Let's assume the user is the creator and use get_my_events to find it.
server_fns::get_my_events().await
});
let mut is_creator = use_signal(|| false);
let mut event_details = use_resource(move || async move { server_fns::get_my_events().await });
let mut user_role =
use_resource(move || async move { server_fns::get_user_role_in_event(id).await });
let mut current_user_id = use_signal(|| None::<i32>);
let mut is_admin = use_signal(|| false);
let mut edit_budget = use_signal(|| false);
let mut edit_date = use_signal(|| false);
let mut new_budget = use_signal(|| String::new());
@ -28,7 +24,6 @@ pub fn EventPage(id: i32) -> Element {
let invite_link = use_memo(move || {
if let Some(Ok(events)) = event_details.read().as_ref() {
if let Some(event) = events.iter().find(|e| e.id == id) {
is_creator.set(true);
new_budget.set(event.budget.clone());
new_date.set(event.date.clone());
return Some(format!("http://localhost:8080/join/{}", event.invite_token));
@ -37,6 +32,23 @@ pub fn EventPage(id: i32) -> Element {
None
});
// Update admin status and current user ID based on user role
use_effect(move || {
if let Some(Ok(role)) = user_role.read().as_ref() {
is_admin.set(role.as_ref().map_or(false, |r| r == "Admin"));
}
});
// Get current user ID for self-check in role management
let mut user_id_resource =
use_resource(move || async move { server_fns::get_current_user_id().await });
use_effect(move || {
if let Some(Ok(user_id)) = user_id_resource.read().as_ref() {
current_user_id.set(Some(*user_id));
}
});
rsx! {
div {
class: "min-h-full py-12 px-4 sm:px-6 lg:px-8",
@ -72,8 +84,8 @@ pub fn EventPage(id: i32) -> Element {
}
}
// Administrative Controls (only for creator)
if is_creator() {
// Administrative Controls (only for admins)
if is_admin() {
div {
class: "mb-8 bg-indigo-900/10 border border-indigo-500/20 rounded-2xl p-6 backdrop-blur-sm",
div {
@ -218,7 +230,11 @@ pub fn EventPage(id: i32) -> Element {
}
button {
class: "bg-indigo-600 hover:bg-indigo-500 text-white px-6 py-3 rounded-xl font-medium shadow-lg shadow-indigo-600/20 transition-all",
disabled: !is_admin(),
onclick: move |_| async move {
if !is_admin() {
return;
}
let email = if new_participant_email().is_empty() { None } else { Some(new_participant_email()) };
if server_fns::add_participant(id, new_participant_name(), email).await.is_ok() {
participants.restart();
@ -231,13 +247,20 @@ pub fn EventPage(id: i32) -> Element {
}
// List
match participants().as_ref() {
Some(Ok(list)) => rsx! {
{
let participants_list = participants();
match participants_list.as_ref() {
Some(Ok(list)) => {
let list = list.clone();
rsx! {
div {
class: "space-y-3",
for p in list {
{
let p_id = p.id;
rsx! {
div {
key: "{p.id}",
key: "{p_id}",
class: "group flex items-center justify-between p-4 bg-slate-700/30 border border-white/5 rounded-xl hover:bg-slate-700/50 transition-all",
div {
class: "flex items-center gap-4",
@ -250,20 +273,96 @@ pub fn EventPage(id: i32) -> Element {
if let Some(email) = &p.email {
p { class: "text-sm text-slate-400", "{email}" }
}
// Role badge
{
#[cfg(feature = "server")]
let role_str = match p.role {
crate::db::participants::Role::Admin => "Admin",
crate::db::participants::Role::Member => "Member",
};
#[cfg(not(feature = "server"))]
let role_str = p.role.as_str();
if role_str == "Admin" {
rsx! { span { class: "ml-2 px-2 py-1 text-xs font-semibold bg-indigo-600 text-white rounded-full", "Admin" } }
} else {
rsx! { span { class: "ml-2 px-2 py-1 text-xs font-semibold bg-slate-600 text-white rounded-full", "Member" } }
}
}
}
}
div {
class: "flex items-center gap-2",
// Role management buttons (only for admins)
if is_admin() {
// Show different buttons based on current role
{
#[cfg(feature = "server")]
let role_str = match p.role {
crate::db::participants::Role::Admin => "Admin",
crate::db::participants::Role::Member => "Member",
};
#[cfg(not(feature = "server"))]
let role_str = p.role.as_str();
if role_str == "Member" {
rsx! {
button {
class: "text-xs px-3 py-1 bg-indigo-600 hover:bg-indigo-500 text-white rounded transition-colors",
onclick: move |_| async move {
if server_fns::change_participant_role(p_id, id).await.is_ok() {
participants.restart();
}
},
"Make Admin"
}
}
} else if role_str == "Admin" {
rsx! {
button {
class: "text-xs px-3 py-1 bg-red-600 hover:bg-red-500 text-white rounded transition-colors",
onclick: move |_| async move {
if server_fns::remove_admin_role(p_id, id).await.is_ok() {
participants.restart();
}
},
"Remove Admin"
}
}
} else {
rsx! {}
}
}
}
// Remove participant button (only for admins)
if is_admin() {
button {
class: "p-2 text-red-400 hover:text-red-300 transition-colors",
onclick: move |_| async move {
if server_fns::remove_participant(p_id).await.is_ok() {
participants.restart();
}
},
svg { class: "w-4 h-4", fill: "none", stroke: "currentColor", view_box: "0 0 24 24", path { stroke_linecap: "round", stroke_linejoin: "round", stroke_width: "2", d: "M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" } }
}
}
}
}
}
}
}
}
} },
Some(Err(e)) => rsx! { p { class: "text-red-400", "Error: {e}" } },
None => rsx! {
div { class: "flex justify-center py-8", div { class: "animate-spin rounded-full h-8 w-8 border-b-2 border-indigo-500" } }
}
}
}
}
// Draw Button
// Draw Button (only for admins)
if is_admin() {
button {
class: "w-full py-4 bg-gradient-to-r from-purple-600 to-pink-600 hover:from-purple-500 hover:to-pink-500 text-white text-lg font-bold rounded-2xl shadow-lg shadow-purple-600/20 hover:shadow-purple-600/40 transform hover:-translate-y-0.5 transition-all",
onclick: move |_| async move {
@ -278,4 +377,5 @@ pub fn EventPage(id: i32) -> Element {
}
}
}
}
}

BIN
wichtl.db

Binary file not shown.