Skip to content

Commit

Permalink
Merge 33e26bb into df382f2
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhenry committed Aug 18, 2015
2 parents df382f2 + 33e26bb commit c139927
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 33 deletions.
2 changes: 1 addition & 1 deletion api/.env
Expand Up @@ -16,7 +16,7 @@ AUTH_MODEL=App\Models\User
DB_CONNECTION=pgsql
DB_HOST={$DATABASE_PORT_5432_TCP_ADDR}
DB_PORT=5432
DB_DATABASE=spira
DB_DATABASE={$POSTGRES_DB}
DB_USERNAME={$POSTGRES_USER}
DB_PASSWORD={$POSTGRES_PASSWORD}

Expand Down
28 changes: 25 additions & 3 deletions api/app/Http/Controllers/EntityController.php
Expand Up @@ -8,6 +8,7 @@

namespace App\Http\Controllers;

use App\Exceptions\BadRequestException;
use App\Extensions\Controller\RequestValidationTrait;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -66,12 +67,15 @@ public function getAllPaginated(PaginatedRequestDecoratorInterface $request)
* @param string $id
* @return ApiResponse
*/
public function getOne($id)
public function getOne(Request $request, $id)
{
$model = $this->findOrFailEntity($id);

$item = $this->getEntityWithNested($model, $request);

return $this->getResponse()
->transformer($this->getTransformer())
->item($model)
->transformer($this->transformer)
->item($item)
;
}

Expand Down Expand Up @@ -338,6 +342,24 @@ protected function getModel()
return $this->model;
}

private function getEntityWithNested(BaseModel $model, Request $request)
{
$nested = $request->headers->get('With-Nested');
if (!$nested) {
return $model;
}

$requestedRelations = explode(', ', $nested);

try {
$model->load($requestedRelations);
} catch (\BadMethodCallException $e) {
throw new BadRequestException(sprintf('Invalid `With-Nested` request - one or more of the following relationships do not exist for %s:[%s]', get_class($model), $nested), null, $e);
}

return $model;
}

/**
* Get id validation rule from model validation rules
* Can be overriden by validateIdRule property
Expand Down
2 changes: 1 addition & 1 deletion api/app/Http/Transformers/BaseTransformer.php
Expand Up @@ -38,7 +38,7 @@ public function getService()
public function transformCollection($collection)
{
if ($collection instanceof Collection) {
$collection = $collection->all();
$collection = $collection->all(); //remove the items marked as deleted
}

return $this->getService()->collection($collection, $this);
Expand Down
46 changes: 31 additions & 15 deletions api/app/Http/Transformers/EloquentModelTransformer.php
Expand Up @@ -11,6 +11,7 @@
use App\Helpers\RouteHelper;
use Carbon\Carbon;
use Illuminate\Contracts\Support\Arrayable;
use Spira\Model\Collection\Collection;
use Spira\Model\Model\BaseModel;
use Traversable;

Expand Down Expand Up @@ -65,7 +66,9 @@ public function transform($object)
}

if (($object instanceof BaseModel)) {
$this->addSelfKey($object, $array);
$array = $this->addSelfKey($object, $array);

$array = $this->nestRelations($object, $array);
}

return $array;
Expand Down Expand Up @@ -106,24 +109,13 @@ private function castAttribute($castTypes, $key, $value)
* @param BaseModel $model
* @param $array
*/
protected function addSelfKey(BaseModel $model, &$array)
protected function addSelfKey(BaseModel $model, $array)
{
if ($route = RouteHelper::getRoute($model)) {
$array['_self'] = $route;
$array = ['_self' => $route] + $array;
}

foreach ($model->getRelations() as $key => $value) {
$camelCaseKey = camel_case($key);
if ($value instanceof BaseModel && isset($array[$camelCaseKey])) {
$this->addSelfKey($value, $array[$camelCaseKey]);
} elseif ($this->isIterable($value)) {
foreach ($value as $index => $relatedModel) {
if ($relatedModel instanceof BaseModel && isset($array[$camelCaseKey][$index])) {
$this->addSelfKey($relatedModel, $array[$camelCaseKey][$index]);
}
}
}
}
return $array;
}

/**
Expand Down Expand Up @@ -194,4 +186,28 @@ protected function camelCase($str)

return $prefix.camel_case($str);
}

/**
* Get the objects nested entities transformed
* @param $object
* @param $array
* @return mixed
*/
private function nestRelations($object, $array)
{
if (count($object['relations']) > 0) {
foreach ($object['relations'] as $relation => $childModelOrCollection) {
$childTransformed = null;
if ($childModelOrCollection instanceof Collection) {
$childTransformed = $this->transformCollection($childModelOrCollection);
} else {
$childTransformed = $this->transformItem($childModelOrCollection);
}

$array = $array + ['_' . $relation => $childTransformed];
}
}

return $array;
}
}
25 changes: 15 additions & 10 deletions api/tests/Services/SingleSignOnTest.php
Expand Up @@ -274,21 +274,26 @@ public function testSsoString()
{
$user = [
'username' => null,
'avatar_img_url' => null
'avatar_img_url' => null,
];

$requester = Mockery::mock(VanillaSingleSignOn::class);
$response = $requester->ssoString($user);
$data = explode(' ', $response);
$callback = 'categories';
$request = $this->mockRequest(false, false, false, $callback);

$ssoClass = Mockery::mock(VanillaSingleSignOn::class, [$request, $user]);

$ssoString = $ssoClass->ssoString($user);
$ssoStringPieces = explode(' ', $ssoString);

$string = $ssoStringPieces[0];
$hash = $ssoStringPieces[1];
$timestamp = $ssoStringPieces[2];
$algo = $ssoStringPieces[3];

$string = $data[0];
$hash = $data[1];
$timestamp = $data[2];
$algo = $data[3];
$user['client_id'] = null;
$user['client_id'] = env('VANILLA_JSCONNECT_CLIENT_ID');

$this->assertEquals(base64_encode(json_encode($user)), $string);
$this->assertEquals(hash_hmac('sha1', "$string $timestamp", null), $hash);
$this->assertEquals(hash_hmac('sha1', "$string $timestamp", env('VANILLA_JSCONNECT_SECRET')), $hash);
$this->assertEquals('hmacsha1', $algo);
}
}
4 changes: 3 additions & 1 deletion app/src/common/services/article/articleService.ts
Expand Up @@ -55,7 +55,9 @@ namespace common.services.article {
*/
public getArticle(identifier:string):ng.IPromise<common.models.Article> {

return this.ngRestAdapter.get('/articles/'+identifier)
return this.ngRestAdapter.get('/articles/'+identifier, {
'With-Nested' : 'permalinks, metas'
})
.then((res) => ArticleService.articleFactory(res.data));

}
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.build.yml
Expand Up @@ -5,6 +5,8 @@ data:
extends:
file: ./docker/common-services.yml
service: datavolume
volumes:
- %build_directory%:/data
env_file:
- ./docker/.local.env

Expand Down
1 change: 1 addition & 0 deletions docker/.local.env
Expand Up @@ -4,6 +4,7 @@ APP_HOST=http://local.app.spira.io
XDEBUG_ENABLED=true

# spira main postgres database
POSTGRES_DB=spira
POSTGRES_PASSWORD=spira
POSTGRES_USER=spira

Expand Down
1 change: 1 addition & 0 deletions docker/.qa.env
Expand Up @@ -4,6 +4,7 @@ APP_HOST=http://qa.app.spira.io
XDEBUG_ENABLED=false

# spira main postgres database
POSTGRES_DB=spira_qa
POSTGRES_PASSWORD=spira_qa
POSTGRES_USER=spira_qa

Expand Down
6 changes: 4 additions & 2 deletions docker/common-services.yml
Expand Up @@ -44,6 +44,7 @@ php:
env_file:
- .common.env
- .local.env
- .secret.env
privileged: true

web:
Expand All @@ -53,8 +54,9 @@ web:
privileged: true

devtools:
image: spira/docker-dev-tools:latest
# image: spira/docker-dev-tools:phantomjs-1.9.8
# image: spira/docker-dev-tools:latest #built version of latest phantomjs (build often fails)
image: spira/docker-dev-tools:phantomjs-2.0.0 #static binary of phantomjs 2.0.0
# image: spira/docker-dev-tools:phantomjs-1.9.8 #static bindary of phantomjs 1.9.8
working_dir: /data
privileged: true
env_file:
Expand Down

0 comments on commit c139927

Please sign in to comment.