Completed
Push — master ( 014888...f34bf4 )
by Aimeos
02:25
created

Support::checkGroup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
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
	/**
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 string $userid Unique user ID
0 ignored issues
show
Bug introduced by
There is no parameter named $userid. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
53
	 * @param string|array $groupcodes Unique user/customer group codes that are allowed
54
	 * @return boolean True if user is part of the group, false if not
55
	 */
56
	public function checkUserGroup( \Illuminate\Foundation\Auth\User $user, $groupcodes )
57
	{
58
		$context = $this->context->get( false );
59
60
		try {
61
			$site = \Aimeos\MShop\Factory::createManager( $context, 'locale/site' )->getItem( $user->siteid )->getCode();
62
		} catch( \Exception $e ) {
63
			$site = ( Route::current() ? Route::input( 'site', Input::get( 'site', 'default' ) ) : 'default' );
64
		}
65
66
		$context->setLocale( $this->locale->getBackend( $context, $site ) );
67
68
		foreach( array_reverse( $context->getLocale()->getSitePath() ) as $siteid )
69
		{
70
			if( $user->siteid === $siteid ) {
71
				return $this->checkGroups( $context, $user->id, $groupcodes );
72
			}
73
		}
74
75
		return false;
76
	}
77
78
79
	/**
80
	 * Checks if the user with the given ID is in the specified group
81
	 *
82
	 * @param string $userid Unique user ID
83
	 * @param string|array $groupcodes Unique user/customer group codes that are allowed
84
	 * @return boolean True if user is part of the group, false if not
85
	 * @deprecated Use checkUserGroup() instead
86
	 */
87
	public function checkGroup( $userid, $groupcodes )
88
	{
89
		$site = ( Route::current() ? Route::input( 'site', Input::get( 'site', 'default' ) ) : 'default' );
90
91
		$context = $this->context->get( false );
92
		$context->setLocale( $this->locale->getBackend( $context, $site ) );
93
94
		return $this->checkGroups( $context, $userid, $groupcodes );
95
	}
96
97
98
	/**
99
	 * Returns the available group codes
100
	 *
101
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item
102
	 * @return string[] List of group codes
103
	 */
104
	public function getGroups( \Aimeos\MShop\Context\Item\Iface $context )
105
	{
106
		$list = array();
107
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/group' );
108
109
		$search = $manager->createSearch();
110
		$search->setConditions( $search->compare( '==', 'customer.group.id', $context->getGroupIds() ) );
111
112
		foreach( $manager->searchItems( $search ) as $item ) {
113
			$list[] = $item->getCode();
114
		}
115
116
		return $list;
117
	}
118
119
120
	/**
121
	 * Checks if one of the groups is associated to the given user ID
122
	 *
123
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item
124
	 * @param string $userid ID of the logged in user
125
	 * @param string[] $groupcodes List of group codes to check against
126
	 * @return boolean True if the user is in one of the groups, false if not
127
	 */
128
	protected function checkGroups( \Aimeos\MShop\Context\Item\Iface $context, $userid, $groupcodes )
129
	{
130
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/group' );
131
132
		$search = $manager->createSearch();
133
		$search->setConditions( $search->compare( '==', 'customer.group.code', (array) $groupcodes ) );
134
		$groupItems = $manager->searchItems( $search );
135
136
137
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/lists' );
138
139
		$search = $manager->createSearch();
140
		$expr = array(
141
			$search->compare( '==', 'customer.lists.parentid', $userid ),
142
			$search->compare( '==', 'customer.lists.refid', array_keys( $groupItems ) ),
143
			$search->compare( '==', 'customer.lists.domain', 'customer/group' ),
144
		);
145
		$search->setConditions( $search->combine( '&&', $expr ) );
146
		$search->setSlice( 0, 1 );
147
148
		return (bool) count( $manager->searchItems( $search ) );
149
	}
150
}