Permalink
Browse files

Normalise Wordpress role names to upper case, with a prefix of 'ROLE_…

…WP_'
  • Loading branch information...
mrtorrent committed Oct 9, 2011
1 parent 7d62211 commit 93fa325ac9d9c11b86a2a88113b6db71126a7615
@@ -16,6 +16,7 @@
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Hypebeast\WordpressBundle\Wordpress\ApiAbstraction;
+use Hypebeast\WordpressBundle\Utilities\RoleUtilities;
/**
* WordpressCookieAuthenticationProvider will verify that the current user has been authenticated
@@ -49,7 +50,8 @@ public function authenticate(TokenInterface $token)
{
$user = $this->api->wp_get_current_user();
if ($user->ID != 0) {
- $authenticatedToken = new WordpressCookieToken($user->roles);
+ $authenticatedToken = new WordpressCookieToken(
+ RoleUtilities::normalise_role_names($user->roles));
$authenticatedToken->setUser($user->user_login);
return $authenticatedToken;
}
@@ -17,6 +17,7 @@
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
use Symfony\Component\DependencyInjection\ContainerInterface;
+use Hypebeast\WordpressBundle\Utilities\RoleUtilities;
/**
* WordpressLoginAuthenticationProvider will authenticate the user with Wordpress
@@ -73,7 +74,7 @@ public function authenticate(TokenInterface $token)
if ($user instanceof \WP_User) {
$authenticatedToken = new UsernamePasswordToken(
$user->user_login, $token->getCredentials(), $token->getProviderKey(),
- $user->roles);
+ RoleUtilities::normalise_role_names($user->roles));
return $authenticatedToken;
@@ -3,6 +3,7 @@
namespace Hypebeast\WordpressBundle\Tests\Security\Authentication\Provider;
use Hypebeast\WordpressBundle\Security\Authentication\Provider\WordpressCookieAuthenticationProvider;
+use Symfony\Component\Security\Core\Role\Role;
/**
* Test class for WordpressCookieAuthenticationProvider.
@@ -61,6 +62,10 @@ public function testAuthenticateLoggedInUser() {
);
$this->assertTrue($result->isAuthenticated());
$this->assertEquals($user->user_login, $result->getUser());
+ $this->assertEquals(
+ array(new Role('ROLE_WP_SOMEROLE'), new Role('ROLE_WP_ANOTHERROLE')),
+ $result->getRoles()
+ );
}
/**
@@ -4,6 +4,7 @@
use Hypebeast\WordpressBundle\Security\Authentication\Provider\WordpressLoginAuthenticationProvider;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
+use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\HttpFoundation\Request;
/**
@@ -71,6 +72,10 @@ public function testAuthenticateLogsUserIntoWordpress()
$this->assertTrue($result->isAuthenticated());
$this->assertEquals($username, $result->getUsername());
$this->assertEquals($password, $result->getCredentials());
+ $this->assertEquals(
+ array(new Role('ROLE_WP_SOMEROLE'), new Role('ROLE_WP_ANOTHERROLE')),
+ $result->getRoles()
+ );
}
public function testAuthenticateWithRememberMeUsesWordpressRememberMe()
@@ -0,0 +1,47 @@
+<?php
+
+namespace Hypebeast\WordpressBundle\Tests\Utilities;
+
+use Hypebeast\WordpressBundle\Utilities\RoleUtilities;
+
+/**
+ * Test class for RoleUtilities.
+ * Generated by PHPUnit on 2011-10-09 at 19:00:32.
+ */
+class RoleUtilitiesTest extends \PHPUnit_Framework_TestCase
+{
+
+ /**
+ * Sets up the fixture, for example, opens a network connection.
+ * This method is called before a test is executed.
+ */
+ protected function setUp()
+ {
+
+ }
+
+ /**
+ * Tears down the fixture, for example, closes a network connection.
+ * This method is called after a test is executed.
+ */
+ protected function tearDown()
+ {
+
+ }
+
+ public function testNormaliseRoleNames()
+ {
+ $this->assertEquals(
+ array('ROLE_WP_A', 'ROLE_WP_B'),
+ RoleUtilities::normalise_role_names(array('a', 'b'))
+ );
+
+ $this->assertEquals(
+ array('PRE-A-SUF', 'PRE-B-SUF'),
+ RoleUtilities::normalise_role_names(array('a', 'b'), 'pre-', '-suf')
+ );
+ }
+
+}
+
+?>
@@ -0,0 +1,43 @@
+<?php
+
+/**
+ * Contains the RoleUtilities class, part of the Symfony Wordpress Bundle
+ *
+ * @package Hypebeast\WordpressBundle
+ * @subpackage Utilities
+ * @author Miquel Rodríguez Telep / Michael Rodríguez-Torrent <mike@themikecam.com>
+ */
+
+namespace Hypebeast\WordpressBundle\Utilities;
+
+/**
+ * RoleUtilities provides utility methods for manipulating roles
+ *
+ * @package Hypebeast\WordpressBundle
+ * @subpackage Utilities
+ * @author Miquel Rodríguez Telep / Michael Rodríguez-Torrent <mike@themikecam.com>
+ */
+abstract class RoleUtilities
+{
+
+ /**
+ * Normalises role names to upper case with the specified prefix and suffix
+ *
+ * @param array $roles the role names to normalise
+ * @param type $prefix defaults to 'ROLE_WP_'
+ * @param type $suffix defaults to nothing ('')
+ *
+ * @return string[] the transformed role names
+ */
+ static public function normalise_role_names(array $roles, $prefix = 'ROLE_WP_', $suffix = '')
+ {
+ return array_map(
+ function ($role) use ($prefix, $suffix)
+ {
+ return strtoupper($prefix . $role . $suffix);
+ },
+ $roles
+ );
+ }
+
+}

0 comments on commit 93fa325

Please sign in to comment.