Skip to content

Commit

Permalink
Merge branch 'master' of github.com:spira/spira into feature/gulpfile…
Browse files Browse the repository at this point in the history
…-performance-improvements

* 'master' of github.com:spira/spira:
  style patch
  user roles in token represented by the array
  style patch
  createUser helper method fix to create really unique user
  • Loading branch information
zakhenry committed Nov 11, 2015
2 parents 4cd7593 + f29aed9 commit 0a2cc98
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 27 deletions.
27 changes: 27 additions & 0 deletions api/app/Http/Transformers/UserTokenTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Spira framework.
*
* @link https://github.com/spira/spira
*
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
*/

namespace App\Http\Transformers;

use App\Models\User;

class UserTokenTransformer extends EloquentModelTransformer
{
public function transform($object)
{
/* @var User $object */
$object->setHidden(['roles']);
$roles = $object->roles->lists('key')->toArray();
$object = parent::transform($object);
$object['roles'] = $roles;

return $object;
}
}
7 changes: 3 additions & 4 deletions api/app/Providers/AuthDriverServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

namespace App\Providers;

use App\Http\Transformers\UserTokenTransformer;
use Illuminate\Http\Request;
use Spira\Auth\User\UserProvider;
use Spira\Auth\User\SocialiteAuthenticatable;
use Illuminate\Contracts\Auth\Authenticatable;
use App\Http\Transformers\EloquentModelTransformer;
use Spira\Auth\Providers\JWTAuthDriverServiceProvider;

class AuthDriverServiceProvider extends JWTAuthDriverServiceProvider
Expand All @@ -32,9 +32,8 @@ protected function getPayloadGenerators()
parent::getPayloadGenerators(),
[
'_user' => function (Authenticatable $user) {
$user->roles;
/** @var EloquentModelTransformer $transformer */
$transformer = $this->app->make(EloquentModelTransformer::class);
/** @var UserTokenTransformer $transformer */
$transformer = $this->app->make(UserTokenTransformer::class);

return $transformer->transformItem($user);
},
Expand Down
17 changes: 17 additions & 0 deletions api/tests/SpiraAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,26 @@
*/

use Illuminate\Http\Request;
use Spira\Auth\Driver\Guard;

class SpiraAuthTest extends TestCase
{
public function testRolesInToken()
{
$user = $this->createUser();
$this->assignAdmin($user);

$token = $this->tokenFromUser($user);

/** @var Guard $auth */
$auth = $this->app->make('auth');
$payload = $auth->getTokenizer()->decode($token);
$this->assertArrayHasKey('_user', $payload);
$this->assertArrayNotHasKey('_roles', $payload['_user']);
$this->assertArrayHasKey('roles', $payload['_user']);
$this->assertCount(2, $payload['_user']['roles'], 'User has 2 roles - default and admin');
}

public function testRequestUserResolver()
{
$user = $this->createUser();
Expand Down
46 changes: 23 additions & 23 deletions api/tests/Traits/HelpersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,43 @@ trait HelpersTrait
*
* @var array
*/
protected $uniqueUserValues;
protected static $userFaker;

/**
* @return Faker
*/
protected function getFakerWithUniqueUserData()
{
// Prepare an array with user data already used
$users = User::all();
if (! $this->uniqueUserValues) {
if (is_null(self::$userFaker)) {
// Prepare an array with user data already used
$users = User::all();

$uniques = ['username' => [], 'email' => []];
foreach ($users as $user) {
array_push($uniques['username'], [$user->username => null]);
array_push($uniques['email'], [$user->email => null]);
$uniques['username'][$user->username] = null;
$uniques['email'][$user->email] = null;
}

$this->uniqueUserValues = $uniques;
}

// As the array of already used faker data is protected in Faker and
// has no accessor method, we'll rely on ReflectionObject to modify
// the property before letting faker generate data.
// As the array of already used faker data is protected in Faker and
// has no accessor method, we'll rely on ReflectionObject to modify
// the property before letting faker generate data.

//though reflected object should be added to the faker itself somehow
// which is hacky
//so we decided to overcome it with bindTo hack
$faker = Faker::create();
$unique = $faker->unique();
//though reflected object should be added to the faker itself somehow
// which is hacky
//so we decided to overcome it with bindTo hack
$faker = Faker::create();
$unique = $faker->unique();

$binder = function ($value) {
$this->uniques = $value;
};
$binder = function ($value) {
$this->uniques = $value;
};

$uniqueBinder = $binder->bindTo($unique, $unique);
$uniqueBinder($this->uniqueUserValues);
$uniqueBinder = $binder->bindTo($unique, $unique);
$uniqueBinder($uniques);
self::$userFaker = $faker;
}

return $faker;
return self::$userFaker;
}

/**
Expand Down

0 comments on commit 0a2cc98

Please sign in to comment.