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 $access = []; |
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->access[$user->id][$groups] ) ) { |
66
|
|
|
return $this->access[$user->id][$groups]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->access[$user->id][$groups] = false; |
70
|
|
|
|
71
|
|
|
if( $siteid = current( array_reverse( explode( '.', trim( $user->siteid, '.' ) ) ) ) ) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$context = $this->context->get( false ); |
74
|
|
|
$site = \Aimeos\MShop::create( $context, 'locale/site' )->get( $siteid )->getCode(); |
75
|
|
|
$site = ( Route::current() ? Route::input( 'site', Request::get( 'site', $site ) ) : $site ); |
76
|
|
|
|
77
|
|
|
$context->setLocale( $this->locale->getBackend( $context, $site ) ); |
78
|
|
|
|
79
|
|
|
foreach( array_reverse( $context->locale()->getSitePath() ) as $siteid ) |
80
|
|
|
{ |
81
|
|
|
if( (string) $user->siteid === (string) $siteid ) { |
82
|
|
|
$this->access[$user->id][$groups] = $this->checkGroups( $context, $user->id, $groupcodes ); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->access[$user->id][$groups]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns the available group codes |
93
|
|
|
* |
94
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context item |
95
|
|
|
* @return string[] List of group codes |
96
|
|
|
*/ |
97
|
|
|
public function getGroups( \Aimeos\MShop\ContextIface $context ) : array |
98
|
|
|
{ |
99
|
|
|
$manager = \Aimeos\MShop::create( $context, 'customer/group' ); |
100
|
|
|
|
101
|
|
|
$search = $manager->filter(); |
102
|
|
|
$search->setConditions( $search->compare( '==', 'customer.group.id', $context->groups() ) ); |
103
|
|
|
|
104
|
|
|
return $manager->search( $search )->getCode()->toArray(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Checks if one of the groups is associated to the given user ID |
110
|
|
|
* |
111
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context item |
112
|
|
|
* @param string $userid ID of the logged in user |
113
|
|
|
* @param string[]|string $groupcodes List of group codes to check against |
114
|
|
|
* @return bool True if the user is in one of the groups, false if not |
115
|
|
|
*/ |
116
|
|
|
protected function checkGroups( \Aimeos\MShop\ContextIface $context, string $userid, $groupcodes ) : bool |
117
|
|
|
{ |
118
|
|
|
$manager = \Aimeos\MShop::create( $context, 'customer/group' ); |
119
|
|
|
|
120
|
|
|
$search = $manager->filter(); |
121
|
|
|
$search->setConditions( $search->compare( '==', 'customer.group.code', (array) $groupcodes ) ); |
122
|
|
|
$groupIds = $manager->search( $search )->keys()->toArray(); |
123
|
|
|
|
124
|
|
|
$manager = \Aimeos\MShop::create( $context, 'customer/lists' ); |
125
|
|
|
|
126
|
|
|
$search = $manager->filter()->slice( 0, 1 ); |
127
|
|
|
$expr = array( |
128
|
|
|
$search->compare( '==', 'customer.lists.parentid', $userid ), |
129
|
|
|
$search->compare( '==', 'customer.lists.refid', $groupIds ), |
130
|
|
|
$search->compare( '==', 'customer.lists.domain', 'customer/group' ), |
131
|
|
|
); |
132
|
|
|
$search->setConditions( $search->combine( '&&', $expr ) ); |
133
|
|
|
|
134
|
|
|
return !$manager->search( $search )->isEmpty(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.