1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @license MIT, http://opensource.org/licenses/MIT |
||
5 | * @copyright Aimeos (aimeos.org), 2015-2023 |
||
6 | */ |
||
7 | |||
8 | namespace Aimeos\Shop\Base; |
||
9 | |||
10 | |||
11 | use Illuminate\Support\Facades\Request; |
||
12 | use Illuminate\Support\Facades\Route; |
||
13 | |||
14 | |||
15 | /** |
||
16 | * Service providing the supporting functionality |
||
17 | */ |
||
18 | class Support |
||
19 | { |
||
20 | /** |
||
21 | * @var \Aimeos\Shop\Base\Context |
||
22 | */ |
||
23 | private $context; |
||
24 | |||
25 | /** |
||
26 | * @var \Aimeos\Shop\Base\Locale |
||
27 | */ |
||
28 | private $locale; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $access = []; |
||
34 | |||
35 | |||
36 | /** |
||
37 | * Initializes the object |
||
38 | * |
||
39 | * @param \Aimeos\Shop\Base\Context $context Context provider |
||
40 | * @param \Aimeos\Shop\Base\Locale $locale Locale provider |
||
41 | */ |
||
42 | public function __construct( \Aimeos\Shop\Base\Context $context, \Aimeos\Shop\Base\Locale $locale ) |
||
43 | { |
||
44 | $this->context = $context; |
||
45 | $this->locale = $locale; |
||
46 | } |
||
47 | |||
48 | |||
49 | /** |
||
50 | * Checks if the user is in the specified group and associatied to the site |
||
51 | * |
||
52 | * @param \Illuminate\Foundation\Auth\User $user Authenticated user |
||
53 | * @param string|array $groupcodes Unique user/customer group codes that are allowed |
||
54 | * @return bool True if user is part of the group, false if not |
||
55 | */ |
||
56 | public function checkUserGroup( \Illuminate\Foundation\Auth\User $user, $groupcodes ) : bool |
||
57 | { |
||
58 | $groups = ( is_array( $groupcodes ) ? implode( ',', $groupcodes ) : $groupcodes ); |
||
59 | |||
60 | if( isset( $this->access[$user->id][$groups] ) ) { |
||
61 | return $this->access[$user->id][$groups]; |
||
62 | } |
||
63 | |||
64 | $this->access[$user->id][$groups] = false; |
||
65 | |||
66 | $context = $this->context->get( false ); |
||
67 | $siteid = current( array_reverse( explode( '.', trim( $user->siteid, '.' ) ) ) ); |
||
68 | |||
69 | if( $siteid ) { |
||
70 | $site = \Aimeos\MShop::create( $context, 'locale/site' )->get( $siteid )->getCode(); |
||
71 | } else { |
||
72 | $site = config( 'shop.mshop.locale.site', 'default' ); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
73 | } |
||
74 | |||
75 | $site = ( Route::current() ? Route::input( 'site', Request::get( 'site', $site ) ) : $site ); |
||
76 | $context->setLocale( $this->locale->getBackend( $context, $site ) ); |
||
77 | |||
78 | foreach( array_reverse( $context->locale()->getSitePath() ) as $siteid ) |
||
79 | { |
||
80 | if( $user->siteid === '' || $user->siteid === $siteid ) { |
||
81 | $this->access[$user->id][$groups] = $this->checkGroups( $context, $user->id, $groupcodes ); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | return $this->access[$user->id][$groups]; |
||
86 | } |
||
87 | |||
88 | |||
89 | /** |
||
90 | * Checks if one of the groups is associated to the given user ID |
||
91 | * |
||
92 | * @param \Aimeos\MShop\ContextIface $context Context item |
||
93 | * @param string $userid ID of the logged in user |
||
94 | * @param string[]|string $groupcodes List of group codes to check against |
||
95 | * @return bool True if the user is in one of the groups, false if not |
||
96 | */ |
||
97 | protected function checkGroups( \Aimeos\MShop\ContextIface $context, string $userid, $groupcodes ) : bool |
||
98 | { |
||
99 | $manager = \Aimeos\MShop::create( $context, 'group' ); |
||
100 | |||
101 | $search = $manager->filter(); |
||
102 | $search->setConditions( $search->compare( '==', 'group.code', (array) $groupcodes ) ); |
||
103 | $groupIds = $manager->search( $search )->keys()->toArray(); |
||
104 | |||
105 | $manager = \Aimeos\MShop::create( $context, 'customer/lists' ); |
||
106 | |||
107 | $search = $manager->filter()->slice( 0, 1 ); |
||
108 | $expr = array( |
||
109 | $search->compare( '==', 'customer.lists.parentid', $userid ), |
||
110 | $search->compare( '==', 'customer.lists.refid', $groupIds ), |
||
111 | $search->compare( '==', 'customer.lists.domain', 'group' ), |
||
112 | ); |
||
113 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||
114 | |||
115 | return !$manager->search( $search )->isEmpty(); |
||
116 | } |
||
117 | } |
||
118 |