Skip to content

Commit

Permalink
Merge pull request #4 from markhuot/scopes
Browse files Browse the repository at this point in the history
Adds token scopes
  • Loading branch information
markhuot authored Aug 13, 2017
2 parents 9373ca0 + f41f8a7 commit 54bbc60
Show file tree
Hide file tree
Showing 45 changed files with 1,107 additions and 449 deletions.
54 changes: 34 additions & 20 deletions src/Controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,31 @@ function __construct(

function actionIndex()
{
// \Yii::beginProfile('craftQl');

// You must set the header to JSON, otherwise Craft will see HTML and try to insert
// javascript at the bottom to run pending tasks
$response = \Craft::$app->getResponse();
$response->headers->add('Content-Type', 'application/json; charset=UTF-8');

$writable = true;
$token = false;
$user = Craft::$app->getUser()->getIdentity();

if (!$user) {
$authorization = Craft::$app->request->headers->get('Authorization');
preg_match('/^bearer\s+(?<tokenId>.+)/', $authorization, $matches);
$tokenId = @$matches['tokenId'];
if ($tokenId) {
$token = Token::find()->where(['token' => $tokenId])->one();
if ($token) {
$writable = $token->isWritable;
$user = User::find()->where(['id' => $token->userId])->one();
Craft::$app->getUser()->loginByUserId($user->id);
}

$authorization = Craft::$app->request->headers->get('authorization');
preg_match('/^(?:b|B)earer\s+(?<tokenId>.+)/', $authorization, $matches);
$tokenId = @$matches['tokenId'];
if ($tokenId) {
$token = Token::find()->where(['token' => $tokenId])->one();
}
else {
$user = Craft::$app->getUser()->getIdentity();
if ($user) {
$token = Token::forUser($user);
}
}

// @todo, check user permissions when PRO license

if (!$user) {
if (!$token) {
http_response_code(403);
$this->asJson([
'errors' => [
Expand All @@ -68,18 +67,33 @@ function actionIndex()
$this->graphQl->bootstrap();

try {
$result = $this->graphQl->execute($this->request->input(), $this->request->variables());
$schema = $this->graphQl->getSchema($token);
$result = $this->graphQl->execute($schema, $this->request->input(), $this->request->variables());
} catch (\Exception $e) {
$backtrace = [];
foreach ($e->getTrace() as $index => $trace) {
if ($index > 10) { break; }

$backtrace[] = [
'function' => $trace['function'],
'file' => @$trace['file'],
'line' => @$trace['line'],
];
}

$result = [
'errors' => [
'message' => $e->getMessage()
'message' => $e->getMessage(),
'line' => $e->getLine(),
'file' => $e->getFile(),
'backtrace' => $backtrace,
]
];
}

// $index = 1;
// foreach ($this->graphQl->getTimers() as $key => $timer) {
// header('X-Timer-'.$index++.'-'.ucfirst($key).': '.$timer);
// \Yii::endProfile('craftQl');
// if (true) {
// $result['timings'] = \Yii::getLogger()->getProfiling(['yii\db*']);
// }

$this->asJson($result);
Expand Down
32 changes: 32 additions & 0 deletions src/Controllers/CpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ function actionTokendelete($tokenId)
$this->redirect('/admin/settings/plugins/craftql');
}

function actionTokenscopes($tokenId)
{
$this->renderTemplate('craftql/scopes', [
'token' => Token::find()->where(['id' => $tokenId])->one()
]);
}

function actionSavetokenscopes($tokenId)
{
$token = Token::find()->where(['id' => $tokenId])->one();
$token->name = $_POST['token']['name'];
$token->scopes = json_encode(@$_POST['scope'] ?: []);
$token->save();

Craft::$app->getSession()->setNotice(Craft::t('app', 'Scopes saved.'));

$this->redirect('/admin/craftql/token/'.$tokenId.'/scopes');
}

function actionIndex()
{
$this->redirect('craftql/browse');
Expand All @@ -42,6 +61,19 @@ function actionGraphiql()

$this->renderTemplate('craftql/graphiql', [
'url' => "{$url}{$uri}",
'token' => false,
]);
}

function actionGraphiqlas($token)
{
$url = \craft\helpers\UrlHelper::siteUrl();
$instance = \markhuot\CraftQL\Plugin::getInstance();
$uri = $instance->settings->uri;

$this->renderTemplate('craftql/graphiql', [
'url' => "{$url}{$uri}",
'token' => $token,
]);
}
}
72 changes: 72 additions & 0 deletions src/Factories/BaseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace markhuot\CraftQL\Factories;

use GraphQL\Type\Definition\EnumType;

abstract class BaseFactory {

protected $repository;
protected $request;
private $objects = [];
private $enum;

function __construct($repository, $request) {
$this->repository = $repository;
$this->request = $request;
}

// function repository() {
// return $this->repository;
// }

function get($id, $mode='query') {
if ($this->can($id, $mode) === false) {
return false;
}

if (isset($this->objects[$id])) {
return $this->objects[$id];
}

return $this->objects[$id] = $this->make($this->repository->get($id), $this->request);
}

abstract function can($id, $mode='query');
abstract function make($raw, $request);

function all($mode='query') {
$objects = [];

foreach ($this->repository->all() as $raw) {
if ($object = $this->get($raw->id, $mode)) {
$objects[] = $object;
}
}

return $objects;
}

function enumValueName($object) {
return $object->name;
}

function enum() {
if (!empty($this->enum)) {
return $this->enum;
}

$values = [];

foreach ($this->all() as $index => $object) {
$values[$this->enumValueName($object)] = @$object->config['id'];
}

$reflect = new \ReflectionClass($this);
return $this->enum = new EnumType([
'name' => $reflect->getShortName().'sEnum',
'values' => $values,
]);
}

}
18 changes: 18 additions & 0 deletions src/Factories/CategoryGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace markhuot\CraftQL\Factories;

use markhuot\CraftQL\Factories\BaseFactory;
use markhuot\CraftQL\Types\CategoryGroup as CategoryGroupObjectType;

class CategoryGroup extends BaseFactory {

function make($raw, $request) {
return new CategoryGroupObjectType($raw, $request);
}

function can($id, $mode='query') {
return true;
}

}
18 changes: 18 additions & 0 deletions src/Factories/EntryType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace markhuot\CraftQL\Factories;

use markhuot\CraftQL\Factories\BaseFactory;
use markhuot\CraftQL\Types\EntryType as EntryTypeObjectType;

class EntryType extends BaseFactory {

function make($raw, $request) {
return new EntryTypeObjectType($raw, $request);
}

function can($id, $mode='query') {
return $this->request->token()->can("{$mode}:entryType:{$id}");
}

}
29 changes: 29 additions & 0 deletions src/Factories/Section.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace markhuot\CraftQL\Factories;

use markhuot\CraftQL\Factories\BaseFactory;
use markhuot\CraftQL\Types\Section as SectionObjectType;

class Section extends BaseFactory {

function make($raw, $request) {
return new SectionObjectType($raw, $request);
}

function can($id, $mode='query') {
$section = $this->repository->get($id);
foreach ($section->entryTypes as $type) {
if ($this->request->token()->canNot("{$mode}:entryType:{$type->id}")) {
return false;
}
}

return true;
}

function enumValueName($object) {
return preg_replace('/Section$/', '', $object->name);
}

}
18 changes: 18 additions & 0 deletions src/Factories/Volume.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace markhuot\CraftQL\Factories;

use markhuot\CraftQL\Factories\BaseFactory;
use markhuot\CraftQL\Types\Volume as VolumeObjectType;

class Volume extends BaseFactory {

function make($raw, $request) {
return new VolumeObjectType($raw, $request);
}

function can($id, $mode='query') {
return true;
}

}
2 changes: 1 addition & 1 deletion src/Fields/AssetsBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function getGraphQLMutationArgs() {
];
}

public function getGraphQLQueryFields() {
public function getGraphQLQueryFields($token) {
$field = $this->owner;

return [
Expand Down
4 changes: 2 additions & 2 deletions src/Fields/CategoriesBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public function getGraphQLMutationArgs() {
];
}

public function getGraphQLQueryFields() {
public function getGraphQLQueryFields($request) {
$field = $this->owner;

preg_match('/^group:(\d+)$/', $field->source, $matches);
$groupId = $matches[1];

return [
$field->handle => [
'type' => Type::listOf(\markhuot\CraftQL\Repositories\CategoryGroup::getGroup($groupId)),
'type' => Type::listOf($request->categoryGroup($groupId)),
'description' => $field->instructions,
'resolve' => function ($root, $args) use ($field) {
return $root->{$field->handle}->all();
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/DateBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function getGraphQLMutationArgs() {
];
}

public function getGraphQLQueryFields() {
public function getGraphQLQueryFields($token) {
$field = $this->owner;

return [
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/DefaultBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function getGraphQLMutationArgs() {
];
}

public function getGraphQLQueryFields() {
public function getGraphQLQueryFields($token) {
$field = $this->owner;

return [
Expand Down
Loading

0 comments on commit 54bbc60

Please sign in to comment.