gmsol_store/instructions/
roles.rs1use anchor_lang::prelude::*;
2
3use crate::{states::Store, utils::internal};
4
5#[derive(Accounts)]
8pub struct CheckRole<'info> {
9 pub authority: Signer<'info>,
11 pub store: AccountLoader<'info, Store>,
13}
14
15pub(crate) fn check_admin(ctx: Context<CheckRole>) -> Result<bool> {
17 ctx.accounts
18 .store
19 .load()?
20 .has_admin_role(ctx.accounts.authority.key)
21}
22
23pub(crate) fn check_role(ctx: Context<CheckRole>, role: String) -> Result<bool> {
25 ctx.accounts
26 .store
27 .load()?
28 .has_role(ctx.accounts.authority.key, &role)
29}
30
31#[derive(Accounts)]
34pub struct HasRole<'info> {
35 pub store: AccountLoader<'info, Store>,
37}
38
39pub fn has_admin(ctx: Context<HasRole>, authority: Pubkey) -> Result<bool> {
41 ctx.accounts.store.load()?.has_admin_role(&authority)
42}
43
44pub fn has_role(ctx: Context<HasRole>, authority: Pubkey, role: String) -> Result<bool> {
46 ctx.accounts.store.load()?.has_role(&authority, &role)
47}
48
49#[derive(Accounts)]
53pub struct EnableRole<'info> {
54 pub authority: Signer<'info>,
56 #[account(mut)]
58 pub store: AccountLoader<'info, Store>,
59}
60
61pub(crate) fn unchecked_enable_role(ctx: Context<EnableRole>, role: String) -> Result<()> {
66 ctx.accounts.store.load_mut()?.enable_role(&role)
67}
68
69impl<'info> internal::Authentication<'info> for EnableRole<'info> {
70 fn authority(&self) -> &Signer<'info> {
71 &self.authority
72 }
73
74 fn store(&self) -> &AccountLoader<'info, Store> {
75 &self.store
76 }
77}
78
79#[derive(Accounts)]
83pub struct DisableRole<'info> {
84 pub authority: Signer<'info>,
86 #[account(mut)]
88 pub store: AccountLoader<'info, Store>,
89}
90
91pub fn unchecked_disable_role(ctx: Context<DisableRole>, role: String) -> Result<()> {
96 ctx.accounts.store.load_mut()?.disable_role(&role)
97}
98
99impl<'info> internal::Authentication<'info> for DisableRole<'info> {
100 fn authority(&self) -> &Signer<'info> {
101 &self.authority
102 }
103
104 fn store(&self) -> &AccountLoader<'info, Store> {
105 &self.store
106 }
107}
108
109#[derive(Accounts)]
113pub struct GrantRole<'info> {
114 pub authority: Signer<'info>,
116 #[account(mut)]
117 pub store: AccountLoader<'info, Store>,
119}
120
121pub(crate) fn unchecked_grant_role(
126 ctx: Context<GrantRole>,
127 user: Pubkey,
128 role: String,
129) -> Result<()> {
130 ctx.accounts.store.load_mut()?.grant(&user, &role)
131}
132
133impl<'info> internal::Authentication<'info> for GrantRole<'info> {
134 fn authority(&self) -> &Signer<'info> {
135 &self.authority
136 }
137
138 fn store(&self) -> &AccountLoader<'info, Store> {
139 &self.store
140 }
141}
142
143#[derive(Accounts)]
147pub struct RevokeRole<'info> {
148 pub authority: Signer<'info>,
150 #[account(mut)]
152 pub store: AccountLoader<'info, Store>,
153}
154
155pub(crate) fn unchecked_revoke_role(
160 ctx: Context<RevokeRole>,
161 user: Pubkey,
162 role: String,
163) -> Result<()> {
164 ctx.accounts.store.load_mut()?.revoke(&user, &role)
165}
166
167impl<'info> internal::Authentication<'info> for RevokeRole<'info> {
168 fn authority(&self) -> &Signer<'info> {
169 &self.authority
170 }
171
172 fn store(&self) -> &AccountLoader<'info, Store> {
173 &self.store
174 }
175}