Skip to content

added user_data and user_data_template endpoints. added authorization. #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,36 @@
'byIdRelationships',
Phramework::METHOD_ANY
],
[
'user_data_template/', //URI
NS . 'UserDataTemplateController', //Class
'GET', //Class method
Phramework::METHOD_GET, //HTTP Method
],
[
'user_data_template/{id}', //URI
NS . 'UserDataTemplateController', //Class
'GETById', //Class method
Phramework::METHOD_GET, //HTTP Method
],
[
'user_data/', //URI
NS . 'UserDataController', //Class
'GET', //Class method
Phramework::METHOD_GET, //HTTP Method
],
[
'user_data/{id}', //URI
NS . 'UserDataController', //Class
'GETById', //Class method
Phramework::METHOD_GET, //HTTP Method
],
[
'user_data/', //URI
NS . 'UserDataController', //Class
'POST', //Class method
Phramework::METHOD_POST, //HTTP Method
],
]);

//Initialize API with settings and routing
Expand All @@ -109,6 +139,22 @@
Phramework::setViewer(
\Phramework\JSONAPI\Viewers\JSONAPI::class
);
//Set authentication class
\Phramework\Authentication\Manager::register(
\Phramework\Authentication\BasicAuthentication\BasicAuthentication::class
);

//Set method to fetch user object, including password attribute
\Phramework\Authentication\Manager::setUserGetByEmailMethod(
[
\Phramework\Examples\JSONAPI\Models\Administrator\Authentication::class,
'getByEmailWithPassword'
]
);

\Phramework\Authentication\Manager::setAttributes(
['email']
);

unset($settings);

Expand Down
150 changes: 150 additions & 0 deletions src/Controllers/UserDataController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php
declare(strict_types=1);
/*
* Copyright 2015-2016 Xenofon Spafaridis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Phramework\Examples\JSONAPI\Controllers;

use Phramework\Examples\JSONAPI\Models\UserDataTemplate;
use Phramework\Examples\JSONAPI\Request;
use Phramework\Examples\JSONAPI\Models\UserData;
use Phramework\Phramework;
use Phramework\Validate\ObjectValidator;

/**
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Kostas Nikolopoulos <[email protected]>
*/
class UserDataController extends \Phramework\Examples\JSONAPI\Controller
{
/**
* Get collection
* @param \stdClass $params Request parameters
* @param string $method Request method
* @param array $headers Request headers
*/
public static function GET( \stdClass $params, string $method, array $headers)
{
$user = Request::checkPermission();

static::handleGET(
$params,
UserData::class,
[$user->id],
[]
);
}

/**
* Get a resource
* @param \stdClass $params Request parameters
* @param string $method Request method
* @param array $headers Request headers
* @param string $id Resource id
*/
public static function GETById(\stdClass $params, string $method, array $headers, string $id)
{
$user = Request::checkPermission();

static::handleGETById(
$params,
$id,
UserData::class,
[$user->id],
[]
);
}

/**
* Post new resource
* @param \stdClass $params Request parameters
* @param string $method Request method
* @param array $headers Request headers
*/
public static function POST(\stdClass $params, string $method, array $headers)
{
$user = Request::checkPermission();

static::handlePOST(
$params,
$method,
$headers,
UserData::class,
[],
[],
[
/**
* A validation callback to inject user id
*/
function (
\stdClass $requestAttributes,
\stdClass $requestRelationships,
\stdClass $attributes,
\stdClass $parsedRelationshipAttributes
) use ($user) {
$attributes->user_id = $user->id; // inject user id from authorization
$template = UserDataTemplate::getById($requestRelationships->template->data->id); // get template in order to create validator
$validator = new ObjectValidator();
$templateJson = json_decode($template->attributes->value);
$validator = $validator->createFromObject($templateJson); // create validator
$attributes->value = $validator->parse($attributes->value); // validate data
}
],
/**
* Override view, by setting a view callback
* to response with 204 and a Location header
*/
function (array $ids) {
//Prepare response with 201 Created status code and Location header
\Phramework\Models\Response::created(
UserData::getSelfLink($ids[0])
);
\Phramework\JSONAPI\Viewers\JSONAPI::header();
//Will overwrite 201 with 204 status code
\Phramework\Models\Response::noContent();
}
);
}

/**
* Manage resource's relationships
* `/article/{id}/relationships/{relationship}/` handler
* @param \stdClass $params Request parameters
* @param string $method Request method
* @param array $headers Request headers
* @param string $id Resource id
* @param string $relationship Relationship
*/
public static function byIdRelationships(
\stdClass $params,
string $method,
array $headers,
string $id,
string $relationship
) {
static::handleByIdRelationships(
$params,
$method,
$headers,
$id,
$relationship,
UserData::class,
[Phramework::METHOD_GET],
[],
[]
);
}
}
65 changes: 65 additions & 0 deletions src/Controllers/UserDataTemplateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
/*
* Copyright 2015-2016 Xenofon Spafaridis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Phramework\Examples\JSONAPI\Controllers;

use Phramework\Phramework;
use Phramework\Examples\JSONAPI\Models\UserDataTemplate;

/**
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <[email protected]>
*/
class UserDataTemplateController extends \Phramework\Examples\JSONAPI\Controller
{
/**
* Get collection
* @param \stdClass $params Request parameters
* @param string $method Request method
* @param array $headers Request headers
*/
public static function GET( \stdClass $params, string $method, array $headers)
{
static::handleGET(
$params,
UserDataTemplate::class,
[],
[]
);
}

/**
* Get a resource
* @param \stdClass $params Request parameters
* @param string $method Request method
* @param array $headers Request headers
* @param string $id Resource id
*/
public static function GETById( \stdClass $params, string $method, array $headers, string $id)
{
static::handleGETById(
$params,
$id,
UserDataTemplate::class,
[],
[]
);
}


}
Loading