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