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

            
4
use crate::{
5
    document::{DocumentId, 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
5825303
pub fn bonsaidb_resource_name<'a>() -> ResourceName<'a> {
13
5825303
    ResourceName::named("bonsaidb")
14
5825303
}
15

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

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

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

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

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

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

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

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

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

            
95
/// Creates a resource name for `role_id`.
96
#[must_use]
97
62
pub fn role_resource_name<'a>(role_id: u64) -> ResourceName<'a> {
98
62
    bonsaidb_resource_name().and("role").and(role_id)
99
62
}
100

            
101
/// Actions that can be permitted within BonsaiDb.
102
2600745
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
103
pub enum BonsaiAction {
104
    /// Actions that operate on a server
105
    Server(ServerAction),
106
    /// Actions that operate on a specific database.
107
    Database(DatabaseAction),
108
}
109

            
110
/// Actions that operate on a server.
111
40858
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
112
pub enum ServerAction {
113
    /// Permits connecting to the server. Upon negotiating authentication, the
114
    /// effective permissions of the connected party will be checked for
115
    /// permissions to `Connect`. If not allowed, the connection will be
116
    /// terminated.
117
    Connect,
118
    /// Permits [`StorageConnection::list_available_schemas`](crate::connection::StorageConnection::list_available_schemas).
119
    ListAvailableSchemas,
120
    /// Permits [`StorageConnection::list_databases`](crate::connection::StorageConnection::list_databases).
121
    ListDatabases,
122
    /// Permits [`StorageConnection::create_database`](crate::connection::StorageConnection::create_database).
123
    CreateDatabase,
124
    /// Permits [`StorageConnection::delete_database`](crate::connection::StorageConnection::delete_database).
125
    DeleteDatabase,
126
    /// Permits [`StorageConnection::create_user`](crate::connection::StorageConnection::create_user).
127
    CreateUser,
128
    /// Permits [`StorageConnection::delete_user`](crate::connection::StorageConnection::delete_user).
129
    DeleteUser,
130
    /// Permits [`StorageConnection::set_user_password`](crate::connection::StorageConnection::set_user_password).
131
    SetPassword,
132
    /// Permits the ability to log in with a password.
133
    Authenticate(AuthenticationMethod),
134
    /// Permits the ability to assume an identity without authenticating that
135
    /// they are the identity in question.
136
    AssumeIdentity,
137
    /// 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).
138
    ModifyUserPermissionGroups,
139
    /// Permits .
140
    /// 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).
141
    ModifyUserRoles,
142
}
143

            
144
/// Methods for user authentication.
145
279
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
146
pub enum AuthenticationMethod {
147
    /// Authenticate the user using password hashing (Argon2).
148
    PasswordHash,
149
}
150

            
151
/// Actions that operate on a specific database.
152
2559887
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
153
pub enum DatabaseAction {
154
    /// The ability to compact data to reclaim space.
155
    Compact,
156
    /// Actions that operate on a document.
157
    Document(DocumentAction),
158
    /// Actions that operate on a view.
159
    View(ViewAction),
160
    /// Actions that operate on transactions.
161
    Transaction(TransactionAction),
162
    /// Actions that operate on the `PubSub` system.
163
    PubSub(PubSubAction),
164
    /// Actions that operate on the key-value store.
165
    KeyValue(KeyValueAction),
166
}
167

            
168
/// Actions that operate on a document.
169
948910
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
170
pub enum DocumentAction {
171
    /// Allows document retrieval through
172
    /// [`Connection::get()`](crate::connection::LowLevelConnection::get) and
173
    /// [`Connection::get_multiple()`](crate::connection::LowLevelConnection::get_multiple).
174
    /// See [`document_resource_name()`] for the format of document resource
175
    /// names.
176
    Get,
177
    /// Allows listing documents through
178
    /// [`Connection::list()`](crate::connection::LowLevelConnection::list). See
179
    /// [`collection_resource_name()`] for the format of collection resource
180
    /// names.
181
    List,
182
    /// Allows listing documents through
183
    /// [`Connection::list_headers()`](crate::connection::LowLevelConnection::list_headers). See
184
    /// [`collection_resource_name()`] for the format of collection resource
185
    /// names.
186
    ListHeaders,
187
    /// Allows counting documents through
188
    /// [`Connection::count()`](crate::connection::LowLevelConnection::count). See
189
    /// [`collection_resource_name()`] for the format of collection resource
190
    /// names.
191
    Count,
192
    /// Allows inserting a document through
193
    /// [`Connection::apply_transaction()`](crate::connection::LowLevelConnection::apply_transaction).
194
    /// See [`collection_resource_name()`] for the format of collection resource
195
    /// names.
196
    Insert,
197
    /// Allows updating a document through
198
    /// [`Connection::apply_transaction()`](crate::connection::LowLevelConnection::apply_transaction).
199
    /// See [`document_resource_name()`] for the format of document resource
200
    /// names.
201
    Update,
202
    /// Allows overwriting a document by id with
203
    /// [`Connection::apply_transaction()`](crate::connection::LowLevelConnection::apply_transaction).
204
    /// No revision information will be checked. See
205
    /// [`document_resource_name()`] for the format of document resource names.
206
    Overwrite,
207
    /// Allows deleting a document through
208
    /// [`Connection::apply_transaction()`](crate::connection::LowLevelConnection::apply_transaction).
209
    /// See [`document_resource_name()`] for the format of document resource
210
    /// names.
211
    Delete,
212
}
213

            
214
/// Actions that operate on a view.
215
419430
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
216
pub enum ViewAction {
217
    /// Allows querying a view with
218
    /// [`Connection::query()`](crate::connection::LowLevelConnection::query). See
219
    /// [`view_resource_name`] for the format of view resource names.
220
    Query,
221
    /// Allows reducing a view with
222
    /// [`Connection::reduce()`](crate::connection::LowLevelConnection::reduce). See
223
    /// [`view_resource_name`] for the format of view resource names.
224
    Reduce,
225
    /// Allows deleting associated docs with
226
    /// [`Connection::delete_docs()`](crate::connection::LowLevelConnection::delete_docs).
227
    /// See [`view_resource_name`] for the format of view resource names.
228
    DeleteDocs,
229
}
230

            
231
/// Actions that operate on transactions.
232
248062
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
233
pub enum TransactionAction {
234
    /// Allows listing executed transactions with
235
    /// [`Connection::list_executed_transactions()`](crate::connection::Connection::list_executed_transactions).
236
    /// This action is checked against the database's resource name. See
237
    /// [`database_resource_name()`] for the format of database resource names.
238
    ListExecuted,
239
    /// Allows retrieving the last executed transaction id with
240
    /// [`Connection::last_transaction_id()`](crate::connection::Connection::last_transaction_id).
241
    /// This action is checked against the database's resource name. See
242
    /// [`database_resource_name()`] for the format of database resource names.
243
    GetLastId,
244
}
245

            
246
/// Actions that operate on the `PubSub` system.
247
3255
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
248
pub enum PubSubAction {
249
    /// Allows creating a subscriber with
250
    /// [`PubSub::create_subscriber()`](crate::pubsub::PubSub::create_subscriber).
251
    /// This action is checked against the database's resource name. See
252
    /// [`database_resource_name()`] for the format of database resource names.
253
    CreateSuscriber,
254
    /// Allows publishing a payload to a `PubSub` topic with
255
    /// [`PubSub::publish()`](crate::pubsub::PubSub::publish). See
256
    /// [`pubsub_topic_resource_name()`] for the format of `PubSub` topic
257
    /// resource names.
258
    Publish,
259
    /// Allows subscribing to a `PubSub` topic with
260
    /// [`PubSub::subscribe_to()`](crate::pubsub::Subscriber::subscribe_to). See
261
    /// [`pubsub_topic_resource_name()`] for the format of `PubSub` topic
262
    /// resource names.
263
    SubscribeTo,
264
    /// Allows unsubscribing from a `PubSub` topic with
265
    /// [`PubSub::unsubscribe_from()`](crate::pubsub::Subscriber::unsubscribe_from). See
266
    /// [`pubsub_topic_resource_name()`] for the format of `PubSub` topic
267
    /// resource names.
268
    UnsubscribeFrom,
269
}
270

            
271
/// Actions that operate on the key-value store.
272
939951
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
273
pub enum KeyValueAction {
274
    /// Allows executing a key-value store operation with
275
    /// [`KeyValue::execute_key_operation()`](crate::keyvalue::KeyValue::execute_key_operation).
276
    /// See [`keyvalue_key_resource_name()`] for the format of key resource names.
277
    ExecuteOperation,
278
}
279

            
280
/// Actions that use encryption keys.
281
124
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
282
pub enum EncryptionKeyAction {
283
    /// Uses a key to encrypt data.
284
    Encrypt,
285
    /// Uses a key to decrypt data.
286
    Decrypt,
287
}