Skip to main content

openstack_keystone_core/api/v3/
user.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13// SPDX-License-Identifier: Apache-2.0
14
15use axum::{
16    Json,
17    http::StatusCode,
18    response::{IntoResponse, Response},
19};
20
21pub use openstack_keystone_api_types::v3::user::*;
22
23use crate::identity::types as identity_types;
24
25impl From<identity_types::UserOptions> for UserOptions {
26    fn from(value: identity_types::UserOptions) -> Self {
27        Self {
28            ignore_change_password_upon_first_use: value.ignore_change_password_upon_first_use,
29            ignore_password_expiry: value.ignore_password_expiry,
30            ignore_lockout_failure_attempts: value.ignore_lockout_failure_attempts,
31            lock_password: value.lock_password,
32            ignore_user_inactivity: value.ignore_user_inactivity,
33            multi_factor_auth_rules: value.multi_factor_auth_rules,
34            multi_factor_auth_enabled: value.multi_factor_auth_enabled,
35        }
36    }
37}
38
39impl From<UserOptions> for identity_types::UserOptions {
40    fn from(value: UserOptions) -> Self {
41        Self {
42            ignore_change_password_upon_first_use: value.ignore_change_password_upon_first_use,
43            ignore_password_expiry: value.ignore_password_expiry,
44            ignore_lockout_failure_attempts: value.ignore_lockout_failure_attempts,
45            lock_password: value.lock_password,
46            ignore_user_inactivity: value.ignore_user_inactivity,
47            multi_factor_auth_rules: value.multi_factor_auth_rules,
48            multi_factor_auth_enabled: value.multi_factor_auth_enabled,
49            is_service_account: None,
50        }
51    }
52}
53
54impl From<identity_types::UserResponse> for User {
55    fn from(value: identity_types::UserResponse) -> Self {
56        let opts: UserOptions = value.options.clone().into();
57        // We only want to see user options if there is at least 1 option set
58        let opts = if opts.ignore_change_password_upon_first_use.is_some()
59            || opts.ignore_password_expiry.is_some()
60            || opts.ignore_lockout_failure_attempts.is_some()
61            || opts.lock_password.is_some()
62            || opts.ignore_user_inactivity.is_some()
63            || opts.multi_factor_auth_rules.is_some()
64            || opts.multi_factor_auth_enabled.is_some()
65        {
66            Some(opts)
67        } else {
68            None
69        };
70        Self {
71            default_project_id: value.default_project_id,
72            domain_id: value.domain_id,
73            enabled: value.enabled,
74            extra: value.extra,
75            federated: value
76                .federated
77                .map(|val| val.into_iter().map(Into::into).collect()),
78            id: value.id,
79            name: value.name,
80            options: opts,
81            password_expires_at: value.password_expires_at,
82        }
83    }
84}
85
86impl From<UserCreateRequest> for identity_types::UserCreate {
87    fn from(value: UserCreateRequest) -> Self {
88        let user = value.user;
89        Self {
90            default_project_id: user.default_project_id,
91            domain_id: user.domain_id,
92            enabled: Some(user.enabled),
93            extra: user.extra,
94            id: None,
95            federated: None,
96            name: user.name,
97            options: user.options.map(Into::into),
98            password: user.password,
99        }
100    }
101}
102
103impl From<identity_types::Federation> for Federation {
104    fn from(value: identity_types::Federation) -> Self {
105        Self {
106            idp_id: value.idp_id,
107            protocols: value.protocols.into_iter().map(Into::into).collect(),
108        }
109    }
110}
111impl From<identity_types::FederationProtocol> for FederationProtocol {
112    fn from(value: identity_types::FederationProtocol) -> Self {
113        Self {
114            protocol_id: value.protocol_id,
115            unique_id: value.unique_id,
116        }
117    }
118}
119
120impl IntoResponse for identity_types::UserResponse {
121    fn into_response(self) -> Response {
122        (
123            StatusCode::OK,
124            Json(UserResponse {
125                user: User::from(self),
126            }),
127        )
128            .into_response()
129    }
130}
131
132impl From<UserListParameters> for identity_types::UserListParameters {
133    fn from(value: UserListParameters) -> Self {
134        Self {
135            domain_id: value.domain_id,
136            name: value.name,
137            unique_id: value.unique_id,
138            ..Default::default() //    limit: value.limit,
139        }
140    }
141}