systemprompt_security/authz/types/
kinds.rs1use std::fmt;
2use std::str::FromStr;
3
4use serde::{Deserialize, Serialize};
5
6use crate::authz::error::AuthzError;
7
8#[derive(
9 Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, sqlx::Type,
10)]
11#[sqlx(type_name = "TEXT", rename_all = "lowercase")]
12#[serde(rename_all = "lowercase")]
13pub enum RuleType {
14 User,
15 Role,
16}
17
18impl fmt::Display for RuleType {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 f.write_str(match *self {
21 Self::User => "user",
22 Self::Role => "role",
23 })
24 }
25}
26
27impl FromStr for RuleType {
28 type Err = AuthzError;
29
30 fn from_str(s: &str) -> Result<Self, Self::Err> {
31 match s {
32 "user" => Ok(Self::User),
33 "role" => Ok(Self::Role),
34 other => Err(AuthzError::InvalidRuleType(other.to_owned())),
35 }
36 }
37}
38
39#[derive(
40 Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, sqlx::Type,
41)]
42#[sqlx(type_name = "TEXT", rename_all = "lowercase")]
43#[serde(rename_all = "lowercase")]
44pub enum Access {
45 Allow,
46 Deny,
47}
48
49impl fmt::Display for Access {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 f.write_str(match *self {
52 Self::Allow => "allow",
53 Self::Deny => "deny",
54 })
55 }
56}
57
58impl FromStr for Access {
59 type Err = AuthzError;
60
61 fn from_str(s: &str) -> Result<Self, Self::Err> {
62 match s {
63 "allow" => Ok(Self::Allow),
64 "deny" => Ok(Self::Deny),
65 other => Err(AuthzError::InvalidAccess(other.to_owned())),
66 }
67 }
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
71#[serde(rename_all = "snake_case")]
72pub enum EntityKind {
73 GatewayRoute,
74 McpServer,
75 Plugin,
76 Agent,
77 Marketplace,
78 Skill,
79 Hook,
80 SlackWorkspace,
81 SlackChannel,
82 TeamsTenant,
83 TeamsConversation,
84}
85
86impl EntityKind {
87 pub const fn as_str(self) -> &'static str {
88 match self {
89 Self::GatewayRoute => "gateway_route",
90 Self::McpServer => "mcp_server",
91 Self::Plugin => "plugin",
92 Self::Agent => "agent",
93 Self::Marketplace => "marketplace",
94 Self::Skill => "skill",
95 Self::Hook => "hook",
96 Self::SlackWorkspace => "slack_workspace",
97 Self::SlackChannel => "slack_channel",
98 Self::TeamsTenant => "teams_tenant",
99 Self::TeamsConversation => "teams_conversation",
100 }
101 }
102}
103
104impl FromStr for EntityKind {
105 type Err = AuthzError;
106
107 fn from_str(s: &str) -> Result<Self, Self::Err> {
108 match s {
109 "gateway_route" => Ok(Self::GatewayRoute),
110 "mcp_server" => Ok(Self::McpServer),
111 "plugin" => Ok(Self::Plugin),
112 "agent" => Ok(Self::Agent),
113 "marketplace" => Ok(Self::Marketplace),
114 "skill" => Ok(Self::Skill),
115 "hook" => Ok(Self::Hook),
116 "slack_workspace" => Ok(Self::SlackWorkspace),
117 "slack_channel" => Ok(Self::SlackChannel),
118 "teams_tenant" => Ok(Self::TeamsTenant),
119 "teams_conversation" => Ok(Self::TeamsConversation),
120 other => Err(AuthzError::Validation(format!(
121 "unknown entity_type: {other}"
122 ))),
123 }
124 }
125}
126
127impl fmt::Display for EntityKind {
128 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129 f.write_str(self.as_str())
130 }
131}