update auth result

This commit is contained in:
DieMyst
2021-01-07 21:20:24 +03:00
parent c3a45d6c9f
commit 11d222275d
2 changed files with 30 additions and 8 deletions

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
use crate::service_api::EmptyServiceResult;
use crate::service_api::{EmptyServiceResult, AuthResult};
use crate::service_api::ExistsServiceResult;
use crate::service_api::GetUsersServiceResult;
use crate::user::User;
@@ -67,6 +67,23 @@ impl From<Result<()>> for EmptyServiceResult {
}
}
impl From<Result<()>> for AuthResult {
fn from(result: Result<()>) -> Self {
match result {
Ok(users) => Self {
ret_code: crate::service_api::SUCCESS_CODE,
err_msg: String::new(),
is_authenticated: true,
},
Err(err) => Self {
ret_code: to_error_core(&err),
err_msg: format!("{}", err),
is_authenticated: false,
},
}
}
}
impl From<Result<Vec<User>>> for GetUsersServiceResult {
fn from(result: Result<Vec<User>>) -> Self {
match result {

View File

@@ -79,11 +79,15 @@ fn is_exists(user_name: String) -> ExistsServiceResult {
}
#[fce]
fn is_authenticated() -> bool {
match check_auth() {
Ok(_) => true,
Err(_) => false
}
pub struct AuthResult {
pub ret_code: i32,
pub err_msg: String,
pub is_authenticated: bool,
}
#[fce]
fn is_authenticated() -> AuthResult {
check_auth().into()
}
fn check_auth() -> Result<()> {
@@ -91,9 +95,10 @@ fn check_auth() -> Result<()> {
use boolinator::Boolinator;
let call_parameters: CallParameters = fluence::get_call_parameters();
let init_peer_id = call_parameters.init_peer_id;
let init_peer_id = call_parameters.init_peer_id.clone();
let existed = get_user_by_peer_id(init_peer_id.clone())?.pop();
(init_peer_id == call_parameters.service_creator_peer_id || existed.is_some()).ok_or_else(|| UserNotExist(init_peer_id.clone()))
(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)))
}