Skip to content

Commit 1b99a25

Browse files
committed
cleaning authorization function
1 parent efeec06 commit 1b99a25

2 files changed

Lines changed: 13 additions & 27 deletions

File tree

src/authorization/mod.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
use mockall::*;
22
use serde::{Deserialize, Serialize};
3-
use thiserror::Error;
43
use uuid::Uuid;
54

6-
#[derive(Error, Debug)]
7-
pub enum AuthorizationError {
8-
#[error("authorization error")]
9-
Error,
10-
}
11-
125
#[derive(Clone)]
136
pub struct User {
147
pub user_id: Uuid,
@@ -17,34 +10,28 @@ pub struct User {
1710

1811
#[automock]
1912
pub trait IAuthorization: Send + Sync {
20-
fn can_get_user(&self, actor: User, resource_id: Uuid) -> Result<bool, AuthorizationError>;
13+
fn can_get_user(&self, actor: User, resource_id: Uuid) -> bool;
2114
// fn can_modify_user(&self, actor: User, resource_id: Uuid) -> Result<bool, AuthorizationError>;
2215
// fn can_delete_user(&self, actor: User, resource_id: Uuid) -> Result<bool, AuthorizationError>;
23-
fn can_list_users(&self, actor: User) -> Result<bool, AuthorizationError>;
16+
fn can_list_users(&self, actor: User) -> bool;
2417
// fn can_create_user(&self, actor: User) -> Result<bool, AuthorizationError>;
2518
}
2619

2720
#[derive(Clone, Serialize, Deserialize)]
2821
pub struct Authorization;
2922

3023
impl Authorization {
31-
fn is_user_admin(&self, actor: User) -> Result<bool, AuthorizationError> {
32-
if actor.role == "admin" {
33-
return Ok(true);
34-
}
35-
Err(AuthorizationError::Error)
24+
fn is_user_admin(&self, actor: User) -> bool {
25+
return actor.role == "admin";
3626
}
3727
}
3828

3929
impl IAuthorization for Authorization {
40-
fn can_get_user(&self, actor: User, resource_id: Uuid) -> Result<bool, AuthorizationError> {
41-
self.is_user_admin(actor.clone())
42-
.or(match actor.user_id == resource_id {
43-
true => Ok(true),
44-
false => Err(AuthorizationError::Error),
45-
})
30+
fn can_get_user(&self, actor: User, resource_id: Uuid) -> bool {
31+
return self.is_user_admin(actor.clone()) || actor.user_id == resource_id;
4632
}
47-
fn can_list_users(&self, actor: User) -> Result<bool, AuthorizationError> {
33+
34+
fn can_list_users(&self, actor: User) -> bool {
4835
self.is_user_admin(actor)
4936
}
5037
}

src/handlers/authorization.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@ pub async fn can_list_users(
3333
.map_err(|err| errors::ServerError::Internal(anyhow!(err)))?
3434
.ok_or(errors::ServerError::Unauthorized)?;
3535

36-
let is_authorized = authorization
37-
.can_list_users(AuthzUser {
38-
user_id: user.user_id.to_owned(),
39-
role: user.role.to_owned(),
40-
})
41-
.or(Err(errors::ServerError::Unauthorized))?;
36+
let is_authorized = authorization.can_list_users(AuthzUser {
37+
user_id: user.user_id.to_owned(),
38+
role: user.role.to_owned(),
39+
});
4240

4341
if is_authorized {
4442
return Ok(next.run(req).await);
4543
}
44+
4645
Err(errors::ServerError::Unauthorized)
4746
}

0 commit comments

Comments
 (0)