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\Input; |
||||
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 boolean True if user is part of the group, false if not |
||||
60 | */ |
||||
61 | public function checkUserGroup( \Illuminate\Foundation\Auth\User $user, $groupcodes ) |
||||
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(); |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
74 | } catch( \Exception $e ) { |
||||
75 | $site = ( Route::current() ? Route::input( 'site', Input::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 ); |
||||
0 ignored issues
–
show
It seems like
$groupcodes can also be of type string ; however, parameter $groupcodes of Aimeos\Shop\Base\Support::checkGroups() does only seem to accept string[] , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
84 | } |
||||
85 | } |
||||
86 | |||||
87 | return $this->cache[$user->id][$groups]; |
||||
88 | } |
||||
89 | |||||
90 | |||||
91 | /** |
||||
92 | * Checks if the user with the given ID is in the specified group |
||||
93 | * |
||||
94 | * @param string $userid Unique user ID |
||||
95 | * @param string|array $groupcodes Unique user/customer group codes that are allowed |
||||
96 | * @return boolean True if user is part of the group, false if not |
||||
97 | * @deprecated Use checkUserGroup() instead |
||||
98 | */ |
||||
99 | public function checkGroup( $userid, $groupcodes ) |
||||
100 | { |
||||
101 | $groups = ( is_array( $groupcodes ) ? implode( ',', $groupcodes ) : $groupcodes ); |
||||
102 | |||||
103 | if( isset( $this->cache[$userid][$groups] ) ) { |
||||
104 | return $this->cache[$userid][$groups]; |
||||
105 | } |
||||
106 | |||||
107 | $site = ( Route::current() ? Route::input( 'site', Input::get( 'site', 'default' ) ) : 'default' ); |
||||
108 | |||||
109 | $context = $this->context->get( false ); |
||||
110 | $context->setLocale( $this->locale->getBackend( $context, $site ) ); |
||||
0 ignored issues
–
show
It seems like
$site can also be of type object ; however, parameter $site of Aimeos\Shop\Base\Locale::getBackend() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
111 | |||||
112 | return $this->cache[$userid][$groups] = $this->checkGroups( $context, $userid, $groupcodes ); |
||||
0 ignored issues
–
show
It seems like
$groupcodes can also be of type string ; however, parameter $groupcodes of Aimeos\Shop\Base\Support::checkGroups() does only seem to accept string[] , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
113 | } |
||||
114 | |||||
115 | |||||
116 | /** |
||||
117 | * Returns the available group codes |
||||
118 | * |
||||
119 | * @param \Aimeos\MShop\Context\Item\Iface $context Context item |
||||
120 | * @return string[] List of group codes |
||||
121 | */ |
||||
122 | public function getGroups( \Aimeos\MShop\Context\Item\Iface $context ) |
||||
123 | { |
||||
124 | $list = array(); |
||||
125 | $manager = \Aimeos\MShop::create( $context, 'customer/group' ); |
||||
126 | |||||
127 | $search = $manager->createSearch(); |
||||
128 | $search->setConditions( $search->compare( '==', 'customer.group.id', $context->getGroupIds() ) ); |
||||
129 | |||||
130 | foreach( $manager->searchItems( $search ) as $item ) { |
||||
131 | $list[] = $item->getCode(); |
||||
132 | } |
||||
133 | |||||
134 | return $list; |
||||
135 | } |
||||
136 | |||||
137 | |||||
138 | /** |
||||
139 | * Checks if one of the groups is associated to the given user ID |
||||
140 | * |
||||
141 | * @param \Aimeos\MShop\Context\Item\Iface $context Context item |
||||
142 | * @param string $userid ID of the logged in user |
||||
143 | * @param string[] $groupcodes List of group codes to check against |
||||
144 | * @return boolean True if the user is in one of the groups, false if not |
||||
145 | */ |
||||
146 | protected function checkGroups( \Aimeos\MShop\Context\Item\Iface $context, $userid, $groupcodes ) |
||||
147 | { |
||||
148 | $manager = \Aimeos\MShop::create( $context, 'customer/group' ); |
||||
149 | |||||
150 | $search = $manager->createSearch(); |
||||
151 | $search->setConditions( $search->compare( '==', 'customer.group.code', (array) $groupcodes ) ); |
||||
152 | $groupItems = $manager->searchItems( $search ); |
||||
153 | |||||
154 | |||||
155 | $manager = \Aimeos\MShop::create( $context, 'customer/lists' ); |
||||
156 | |||||
157 | $search = $manager->createSearch(); |
||||
158 | $expr = array( |
||||
159 | $search->compare( '==', 'customer.lists.parentid', $userid ), |
||||
160 | $search->compare( '==', 'customer.lists.refid', array_keys( $groupItems ) ), |
||||
161 | $search->compare( '==', 'customer.lists.domain', 'customer/group' ), |
||||
162 | ); |
||||
163 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||||
164 | $search->setSlice( 0, 1 ); |
||||
165 | |||||
166 | return (bool) count( $manager->searchItems( $search ) ); |
||||
167 | } |
||||
168 | } |
||||
169 |