use chrono::{DateTime, Utc};
use rustango::sql::Auto;
use rustango::Model;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RestrictionKind {
Password,
Login,
Groups,
Permission,
}
impl RestrictionKind {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::Password => "password",
Self::Login => "login",
Self::Groups => "groups",
Self::Permission => "permission",
}
}
#[must_use]
pub fn parse(s: &str) -> Option<Self> {
Some(match s {
"password" => Self::Password,
"login" => Self::Login,
"groups" => Self::Groups,
"permission" => Self::Permission,
_ => return None,
})
}
}
#[derive(Debug, Clone)]
pub struct TypeViewRestriction {
pub kind: RestrictionKind,
pub codenames: Vec<String>,
}
impl TypeViewRestriction {
#[must_use]
pub fn login() -> Self {
Self {
kind: RestrictionKind::Login,
codenames: Vec::new(),
}
}
#[must_use]
pub fn permission<I, S>(codenames: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
Self {
kind: RestrictionKind::Permission,
codenames: codenames.into_iter().map(Into::into).collect(),
}
}
}
#[derive(Model, Debug, Clone, Serialize, Deserialize)]
#[rustango(
table = "cms_page_view_restriction",
app = "cms",
display = "page_id",
admin(
list_display = "page_id, kind, created_at",
ordering = "-created_at",
list_filter = "kind",
)
)]
pub struct PageViewRestriction {
#[rustango(primary_key)]
pub id: Auto<i64>,
#[rustango(fk = "cms_page", on = "id", index)]
pub page_id: i64,
#[rustango(max_length = 16, index)]
pub kind: String,
#[rustango(max_length = 255)]
pub password_hash: String,
pub group_ids: serde_json::Value,
#[rustango(default = "'[]'")]
pub codenames: serde_json::Value,
#[rustango(auto_now_add)]
pub created_at: Auto<DateTime<Utc>>,
#[rustango(auto_now)]
pub updated_at: Auto<DateTime<Utc>>,
}
impl PageViewRestriction {
#[must_use]
pub fn parsed_kind(&self) -> Option<RestrictionKind> {
RestrictionKind::parse(&self.kind)
}
#[must_use]
pub fn parsed_groups(&self) -> Vec<i64> {
self.group_ids
.as_array()
.map(|arr| arr.iter().filter_map(|v| v.as_i64()).collect::<Vec<_>>())
.unwrap_or_default()
}
#[must_use]
pub fn parsed_codenames(&self) -> Vec<String> {
self.codenames
.as_array()
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str().map(str::to_owned))
.collect::<Vec<_>>()
})
.unwrap_or_default()
}
}
pub async fn direct_for_page(
pool: &rustango::sql::Pool,
page_id: i64,
) -> Result<Option<PageViewRestriction>, rustango::sql::ExecError> {
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
let rows: Vec<PageViewRestriction> = PageViewRestriction::objects()
.where_(PageViewRestriction::page_id.eq(page_id))
.fetch(pool)
.await?;
Ok(rows.into_iter().next())
}
pub async fn effective_for_page(
pool: &rustango::sql::Pool,
page: &crate::page::Page,
) -> Result<Option<PageViewRestriction>, rustango::sql::ExecError> {
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
let own_id = page.id.get().copied().unwrap_or_default();
let ancestor_ids: Vec<i64> = if let Some(parent_id) = page.parent_id {
let ancestor_paths = crate::tree::MaterializedPath::ancestors_of(&page.path);
let ancestors: Vec<crate::page::Page> = if ancestor_paths.is_empty() {
Vec::new()
} else {
crate::page::Page::objects()
.where_(crate::page::Page::path.is_in(ancestor_paths.iter().cloned()))
.fetch(pool)
.await?
};
let mut by_id: std::collections::HashMap<i64, &crate::page::Page> = ancestors
.iter()
.filter_map(|a| a.id.get().copied().map(|id| (id, a)))
.collect();
let mut chain: Vec<i64> = Vec::new();
let mut current_parent = Some(parent_id);
while let Some(pid) = current_parent {
chain.push(pid);
current_parent = by_id.remove(&pid).and_then(|a| a.parent_id);
}
chain
} else {
Vec::new()
};
let mut search: Vec<i64> = Vec::with_capacity(ancestor_ids.len() + 1);
search.push(own_id);
search.extend(ancestor_ids);
let restrictions: Vec<PageViewRestriction> = if search.is_empty() {
Vec::new()
} else {
PageViewRestriction::objects()
.where_(PageViewRestriction::page_id.is_in(search.iter().copied()))
.fetch(pool)
.await?
};
if restrictions.is_empty() {
return Ok(None);
}
let by_page: std::collections::HashMap<i64, PageViewRestriction> =
restrictions.into_iter().map(|r| (r.page_id, r)).collect();
for pid in search {
if let Some(r) = by_page.get(&pid) {
return Ok(Some(r.clone()));
}
}
Ok(None)
}
pub async fn effective_or_type_for_page(
pool: &rustango::sql::Pool,
page: &crate::page::Page,
) -> Result<Option<PageViewRestriction>, rustango::sql::ExecError> {
if let Some(r) = effective_for_page(pool, page).await? {
return Ok(Some(r));
}
Ok(type_restriction_for_page(pool, page)
.await?
.map(|tvr| synthesize_type_restriction(page, &tvr)))
}
async fn type_restriction_for_page(
pool: &rustango::sql::Pool,
page: &crate::page::Page,
) -> Result<Option<TypeViewRestriction>, rustango::sql::ExecError> {
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
let pts: Vec<crate::PageType> = crate::PageType::objects()
.where_(crate::PageType::id.eq(page.page_type_id))
.fetch(pool)
.await?;
Ok(pts
.into_iter()
.next()
.and_then(|pt| crate::page_type::find_handler(&pt.type_name)?.view_restriction()))
}
fn synthesize_type_restriction(
page: &crate::page::Page,
tvr: &TypeViewRestriction,
) -> PageViewRestriction {
synthesize_restriction(page.id.get().copied().unwrap_or_default(), tvr)
}
fn synthesize_restriction(page_id: i64, tvr: &TypeViewRestriction) -> PageViewRestriction {
use rustango::sql::Auto;
PageViewRestriction {
id: Auto::Unset,
page_id,
kind: tvr.kind.as_str().to_owned(),
password_hash: String::new(),
group_ids: serde_json::json!([]),
codenames: serde_json::json!(tvr.codenames),
created_at: Auto::Unset,
updated_at: Auto::Unset,
}
}
pub async fn effective_restrictions_for(
pool: &rustango::sql::Pool,
pages: &[(i64, String)],
) -> std::collections::HashMap<i64, PageViewRestriction> {
use rustango::sql::FetcherPool as _;
let mut out = std::collections::HashMap::new();
if pages.is_empty() {
return out;
}
let all: Vec<PageViewRestriction> = match PageViewRestriction::objects().fetch(pool).await {
Ok(rows) => rows,
Err(e) => return deny_every_page(pages, &e),
};
if all.is_empty() {
return out;
}
let restricted_ids: Vec<i64> = all.iter().map(|r| r.page_id).collect();
let restricted_pages: Vec<crate::page::Page> = {
use rustango::core::Column as _;
match crate::page::Page::objects()
.where_(crate::page::Page::id.is_in(restricted_ids.iter().copied()))
.fetch(pool)
.await
{
Ok(rows) => rows,
Err(e) => return deny_every_page(pages, &e),
}
};
let restriction_by_id: std::collections::HashMap<i64, &PageViewRestriction> =
all.iter().map(|r| (r.page_id, r)).collect();
let mut restricted: Vec<(String, i64)> = restricted_pages
.iter()
.filter_map(|p| p.id.get().copied().map(|id| (p.path.clone(), id)))
.collect();
restricted.sort_by_key(|(path, _)| std::cmp::Reverse(path.len()));
for (pid, mpath) in pages {
for (rpath, rid) in &restricted {
if mpath.starts_with(rpath.as_str()) {
if let Some(r) = restriction_by_id.get(rid) {
out.insert(*pid, (*r).clone());
}
break;
}
}
}
out
}
#[must_use]
pub fn type_restriction_for_type(
page_type_id: i64,
type_name: &str,
) -> Option<PageViewRestriction> {
let tvr = crate::page_type::find_handler(type_name)?.view_restriction()?;
Some(synthesize_restriction(page_type_id, &tvr))
}
#[must_use]
pub fn viewer_satisfies(
restriction: &PageViewRestriction,
is_authed: bool,
is_super: bool,
perm_set: &std::collections::HashSet<String>,
role_ids: &std::collections::HashSet<i64>,
) -> bool {
ViewerFacts {
is_authed,
is_super,
perm_set: perm_set.clone(),
role_ids: role_ids.clone(),
}
.satisfies(
restriction.parsed_kind(),
&restriction.parsed_groups(),
&restriction.parsed_codenames(),
)
}
#[derive(Debug, Clone, Default)]
pub struct ViewerFacts {
pub is_authed: bool,
pub is_super: bool,
pub perm_set: std::collections::HashSet<String>,
pub role_ids: std::collections::HashSet<i64>,
}
impl ViewerFacts {
#[must_use]
pub fn satisfies(
&self,
kind: Option<RestrictionKind>,
groups: &[i64],
codenames: &[String],
) -> bool {
if self.is_super {
return true;
}
match kind {
Some(RestrictionKind::Login) => self.is_authed,
Some(RestrictionKind::Groups) => groups.iter().any(|g| self.role_ids.contains(g)),
Some(RestrictionKind::Permission) => {
codenames.iter().any(|c| self.perm_set.contains(c))
}
Some(RestrictionKind::Password) => true,
None => false,
}
}
}
pub async fn viewer_facts(
pool: &rustango::sql::Pool,
viewer: Option<&rustango::tenancy::auth::User>,
gated: bool,
) -> ViewerFacts {
let is_super = viewer.is_some_and(|u| u.is_superuser);
let (perm_set, role_ids) = match viewer {
Some(u) if gated && !is_super => {
let uid = u.id.get().copied().unwrap_or_default();
let perms = rustango::tenancy::user_permissions_pool(uid, pool)
.await
.unwrap_or_default();
(
perms.into_iter().collect(),
viewer_role_ids(pool, uid).await,
)
}
_ => (
std::collections::HashSet::new(),
std::collections::HashSet::new(),
),
};
ViewerFacts {
is_authed: viewer.is_some(),
is_super,
perm_set,
role_ids,
}
}
pub struct AccessContext {
is_authed: bool,
is_super: bool,
perm_set: std::collections::HashSet<String>,
role_ids: std::collections::HashSet<i64>,
restricted: Vec<(String, PageViewRestriction)>,
type_restrictions: std::collections::HashMap<i64, PageViewRestriction>,
}
impl AccessContext {
#[must_use]
pub fn is_unrestricted(&self) -> bool {
self.restricted.is_empty() && self.type_restrictions.is_empty()
}
#[must_use]
pub fn gates_by_type(&self) -> bool {
!self.type_restrictions.is_empty()
}
#[must_use]
pub fn can_view(&self, path: &str, page_type_id: i64) -> bool {
let restriction = self
.restricted
.iter()
.find(|(rpath, _)| path.starts_with(rpath.as_str()))
.map(|(_, r)| r)
.or_else(|| self.type_restrictions.get(&page_type_id));
match restriction {
None => true,
Some(r) => viewer_satisfies(
r,
self.is_authed,
self.is_super,
&self.perm_set,
&self.role_ids,
),
}
}
}
impl AccessContext {
fn denied_scopes(&self) -> (Vec<String>, Vec<i64>) {
let satisfies =
|r| viewer_satisfies(r, self.is_authed, self.is_super, &self.perm_set, &self.role_ids);
let prefixes = self
.restricted
.iter()
.filter(|(_, r)| !satisfies(r))
.map(|(path, _)| path.clone())
.collect();
let types = self
.type_restrictions
.iter()
.filter(|(_, r)| !satisfies(r))
.map(|(tid, _)| *tid)
.collect();
(prefixes, types)
}
}
pub async fn only_anonymous_visible(
pool: &rustango::sql::Pool,
mut qs: rustango::query::QuerySet<crate::page::Page>,
) -> rustango::query::QuerySet<crate::page::Page> {
use rustango::core::Column as _;
let (prefixes, types) = access_context(pool, None).await.denied_scopes();
for prefix in prefixes {
qs = qs.where_(crate::page::Page::path.not_like(format!("{prefix}%")));
}
if !types.is_empty() {
qs = qs.where_(crate::page::Page::page_type_id.not_in(types));
}
qs
}
impl AccessContext {
fn deny_all(is_super: bool) -> Self {
Self {
is_authed: false,
is_super,
perm_set: std::collections::HashSet::new(),
role_ids: std::collections::HashSet::new(),
restricted: vec![(String::new(), deny_restriction(0))],
type_restrictions: std::collections::HashMap::new(),
}
}
}
#[cfg(test)]
impl AccessContext {
pub(crate) fn deny_prefix_for_test(deny_prefix: &str) -> Self {
use rustango::sql::Auto;
let r = PageViewRestriction {
id: Auto::Unset,
page_id: 0,
kind: RestrictionKind::Login.as_str().to_owned(),
password_hash: String::new(),
group_ids: serde_json::json!([]),
codenames: serde_json::json!([]),
created_at: Auto::Unset,
updated_at: Auto::Unset,
};
Self {
is_authed: false,
is_super: false,
perm_set: std::collections::HashSet::new(),
role_ids: std::collections::HashSet::new(),
restricted: vec![(deny_prefix.to_owned(), r)],
type_restrictions: std::collections::HashMap::new(),
}
}
pub(crate) fn deny_type_for_test(page_type_id: i64) -> Self {
let mut ctx = Self::deny_prefix_for_test("\u{0}");
let (_, r) = ctx.restricted.pop().expect("one restriction");
ctx.type_restrictions.insert(page_type_id, r);
ctx
}
pub(crate) fn unrestricted_for_test() -> Self {
Self {
is_authed: false,
is_super: false,
perm_set: std::collections::HashSet::new(),
role_ids: std::collections::HashSet::new(),
restricted: Vec::new(),
type_restrictions: std::collections::HashMap::new(),
}
}
}
pub async fn access_context(
pool: &rustango::sql::Pool,
viewer: Option<&rustango::tenancy::auth::User>,
) -> AccessContext {
match try_access_context(pool, viewer).await {
Ok(ctx) => ctx,
Err(e) => {
tracing::warn!(
target: "rustango_cms::view_restriction",
error = %e,
"restriction lookup failed; hiding restricted listings"
);
AccessContext::deny_all(viewer.is_some_and(|u| u.is_superuser))
}
}
}
async fn try_access_context(
pool: &rustango::sql::Pool,
viewer: Option<&rustango::tenancy::auth::User>,
) -> Result<AccessContext, rustango::sql::ExecError> {
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
use std::collections::HashMap;
let all: Vec<PageViewRestriction> = PageViewRestriction::objects().fetch(pool).await?;
let restricted: Vec<(String, PageViewRestriction)> = if all.is_empty() {
Vec::new()
} else {
let ids: Vec<i64> = all.iter().map(|r| r.page_id).collect();
let pages: Vec<crate::page::Page> = crate::page::Page::objects()
.where_(crate::page::Page::id.is_in(ids.iter().copied()))
.fetch(pool)
.await?;
let path_by_id: HashMap<i64, String> = pages
.iter()
.filter_map(|p| p.id.get().copied().map(|id| (id, p.path.clone())))
.collect();
let mut v: Vec<(String, PageViewRestriction)> = all
.into_iter()
.filter_map(|r| path_by_id.get(&r.page_id).map(|p| (p.clone(), r)))
.collect();
v.sort_by_key(|(path, _)| std::cmp::Reverse(path.len()));
v
};
let type_restrictions: HashMap<i64, PageViewRestriction> =
if crate::page_type::any_type_view_restriction() {
let type_rows: Vec<crate::PageType> = crate::PageType::objects().fetch(pool).await?;
type_rows
.into_iter()
.filter_map(|pt| {
let tid = pt.id.get().copied()?;
type_restriction_for_type(tid, &pt.type_name).map(|r| (tid, r))
})
.collect()
} else {
HashMap::new()
};
let gated = !restricted.is_empty() || !type_restrictions.is_empty();
let facts = viewer_facts(pool, viewer, gated).await;
Ok(AccessContext {
is_authed: facts.is_authed,
is_super: facts.is_super,
perm_set: facts.perm_set,
role_ids: facts.role_ids,
restricted,
type_restrictions,
})
}
fn deny_restriction(page_id: i64) -> PageViewRestriction {
PageViewRestriction {
id: Auto::Unset,
page_id,
kind: RestrictionKind::Groups.as_str().to_owned(),
password_hash: String::new(),
group_ids: serde_json::json!([]),
codenames: serde_json::json!([]),
created_at: Auto::Unset,
updated_at: Auto::Unset,
}
}
fn deny_every_page(
pages: &[(i64, String)],
e: &rustango::sql::ExecError,
) -> std::collections::HashMap<i64, PageViewRestriction> {
tracing::warn!(
target: "rustango_cms::view_restriction",
error = %e,
"restriction lookup failed; treating every listed page as restricted"
);
pages.iter().map(|(id, _)| (*id, deny_restriction(*id))).collect()
}
pub async fn denied_page_ids(
pool: &rustango::sql::Pool,
viewer: Option<&rustango::tenancy::auth::User>,
pages: &[(i64, String, i64)],
) -> std::collections::HashSet<i64> {
let mut denied = std::collections::HashSet::new();
if pages.is_empty() {
return denied;
}
let ctx = access_context(pool, viewer).await;
if ctx.is_unrestricted() {
return denied;
}
for (id, path, type_id) in pages {
if !ctx.can_view(path, *type_id) {
denied.insert(*id);
}
}
denied
}
async fn viewer_role_ids(pool: &rustango::sql::Pool, uid: i64) -> std::collections::HashSet<i64> {
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
let rows: Vec<rustango::tenancy::permissions::UserRole> =
rustango::tenancy::permissions::UserRole::objects()
.where_(rustango::tenancy::permissions::UserRole::user_id.eq(uid))
.fetch(pool)
.await
.unwrap_or_default();
rows.into_iter().map(|r| r.role_id).collect()
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashSet;
fn restriction(
kind: RestrictionKind,
codenames: &[&str],
groups: &[i64],
) -> PageViewRestriction {
use rustango::sql::Auto;
PageViewRestriction {
id: Auto::Unset,
page_id: 1,
kind: kind.as_str().to_owned(),
password_hash: String::new(),
group_ids: serde_json::json!(groups),
codenames: serde_json::json!(codenames),
created_at: Auto::Unset,
updated_at: Auto::Unset,
}
}
#[test]
fn kind_roundtrips_including_permission() {
for k in [
RestrictionKind::Password,
RestrictionKind::Login,
RestrictionKind::Groups,
RestrictionKind::Permission,
] {
assert_eq!(RestrictionKind::parse(k.as_str()), Some(k));
}
assert_eq!(RestrictionKind::parse("bogus"), None);
assert_eq!(RestrictionKind::Permission.as_str(), "permission");
}
#[test]
fn parsed_codenames_reads_strings_only() {
let r = restriction(RestrictionKind::Permission, &["a", "b"], &[]);
assert_eq!(r.parsed_codenames(), vec!["a".to_owned(), "b".to_owned()]);
let g = restriction(RestrictionKind::Groups, &[], &[3, 4]);
assert!(g.parsed_codenames().is_empty());
assert_eq!(g.parsed_groups(), vec![3, 4]);
}
#[test]
fn viewer_satisfies_login_needs_auth() {
let r = restriction(RestrictionKind::Login, &[], &[]);
let (p, ro) = (HashSet::new(), HashSet::new());
assert!(!viewer_satisfies(&r, false, false, &p, &ro));
assert!(viewer_satisfies(&r, true, false, &p, &ro));
}
#[test]
fn viewer_satisfies_permission_needs_codename_or_super() {
let r = restriction(RestrictionKind::Permission, &["members.pro"], &[]);
let ro = HashSet::new();
assert!(!viewer_satisfies(&r, true, false, &HashSet::new(), &ro));
let mut p = HashSet::new();
p.insert("members.pro".to_owned());
assert!(viewer_satisfies(&r, true, false, &p, &ro));
assert!(viewer_satisfies(&r, false, true, &HashSet::new(), &ro));
}
#[test]
fn viewer_satisfies_groups_needs_role_or_super() {
let r = restriction(RestrictionKind::Groups, &[], &[7]);
let p = HashSet::new();
assert!(!viewer_satisfies(&r, true, false, &p, &HashSet::new()));
let mut roles = HashSet::new();
roles.insert(7);
assert!(viewer_satisfies(&r, true, false, &p, &roles));
}
#[test]
fn viewer_satisfies_password_stays_visible() {
let r = restriction(RestrictionKind::Password, &[], &[]);
assert!(viewer_satisfies(
&r,
false,
false,
&HashSet::new(),
&HashSet::new()
));
}
#[test]
fn access_context_can_view_prefix_and_unrestricted() {
let ctx = AccessContext::deny_prefix_for_test("0001/0002/");
assert!(!ctx.can_view("0001/0002/", 5)); assert!(!ctx.can_view("0001/0002/0007/", 5)); assert!(ctx.can_view("0001/0003/", 5)); assert!(ctx.can_view("0001/", 4)); assert!(!ctx.is_unrestricted());
let open = AccessContext::unrestricted_for_test();
assert!(open.is_unrestricted());
assert!(open.can_view("0001/0002/", 5));
}
#[test]
fn type_view_restriction_constructors() {
let l = TypeViewRestriction::login();
assert_eq!(l.kind, RestrictionKind::Login);
assert!(l.codenames.is_empty());
let p = TypeViewRestriction::permission(["members.pro", "members.access"]);
assert_eq!(p.kind, RestrictionKind::Permission);
assert_eq!(
p.codenames,
vec!["members.pro".to_owned(), "members.access".to_owned()]
);
}
}