ng_net/actors/admin/
create_user.rs

1/*
2 * Copyright (c) 2022-2025 Niko Bonnieure, Par le Peuple, NextGraph.org developers
3 * All rights reserved.
4 * Licensed under the Apache License, Version 2.0
5 * <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
6 * or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
7 * at your option. All files in the project carrying such
8 * notice may not be copied, modified, or distributed except
9 * according to those terms.
10*/
11
12use std::sync::Arc;
13
14use async_std::sync::Mutex;
15use ng_repo::types::UserId;
16use serde::{Deserialize, Serialize};
17
18use ng_repo::errors::*;
19use ng_repo::log::*;
20
21use super::super::StartProtocol;
22
23use crate::broker::BROKER;
24use crate::connection::NoiseFSM;
25use crate::types::*;
26use crate::{actor::*, types::ProtocolMessage};
27
28/// Create user and keeps credentials in the server (for use with headless API)
29#[doc(hidden)]
30#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
31pub struct CreateUserV0 {}
32
33/// Create user
34#[doc(hidden)]
35#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
36pub enum CreateUser {
37    V0(CreateUserV0),
38}
39
40impl TryFrom<ProtocolMessage> for CreateUser {
41    type Error = ProtocolError;
42    fn try_from(msg: ProtocolMessage) -> Result<Self, Self::Error> {
43        if let ProtocolMessage::Start(StartProtocol::Admin(AdminRequest::V0(AdminRequestV0 {
44            content: AdminRequestContentV0::CreateUser(a),
45            ..
46        }))) = msg
47        {
48            Ok(a)
49        } else {
50            log_debug!("INVALID {:?}", msg);
51            Err(ProtocolError::InvalidValue)
52        }
53    }
54}
55
56impl From<CreateUser> for ProtocolMessage {
57    fn from(_msg: CreateUser) -> ProtocolMessage {
58        unimplemented!();
59    }
60}
61
62impl From<UserId> for ProtocolMessage {
63    fn from(_msg: UserId) -> ProtocolMessage {
64        unimplemented!();
65    }
66}
67
68impl TryFrom<ProtocolMessage> for UserId {
69    type Error = ProtocolError;
70    fn try_from(_msg: ProtocolMessage) -> Result<Self, Self::Error> {
71        unimplemented!();
72    }
73}
74
75impl From<CreateUser> for AdminRequestContentV0 {
76    fn from(msg: CreateUser) -> AdminRequestContentV0 {
77        AdminRequestContentV0::CreateUser(msg)
78    }
79}
80
81impl CreateUser {
82    pub fn get_actor(&self) -> Box<dyn EActor> {
83        Actor::<CreateUser, UserId>::new_responder(0)
84    }
85}
86
87impl Actor<'_, CreateUser, UserId> {}
88
89#[async_trait::async_trait]
90impl EActor for Actor<'_, CreateUser, UserId> {
91    async fn respond(
92        &mut self,
93        msg: ProtocolMessage,
94        fsm: Arc<Mutex<NoiseFSM>>,
95    ) -> Result<(), ProtocolError> {
96        let _req = CreateUser::try_from(msg)?;
97
98        let res = {
99            let (broker_id, sb) = {
100                let b = BROKER.read().await;
101                (b.get_server_peer_id(), b.get_server_broker()?)
102            };
103            let lock = sb.read().await;
104            lock.create_user(&broker_id).await
105        };
106
107        let response: AdminResponseV0 = res.into();
108        fsm.lock().await.send(response.into()).await?;
109        Ok(())
110    }
111}