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
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

use crate::models::token::ApiToken;
use chrono::Utc;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Deserialize, Clone, Serialize)]
pub struct UserConfig {
    pub id: Uuid,
    pub email: String,
    pub password: String,
    pub tenant_id: Uuid,
    pub initial_api_tokens: Vec<ApiToken>,
    pub roles: Vec<String>,
    pub auth_provider: Option<String>,
    pub verified: Option<bool>,
    pub metadata: Option<String>,
}

impl UserConfig {
    pub fn generate(tenant_id: Uuid, email: impl Into<String>, roles: Vec<String>) -> Self {
        Self {
            id: Uuid::new_v4(),
            email: email.into(),
            password: Uuid::new_v4().to_string(),
            tenant_id,
            initial_api_tokens: vec![ApiToken {
                client_id: Uuid::new_v4(),
                secret: Uuid::new_v4(),
                description: Some("Initial API Token".to_string()),
                created_at: Utc::now(),
            }],
            roles,
            auth_provider: None,
            verified: None,
            metadata: None,
        }
    }

    pub fn client_id(&self) -> &Uuid {
        &self.initial_api_tokens[0].client_id
    }

    pub fn secret(&self) -> &Uuid {
        &self.initial_api_tokens[0].secret
    }

    pub fn frontegg_password(&self) -> String {
        format!("mzp_{}{}", self.client_id(), self.secret())
    }
}

#[derive(Deserialize, Clone, Serialize)]
pub struct UserCreate {
    pub email: String,
    #[serde(rename = "roleIds")]
    pub role_ids: Option<Vec<String>>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct UserRole {
    pub id: String,
    pub name: String,
    pub key: String,
}

#[derive(Serialize, Deserialize)]
pub struct UserResponse {
    pub id: Uuid,
    pub email: String,
    pub verified: bool,
    pub metadata: String,
    pub provider: String,
    pub roles: Vec<UserRole>,
}

#[derive(Serialize, Deserialize)]
pub struct UserRolesResponse {
    pub items: Vec<UserRole>,
    pub _metadata: UserRolesMetadata,
}

#[derive(Serialize, Deserialize)]
pub struct UserRolesMetadata {
    pub total_items: usize,
    pub total_pages: usize,
}

#[derive(Deserialize)]
pub struct UpdateUserRolesRequest {
    pub email: String,
    #[serde(rename = "roleIds")]
    pub role_ids: Vec<String>,
}

#[derive(Deserialize)]
pub struct UsersV3Query {
    #[serde(rename = "_email")]
    pub email: Option<String>,
    #[serde(rename = "_limit")]
    pub limit: Option<usize>,
    #[serde(rename = "_offset")]
    pub offset: Option<usize>,
    pub ids: Option<String>,
    #[serde(rename = "_sortBy")]
    pub sort_by: Option<String>,
    #[serde(rename = "_order")]
    pub order: Option<String>,
    #[serde(rename = "_tenantId")]
    pub tenant_id: Option<Uuid>,
    #[serde(rename = "_includeSubTenants")]
    pub include_sub_tenants: Option<bool>,
}

#[derive(Serialize)]
pub struct UsersV3Response {
    pub items: Vec<UserResponse>,
    pub _metadata: UsersV3Metadata,
}

#[derive(Serialize)]
pub struct UsersV3Metadata {
    pub total_items: usize,
}

#[derive(Deserialize)]
pub struct AddRolesToGroupParams {
    #[serde(rename = "roleIds")]
    pub role_ids: Vec<String>,
}

#[derive(Deserialize)]
pub struct RemoveRolesFromGroupParams {
    #[serde(rename = "roleIds")]
    pub role_ids: Vec<String>,
}

#[derive(Deserialize)]
pub struct GetUserPasswordRequest {
    pub email: String,
}

#[derive(Serialize)]
pub struct GetUserPasswordResponse {
    pub email: String,
    pub password: String,
}