komple_framework_utils/
lib.rs

1use cosmwasm_std::{Addr, StdError};
2use thiserror::Error;
3
4#[cfg(feature = "funds")]
5pub mod funds;
6#[cfg(feature = "response")]
7pub mod response;
8pub mod shared;
9#[cfg(feature = "storage")]
10pub mod storage;
11
12pub fn check_admin_privileges(
13    sender: &Addr,
14    contract_addr: &Addr,
15    admin: &Addr,
16    parent_addr: Option<Addr>,
17    operators: Option<Vec<Addr>>,
18) -> Result<(), UtilError> {
19    let mut has_privileges = sender == contract_addr;
20
21    if !has_privileges && sender == admin {
22        has_privileges = true;
23    }
24
25    if !has_privileges {
26        if let Some(parent) = parent_addr {
27            if sender == &parent {
28                has_privileges = true;
29            }
30        }
31    }
32
33    if !has_privileges {
34        if let Some(operators) = operators {
35            for operator in operators {
36                if sender == &operator {
37                    has_privileges = true;
38                    break;
39                }
40            }
41        }
42    }
43
44    match has_privileges {
45        true => Ok(()),
46        false => Err(UtilError::Unauthorized {}),
47    }
48}
49
50#[derive(Error, Debug, PartialEq)]
51pub enum UtilError {
52    #[error("{0}")]
53    Std(#[from] StdError),
54
55    #[error("Unauthorized")]
56    Unauthorized {},
57}