rustpbx 0.4.9

A SIP PBX implementation in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
use sea_orm::Set;
use sea_orm::entity::prelude::*;
use sea_orm_migration::prelude::*;
use sea_orm_migration::schema::{
    big_integer, boolean, string_len, text_null, timestamp_with_time_zone as timestamp,
};
use sea_orm_migration::sea_query::{ColumnDef, ForeignKeyAction};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "rustpbx_roles")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = true)]
    pub id: i64,
    #[sea_orm(unique)]
    pub name: String,
    #[sea_orm(nullable)]
    pub description: Option<String>,
    pub is_system: bool,
    pub created_at: DateTimeUtc,
    pub updated_at: DateTimeUtc,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(has_many = "role_permission::Entity")]
    RolePermissions,
    #[sea_orm(has_many = "user_role::Entity")]
    UserRoles,
}

impl Related<role_permission::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::RolePermissions.def()
    }
}

impl Related<user_role::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::UserRoles.def()
    }
}

impl ActiveModelBehavior for ActiveModel {}

pub mod role_permission {
    use sea_orm::entity::prelude::*;
    use serde::{Deserialize, Serialize};

    #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
    #[sea_orm(table_name = "rustpbx_role_permissions")]
    pub struct Model {
        #[sea_orm(primary_key, auto_increment = true)]
        pub id: i64,
        pub role_id: i64,
        pub resource: String,
        pub action: String,
    }

    #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
    pub enum Relation {
        #[sea_orm(
            belongs_to = "super::Entity",
            from = "Column::RoleId",
            to = "super::Column::Id"
        )]
        Role,
    }

    impl Related<super::Entity> for Entity {
        fn to() -> RelationDef {
            Relation::Role.def()
        }
    }

    impl ActiveModelBehavior for ActiveModel {}
}

pub mod user_role {
    use chrono::Utc;
    use sea_orm::ActiveValue::Set;
    use sea_orm::entity::prelude::*;
    use serde::{Deserialize, Serialize};

    #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
    #[sea_orm(table_name = "rustpbx_user_roles")]
    pub struct Model {
        #[sea_orm(primary_key, auto_increment = true)]
        pub id: i64,
        pub user_id: i64,
        pub role_id: i64,
        pub created_at: DateTimeUtc,
    }

    impl Model {
        #[allow(clippy::new_ret_no_self)]
        pub fn new(user_id: i64, role_id: i64) -> ActiveModel {
            ActiveModel {
                user_id: Set(user_id),
                role_id: Set(role_id),
                created_at: Set(Utc::now()),
                ..Default::default()
            }
        }
    }

    #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
    pub enum Relation {
        #[sea_orm(
            belongs_to = "super::Entity",
            from = "Column::RoleId",
            to = "super::Column::Id"
        )]
        Role,
    }

    impl Related<super::Entity> for Entity {
        fn to() -> RelationDef {
            Relation::Role.def()
        }
    }

    impl ActiveModelBehavior for ActiveModel {}
}

pub const SYSTEM_ROLES: &[(&str, &str)] = &[
    ("superadmin", "Super administrator with all permissions"),
    ("admin", "System administrator for day-to-day management"),
    ("supervisor", "Call center supervisor monitoring agents"),
    ("operator", "Operator with view and call control access"),
    ("viewer", "Read-only auditor for compliance"),
    #[cfg(feature = "addon-ivr-editor")]
    ("ivr_editor", "IVR flow editor"),
    #[cfg(feature = "addon-cc")]
    (
        "cc_supervisor",
        "Contact Center Supervisor - can monitor agents and view reports",
    ),
    #[cfg(feature = "addon-cc")]
    (
        "cc_manager",
        "Contact Center Manager - can manage agents, skill groups, and monitor",
    ),
    #[cfg(feature = "addon-wholesale")]
    ("wholesale_admin", "Wholesale tenant and billing manager"),
    #[cfg(feature = "addon-wholesale")]
    (
        "wholesale_finance",
        "Wholesale finance role - read all data including costs, no writes",
    ),
    #[cfg(feature = "addon-wholesale")]
    (
        "wholesale_sales",
        "Wholesale sales role - read assigned tenants only",
    ),
];

const ROLE_PERMISSIONS: &[(&str, &[(&str, &str)])] = &[
    (
        "superadmin",
        &[
            ("system", "read"),
            ("system", "write"),
            ("users", "manage"),
            ("departments", "write"),
            ("extensions", "read"),
            ("extensions", "write"),
            ("extensions", "delete"),
            ("trunks", "read"),
            ("trunks", "write"),
            ("routes", "read"),
            ("routes", "write"),
            ("queues", "read"),
            ("queues", "write"),
            ("queues", "realtime"),
            ("ivr", "read"),
            ("ivr", "write"),
            ("ivr", "publish"),
            ("cdr", "read"),
            ("cdr", "read:recording"),
            ("cdr", "delete"),
            ("cdr", "export"),
            ("calls", "read"),
            ("calls", "control"),
            ("ami", "access"),
            ("voicemail", "read"),
            ("voicemail", "write"),
            ("voicemail", "settings"),
            ("endpoints", "read"),
            ("endpoints", "write"),
            ("endpoints", "reboot"),
            ("wholesale", "read"),
            ("wholesale", "write"),
            ("wholesale", "billing"),
            ("wholesale", "settings"),
            ("metrics", "read"),
            ("diagnostics", "read"),
        ],
    ),
    (
        "admin",
        &[
            ("system", "read"),
            ("users", "manage"),
            ("departments", "write"),
            ("extensions", "read"),
            ("extensions", "write"),
            ("extensions", "delete"),
            ("trunks", "read"),
            ("trunks", "write"),
            ("routes", "read"),
            ("routes", "write"),
            ("queues", "read"),
            ("queues", "write"),
            ("queues", "realtime"),
            ("ivr", "read"),
            ("ivr", "write"),
            ("ivr", "publish"),
            ("cdr", "read"),
            ("cdr", "read:recording"),
            ("cdr", "delete"),
            ("cdr", "export"),
            ("calls", "read"),
            ("calls", "control"),
            ("ami", "access"),
            ("voicemail", "read"),
            ("voicemail", "write"),
            ("voicemail", "settings"),
            ("endpoints", "read"),
            ("endpoints", "write"),
            ("endpoints", "reboot"),
            ("wholesale", "read"),
            ("wholesale", "write"),
            ("wholesale", "billing"),
            ("metrics", "read"),
            ("diagnostics", "read"),
        ],
    ),
    (
        "supervisor",
        &[
            ("extensions", "read"),
            ("queues", "read"),
            ("queues", "write"),
            ("queues", "realtime"),
            ("cdr", "read"),
            ("cdr", "read:recording"),
            ("cdr", "export"),
            ("calls", "read"),
            ("calls", "control"),
            ("voicemail", "read"),
        ],
    ),
    (
        "operator",
        &[
            ("extensions", "read"),
            ("queues", "read"),
            ("queues", "realtime"),
            ("calls", "read"),
            ("calls", "control"),
        ],
    ),
    (
        "viewer",
        &[
            ("extensions", "read"),
            ("trunks", "read"),
            ("routes", "read"),
            ("queues", "read"),
            ("ivr", "read"),
            ("cdr", "read"),
            ("cdr", "export"),
            ("voicemail", "read"),
            ("endpoints", "read"),
            ("wholesale", "read"),
        ],
    ),
    #[cfg(feature = "addon-ivr-editor")]
    ("ivr_editor", &[("ivr", "read"), ("ivr", "write")]),
    #[cfg(feature = "addon-cc")]
    (
        "cc_supervisor",
        &[
            ("cc_agent", "read"),
            ("cc_skill_group", "read"),
            ("cc_supervisor", "monitor"),
            ("cc_queue", "realtime"),
            ("cc_reports", "read"),
            ("cc_conference", "read"),
        ],
    ),
    #[cfg(feature = "addon-cc")]
    (
        "cc_manager",
        &[
            ("cc_agent", "read"),
            ("cc_agent", "write"),
            ("cc_skill_group", "read"),
            ("cc_skill_group", "write"),
            ("cc_supervisor", "monitor"),
            ("cc_supervisor", "takeover"),
            ("cc_queue", "realtime"),
            ("cc_reports", "read"),
            ("cc_transfer", "settings"),
            ("cc_conference", "read"),
            ("cc_conference", "write"),
        ],
    ),
    #[cfg(feature = "addon-wholesale")]
    (
        "wholesale_admin",
        &[
            ("wholesale", "read"),
            ("wholesale", "write"),
            ("wholesale", "billing"),
            ("wholesale", "settings"),
        ],
    ),
    #[cfg(feature = "addon-wholesale")]
    (
        "wholesale_finance",
        &[("wholesale", "read"), ("wholesale", "billing")],
    ),
    #[cfg(feature = "addon-wholesale")]
    ("wholesale_sales", &[("wholesale", "read")]),
];

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(Entity)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(Column::Id)
                            .big_integer()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(
                        ColumnDef::new(Column::Name)
                            .string_len(100)
                            .unique_key()
                            .not_null(),
                    )
                    .col(text_null(Column::Description))
                    .col(boolean(Column::IsSystem).default(false))
                    .col(timestamp(Column::CreatedAt).default(Expr::current_timestamp()))
                    .col(timestamp(Column::UpdatedAt).default(Expr::current_timestamp()))
                    .to_owned(),
            )
            .await?;

        manager
            .create_table(
                Table::create()
                    .table(role_permission::Entity)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(role_permission::Column::Id)
                            .big_integer()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(big_integer(role_permission::Column::RoleId))
                    .col(string_len(role_permission::Column::Resource, 100))
                    .col(string_len(role_permission::Column::Action, 100))
                    .foreign_key(
                        ForeignKey::create()
                            .from(role_permission::Entity, role_permission::Column::RoleId)
                            .to(Entity, Column::Id)
                            .on_delete(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_table(
                Table::create()
                    .table(user_role::Entity)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(user_role::Column::Id)
                            .big_integer()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(big_integer(user_role::Column::UserId))
                    .col(big_integer(user_role::Column::RoleId))
                    .col(timestamp(user_role::Column::CreatedAt).default(Expr::current_timestamp()))
                    .foreign_key(
                        ForeignKey::create()
                            .from(user_role::Entity, user_role::Column::RoleId)
                            .to(Entity, Column::Id)
                            .on_delete(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        let db = manager.get_connection();
        let now = chrono::Utc::now();

        for (name, description) in SYSTEM_ROLES {
            let role = ActiveModel {
                name: Set(ToString::to_string(name)),
                description: Set(Some(ToString::to_string(description))),
                is_system: Set(true),
                created_at: Set(now),
                updated_at: Set(now),
                ..Default::default()
            };
            let inserted = Entity::insert(role)
                .exec_with_returning(db)
                .await
                .map_err(|e| DbErr::Custom(format!("failed to seed role {}: {}", name, e)))?;

            for (resource, action) in ROLE_PERMISSIONS
                .iter()
                .find(|(rname, _)| *rname == *name)
                .map(|(_, perms)| *perms)
                .unwrap_or(&[])
            {
                let perm = role_permission::ActiveModel {
                    role_id: Set(inserted.id),
                    resource: Set(ToString::to_string(resource)),
                    action: Set(ToString::to_string(action)),
                    ..Default::default()
                };
                role_permission::Entity::insert(perm)
                    .exec(db)
                    .await
                    .map_err(|e| {
                        DbErr::Custom(format!(
                            "failed to seed permission {}:{} for role {}: {}",
                            resource, action, name, e
                        ))
                    })?;
            }
        }

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(user_role::Entity).to_owned())
            .await?;
        manager
            .drop_table(Table::drop().table(role_permission::Entity).to_owned())
            .await?;
        manager
            .drop_table(Table::drop().table(Entity).to_owned())
            .await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::migration::Migrator;
    use sea_orm::{Database, EntityTrait};
    use sea_orm_migration::MigratorTrait;

    async fn setup_db() -> sea_orm::DatabaseConnection {
        let db = Database::connect("sqlite::memory:")
            .await
            .expect("connect sqlite memory");
        Migrator::up(&db, None).await.expect("run migrations");
        db
    }

    #[tokio::test]
    async fn roles_are_seeded() {
        let db = setup_db().await;
        let roles = Entity::find().all(&db).await.expect("query roles");
        assert_eq!(roles.len(), SYSTEM_ROLES.len());
        let names: Vec<&str> = roles.iter().map(|r| r.name.as_str()).collect();
        for (name, _) in SYSTEM_ROLES {
            assert!(names.contains(name), "role {} not found", name);
        }
    }

    #[tokio::test]
    async fn roles_are_system() {
        let db = setup_db().await;
        let roles = Entity::find().all(&db).await.expect("query roles");
        for role in &roles {
            assert!(role.is_system, "role {} should be system", role.name);
        }
    }

    #[tokio::test]
    async fn superadmin_has_ami_access() {
        let db = setup_db().await;
        let roles = Entity::find().all(&db).await.expect("query roles");
        let superadmin = roles
            .iter()
            .find(|r| r.name == "superadmin")
            .expect("superadmin");
        let perms = role_permission::Entity::find()
            .filter(role_permission::Column::RoleId.eq(superadmin.id))
            .all(&db)
            .await
            .expect("query perms");
        let has_ami = perms
            .iter()
            .any(|p| p.resource == "ami" && p.action == "access");
        assert!(has_ami, "superadmin should have ami:access");
    }

    #[tokio::test]
    async fn viewer_has_no_write_permissions() {
        let db = setup_db().await;
        let roles = Entity::find().all(&db).await.expect("query roles");
        let viewer = roles.iter().find(|r| r.name == "viewer").expect("viewer");
        let perms = role_permission::Entity::find()
            .filter(role_permission::Column::RoleId.eq(viewer.id))
            .all(&db)
            .await
            .expect("query perms");
        let has_write = perms
            .iter()
            .any(|p| p.action == "write" || p.action == "delete" || p.action == "manage");
        assert!(
            !has_write,
            "viewer should not have write/delete/manage permissions"
        );
    }

    #[tokio::test]
    async fn admin_has_no_system_write() {
        let db = setup_db().await;
        let roles = Entity::find().all(&db).await.expect("query roles");
        let admin = roles.iter().find(|r| r.name == "admin").expect("admin");
        let perms = role_permission::Entity::find()
            .filter(role_permission::Column::RoleId.eq(admin.id))
            .all(&db)
            .await
            .expect("query perms");
        let has_system_write = perms
            .iter()
            .any(|p| p.resource == "system" && p.action == "write");
        assert!(!has_system_write, "admin should not have system:write");
    }

    #[tokio::test]
    async fn user_role_assignment() {
        let db = setup_db().await;
        let roles = Entity::find().all(&db).await.expect("query roles");
        let admin = roles.iter().find(|r| r.name == "admin").expect("admin");

        let assignment = user_role::ActiveModel {
            user_id: Set(42),
            role_id: Set(admin.id),
            created_at: Set(chrono::Utc::now()),
            ..Default::default()
        };
        user_role::Entity::insert(assignment)
            .exec(&db)
            .await
            .expect("insert user role");

        let found = user_role::Entity::find()
            .filter(user_role::Column::UserId.eq(42i64))
            .all(&db)
            .await
            .expect("query user roles");
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].role_id, admin.id);
    }
}