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

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

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

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

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

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

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

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

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

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

            
95
/// Actions that can be permitted within BonsaiDb.
96
1454310
#[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
31954
#[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
234
#[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
1422356
#[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
566046
#[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 overwriting a document by id with
182
    /// [`Connection::apply_transaction()`](crate::connection::Connection::apply_transaction).
183
    /// No revision information will be checked. See
184
    /// [`document_resource_name()`] for the format of document resource names.
185
    Overwrite,
186
    /// Allows deleting a document through
187
    /// [`Connection::apply_transaction()`](crate::connection::Connection::apply_transaction).
188
    /// See [`document_resource_name()`] for the format of document resource
189
    /// names.
190
    Delete,
191
}
192

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

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

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

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

            
259
/// Actions that use encryption keys.
260
104
#[derive(Action, Serialize, Deserialize, Clone, Copy, Debug)]
261
pub enum EncryptionKeyAction {
262
    /// Uses a key to encrypt data.
263
    Encrypt,
264
    /// Uses a key to decrypt data.
265
    Decrypt,
266
}