91 lines
2.4 KiB
Rust
Raw Normal View History

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-11 17:13:51 +03:00
use crate::results::{GetUsersServiceResult, ExistsServiceResult, AuthResult, EmptyServiceResult};
2021-01-06 19:38:47 +03:00
pub const SUCCESS_CODE: i32 = 0;
2021-01-11 17:13:51 +03:00
// get all users
2021-01-06 19:38:47 +03:00
#[fce]
fn get_users() -> GetUsersServiceResult {
get_all_users().into()
}
2021-01-11 17:13:51 +03:00
// get user by peer_id
2021-01-06 19:38:47 +03:00
#[fce]
fn get_user(peer_id: String) -> GetUsersServiceResult {
get_user_by_peer_id(peer_id).into()
}
2021-01-11 17:13:51 +03:00
// add a user too the service
2021-01-06 19:38:47 +03:00
#[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()
}
2021-01-11 17:13:51 +03:00
// delete a user from the service
2021-01-06 19:38:47 +03:00
#[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()
}
2021-01-11 17:13:51 +03:00
// check if a user is exists in the service
2021-01-06 19:38:47 +03:00
#[fce]
2021-01-11 17:13:51 +03:00
fn is_exists(peer_id: String) -> ExistsServiceResult {
user_exists(peer_id).into()
2021-01-07 21:20:24 +03:00
}
2021-01-11 17:13:51 +03:00
// check if a caller is authenticated in this service
2021-01-07 21:20:24 +03:00
#[fce]
fn is_authenticated() -> AuthResult {
check_auth().into()
2021-01-07 19:12:42 +03:00
}
2021-01-11 17:13:51 +03:00
// return an error if request is not authenticated
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-08 15:50:12 +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
}