Skip to content

Latest commit

 

History

History
403 lines (312 loc) · 12.6 KB

README.md

File metadata and controls

403 lines (312 loc) · 12.6 KB

CakePHP example implementation

This is a small example of an API implementation using the framework CakePHP in version #5.

The use case chosen is the creation and processing of applicants for a company.

The main documentation for CakePHP 5 can be read here.

Goal and Status

Status legend:

  • ✅ Done
  • 🛠 WIP (Work in progress)
  • 📜 TODO
  • Authentication via:
    • ✅ Username + Password
    • ✅ Token
  • Authorizations:
    • ✅ to view applicants
    • ✅ to edit applicants
    • ✅ Admin
  • REST API
    • ✅ Manage applicants
    • ✅ Manage users
  • Web GUI
    • ✅ Login
    • ✅ Manage applicants
    • ✅ Manage users
  • Automated tests
    • Unit tests
      • 🛠 Applicant
      • ✅ User
    • Integration tests
      • 📜 General
      • 🛠 Applicant
      • 🛠 User
  • API documentation:
  • Fixing issues:
    • 📜 #1 Reactivating Cross Site Request Forgery (CSRF) Protection Middleware
    • 🛠 #2 Deprecated-Warning when running integration-tests dor Controllers
    • 📜 #3 Beautyfy the Web-GUI
    • 📜 #4 command bin/cake add_admin prints the password at input and the token
    • 📜 #5 force secure passwords

Setup

  • You have to install PHP 8.1 or newer.
  • You have to install MySQL.
  • Create a Database and a user for MySQL for this project
  • Clone this project.
  • Reinstall the CakePHP-project with
    php composer.phar install --working-dir="cms"
  • configure your database at cms/config/app_local.php
  • migrate the database:
    cd cms
    bin/cake migrations migrate
  • Then you can run a dev-server:
    cd cms
    bin/cake server
    # bin/cake server --help

There is a more detailed documentation, how i setup my environment on linux at:

/doc/001-Setup.md

(but you can also inspire there if you use windows)

Authentication

Using

Add a admin

You can add a new Admin-user

cd cms
bin/cake add_admin

This will also create and display a random Toke.

Login with Username and Password (in Web-GUI):

http://localhost:8765/users/login

and to logout: http://localhost:8765/users/logout

To change your password: http://localhost:8765/users/edit-password/{id}

(replace {id} with the id of your user)

Use a Token

You can authentikate at every Request with the HTTP-Header Authorization and token with the prefix Token like shown in this example using curl:

curl -i -X GET \
    -H "Authorization: Token f6f376ceeb5170c4dcd07a7f3fbcb2fc8432f846a6fb0c2c8ae67ea37dbdc962" \
    http://localhost:8765/api/users.json

.. or you use the url-query-parameter token

curl -i -X GET \
    http://localhost:8765/api/users.json?token=f6f376ceeb5170c4dcd07a7f3fbcb2fc8432f846a6fb0c2c8ae67ea37dbdc962

To reset your token to a new one: http://localhost:8765/users/edit-token/{id}

(replace {id})

.. or use http://localhost:8765/api/users/edit-token/{id}.json

(replace {id} and {token})

curl -i -X PUT \
    -H "Authorization: Token {token}" \
    -H "Content-Type: application/json" \
    -d '' \
    http://localhost:8765/api/users/edit-token/{id}.json

Development

For Authentication the Authentication-plugin is used:

TODO: Issue 📜 #4 command bin/cake add_admin prints the password at input and the token

Authorizations

Using

There are 3 Permissions:

  • isAdmin
  • canViewApplicants
  • canEditApplicants

Everyone can add a user without login.

Only you or an Admin can change your Password or your Token.

Only a Admin can change permissions at:

Only users with Permission canViewApplicants (or isAdmin) can view the Applicants.

Only users with Permission canViewApplicants AND canEditApplicants (or isAdmin) can add, edit or delete the Applicants (only canEditApplicants without canViewApplicants will not work).

Admin (user with permission isAdmin) can everything.

Development

The Authorization-plugin is used.

see:

The implemented Policies are at cms/src/Policy .

REST API

Manage applicants

Manage users

Web GUI

Login

Manage applicants

Manage users

Automated tests

Development

For testing PHPUnit in combination with CakePHP is used.

Documentation can be read here:

Implementations:

  • The tests are located at cms/tests .
  • There are Fixure-classes at cms/tests/Fixture used for test-database setup.
  • There are Unit-Tests at cms/tests/TestCase/Model .
  • There are Integration-Tests at cms/tests/TestCase/Controller .

You can run test by:

cd cms
vendor/bin/phpunit {otional-directory}

Or for beautiful output:

cd cms
vendor/bin/phpunit --testdox {otional-directory}

The Tests

HINT: The tests are not complete at this point!

Status legend:

  • ✅ Done
  • 🛠 WIP (Work in progress)
  • 📜 TODO
  • Unit tests
    • 🛠 Applicant
    • ✅ User
  • Integration tests
    • 📜 General
    • 🛠 Applicant
    • 🛠 User

To run all tests:

cd cms
vendor/bin/phpunit tests

or

cd cms
vendor/bin/phpunit --testdox tests

To run single tests, run one of this lines:

cd cms

vendor/bin/phpunit tests/TestCase/Model/Table/ApplicantsTableTest.php
vendor/bin/phpunit tests/TestCase/Model/Table/JobAdvertisementsTableTest.php
vendor/bin/phpunit tests/TestCase/Model/Table/UsersTableTest.php

vendor/bin/phpunit tests/TestCase/Controller/ApplicantsControllerTest.php
vendor/bin/phpunit tests/TestCase/Controller/JobAdvertisementsControllerTest.php
vendor/bin/phpunit tests/TestCase/Controller/UsersControllerTest.php

vendor/bin/phpunit tests/TestCase/Controller/Api/ApplicantsControllerTest.php
vendor/bin/phpunit tests/TestCase/Controller/Api/UsersControllerTest.php

or

cd cms

vendor/bin/phpunit --testdox tests/TestCase/Model/Table/ApplicantsTableTest.php
vendor/bin/phpunit --testdox tests/TestCase/Model/Table/JobAdvertisementsTableTest.php
vendor/bin/phpunit --testdox tests/TestCase/Model/Table/UsersTableTest.php

vendor/bin/phpunit --testdox tests/TestCase/Controller/ApplicantsControllerTest.php
vendor/bin/phpunit --testdox tests/TestCase/Controller/JobAdvertisementsControllerTest.php
vendor/bin/phpunit --testdox tests/TestCase/Controller/UsersControllerTest.php

vendor/bin/phpunit --testdox tests/TestCase/Controller/Api/ApplicantsControllerTest.php
vendor/bin/phpunit --testdox tests/TestCase/Controller/Api/UsersControllerTest.php

API documentation:

Fixing Issues

📜 #1 Reactivating Cross Site Request Forgery (CSRF) Protection Middleware

  • i have deaktivated the Cross Site Request Forgery (CSRF) Protection Middleware, because of testing the API with curl
  • i have to understand CSRF
  • see cms/src/Application.php

🛠 #2 Deprecated-Warning when running integration-tests dor Controllers

I suspress this warning by adding the entry 'vendor/cakephp/cakephp/src/I18n/I18n.php' to Error=>ignoredDeprecationPaths into the file cms/config/app.php:

    'Error' => [
        'errorLevel' => E_ALL,
        'skipLog' => [],
        'log' => true,
        'trace' => true,
        'ignoredDeprecationPaths' => [
            'vendor/cakephp/cakephp/src/I18n/I18n.php',
        ],

But is there not a better solution?

📜 #3 Beautyfy the Web-GUI

TODO

📜 #4 command bin/cake add_admin prints the password at input and the token

  • see cms/src/Command/AddAdminCommand.php