@@ -65,40 +65,56 @@ protected function registerMiddlewares(): void
6565 */
6666 protected function registerBladeDirectives (): void
6767 {
68- Blade::if ('impersonating ' , function (?string $ guard = null ): bool {
69- if ($ guard !== null ) {
70- return session ()->has ('mirror.impersonating ' ) && session ('mirror.guard_name ' ) === $ guard ;
71- }
68+ Blade::if ('impersonating ' , fn (?string $ guard = null ): bool => $ this ->checkImpersonating ($ guard ));
69+ Blade::if ('canImpersonate ' , fn (?string $ guard = null ): bool => $ this ->checkCanImpersonate ($ guard ));
70+ Blade::if ('canBeImpersonated ' , fn (?Authenticatable $ user = null , ?string $ guard = null ): bool => $ this ->checkCanBeImpersonated ($ user , $ guard ));
71+ }
72+
73+ /**
74+ * Check if the authenticated model is impersonating.
75+ */
76+ protected function checkImpersonating (?string $ guard ): bool
77+ {
78+ if ($ guard !== null ) {
79+ return session ()->has ('mirror.impersonating ' ) && session ('mirror.guard_name ' ) === $ guard ;
80+ }
7281
73- return Mirror::isImpersonating ();
74- });
82+ return Mirror::isImpersonating ();
83+ }
84+
85+ /**
86+ * Check if the authenticated model can impersonate.
87+ */
88+ protected function checkCanImpersonate (?string $ guard ): bool
89+ {
90+ $ user = auth ($ guard )->user ();
7591
76- Blade::if ('canImpersonate ' , function (?string $ guard = null ): bool {
92+ if (! $ user ) {
93+ return false ;
94+ }
95+
96+ // @phpstan-ignore-next-line function.alreadyNarrowedType
97+ return method_exists ($ user , 'canImpersonate ' )
98+ ? $ user ->canImpersonate ()
99+ : true ;
100+ }
101+
102+ /**
103+ * Check if the authenticated model can be impersonated.
104+ */
105+ protected function checkCanBeImpersonated (?Authenticatable $ user , ?string $ guard ): bool
106+ {
107+ if (! $ user instanceof Authenticatable) {
77108 $ user = auth ($ guard )->user ();
109+ }
110+
111+ if (! $ user ) {
112+ return false ;
113+ }
78114
79- if (! $ user ) {
80- return false ;
81- }
82-
83- // @phpstan-ignore-next-line function.alreadyNarrowedType
84- return method_exists ($ user , 'canImpersonate ' )
85- ? $ user ->canImpersonate ()
86- : true ;
87- });
88-
89- Blade::if ('canBeImpersonated ' , function ($ user = null , ?string $ guard = null ): bool {
90- if ($ user === null ) {
91- $ user = auth ($ guard )->user ();
92- }
93-
94- if (! $ user instanceof Authenticatable) {
95- return false ;
96- }
97-
98- // @phpstan-ignore-next-line function.alreadyNarrowedType
99- return method_exists ($ user , 'canBeImpersonated ' )
100- ? $ user ->canBeImpersonated ()
101- : true ;
102- });
115+ // @phpstan-ignore-next-line function.alreadyNarrowedType
116+ return method_exists ($ user , 'canBeImpersonated ' )
117+ ? $ user ->canBeImpersonated ()
118+ : true ;
103119 }
104120}
0 commit comments