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
1532466
pub fn bonsaidb_resource_name<'a>() -> ResourceName<'a> {
13
1532466
    ResourceName::named("bonsaidb")
14
1532466
}
15

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

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

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

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

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

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

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

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

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

            
95
/// Actions that can be permitted within BonsaiDb.
96
1532763
#[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
33237
#[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::delete_user`](crate::connection::StorageConnection::delete_user).
123
    DeleteUser,
124
    /// Permits [`StorageConnection::set_user_password`](crate::connection::StorageConnection::set_user_password).
125
    SetPassword,
126
    /// Permits the ability to log in with a password.
127
    Authenticate(AuthenticationMethod),
128
    /// 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).
129
    ModifyUserPermissionGroups,
130
    /// Permits .
131
    /// 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).
132
    ModifyUserRoles,
133
}
134

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

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

            
159
/// Actions that operate on a document.
160
602235
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
161
pub enum DocumentAction {
162
    /// Allows document retrieval through
163
    /// [`Connection::get()`](crate::connection::Connection::get) and
164
    /// [`Connection::get_multiple()`](crate::connection::Connection::get_multiple).
165
    /// See [`document_resource_name()`] for the format of document resource
166
    /// names.
167
    Get,
168
    /// Allows listing documents through
169
    /// [`Connection::list()`](crate::connection::Connection::list). See
170
    /// [`collection_resource_name()`] for the format of collection resource
171
    /// names.
172
    List,
173
    /// Allows counting documents through
174
    /// [`Connection::count()`](crate::connection::Connection::count). See
175
    /// [`collection_resource_name()`] for the format of collection resource
176
    /// names.
177
    Count,
178
    /// Allows inserting a document through
179
    /// [`Connection::apply_transaction()`](crate::connection::Connection::apply_transaction).
180
    /// See [`collection_resource_name()`] for the format of collection resource
181
    /// names.
182
    Insert,
183
    /// Allows updating a document through
184
    /// [`Connection::apply_transaction()`](crate::connection::Connection::apply_transaction).
185
    /// See [`document_resource_name()`] for the format of document resource
186
    /// names.
187
    Update,
188
    /// Allows overwriting a document by id with
189
    /// [`Connection::apply_transaction()`](crate::connection::Connection::apply_transaction).
190
    /// No revision information will be checked. See
191
    /// [`document_resource_name()`] for the format of document resource names.
192
    Overwrite,
193
    /// Allows deleting a document through
194
    /// [`Connection::apply_transaction()`](crate::connection::Connection::apply_transaction).
195
    /// See [`document_resource_name()`] for the format of document resource
196
    /// names.
197
    Delete,
198
}
199

            
200
/// Actions that operate on a view.
201
321462
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
202
pub enum ViewAction {
203
    /// Allows querying a view with
204
    /// [`Connection::query()`](crate::connection::Connection::query). See
205
    /// [`view_resource_name`] for the format of view resource names.
206
    Query,
207
    /// Allows reducing a view with
208
    /// [`Connection::reduce()`](crate::connection::Connection::reduce). See
209
    /// [`view_resource_name`] for the format of view resource names.
210
    Reduce,
211
    /// Allows deleting associated docs with
212
    /// [`Connection::delete_docs()`](crate::connection::Connection::delete_docs).
213
    /// See [`view_resource_name`] for the format of view resource names.
214
    DeleteDocs,
215
}
216

            
217
/// Actions that operate on transactions.
218
28026
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
219
pub enum TransactionAction {
220
    /// Allows listing executed transactions with
221
    /// [`Connection::list_executed_transactions()`](crate::connection::Connection::list_executed_transactions).
222
    /// This action is checked against the database's resource name. See
223
    /// [`database_resource_name()`] for the format of database resource names.
224
    ListExecuted,
225
    /// Allows retrieving the last executed transaction id with
226
    /// [`Connection::last_transaction_id()`](crate::connection::Connection::last_transaction_id).
227
    /// This action is checked against the database's resource name. See
228
    /// [`database_resource_name()`] for the format of database resource names.
229
    GetLastId,
230
}
231

            
232
/// Actions that operate on the `PubSub` system.
233
1674
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
234
pub enum PubSubAction {
235
    /// Allows creating a subscriber with
236
    /// [`PubSub::create_subscriber()`](crate::pubsub::PubSub::create_subscriber).
237
    /// This action is checked against the database's resource name. See
238
    /// [`database_resource_name()`] for the format of database resource names.
239
    CreateSuscriber,
240
    /// Allows publishing a payload to a `PubSub` topic with
241
    /// [`PubSub::publish()`](crate::pubsub::PubSub::publish). See
242
    /// [`pubsub_topic_resource_name()`] for the format of `PubSub` topic
243
    /// resource names.
244
    Publish,
245
    /// Allows subscribing to a `PubSub` topic with
246
    /// [`PubSub::subscribe_to()`](crate::pubsub::Subscriber::subscribe_to). See
247
    /// [`pubsub_topic_resource_name()`] for the format of `PubSub` topic
248
    /// resource names.
249
    SubscribeTo,
250
    /// Allows unsubscribing from a `PubSub` topic with
251
    /// [`PubSub::unsubscribe_from()`](crate::pubsub::Subscriber::unsubscribe_from). See
252
    /// [`pubsub_topic_resource_name()`] for the format of `PubSub` topic
253
    /// resource names.
254
    UnsubscribeFrom,
255
}
256

            
257
/// Actions that operate on the key-value store.
258
545994
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
259
pub enum KeyValueAction {
260
    /// Allows executing a key-value store operation with
261
    /// [`KeyValue::execute_key_operation()`](crate::keyvalue::KeyValue::execute_key_operation).
262
    /// See [`keyvalue_key_resource_name()`] for the format of key resource names.
263
    ExecuteOperation,
264
}
265

            
266
/// Actions that use encryption keys.
267
108
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
268
pub enum EncryptionKeyAction {
269
    /// Uses a key to encrypt data.
270
    Encrypt,
271
    /// Uses a key to decrypt data.
272
    Decrypt,
273
}