Skip to content
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

added admin user to migrations and read me, reformatted to PSR-1/PSR-2 #3

Open
wants to merge 3 commits into
base: rewrite
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ The recommended way to install composer packages is:

```
$ composer require bakkerij/cakeadmin:dev-rewrite
$ composer require friendsofcake/crud:^4.3
$ composer require gourmet/knp-menu:~0.4
$ composer require holt59/cakephp3-bootstrap-helpers:dev-master

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dependencies can be removed from documentation and added to our composer.json right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I had them in our dependencies they were not being updated with composer so I moved them into app and it worked. This would be another shell solution.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because you added the plugin to your plugins/Bakkerij/CakeAdmin for development reasons. Composer won't get that file.

When the package is loaded by an user, the package is located in the vendor folder, and Composer will recognize it, and load its dependencies.

TL;DR: This is only needed for development reasons but it might be solved another way...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I used composer to download your version into vendor. Deleted the cakeadmin directory from vendor/bakkerij and then cloned my fork into the directory. This was the only way I could work on the files without routing issues.

Either way you had the CRUD version in the dependencies of cakeadmin and composer did not pull it in.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stand corrected when I just started from scratch and the 2 were imported correctly when I used

composer require bakkerij/cakeadmin:dev-rewrite

```

## Load Plugin
Expand All @@ -34,3 +37,7 @@ Navigate to the new project's config/app.php and update your Datasources usernam
$ bin/cake migrations migrate -p Bakkerij/CakeAdmin
```

After migrations you will have a default Admin user with the following credentials:
email - [email protected]
password - test

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"php": ">=5.4.16",
"cakephp/cakephp": "~3.0",
"friendsofcake/crud": "^4.3",
"gourmet/knp-menu": "~0.4"
"gourmet/knp-menu": "~0.4",
"holt59/cakephp3-bootstrap-helpers": "dev-master"
},
"require-dev": {
"phpunit/phpunit": "*"
Expand Down
84 changes: 43 additions & 41 deletions config/Migrations/20160716113123_Initial.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@
<?php
use Migrations\AbstractMigration;

class Initial extends AbstractMigration
{
use Migrations\AbstractMigration;

class Initial extends AbstractMigration {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a standard according to brackets {?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just used NetBeans auto format

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CakePHP has its own standard: http://book.cakephp.org/3.0/en/intro/conventions.html

You can validate your code using: https://github.com/cakephp/cakephp-codesniffer

It might be a good idea to follow those standards and conventions... Probably there is a plugin for Netbeans to auto format to CakePHP's conventions, else you should do it manually (http://blog.bobbyallen.me/2013/03/24/configuring-netbeans-for-psr-1-and-psr-2-coding-guidelines/).

/**
* Change Method.
*
* More information on this method is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-change-method
* @return void
*/
public function change()
{
$table = $this->table('cakeadmin_administrators', ['id' => false, 'primary_key' => ['id']]);
$table
->addColumn('id', 'uuid', [
'default' => null,
'limit' => null,
'null' => false,
])
->addColumn('name', 'string', [
'default' => null,
'limit' => 150,
'null' => false,
])
->addColumn('email', 'string', [
'default' => null,
'limit' => 50,
'null' => true,
])
->addColumn('password', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('active', 'integer', [
'default' => 0,
'limit' => 1,
'null' => true,
])
->addColumn('request_key', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('created', 'datetime')
->addColumn('modified', 'datetime')
->create();
public function change() {
$table = $this->table('cakeadmin_administrators', ['id' => false, 'primary_key' => ['id']]);
$table
->addColumn('id', 'uuid', [
'default' => null,
'limit' => null,
'null' => false,
])
->addColumn('name', 'string', [
'default' => null,
'limit' => 150,
'null' => false,
])
->addColumn('email', 'string', [
'default' => null,
'limit' => 50,
'null' => true,
])
->addColumn('password', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('active', 'integer', [
'default' => 0,
'limit' => 1,
'null' => true,
])
->addColumn('request_key', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('created', 'datetime')
->addColumn('modified', 'datetime')
->create();
}
}

}

26 changes: 26 additions & 0 deletions config/Migrations/20161123183707_AddAdminUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Cake\ORM\TableRegistry;
use Migrations\AbstractMigration;

class AddAdminUser extends AbstractMigration
{
/**
* Change Method.
*
* More information on this method is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-change-method
* @return void
*/
public function change()
{
$AdminTable = TableRegistry::get('Bakkerij/CakeAdmin.Administrators');
$user = $AdminTable->newEntity([
'name' => 'admin',
'email' => '[email protected]',
'password' => 'test',
'active' => 1
]);
$AdminTable->save($user);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a seed would be cleaner, but personally I prefer a shell to create an user yourself...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree a shell would be the final solution but this can get us up and testing very quickly until the shell has been created

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So let's accept this as a quick dirty solution for now :P

}
}
5 changes: 5 additions & 0 deletions src/Controller/Admin/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@

use Bakkerij\CakeAdmin\Controller\AppController;
use Cake\Datasource\ConnectionManager;
use \Cake\Event\Event;

/**
* Dashboard Controller
*
*/
class DashboardController extends AppController
{
public function beforeRender(Event $event) {
parent::beforeRender($event);
$this->viewBuilder()->layout('admin');
}

/**
* Index method
Expand Down
1 change: 1 addition & 0 deletions src/Controller/Admin/PostTypesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function beforeFilter(Event $event)

public function beforeRender(Event $event)
{
$this->viewBuilder()->layout('admin');
$this->viewBuilder()->helpers(['Bakkerij/CakeAdmin.PostType' => [
'data' => $this->postType
]]);
Expand Down
18 changes: 17 additions & 1 deletion src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,24 @@ class AppController extends BaseController
{

use ControllerTrait;

use CakeAdminTrait;

// https://holt59.github.io/cakephp3-bootstrap-helpers/
public $helpers = [
'Html' => [
'className' => 'Bootstrap.BootstrapHtml',
'useCustomFileInput' => true
],
'Form' => [
'className' => 'Bootstrap.BootstrapForm'
],
'Paginator' => [
'className' => 'Bootstrap.BootstrapPaginator'
],
'Modal' => [
'className' => 'Bootstrap.BootstrapModal'
]
];

/**
* Initialize AppController
Expand Down
50 changes: 50 additions & 0 deletions src/Template/Bake/Controller/controller.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<%
/**
* Controller bake template file
*
* Allows templating of Controllers generated from bake.
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 0.1.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use Cake\Utility\Inflector;

$defaultModel = $name;
%>
<?php
namespace <%= $namespace %>\Controller<%= $prefix %>;

use <%= $namespace %>\Controller\AppController;
use Cake\Event\Event;

/**
* <%= $name %> Controller
*
* @property \<%= $namespace %>\Model\Table\<%= $defaultModel %>Table $<%= $defaultModel %>
<%
foreach ($components as $component):
$classInfo = $this->Bake->classInfo($component, 'Controller/Component', 'Component');
%>
* @property <%= $classInfo['fqn'] %> $<%= $classInfo['name'] %>
<% endforeach; %>
*/
class <%= $name %>Controller extends AppController
{
<% echo $this->element('Controller/auth', $actions); %>
<%
echo $this->Bake->arrayProperty('helpers', $helpers, ['indent' => false]);
echo $this->Bake->arrayProperty('components', $components, ['indent' => false]);
foreach($actions as $action) {
echo $this->element('Controller/' . $action);
}
%>
}
20 changes: 20 additions & 0 deletions src/Template/Bake/Element/Controller/auth.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public function beforeRender(Event $event)
{
$this->viewBuilder()->layout('admin');
}

public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
$this->Auth->deny();
}

public function isAuthorized($user)
{
// Registered Users can access
if (in_array($this->request->action, [<% foreach($actions as $action) {echo "'$action', ";} %>])) {
return true;
}

return parent::isAuthorized($user);
}
79 changes: 79 additions & 0 deletions src/Template/Bake/Element/form.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<%
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 0.1.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use Cake\Utility\Inflector;

$fields = collection($fields)
->filter(function($field) use ($schema) {
return $schema->columnType($field) !== 'binary';
});

if (isset($modelObject) && $modelObject->behaviors()->has('Tree')) {
$fields = $fields->reject(function ($field) {
return $field === 'lft' || $field === 'rght';
});
}
%>
<?php
// Title
$title = __('<%= Inflector::humanize($action) %> <%= $singularHumanName %>');
$smallTitle = null;
$this->assign('title', $title);
echo $this->Layout->setPageTitle($title, $smallTitle);
?>

<?= $this->Form->create($<%= $singularVar %>) ?>
<?php
<%
foreach ($fields as $field) {
if (in_array($field, $primaryKey)) {
continue;
}
if (isset($keyFields[$field])) {
$fieldData = $schema->column($field);
if (!empty($fieldData['null'])) {
%>
echo $this->Form->input('<%= $field %>', ['options' => $<%= $keyFields[$field] %>, 'empty' => true]);
<%
} else {
%>
echo $this->Form->input('<%= $field %>', ['options' => $<%= $keyFields[$field] %>]);
<%
}
continue;
}
if (!in_array($field, ['created', 'modified', 'updated'])) {
$fieldData = $schema->column($field);
if (in_array($fieldData['type'], ['date', 'datetime', 'time']) && (!empty($fieldData['null']))) {
%>
echo $this->Form->input('<%= $field %>', ['empty' => true]);
<%
} else {
%>
echo $this->Form->input('<%= $field %>');
<%
}
}
}
if (!empty($associations['BelongsToMany'])) {
foreach ($associations['BelongsToMany'] as $assocName => $assocData) {
%>
echo $this->Form->input('<%= $assocData['property'] %>._ids', ['options' => $<%= $assocData['variable'] %>]);
<%
}
}
%>
?>
<?= $this->Form->button(__('Submit'), ['type' => 'submit', 'class' => 'btn btn-primary']) ?>
<?= $this->Form->end() ?>
Loading