@@ -59,20 +59,30 @@ function client_id () {
5959 return client ()?->id ?? 0 ;
6060
6161}
62- function is_admin () {
62+ function is_super () {
6363
64- return bool (user ()?->hasRole('admin ' ));
64+ return bool (user ()?->hasRole('admin ' )) && bool ( user ()?->has( ' super ' )) ;
6565
6666}
67- function is_client () {
67+ function is_supervisor () {
6868
69- return bool (user ()?->hasRole('client ' ));
69+ return bool (user ()?->hasRole('admin ' )) && bool (user ()?->has('supervisor ' ));
70+
71+ }
72+ function is_admin () {
73+
74+ return bool (user ()?->hasRole('admin ' ));
7075
7176}
7277function is_vendor () {
7378
7479 return bool (user ()?->hasRole('vendor ' ));
7580
81+ }
82+ function is_client () {
83+
84+ return bool (user ()?->hasRole('client ' ));
85+
7686}
7787function user_store_id () {
7888
@@ -107,15 +117,6 @@ function forced_user () {
107117
108118}
109119
110- function settings ( int $ storeId = null ) {
111-
112- $ storeId = $ storeId ?? store_id ();
113-
114- return remember ('model:setting ' , "public_ {$ storeId }" , 60 , fn () =>
115- Setting::withoutTenant ()->active ()->firstWhere ('store_id ' , $ storeId )
116- );
117-
118- }
119120function site_info ( int $ storeId = null ) {
120121
121122 $ storeId = $ storeId ?? store_id ();
@@ -158,6 +159,11 @@ function store () {
158159
159160 return app ()->bound ('store ' ) ? app ('store ' ) : null ;
160161
162+ }
163+ function store_parent () {
164+
165+ return store ()?->parent ?? null ;
166+
161167}
162168function store_admin () {
163169
@@ -168,16 +174,16 @@ function store_owner () {
168174
169175 return User::withoutTenant ()->where ('id ' , store ()?->owner_id ?? 0 )->first ();
170176
171- }
172- function store_parent () {
173-
174- return store ()?->parent ?? null ;
175-
176177}
177178function store_id () {
178179
179180 return store ()?->id ?? 0 ;
180181
182+ }
183+ function store_parent_id () {
184+
185+ return store ()?->parent_id ?? 0 ;
186+
181187}
182188function store_admin_id () {
183189
@@ -189,25 +195,32 @@ function store_owner_id () {
189195 return store_owner ()?->id ?? 0 ;
190196
191197}
192- function store_parent_id () {
198+ function with_store ( mixed $ store , mixed $ user = null , callable $ callback = null ) {
199+
200+ $ current = store ();
201+ set_store ($ store );
202+
203+ if ( $ user ) set_forced_user ($ user );
204+
205+ try { return $ callback ? $ callback () : null ; }
206+ finally { set_store ($ current ); unset_forced_user (); }
193207
194- return store_parent ()?->id ?? 0 ;
195-
196208}
197209
198210function user_has ( ...$ permissions ) {
199211
200- return user ()?->has(...$ permissions ) ?? false ;
212+ return ( bool ) user ()?->has(...$ permissions );
201213
202214}
203- function settings_has ( ...$ permissions ) {
204-
205- return settings ()?->has (...$ permissions ) ?? false ;
215+ function setting_has ( ...$ permissions ) {
216+
217+ return ( bool ) Setting:: hasPermission (...$ permissions );
206218
207219}
208220function store_has ( ...$ permissions ) {
209-
210- return store ()?->has(...$ permissions ) ?? false ;
221+
222+ $ parent = remember ('model:store ' , 'store_parent_ ' . store_id (), 60 , fn () => store_parent ());
223+ return (bool ) (!$ parent ? true : with_store ($ parent , callback: fn () => Store::hasPermission (...$ permissions )));
211224
212225}
213226function permissionkey ( string $ permission ) {
@@ -233,7 +246,7 @@ function limiter ( int $limit = 5, string $name = null, int $minutes = 1 ) {
233246}
234247function rateLimiter ( int $ limit = null , int $ minutes = 1 ) {
235248
236- $ limit = $ limit ?? 500 ;
249+ $ limit = $ limit ?? ( is_admin () ? 600 : 60 ) ;
237250 return "throttle: {$ limit }, {$ minutes }" ;
238251
239252}
@@ -244,7 +257,7 @@ function tenantMiddleware ( int $limit = null ) {
244257}
245258function authMiddleware ( string $ role = 'client ' , int $ limit = null ) {
246259
247- if ( config ('app.env ' ) === 'local ' ) return ["role: {$ role }" , rateLimiter ($ limit )];
248- return ['auth:sanctum ' , "role: {$ role }" , rateLimiter ($ limit )];
260+ if ( config ('app.env ' ) === 'local ' ) return ["role: {$ role }" , ' has:allow_logins ' , rateLimiter ($ limit )];
261+ return ['auth:sanctum ' , "role: {$ role }" , ' has:allow_logins ' , rateLimiter ($ limit )];
249262
250263}
0 commit comments