pub struct Policy {
pub name: Ident,
pub table: Ident,
pub policy_type: PolicyType,
pub commands: Vec<PolicyCommand>,
pub roles: Vec<SmolStr>,
pub using_expr: Option<String>,
pub check_expr: Option<String>,
pub mssql_schema: Option<SmolStr>,
pub mssql_block_operations: Vec<MssqlBlockOperation>,
pub documentation: Option<Documentation>,
pub span: Span,
}Expand description
A Row-Level Security (RLS) policy definition.
Policies provide fine-grained access control at the row level. They are applied to tables and evaluated for each row operation.
§Example Schema Syntax
policy UserReadOwnData on User {
for SELECT
to authenticated
using "id = current_user_id()"
}
policy UserModifyOwnData on User {
for [INSERT, UPDATE, DELETE]
to authenticated
using "id = current_user_id()"
check "id = current_user_id()"
}§Database Support
- PostgreSQL: Full support via CREATE POLICY
- SQL Server: Supported via Security Policies with predicate functions
Fields§
§name: IdentPolicy name (must be unique per table).
table: IdentThe model/table this policy applies to.
policy_type: PolicyTypePolicy type: PERMISSIVE (default) or RESTRICTIVE.
commands: Vec<PolicyCommand>Commands this policy applies to (SELECT, INSERT, UPDATE, DELETE, or ALL).
roles: Vec<SmolStr>Roles this policy applies to (default: PUBLIC).
using_expr: Option<String>USING expression - evaluated for existing rows (SELECT, UPDATE, DELETE). Should return boolean. Row is visible if expression returns true. In MSSQL, this becomes the FILTER PREDICATE.
check_expr: Option<String>WITH CHECK expression - evaluated for new rows (INSERT, UPDATE). Should return boolean. Row can be inserted/updated if expression returns true. In MSSQL, this becomes BLOCK PREDICATE(s).
mssql_schema: Option<SmolStr>MSSQL-specific: Schema for the predicate function (default: “Security”).
mssql_block_operations: Vec<MssqlBlockOperation>MSSQL-specific: Block operations to apply (default: all applicable).
documentation: Option<Documentation>Documentation comment.
span: SpanSource location.
Implementations§
Source§impl Policy
impl Policy
Sourcepub fn with_type(self, policy_type: PolicyType) -> Policy
pub fn with_type(self, policy_type: PolicyType) -> Policy
Set the policy type.
Sourcepub fn with_commands(self, commands: Vec<PolicyCommand>) -> Policy
pub fn with_commands(self, commands: Vec<PolicyCommand>) -> Policy
Set the commands this policy applies to.
Sourcepub fn add_command(&mut self, command: PolicyCommand)
pub fn add_command(&mut self, command: PolicyCommand)
Add a command this policy applies to.
Sourcepub fn with_roles(self, roles: Vec<SmolStr>) -> Policy
pub fn with_roles(self, roles: Vec<SmolStr>) -> Policy
Set the roles this policy applies to.
Sourcepub fn with_using(self, expr: impl Into<String>) -> Policy
pub fn with_using(self, expr: impl Into<String>) -> Policy
Set the USING expression.
Sourcepub fn with_check(self, expr: impl Into<String>) -> Policy
pub fn with_check(self, expr: impl Into<String>) -> Policy
Set the WITH CHECK expression.
Sourcepub fn with_documentation(self, doc: Documentation) -> Policy
pub fn with_documentation(self, doc: Documentation) -> Policy
Set documentation.
Sourcepub fn with_mssql_schema(self, schema: impl Into<SmolStr>) -> Policy
pub fn with_mssql_schema(self, schema: impl Into<SmolStr>) -> Policy
Set the MSSQL schema for the predicate function.
Sourcepub fn with_mssql_block_operations(
self,
operations: Vec<MssqlBlockOperation>,
) -> Policy
pub fn with_mssql_block_operations( self, operations: Vec<MssqlBlockOperation>, ) -> Policy
Set the MSSQL block operations.
Sourcepub fn add_mssql_block_operation(&mut self, operation: MssqlBlockOperation)
pub fn add_mssql_block_operation(&mut self, operation: MssqlBlockOperation)
Add an MSSQL block operation.
Sourcepub fn applies_to(&self, command: PolicyCommand) -> bool
pub fn applies_to(&self, command: PolicyCommand) -> bool
Check if this policy applies to a specific command.
Sourcepub fn is_restrictive(&self) -> bool
pub fn is_restrictive(&self) -> bool
Check if this policy is restrictive.
Sourcepub fn is_permissive(&self) -> bool
pub fn is_permissive(&self) -> bool
Check if this policy is permissive.
Sourcepub fn effective_roles(&self) -> Vec<&str>
pub fn effective_roles(&self) -> Vec<&str>
Get the effective roles (PUBLIC if none specified).
Sourcepub fn mssql_schema(&self) -> &str
pub fn mssql_schema(&self) -> &str
Get the MSSQL schema (default: “Security”).
Sourcepub fn mssql_predicate_function_name(&self) -> String
pub fn mssql_predicate_function_name(&self) -> String
Get the predicate function name for MSSQL.
Sourcepub fn to_sql(&self, table_name: &str) -> String
pub fn to_sql(&self, table_name: &str) -> String
Generate the PostgreSQL CREATE POLICY statement.
Sourcepub fn to_postgres_sql(&self, table_name: &str) -> String
pub fn to_postgres_sql(&self, table_name: &str) -> String
Generate the PostgreSQL CREATE POLICY statement.
Sourcepub fn to_mssql_sql(
&self,
table_name: &str,
predicate_column: &str,
) -> MssqlPolicyStatements
pub fn to_mssql_sql( &self, table_name: &str, predicate_column: &str, ) -> MssqlPolicyStatements
Generate SQL Server (MSSQL) security policy statements.
Returns a tuple of:
- CREATE FUNCTION statement for the predicate function
- CREATE SECURITY POLICY statement
§Arguments
table_name- The fully qualified table name (e.g., “dbo.Users”)predicate_column- The column name to use in the predicate (e.g., “UserId”)
§Example
let (func_sql, policy_sql) = policy.to_mssql_sql("dbo.Users", "UserId");Trait Implementations§
Source§impl<'de> Deserialize<'de> for Policy
impl<'de> Deserialize<'de> for Policy
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Policy, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Policy, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Serialize for Policy
impl Serialize for Policy
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl StructuralPartialEq for Policy
Auto Trait Implementations§
impl Freeze for Policy
impl RefUnwindSafe for Policy
impl Send for Policy
impl Sync for Policy
impl Unpin for Policy
impl UnwindSafe for Policy
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more