Completed
Push — master ( 734c2f...840a38 )
by Aimeos
03:42
created

AdminController::hasRole()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 8.8571
cc 5
eloc 7
nc 3
nop 1
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package symfony
7
 * @subpackage Controller
8
 */
9
10
11
namespace Aimeos\ShopBundle\Controller;
12
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
16
17
/**
18
 * Aimeos controller for the /admin route
19
 *
20
 * @package symfony
21
 * @subpackage Controller
22
 */
23
class AdminController extends Controller
24
{
25
	/**
26
	 * Returns the initial HTML view for the admin interface.
27
	 *
28
	 * @param Request $request Symfony request object
29
	 * @return Response Generated HTML page for the admin interface
30
	 */
31
	public function indexAction( Request $request )
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
	{
33
		if( $this->hasRole( ['ROLE_ADMIN'] ) )
34
		{
35
			$context = $this->get( 'aimeos_context' )->get( false );
36
			$siteManager = \Aimeos\MShop\Factory::createManager( $context, 'locale/site' );
37
			$siteItem = $siteManager->getItem( $this->getUser()->getSiteId() );
38
39
			$params = array( 'site' => $siteItem->getCode(), 'resource' => 'dashboard' );
40
			return $this->redirect( $this->generateUrl( 'aimeos_shop_jqadm_search', $params ) );
41
		}
42
43
44
		$param = array( 'error' => '', 'username' => '' );
45
46
		if( $this->has( 'security.authentication_utils' ) )
47
		{
48
			$auth = $this->get( 'security.authentication_utils' );
49
50
			$param['error'] = $auth->getLastAuthenticationError();
51
			$param['username'] = $auth->getLastUsername();
52
		}
53
54
		return $this->render( 'AimeosShopBundle:Admin:index.html.twig', $param );
55
	}
56
57
58
	/**
59
	 * Checks if the used is authenticated and has the admin role
60
	 *
61
	 * @param array $roles List of role names where at least one must match
62
	 * @return boolean True if authenticated and is admin, false if not
63
	 */
64
	protected function hasRole( array $roles )
65
	{
66
		if( $this->has( 'security.authorization_checker' ) && $this->get( 'security.token_storage' )->getToken() )
67
		{
68
			$checker = $this->get( 'security.authorization_checker' );
69
70
			foreach( $roles as $role )
71
			{
72
				if( $checker->isGranted( $role ) ) {
73
					return true;
74
				}
75
			}
76
		}
77
78
		return false;
79
	}
80
}
81