Skip to content

Commit ca5270e

Browse files
committed
initial plugin commit
0 parents  commit ca5270e

File tree

163 files changed

+15687
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

163 files changed

+15687
-0
lines changed

Diff for: .editorconfig

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = false
5+
6+
[*]
7+
indent_style = space
8+
indent_size = 4
9+
charset = "utf-8"
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.yml]
15+
indent_style = space
16+
indent_size = 2

Diff for: .gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.idea
2+
*.pyc
3+
*.cmd
4+
docs/_build
5+
phpunit.xml
6+
vendor/
7+
composer.lock
8+
tmp
9+
pmip
10+
webroot/coverage

Diff for: .semver

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
:major: 1
3+
:minor: 0
4+
:patch: 0
5+
:special: ''

Diff for: README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Api plugin for CakePHP
2+
======================
3+
4+
[![Bake Status](https://secure.travis-ci.org/CakeDC/cakephp-api.png?branch=master)](http://travis-ci.org/CakeDC/cakephp-api)
5+
[![Downloads](https://poser.pugx.org/CakeDC/cakephp-api/d/total.png)](https://packagist.org/packages/CakeDC/cakephp-api)
6+
[![Latest Version](https://poser.pugx.org/CakeDC/cakephp-api/v/stable.png)](https://packagist.org/packages/CakeDC/cakephp-api)
7+
[![License](https://poser.pugx.org/CakeDC/cakephp-api/license.svg)](https://packagist.org/packages/CakeDC/cakephp-api)
8+
9+
The **CakePHP-API** Plugin allows to easily expose a versioned API in a few lines of code.
10+
11+
Requirements
12+
------------
13+
14+
* CakePHP 3.2.9+
15+
* PHP 5.5.9+
16+
17+
Documentation
18+
-------------
19+
20+
For documentation, as well as tutorials, see the [Docs](docs/home.md) directory of this repository.
21+
22+
Support
23+
-------
24+
25+
For bugs and feature requests, please use the [issues](https://github.com/CakeDC/api/issues) section of this repository.
26+
27+
Commercial support is also available, [contact us](http://cakedc.com/contact) for more information.
28+
29+
Contributing
30+
------------
31+
32+
This repository follows the [CakeDC Plugin Standard](http://cakedc.com/plugin-standard). If you'd like to contribute new features, enhancements or bug fixes to the plugin, please read our [Contribution Guidelines](http://cakedc.com/contribution-guidelines) for detailed instructions.
33+
34+
License
35+
-------
36+
37+
Copyright 2016 Cake Development Corporation (CakeDC). All rights reserved.
38+
39+
Licensed under the [MIT](http://www.opensource.org/licenses/mit-license.php) License. Redistributions of the source code included in this repository must retain the copyright notice found in each file.

Diff for: composer.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "cakedc/cakephp-api",
3+
"description": "Api plugin for CakePHP",
4+
"type": "cakephp-plugin",
5+
"require": {
6+
"php": ">=5.4.16",
7+
"cakephp/cakephp": "3.*"
8+
},
9+
"require-dev": {
10+
"cakedc/users": "*" ,
11+
"phpunit/phpunit": "*"
12+
},
13+
"autoload": {
14+
"psr-4": {
15+
"CakeDC\\Api\\": "src",
16+
"CakeDC\\Api\\Test\\Fixture\\": "tests\\Fixture"
17+
}
18+
},
19+
"autoload-dev": {
20+
"psr-4": {
21+
"CakeDC\\Api\\Test\\": "tests",
22+
"Cake\\Test\\": "./vendor/cakephp/cakephp/tests"
23+
}
24+
}
25+
}

Diff for: config/api.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright 2016, Cake Development Corporation (http://cakedc.com)
4+
*
5+
* Licensed under The MIT License
6+
* Redistributions of files must retain the above copyright notice.
7+
*
8+
* @copyright Copyright 2016, Cake Development Corporation (http://cakedc.com)
9+
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10+
*/
11+
12+
return [
13+
'Api' => [
14+
// if service class is not defined we use crud fallback service
15+
'ServiceFallback' => '\\CakeDC\\Api\\Service\\FallbackService',
16+
// response rendered as JSend
17+
'renderer' => 'CakeDC/Api.JSend',
18+
// Data parse from cakephp request object
19+
'parser' => 'CakeDC/Api.Form',
20+
21+
// version is not used
22+
'useVersioning' => false,
23+
'versionPrefix' => 'v',
24+
25+
// auth permission uses require auth strategy
26+
'Auth' => [
27+
'Crud' => [
28+
'default' => 'auth'
29+
],
30+
],
31+
32+
'Service' => [
33+
'default' => [
34+
'options' => [],
35+
'Action' => [
36+
'default' => [
37+
//auth configuration
38+
'Auth' => [
39+
'authorize' => [
40+
'CakeDC/Api.Crud' => []
41+
],
42+
'authenticate' => [
43+
'CakeDC/Api.Token' => [
44+
'require_ssl' => false,
45+
]
46+
],
47+
],
48+
// default app extensions
49+
'Extension' => [
50+
// allow request from other domains
51+
'CakeDC/Api.Cors',
52+
// enable sort
53+
'CakeDC/Api.Sort',
54+
// load Hateoas
55+
'CakeDC/Api.CrudHateoas',
56+
// enable relations
57+
'CakeDC/Api.CrudRelations',
58+
]
59+
],
60+
// all index actions configuration
61+
'Index' => [
62+
'Extension' => [
63+
// enable pagination for index actions
64+
'CakeDC/Api.Paginate',
65+
],
66+
],
67+
],
68+
],
69+
],
70+
]
71+
];

Diff for: config/bootstrap.php

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php/** * Copyright 2016, Cake Development Corporation (http://cakedc.com) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @copyright Copyright 2016, Cake Development Corporation (http://cakedc.com) * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */use Cake\Core\Configure;Configure::load('CakeDC/Api.api');collection((array)Configure::read('Api.config'))->each(function ($file) { Configure::load($file);});

Diff for: config/routes.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright 2016, Cake Development Corporation (http://cakedc.com)
4+
*
5+
* Licensed under The MIT License
6+
* Redistributions of files must retain the above copyright notice.
7+
*
8+
* @copyright Copyright 2016, Cake Development Corporation (http://cakedc.com)
9+
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10+
*/
11+
12+
use Cake\Core\Configure;
13+
use Cake\Routing\Router;
14+
15+
Router::plugin('CakeDC/Api', ['path' => '/api'], function ($routes) {
16+
$useVersioning = Configure::read('Api.useVersioning');
17+
$versionPrefix = Configure::read('Api.versionPrefix');
18+
if (empty($versionPrefix)) {
19+
$versionPrefix = 'v';
20+
}
21+
if ($useVersioning) {
22+
$routes->connect('/:version/describe/*', [
23+
'plugin' => 'CakeDC/Api',
24+
'controller' => 'Api',
25+
'action' => 'describe'
26+
], ['version' => $versionPrefix . '\d+', 'pass' => []]);
27+
$routes->connect('/:version/list/*', [
28+
'plugin' => 'CakeDC/Api',
29+
'controller' => 'Api',
30+
'action' => 'listing'
31+
], ['version' => $versionPrefix . '\d+', 'pass' => []]);
32+
$routes->connect('/:version/:service/*', [
33+
'plugin' => 'CakeDC/Api',
34+
'controller' => 'Api',
35+
'action' => 'process'
36+
], ['version' => $versionPrefix . '\d+', 'pass' => []]);
37+
}
38+
$routes->connect('/describe/*', [
39+
'plugin' => 'CakeDC/Api',
40+
'controller' => 'Api',
41+
'action' => 'describe'
42+
]);
43+
$routes->connect('/list/*', [
44+
'plugin' => 'CakeDC/Api',
45+
'controller' => 'Api',
46+
'action' => 'listing'
47+
]);
48+
$routes->connect('/:service/*', [
49+
'plugin' => 'CakeDC/Api',
50+
'controller' => 'Api',
51+
'action' => 'process'
52+
]);
53+
});

Diff for: docs/Documentation/Renderers/jsend.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Jsend
2+
=====
3+
4+
How to use?
5+
-----------
6+
7+
By default we are already using this configuration. Then you don't need to do anything more unless the basic configuration.
8+
9+
10+
Response Types
11+
--------------
12+
13+
**Success:** When an API call is successful, the JSend object is used as a simple envelope for the results, using the data key, as in the following example:
14+
15+
**GET**
16+
17+
When you use this route */api/list* and you have two controllers "Articles" and "Posts" for example you shall get this result.
18+
19+
```
20+
{
21+
"status": "success",
22+
"data": [
23+
"articles",
24+
"posts"
25+
],
26+
"links": []
27+
}
28+
```
29+
30+
**POST**
31+
32+
When you use this route */api/describe?token=YOUR_AUTH_TOKEN*. It'll describe your service. Here we are show an example using a service of the plugin.
33+
34+
We need to send three fields in the post to do this request.
35+
36+
```
37+
username: user
38+
password: password
39+
service: listing
40+
```
41+
42+
This will give a response like that:
43+
```
44+
{
45+
"status": "success",
46+
"data": [],
47+
"links": []
48+
}
49+
```
50+

Diff for: docs/Documentation/Renderers/json.md

Whitespace-only changes.

Diff for: docs/Documentation/Renderers/raw.md

Whitespace-only changes.

Diff for: docs/Documentation/Renderers/xml.md

Whitespace-only changes.

Diff for: docs/Documentation/auth.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
## Authentication and Authorization.
2+
3+
Auth system was inspired from cakephp framework auth component, and follow ideas that put into this system.
4+
5+
6+
## Auth Configuration
7+
8+
Auth configuration should be defined as part of current Action configuration module.
9+
The default strategy for reading Action configuration described in previous step.
10+
11+
So global Auth configuration one should put into `Api.Service.default.Action.default.Auth`.
12+
13+
* `allow` - defines list of allowed for current service actions (could be action name, array of names, or ```'*'```).
14+
* `authorize` - defines what authorizes should be loaded to Authorize access.
15+
* `authenticate` - defines authentication strategy.
16+
17+
Example:
18+
19+
```php
20+
'Auth' => [
21+
'allow' => '*', //public access
22+
'authorize' => [
23+
'CakeDC/Api.Crud' => []
24+
],
25+
'authenticate' => [
26+
'CakeDC/Api.Token' => [
27+
'require_ssl' => false,
28+
]
29+
],
30+
],
31+
```
32+
### TokenAuthenticate
33+
34+
#### Setup
35+
36+
TokenAuthenticate default configuration is
37+
```php
38+
protected $_defaultConfig = [
39+
//type, can be either querystring or header
40+
'type' => self::TYPE_QUERYSTRING,
41+
//name to retrieve the api key value from
42+
'name' => 'token',
43+
//db table where the key is stored
44+
'table' => 'users',
45+
//db field where the key is stored
46+
'field' => 'api_token',
47+
//require SSL to pass the token. You should always require SSL to use tokens for Auth
48+
'require_ssl' => true,
49+
];
50+
```
51+
52+
We are using query strings for passing the api_token token. And we require SSL by default.
53+
Note you can override these options, passing settings in Auth configuration for TokenAuthenticate.
54+
55+
56+
### Simple Rbac Authorize.
57+
58+
Simple Rbac Authorize is based on CakeDC Users plugin Simple Rbac Authorize with modification to api structure.
59+
60+
#### Permission rules syntax
61+
62+
* Rules are evaluated top-down, first matching rule will apply
63+
* Each rule is defined:
64+
```php
65+
[
66+
'role' => 'REQUIRED_NAME_OF_THE_ROLE_OR_[]_OR_*',
67+
'version' => 'OPTIONAL_VERSION_USED_OR_[]_OR_*_DEFAULT_NULL',
68+
'service' => 'REQUIRED_NAME_OF_THE_SERVICE_OR_[]_OR_*'
69+
'action' => 'REQUIRED_NAME_OF_ACTION_OR_[]_OR_*',
70+
'allowed' => 'OPTIONAL_BOOLEAN_OR_CALLABLE_OR_INSTANCE_OF_RULE_DEFAULT_TRUE'
71+
]
72+
```
73+
* If no rule allowed = true is matched for a given user role and url, default return value will be false
74+

0 commit comments

Comments
 (0)