files-sdk 0.4.1

Rust SDK for the Files.com API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//! User management operations
//!
//! Provides comprehensive user account management for Files.com sites, including
//! creation, modification, and administrative operations.
//!
//! # Features
//!
//! - Create and manage user accounts
//! - Configure permissions and access controls
//! - Set user quotas and restrictions
//! - Administrative operations (unlock, reset 2FA)
//! - Group membership management
//! - Protocol-specific permissions (FTP, SFTP, WebDAV, REST API)
//!
//! # Example
//!
//! ```no_run
//! use files_sdk::{FilesClient, UserHandler};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let client = FilesClient::builder()
//!     .api_key("your-api-key")
//!     .build()?;
//!
//! let handler = UserHandler::new(client);
//!
//! // Create a new user with SFTP access
//! let user = handler.create(
//!     "newuser",
//!     Some("user@company.com"),
//!     Some("secure-password"),
//!     Some("New User")
//! ).await?;
//!
//! println!("Created user ID: {}", user.id.unwrap());
//!
//! // List all users
//! let (users, _) = handler.list(None, Some(100)).await?;
//! for user in users {
//!     println!("User: {} ({})",
//!         user.username.unwrap_or_default(),
//!         user.email.unwrap_or_default());
//! }
//! # Ok(())
//! # }
//! ```

use crate::{FilesClient, PaginationInfo, Result};
use futures::stream::Stream;
use serde::{Deserialize, Serialize};
use serde_json::json;

/// User entity from Files.com API
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserEntity {
    /// User ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<i64>,

    /// Username
    #[serde(skip_serializing_if = "Option::is_none")]
    pub username: Option<String>,

    /// User's email address
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,

    /// User's full name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// Company name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub company: Option<String>,

    /// Notes about the user
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notes: Option<String>,

    /// User home directory
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_home: Option<String>,

    /// User root directory
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_root: Option<String>,

    /// Is site admin
    #[serde(skip_serializing_if = "Option::is_none")]
    pub site_admin: Option<bool>,

    /// Is read-only site admin
    #[serde(skip_serializing_if = "Option::is_none")]
    pub readonly_site_admin: Option<bool>,

    /// User is disabled
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disabled: Option<bool>,

    /// User is disabled, expired, or inactive
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disabled_expired_or_inactive: Option<bool>,

    /// SSL/TLS is required for this user
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ssl_required: Option<String>,

    /// Time zone
    #[serde(skip_serializing_if = "Option::is_none")]
    pub time_zone: Option<String>,

    /// Language preference
    #[serde(skip_serializing_if = "Option::is_none")]
    pub language: Option<String>,

    /// Allowed IP addresses
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allowed_ips: Option<String>,

    /// Bypass site allowed IPs
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bypass_site_allowed_ips: Option<bool>,

    /// Group IDs this user belongs to (can be string or array)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub group_ids: Option<String>,

    /// Admin group IDs
    #[serde(skip_serializing_if = "Option::is_none")]
    pub admin_group_ids: Option<Vec<i64>>,

    /// FTP permission
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ftp_permission: Option<bool>,

    /// SFTP permission
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sftp_permission: Option<bool>,

    /// WebDAV permission
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dav_permission: Option<bool>,

    /// REST API permission
    #[serde(skip_serializing_if = "Option::is_none")]
    pub restapi_permission: Option<bool>,

    /// Require 2FA
    #[serde(skip_serializing_if = "Option::is_none")]
    pub require_2fa: Option<String>,

    /// Active 2FA method
    #[serde(skip_serializing_if = "Option::is_none")]
    pub active_2fa: Option<bool>,

    /// Created at timestamp
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_at: Option<String>,

    /// Last login at timestamp
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_login_at: Option<String>,

    /// Password set at timestamp
    #[serde(skip_serializing_if = "Option::is_none")]
    pub password_set_at: Option<String>,

    /// Password validity in days
    #[serde(skip_serializing_if = "Option::is_none")]
    pub password_validity_days: Option<i64>,

    /// API keys count
    #[serde(skip_serializing_if = "Option::is_none")]
    pub api_keys_count: Option<i64>,

    /// Public keys count
    #[serde(skip_serializing_if = "Option::is_none")]
    pub public_keys_count: Option<i64>,
}

/// Handler for user operations
#[derive(Debug, Clone)]
pub struct UserHandler {
    client: FilesClient,
}

impl UserHandler {
    /// Creates a new UserHandler
    pub fn new(client: FilesClient) -> Self {
        Self { client }
    }

    /// List all users on the site
    ///
    /// Returns a paginated list of user accounts with their details and permissions.
    ///
    /// # Arguments
    ///
    /// * `cursor` - Pagination cursor from previous response
    /// * `per_page` - Number of results per page (default 100, max 10,000)
    ///
    /// # Returns
    ///
    /// A tuple containing:
    /// - Vector of `UserEntity` objects
    /// - `PaginationInfo` with cursors for next/previous pages
    ///
    /// # Example
    ///
    /// ```no_run
    /// use files_sdk::{FilesClient, UserHandler};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FilesClient::builder().api_key("key").build()?;
    /// let handler = UserHandler::new(client);
    ///
    /// // List first page of users
    /// let (users, pagination) = handler.list(None, Some(50)).await?;
    ///
    /// for user in users {
    ///     println!("{}: {} - Admin: {}",
    ///         user.username.unwrap_or_default(),
    ///         user.email.unwrap_or_default(),
    ///         user.site_admin.unwrap_or(false));
    /// }
    ///
    /// // Get next page if available
    /// if let Some(next) = pagination.cursor_next {
    ///     let (more_users, _) = handler.list(Some(next), Some(50)).await?;
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn list(
        &self,
        cursor: Option<String>,
        per_page: Option<i32>,
    ) -> Result<(Vec<UserEntity>, PaginationInfo)> {
        let mut path = "/users?".to_string();

        if let Some(c) = cursor {
            path.push_str(&format!("cursor={}&", c));
        }
        if let Some(pp) = per_page {
            path.push_str(&format!("per_page={}&", pp));
        }

        let response = self.client.get_raw(&path).await?;
        let users: Vec<UserEntity> = serde_json::from_value(response)?;

        // TODO: Extract pagination info from response headers
        let pagination = PaginationInfo {
            cursor_next: None,
            cursor_prev: None,
        };

        Ok((users, pagination))
    }

    /// Stream all users with automatic pagination
    ///
    /// Returns a stream that automatically handles pagination, yielding
    /// individual users as they are fetched. This is more memory-efficient
    /// for large user lists.
    ///
    /// # Arguments
    ///
    /// * `per_page` - Number of users per page (optional, default 1000)
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use files_sdk::{FilesClient, UserHandler};
    /// # use futures::stream::StreamExt;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let client = FilesClient::builder().api_key("key").build()?;
    /// let handler = UserHandler::new(client);
    /// let stream = handler.list_stream(Some(100));
    ///
    /// tokio::pin!(stream);
    ///
    /// while let Some(user) = stream.next().await {
    ///     let user = user?;
    ///     println!("{}", user.username.unwrap_or_default());
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn list_stream(
        &self,
        per_page: Option<i32>,
    ) -> impl Stream<Item = Result<UserEntity>> + '_ {
        let per_page = per_page.unwrap_or(1000);

        async_stream::try_stream! {
            let mut cursor: Option<String> = None;

            loop {
                let (users, pagination) = self
                    .list(cursor.clone(), Some(per_page))
                    .await?;

                for user in users {
                    yield user;
                }

                match pagination.cursor_next {
                    Some(next) => cursor = Some(next),
                    None => break,
                }
            }
        }
    }

    /// Get details of a specific user by ID
    ///
    /// # Arguments
    ///
    /// * `id` - The unique user ID
    ///
    /// # Returns
    ///
    /// A `UserEntity` with complete user information
    ///
    /// # Example
    ///
    /// ```no_run
    /// use files_sdk::{FilesClient, UserHandler};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FilesClient::builder().api_key("key").build()?;
    /// let handler = UserHandler::new(client);
    ///
    /// let user = handler.get(12345).await?;
    /// println!("User: {}", user.username.unwrap_or_default());
    /// println!("Last login: {}", user.last_login_at.unwrap_or_default());
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get(&self, id: i64) -> Result<UserEntity> {
        let path = format!("/users/{}", id);
        let response = self.client.get_raw(&path).await?;
        Ok(serde_json::from_value(response)?)
    }

    /// Create a new user account
    ///
    /// Creates a new user with the specified credentials. The user will receive
    /// a welcome email if email is provided.
    ///
    /// # Arguments
    ///
    /// * `username` - Unique username for login (required)
    /// * `email` - User's email address
    /// * `password` - Initial password (if not provided, user must set on first login)
    /// * `name` - User's full name
    ///
    /// # Returns
    ///
    /// The newly created `UserEntity`
    ///
    /// # Example
    ///
    /// ```no_run
    /// use files_sdk::{FilesClient, UserHandler};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FilesClient::builder().api_key("key").build()?;
    /// let handler = UserHandler::new(client);
    ///
    /// // Create user with all details
    /// let user = handler.create(
    ///     "jdoe",
    ///     Some("jdoe@company.com"),
    ///     Some("SecureP@ssw0rd"),
    ///     Some("John Doe")
    /// ).await?;
    ///
    /// println!("Created user: {} (ID: {})",
    ///     user.username.unwrap(),
    ///     user.id.unwrap());
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create(
        &self,
        username: &str,
        email: Option<&str>,
        password: Option<&str>,
        name: Option<&str>,
    ) -> Result<UserEntity> {
        let mut body = json!({
            "username": username,
        });

        if let Some(e) = email {
            body["email"] = json!(e);
        }
        if let Some(p) = password {
            body["password"] = json!(p);
        }
        if let Some(n) = name {
            body["name"] = json!(n);
        }

        let response = self.client.post_raw("/users", body).await?;
        Ok(serde_json::from_value(response)?)
    }

    /// Update user information
    ///
    /// Updates user profile information. Only provided fields are updated;
    /// omitted fields remain unchanged.
    ///
    /// # Arguments
    ///
    /// * `id` - User ID to update
    /// * `email` - New email address
    /// * `name` - New full name
    /// * `company` - New company name
    /// * `notes` - Administrative notes about the user
    ///
    /// # Returns
    ///
    /// The updated `UserEntity`
    ///
    /// # Example
    ///
    /// ```no_run
    /// use files_sdk::{FilesClient, UserHandler};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FilesClient::builder().api_key("key").build()?;
    /// let handler = UserHandler::new(client);
    ///
    /// // Update user's company and notes
    /// let user = handler.update(
    ///     12345,
    ///     None,
    ///     None,
    ///     Some("New Company Inc."),
    ///     Some("Transferred from old company")
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn update(
        &self,
        id: i64,
        email: Option<&str>,
        name: Option<&str>,
        company: Option<&str>,
        notes: Option<&str>,
    ) -> Result<UserEntity> {
        let mut body = json!({});

        if let Some(e) = email {
            body["email"] = json!(e);
        }
        if let Some(n) = name {
            body["name"] = json!(n);
        }
        if let Some(c) = company {
            body["company"] = json!(c);
        }
        if let Some(nt) = notes {
            body["notes"] = json!(nt);
        }

        let path = format!("/users/{}", id);
        let response = self.client.patch_raw(&path, body).await?;
        Ok(serde_json::from_value(response)?)
    }

    /// Delete a user account permanently
    ///
    /// Removes the user account and all associated data. This operation cannot
    /// be undone.
    ///
    /// # Arguments
    ///
    /// * `id` - User ID to delete
    ///
    /// # Example
    ///
    /// ```no_run
    /// use files_sdk::{FilesClient, UserHandler};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FilesClient::builder().api_key("key").build()?;
    /// let handler = UserHandler::new(client);
    ///
    /// handler.delete(12345).await?;
    /// println!("User deleted successfully");
    /// # Ok(())
    /// # }
    /// ```
    pub async fn delete(&self, id: i64) -> Result<()> {
        let path = format!("/users/{}", id);
        self.client.delete_raw(&path).await?;
        Ok(())
    }

    /// Unlock a locked user account
    ///
    /// Removes the lock from a user account that has been locked due to too many
    /// failed login attempts or administrative action.
    ///
    /// # Arguments
    ///
    /// * `id` - User ID to unlock
    ///
    /// # Example
    ///
    /// ```no_run
    /// use files_sdk::{FilesClient, UserHandler};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FilesClient::builder().api_key("key").build()?;
    /// let handler = UserHandler::new(client);
    ///
    /// // Unlock user after failed login attempts
    /// handler.unlock(12345).await?;
    /// println!("User unlocked successfully");
    /// # Ok(())
    /// # }
    /// ```
    pub async fn unlock(&self, id: i64) -> Result<()> {
        let path = format!("/users/{}/unlock", id);
        self.client.post_raw(&path, json!({})).await?;
        Ok(())
    }

    /// Reset two-factor authentication for a user
    ///
    /// Disables 2FA for the user, requiring them to set it up again on next login.
    /// Use this when a user has lost access to their 2FA device.
    ///
    /// # Arguments
    ///
    /// * `id` - User ID
    ///
    /// # Example
    ///
    /// ```no_run
    /// use files_sdk::{FilesClient, UserHandler};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FilesClient::builder().api_key("key").build()?;
    /// let handler = UserHandler::new(client);
    ///
    /// // Reset 2FA for user who lost their device
    /// handler.reset_2fa(12345).await?;
    /// println!("2FA reset - user must configure on next login");
    /// # Ok(())
    /// # }
    /// ```
    pub async fn reset_2fa(&self, id: i64) -> Result<()> {
        let path = format!("/users/{}/2fa/reset", id);
        self.client.post_raw(&path, json!({})).await?;
        Ok(())
    }

    /// Resend welcome email to a user
    ///
    /// Sends a new welcome email to the user with login instructions and
    /// password setup link if needed.
    ///
    /// # Arguments
    ///
    /// * `id` - User ID
    ///
    /// # Example
    ///
    /// ```no_run
    /// use files_sdk::{FilesClient, UserHandler};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FilesClient::builder().api_key("key").build()?;
    /// let handler = UserHandler::new(client);
    ///
    /// // Resend welcome email to new user
    /// handler.resend_welcome_email(12345).await?;
    /// println!("Welcome email sent");
    /// # Ok(())
    /// # }
    /// ```
    pub async fn resend_welcome_email(&self, id: i64) -> Result<()> {
        let path = format!("/users/{}/resend_welcome_email", id);
        self.client.post_raw(&path, json!({})).await?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_handler_creation() {
        let client = FilesClient::builder().api_key("test-key").build().unwrap();
        let _handler = UserHandler::new(client);
    }
}