Skip to content

Commit

Permalink
feat: get_user method for admins
Browse files Browse the repository at this point in the history
  • Loading branch information
ilbertt committed May 19, 2024
1 parent 78530e3 commit be89a78
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
3 changes: 2 additions & 1 deletion frontend/declarations/backend.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ export interface _SERVICE {
'get_deployment' : ActorMethod<[string], GetDeploymentResult>,
'get_deployment_icp_price' : ActorMethod<[], ApiFloatResult>,
'get_deployments' : ActorMethod<[], GetDeploymentsResult>,
'get_user' : ActorMethod<[], GetUserResult>,
'get_my_user' : ActorMethod<[], GetUserResult>,
'get_user' : ActorMethod<[Principal], GetUserResult>,
'list_logs' : ActorMethod<[LogsFilterRequest], ListLogsResponse>,
'promote_user_to_admin' : ActorMethod<[UserId], ApiEmptyResult>,
'query_blocks' : ActorMethod<[GetBlocksArgs], QueryBlocksResult>,
Expand Down
3 changes: 2 additions & 1 deletion frontend/declarations/backend.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ export const idlFactory = ({ IDL }) => {
'get_deployment' : IDL.Func([IDL.Text], [GetDeploymentResult], ['query']),
'get_deployment_icp_price' : IDL.Func([], [ApiFloatResult], []),
'get_deployments' : IDL.Func([], [GetDeploymentsResult], ['query']),
'get_user' : IDL.Func([], [GetUserResult], ['query']),
'get_my_user' : IDL.Func([], [GetUserResult], ['query']),
'get_user' : IDL.Func([IDL.Principal], [GetUserResult], ['query']),
'list_logs' : IDL.Func([LogsFilterRequest], [ListLogsResponse], ['query']),
'promote_user_to_admin' : IDL.Func([UserId], [ApiEmptyResult], []),
'query_blocks' : IDL.Func(
Expand Down
4 changes: 2 additions & 2 deletions frontend/services/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { extractOk } from "@/helpers/result";
import { BackendActor } from "./backend";

export const getCurrentUser = async (actor: BackendActor): Promise<User> => {
const res = await actor.get_user();
const res = await actor.get_my_user();
return extractOk(res);
};

export const getOrCreateCurrentUser = async (actor: BackendActor): Promise<User> => {
const res = await actor.get_user();
const res = await actor.get_my_user();

let user: User;

Expand Down
3 changes: 2 additions & 1 deletion src/backend/backend.did
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ service : (bool) -> {
"address" : () -> (ApiStringResult);
"balance" : () -> (ApiNatResult);
"check_tx" : (text) -> (ApiEmptyResult);
"get_user" : () -> (GetUserResult) query;
"get_user" : (principal) -> (GetUserResult) query;
"get_my_user" : () -> (GetUserResult) query;
"create_user" : () -> (CreateUserResult);
"promote_user_to_admin" : (UserId) -> (ApiEmptyResult);
"get_deployment" : (text) -> (GetDeploymentResult) query;
Expand Down
22 changes: 21 additions & 1 deletion src/backend/src/api/endpoints/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ use crate::api::{
};

#[query]
fn get_user() -> ApiResult<User> {
fn get_user(user_principal: Principal) -> ApiResult<User> {
let calling_principal = caller();

UsersEndpoints::default()
.get_user_as_admin(calling_principal, user_principal)
.into()
}

#[query]
fn get_my_user() -> ApiResult<User> {
let calling_principal = caller();

UsersEndpoints::default()
Expand Down Expand Up @@ -53,6 +62,17 @@ struct UsersEndpoints {
}

impl UsersEndpoints {
fn get_user_as_admin(
&self,
calling_principal: Principal,
user_principal: Principal,
) -> Result<User, ApiError> {
self.access_control_service
.assert_principal_is_admin(&calling_principal)?;

self.users_service.get_user(&user_principal.into())
}

fn get_user_by_principal(&self, calling_principal: Principal) -> Result<User, ApiError> {
self.access_control_service
.assert_principal_not_anonymous(&calling_principal)?;
Expand Down

0 comments on commit be89a78

Please sign in to comment.