1
use actionable::{Action, Identifier, ResourceName};
2
use serde::{Deserialize, Serialize};
3

            
4
use crate::{
5
    document::KeyId,
6
    schema::{CollectionName, ViewName},
7
};
8

            
9
/// The base BonsaiDb resource namespace. All database objects have this as
10
/// their first name segment.
11
#[must_use]
12
1431175
pub fn bonsaidb_resource_name<'a>() -> ResourceName<'a> {
13
1431175
    ResourceName::named("bonsaidb")
14
1431175
}
15

            
16
/// Creates a resource name with the database `name`.
17
#[must_use]
18
835060
pub fn database_resource_name<'a>(name: impl Into<Identifier<'a>>) -> ResourceName<'a> {
19
835060
    bonsaidb_resource_name().and(name)
20
835060
}
21

            
22
/// Creates a resource name for a `collection` within a `database`.
23
#[must_use]
24
22557
pub fn collection_resource_name<'a>(
25
22557
    database: impl Into<Identifier<'a>>,
26
22557
    collection: &CollectionName,
27
22557
) -> ResourceName<'a> {
28
22557
    database_resource_name(database).and(collection.to_string())
29
22557
}
30

            
31
/// Creates a resource name for a document `id` within `collection` within `database`.
32
#[must_use]
33
12130
pub fn document_resource_name<'a>(
34
12130
    database: impl Into<Identifier<'a>>,
35
12130
    collection: &CollectionName,
36
12130
    id: u64,
37
12130
) -> ResourceName<'a> {
38
12130
    collection_resource_name(database, collection)
39
12130
        .and("document")
40
12130
        .and(id)
41
12130
}
42

            
43
/// Creaets a resource name for a `view` within `database`.
44
#[must_use]
45
303575
pub fn view_resource_name<'a>(database: &'a str, view: &'a ViewName) -> ResourceName<'a> {
46
303575
    database_resource_name(database)
47
303575
        .and(view.collection.to_string())
48
303575
        .and("view")
49
303575
        .and(view.name.as_ref())
50
303575
}
51

            
52
/// Creates a resource name for `PubSub` `topic` within `database`.
53
#[must_use]
54
1200
pub fn pubsub_topic_resource_name<'a>(database: &'a str, topic: &'a str) -> ResourceName<'a> {
55
1200
    database_resource_name(database).and("pubsub").and(topic)
56
1200
}
57

            
58
/// Creates a resource name for the key-value store in `database`.
59
#[must_use]
60
505600
pub fn kv_resource_name(database: &str) -> ResourceName<'_> {
61
505600
    database_resource_name(database).and("keyvalue")
62
505600
}
63

            
64
/// Creates a resource name for `key` within `namespace` within the key-value store of `database`.
65
#[must_use]
66
505550
pub fn keyvalue_key_resource_name<'a>(
67
505550
    database: &'a str,
68
505550
    namespace: Option<&'a str>,
69
505550
    key: &'a str,
70
505550
) -> ResourceName<'a> {
71
505550
    kv_resource_name(database)
72
505550
        .and(namespace.unwrap_or(""))
73
505550
        .and(key)
74
505550
}
75

            
76
/// Creates a resource name for encryption key `key_id`.
77
#[must_use]
78
50
pub fn encryption_key_resource_name(key_id: &KeyId) -> ResourceName<'_> {
79
50
    bonsaidb_resource_name()
80
50
        .and("vault")
81
50
        .and("key")
82
50
        .and(match key_id {
83
50
            KeyId::Master => "_master",
84
            KeyId::Id(id) => id.as_ref(),
85
            KeyId::None => unreachable!(),
86
        })
87
50
}
88

            
89
/// Creates a resource name for `user_id`.
90
#[must_use]
91
525
pub fn user_resource_name<'a>(user_id: u64) -> ResourceName<'a> {
92
525
    bonsaidb_resource_name().and("user").and(user_id)
93
525
}
94

            
95
/// Actions that can be permitted within BonsaiDb.
96
1431475
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
97
pub enum BonsaiAction {
98
    /// Actions that operate on a server
99
    Server(ServerAction),
100
    /// Actions that operate on a specific database.
101
    Database(DatabaseAction),
102
}
103

            
104
/// Actions that operate on a server.
105
30725
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
106
pub enum ServerAction {
107
    /// Permits connecting to the server. Upon negotiating authentication, the
108
    /// effective permissions of the connected party will be checked for
109
    /// permissions to `Connect`. If not allowed, the connection will be
110
    /// terminated.
111
    Connect,
112
    /// Permits [`StorageConnection::list_available_schemas`](crate::connection::StorageConnection::list_available_schemas).
113
    ListAvailableSchemas,
114
    /// Permits [`StorageConnection::list_databases`](crate::connection::StorageConnection::list_databases).
115
    ListDatabases,
116
    /// Permits [`StorageConnection::create_database`](crate::connection::StorageConnection::create_database).
117
    CreateDatabase,
118
    /// Permits [`StorageConnection::delete_database`](crate::connection::StorageConnection::delete_database).
119
    DeleteDatabase,
120
    /// Permits [`StorageConnection::create_user`](crate::connection::StorageConnection::create_user).
121
    CreateUser,
122
    /// Permits [`StorageConnection::set_user_password`](crate::connection::StorageConnection::set_user_password).
123
    SetPassword,
124
    /// Permits the ability to log in with a password.
125
    Authenticate(AuthenticationMethod),
126
    /// Permits [`StorageConnection::add_permission_group_to_user`](crate::connection::StorageConnection::add_permission_group_to_user) and [`StorageConnection::remove_permission_group_from_user`](crate::connection::StorageConnection::remove_permission_group_from_user).
127
    ModifyUserPermissionGroups,
128
    /// Permits .
129
    /// Permits [`StorageConnection::add_role_to_user`](crate::connection::StorageConnection::add_role_to_user) and [`StorageConnection::remove_role_from_user`](crate::connection::StorageConnection::remove_role_from_user).
130
    ModifyUserRoles,
131
}
132

            
133
/// Methods for user authentication.
134
225
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
135
pub enum AuthenticationMethod {
136
    /// Authenticate the user using password hashing (Argon2).
137
    PasswordHash,
138
}
139

            
140
/// Actions that operate on a specific database.
141
1400750
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
142
pub enum DatabaseAction {
143
    /// The ability to compact data to reclaim space.
144
    Compact,
145
    /// Actions that operate on a document.
146
    Document(DocumentAction),
147
    /// Actions that operate on a view.
148
    View(ViewAction),
149
    /// Actions that operate on transactions.
150
    Transaction(TransactionAction),
151
    /// Actions that operate on the `PubSub` system.
152
    PubSub(PubSubAction),
153
    /// Actions that operate on the key-value store.
154
    KeyValue(KeyValueAction),
155
}
156

            
157
/// Actions that operate on a document.
158
563975
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
159
pub enum DocumentAction {
160
    /// Allows document retrieval through
161
    /// [`Connection::get()`](crate::connection::Connection::get) and
162
    /// [`Connection::get_multiple()`](crate::connection::Connection::get_multiple).
163
    /// See [`document_resource_name()`] for the format of document resource
164
    /// names.
165
    Get,
166
    /// Allows listing documents through
167
    /// [`Connection::list()`](crate::connection::Connection::list). See
168
    /// [`collection_resource_name()`] for the format of collection resource
169
    /// names.
170
    List,
171
    /// Allows inserting a document through
172
    /// [`Connection::apply_transaction()`](crate::connection::Connection::apply_transaction).
173
    /// See [`collection_resource_name()`] for the format of collection resource
174
    /// names.
175
    Insert,
176
    /// Allows updating a document through
177
    /// [`Connection::apply_transaction()`](crate::connection::Connection::apply_transaction).
178
    /// See [`document_resource_name()`] for the format of document resource
179
    /// names.
180
    Update,
181
    /// Allows deleting a document through
182
    /// [`Connection::apply_transaction()`](crate::connection::Connection::apply_transaction).
183
    /// See [`document_resource_name()`] for the format of document resource
184
    /// names.
185
    Delete,
186
}
187

            
188
/// Actions that operate on a view.
189
303575
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
190
pub enum ViewAction {
191
    /// Allows querying a view with
192
    /// [`Connection::query()`](crate::connection::Connection::query). See
193
    /// [`view_resource_name`] for the format of view resource names.
194
    Query,
195
    /// Allows reducing a view with
196
    /// [`Connection::reduce()`](crate::connection::Connection::reduce). See
197
    /// [`view_resource_name`] for the format of view resource names.
198
    Reduce,
199
    /// Allows deleting associated docs with
200
    /// [`Connection::delete_docs()`](crate::connection::Connection::delete_docs).
201
    /// See [`view_resource_name`] for the format of view resource names.
202
    DeleteDocs,
203
}
204

            
205
/// Actions that operate on transactions.
206
25950
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
207
pub enum TransactionAction {
208
    /// Allows listing executed transactions with
209
    /// [`Connection::list_executed_transactions()`](crate::connection::Connection::list_executed_transactions).
210
    /// This action is checked against the database's resource name. See
211
    /// [`database_resource_name()`] for the format of database resource names.
212
    ListExecuted,
213
    /// Allows retrieving the last executed transaction id with
214
    /// [`Connection::last_transaction_id()`](crate::connection::Connection::last_transaction_id).
215
    /// This action is checked against the database's resource name. See
216
    /// [`database_resource_name()`] for the format of database resource names.
217
    GetLastId,
218
}
219

            
220
/// Actions that operate on the `PubSub` system.
221
1550
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
222
pub enum PubSubAction {
223
    /// Allows creating a subscriber with
224
    /// [`PubSub::create_subscriber()`](crate::pubsub::PubSub::create_subscriber).
225
    /// This action is checked against the database's resource name. See
226
    /// [`database_resource_name()`] for the format of database resource names.
227
    CreateSuscriber,
228
    /// Allows publishing a payload to a `PubSub` topic with
229
    /// [`PubSub::publish()`](crate::pubsub::PubSub::publish). See
230
    /// [`pubsub_topic_resource_name()`] for the format of `PubSub` topic
231
    /// resource names.
232
    Publish,
233
    /// Allows subscribing to a `PubSub` topic with
234
    /// [`PubSub::subscribe_to()`](crate::pubsub::Subscriber::subscribe_to). See
235
    /// [`pubsub_topic_resource_name()`] for the format of `PubSub` topic
236
    /// resource names.
237
    SubscribeTo,
238
    /// Allows unsubscribing from a `PubSub` topic with
239
    /// [`PubSub::unsubscribe_from()`](crate::pubsub::Subscriber::unsubscribe_from). See
240
    /// [`pubsub_topic_resource_name()`] for the format of `PubSub` topic
241
    /// resource names.
242
    UnsubscribeFrom,
243
}
244

            
245
/// Actions that operate on the key-value store.
246
505550
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
247
pub enum KeyValueAction {
248
    /// Allows executing a key-value store operation with
249
    /// [`KeyValue::execute_key_operation()`](crate::keyvalue::KeyValue::execute_key_operation).
250
    /// See [`keyvalue_key_resource_name()`] for the format of key resource names.
251
    ExecuteOperation,
252
}
253

            
254
/// Actions that use encryption keys.
255
100
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
256
pub enum EncryptionKeyAction {
257
    /// Uses a key to encrypt data.
258
    Encrypt,
259
    /// Uses a key to decrypt data.
260
    Decrypt,
261
}