2021-01-06 19:38:47 +03:00
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License , Version 2.0 ( the " License " ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http ://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an " AS IS " BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
use crate ::storage_api ::* ;
use crate ::user ::User ;
use crate ::Result ;
use fluence ::{ fce , CallParameters } ;
2021-01-07 19:12:42 +03:00
use crate ::errors ::UserListError ;
2021-01-06 19:38:47 +03:00
pub const SUCCESS_CODE : i32 = 0 ;
#[ fce ]
pub struct GetUsersServiceResult {
pub ret_code : i32 ,
pub err_msg : String ,
pub users : Vec < User > ,
}
#[ fce ]
fn get_users ( ) -> GetUsersServiceResult {
get_all_users ( ) . into ( )
}
#[ fce ]
fn get_user ( peer_id : String ) -> GetUsersServiceResult {
get_user_by_peer_id ( peer_id ) . into ( )
}
#[ fce ]
pub struct EmptyServiceResult {
pub ret_code : i32 ,
pub err_msg : String ,
}
#[ fce ]
fn join ( user : User ) -> EmptyServiceResult {
fn add_impl ( user : User ) -> Result < ( ) > {
2021-01-07 19:12:42 +03:00
check_auth ( ) ? ;
2021-01-06 19:38:47 +03:00
add_user ( user )
}
add_impl ( user ) . into ( )
}
#[ fce ]
fn delete ( peer_id : String ) -> EmptyServiceResult {
fn delete_impl ( peer_id : String ) -> Result < ( ) > {
2021-01-07 19:12:42 +03:00
check_auth ( ) ? ;
2021-01-06 19:38:47 +03:00
delete_user ( peer_id )
2021-01-06 23:44:04 +03:00
}
2021-01-06 19:38:47 +03:00
delete_impl ( peer_id ) . into ( )
}
#[ fce ]
pub struct ExistsServiceResult {
pub ret_code : i32 ,
pub err_msg : String ,
pub is_exists : bool ,
}
#[ fce ]
fn is_exists ( user_name : String ) -> ExistsServiceResult {
user_exists ( user_name ) . into ( )
}
2021-01-07 19:12:42 +03:00
#[ fce ]
2021-01-07 21:20:24 +03:00
pub struct AuthResult {
pub ret_code : i32 ,
pub err_msg : String ,
pub is_authenticated : bool ,
}
#[ fce ]
fn is_authenticated ( ) -> AuthResult {
check_auth ( ) . into ( )
2021-01-07 19:12:42 +03:00
}
fn check_auth ( ) -> Result < ( ) > {
2021-01-06 19:38:47 +03:00
use crate ::errors ::UserListError ::UserNotExist ;
use boolinator ::Boolinator ;
let call_parameters : CallParameters = fluence ::get_call_parameters ( ) ;
2021-01-07 21:20:24 +03:00
let init_peer_id = call_parameters . init_peer_id . clone ( ) ;
2021-01-06 19:38:47 +03:00
let existed = get_user_by_peer_id ( init_peer_id . clone ( ) ) ? . pop ( ) ;
2021-01-07 21:20:24 +03:00
( init_peer_id = = call_parameters . service_creator_peer_id | | existed . is_some ( ) )
. ok_or_else ( | | UserNotExist ( format! ( " init_peer_id is {:?} and it is not existed or an owner. Owner: {:?} " , & init_peer_id , & call_parameters . service_creator_peer_id ) ) )
2021-01-06 19:38:47 +03:00
}