Issues (66)

src/Aimeos/Shop/Base/Support.php (1 issue)

Labels
Severity
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();
0 ignored issues
show
The property siteid does not seem to exist on Illuminate\Foundation\Auth\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
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
		$manager = \Aimeos\MShop::create( $context, 'customer/group' );
100
101
		$search = $manager->createSearch();
102
		$search->setConditions( $search->compare( '==', 'customer.group.id', $context->getGroupIds() ) );
103
104
		return $manager->searchItems( $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\Context\Item\Iface $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\Context\Item\Iface $context, string $userid, $groupcodes ) : bool
117
	{
118
		$manager = \Aimeos\MShop::create( $context, 'customer/group' );
119
120
		$search = $manager->createSearch();
121
		$search->setConditions( $search->compare( '==', 'customer.group.code', (array) $groupcodes ) );
122
		$groupIds = $manager->searchItems( $search )->keys()->toArray();
123
124
125
		$manager = \Aimeos\MShop::create( $context, 'customer/lists' );
126
127
		$search = $manager->createSearch();
128
		$expr = array(
129
			$search->compare( '==', 'customer.lists.parentid', $userid ),
130
			$search->compare( '==', 'customer.lists.refid', $groupIds ),
131
			$search->compare( '==', 'customer.lists.domain', 'customer/group' ),
132
		);
133
		$search->setConditions( $search->combine( '&&', $expr ) );
134
		$search->setSlice( 0, 1 );
135
136
		return (bool) count( $manager->searchItems( $search ) );
137
	}
138
}
139