From 7dd91a838b40d683bf3c8773dbd4c338bd00060f Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Mon, 16 Sep 2019 14:15:08 +0000 Subject: [PATCH] adding user operator save into db and browser --- mithril_client | 2 +- src/Controller/AppController.php | 36 +++++++ src/Controller/OperatorTypesController.php | 106 +++++++++++++++++++++ src/Controller/OperatorsController.php | 73 +++++++++++++- src/Controller/PagesController.php | 6 ++ src/Controller/ServerUsersController.php | 24 +++++ src/Model/Entity/Operator.php | 7 +- src/Model/Entity/OperatorType.php | 31 ++++++ src/Model/Entity/ServerUser.php | 8 ++ src/Model/Table/OperatorTypesTable.php | 70 ++++++++++++++ src/Model/Table/OperatorsTable.php | 24 +++-- src/Model/Table/ServerUsersTable.php | 6 +- src/Template/Layout/default.ctp | 5 +- src/Template/OperatorTypes/add.ctp | 26 +++++ src/Template/OperatorTypes/edit.ctp | 32 +++++++ src/Template/OperatorTypes/index.ctp | 51 ++++++++++ src/Template/OperatorTypes/view.ctp | 61 ++++++++++++ src/Template/Operators/add.ctp | 3 +- src/Template/Operators/edit.ctp | 3 +- src/Template/Operators/index.ctp | 11 ++- src/Template/Operators/view.ctp | 8 +- src/Template/ServerUsers/add.ctp | 2 - src/Template/ServerUsers/index.ctp | 2 - src/Template/ServerUsers/login.ctp | 19 ++++ webroot/css/style.css | 5 + 25 files changed, 592 insertions(+), 29 deletions(-) create mode 100644 src/Controller/OperatorTypesController.php create mode 100644 src/Model/Entity/OperatorType.php create mode 100644 src/Model/Table/OperatorTypesTable.php create mode 100644 src/Template/OperatorTypes/add.ctp create mode 100644 src/Template/OperatorTypes/edit.ctp create mode 100644 src/Template/OperatorTypes/index.ctp create mode 100644 src/Template/OperatorTypes/view.ctp create mode 100644 src/Template/ServerUsers/login.ctp diff --git a/mithril_client b/mithril_client index 055f2bb57..a4af9311f 160000 --- a/mithril_client +++ b/mithril_client @@ -1 +1 @@ -Subproject commit 055f2bb572244fbd5b4d71c2ca84d4493ae88dd1 +Subproject commit a4af9311f84e31d1b4682ea6de953c3a018b5343 diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php index 49fa03fc4..9c43e6d69 100644 --- a/src/Controller/AppController.php +++ b/src/Controller/AppController.php @@ -46,10 +46,46 @@ class AppController extends Controller ]); $this->loadComponent('Flash'); + $this->loadComponent('Auth', [ + 'loginAction' => [ + 'controller' => 'ServerUsers', + 'action' => 'login' + ], + 'loginRedirect' => [ + 'controller' => 'Transactions', + 'action' => 'index' + ], + 'logoutRedirect' => [ + 'controller' => 'Pages', + 'action' => 'display', + 'gradido' + ], + 'authenticate' => [ + 'all' => ['userModel' => 'ServerUsers'], + 'Form' => [ + 'userModel' => 'ServerUsers', + ] + ] + ]); + + $this->Auth->deny(['index']); + /* * Enable the following component for recommended CakePHP security settings. * see https://book.cakephp.org/3.0/en/controllers/components/security.html */ //$this->loadComponent('Security'); } + /* + public function beforeFilter(Event $event) + { + //$this->Auth->allow(['display']); + } + */ + + public function returnJson($array) { + $this->autoRender = false; + $response = $this->response->withType('application/json'); + return $response->withStringBody(json_encode($array)); + } } diff --git a/src/Controller/OperatorTypesController.php b/src/Controller/OperatorTypesController.php new file mode 100644 index 000000000..41db53f53 --- /dev/null +++ b/src/Controller/OperatorTypesController.php @@ -0,0 +1,106 @@ +paginate($this->OperatorTypes); + + $this->set(compact('operatorTypes')); + } + + /** + * View method + * + * @param string|null $id Operator Type id. + * @return \Cake\Http\Response|null + * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. + */ + public function view($id = null) + { + $operatorType = $this->OperatorTypes->get($id, [ + 'contain' => ['Operators'] + ]); + + $this->set('operatorType', $operatorType); + } + + /** + * Add method + * + * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise. + */ + public function add() + { + $operatorType = $this->OperatorTypes->newEntity(); + if ($this->request->is('post')) { + $operatorType = $this->OperatorTypes->patchEntity($operatorType, $this->request->getData()); + if ($this->OperatorTypes->save($operatorType)) { + $this->Flash->success(__('The operator type has been saved.')); + + return $this->redirect(['action' => 'index']); + } + $this->Flash->error(__('The operator type could not be saved. Please, try again.')); + } + $this->set(compact('operatorType')); + } + + /** + * Edit method + * + * @param string|null $id Operator Type id. + * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise. + * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. + */ + public function edit($id = null) + { + $operatorType = $this->OperatorTypes->get($id, [ + 'contain' => [] + ]); + if ($this->request->is(['patch', 'post', 'put'])) { + $operatorType = $this->OperatorTypes->patchEntity($operatorType, $this->request->getData()); + if ($this->OperatorTypes->save($operatorType)) { + $this->Flash->success(__('The operator type has been saved.')); + + return $this->redirect(['action' => 'index']); + } + $this->Flash->error(__('The operator type could not be saved. Please, try again.')); + } + $this->set(compact('operatorType')); + } + + /** + * Delete method + * + * @param string|null $id Operator Type id. + * @return \Cake\Http\Response|null Redirects to index. + * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. + */ + public function delete($id = null) + { + $this->request->allowMethod(['post', 'delete']); + $operatorType = $this->OperatorTypes->get($id); + if ($this->OperatorTypes->delete($operatorType)) { + $this->Flash->success(__('The operator type has been deleted.')); + } else { + $this->Flash->error(__('The operator type could not be deleted. Please, try again.')); + } + + return $this->redirect(['action' => 'index']); + } +} diff --git a/src/Controller/OperatorsController.php b/src/Controller/OperatorsController.php index 8dd0660f2..ff7874578 100644 --- a/src/Controller/OperatorsController.php +++ b/src/Controller/OperatorsController.php @@ -12,6 +12,12 @@ use App\Controller\AppController; */ class OperatorsController extends AppController { + + public function initialize() + { + parent::initialize(); + $this->Auth->allow(['ajaxSave', 'ajaxLoad']); + } /** * Index method * @@ -19,10 +25,69 @@ class OperatorsController extends AppController */ public function index() { + $this->paginate = [ + 'contain' => ['OperatorTypes'] + ]; $operators = $this->paginate($this->Operators); $this->set(compact('operators')); } + + public function ajaxSave() + { + if ($this->request->is('post')) { + + + + $operatorTypeName = $this->request->getData('operator_type_name'); + $usernamePasswordHash = $this->request->getData('usernamePasswordHash'); + $operatorTypeId = $this->Operators->OperatorTypes-> + find() + ->where(['name' => $operatorTypeName]) + ->select(['id']) + ->first(); + + // load operator from db if already exist + $operator = $this->Operators + ->find() + ->where([ + 'operator_type_id' => $operatorTypeId->id, + 'usernamePasswordHash' => $usernamePasswordHash]) + ->first(); + if(!$operator) { + // create new entity + $operator = $this->Operators->newEntity(); + } + + $operator = $this->Operators->patchEntity($operator, $this->request->getData()); + $operator->operator_type_id = $operatorTypeId->id; + if ($this->Operators->save($operator)) { + return $this->returnJson(['state' => 'success']); + } + return $this->returnJson(['state' => 'error', 'details' => $operator->getErrors()]); + } + return $this->returnJson(['state' => 'error', 'msg' => 'no post request']); + } + + public function ajaxLoad() + { + if ($this->request->is('get')) { + $usernamePasswordHash = $this->request->getQuery('usernamePasswordHash'); + $operators = $this->Operators + ->find() + ->where(['usernamePasswordHash' => $usernamePasswordHash]) + ->contain(['OperatorTypes']) + ->toArray(); + ; + if($operators) { + return $this->returnJson(['state' => 'success', 'operators' => $operators]); + } else { + return $this->returnJson(['state' => 'not found']); + } + + } + return $this->returnJson(['state' => 'error', 'msg' => 'no post request']); + } /** * View method @@ -34,7 +99,7 @@ class OperatorsController extends AppController public function view($id = null) { $operator = $this->Operators->get($id, [ - 'contain' => [] + 'contain' => ['OperatorTypes'] ]); $this->set('operator', $operator); @@ -57,7 +122,8 @@ class OperatorsController extends AppController } $this->Flash->error(__('The operator could not be saved. Please, try again.')); } - $this->set(compact('operator')); + $operatorTypes = $this->Operators->OperatorTypes->find('list', ['limit' => 200]); + $this->set(compact('operator', 'operatorTypes')); } /** @@ -81,7 +147,8 @@ class OperatorsController extends AppController } $this->Flash->error(__('The operator could not be saved. Please, try again.')); } - $this->set(compact('operator')); + $operatorTypes = $this->Operators->OperatorTypes->find('list', ['limit' => 200]); + $this->set(compact('operator', 'operatorTypes')); } /** diff --git a/src/Controller/PagesController.php b/src/Controller/PagesController.php index d02366110..94508ad8b 100644 --- a/src/Controller/PagesController.php +++ b/src/Controller/PagesController.php @@ -29,6 +29,12 @@ use Cake\View\Exception\MissingTemplateException; class PagesController extends AppController { + public function initialize() + { + parent::initialize(); + $this->Auth->allow(['display']); + } + /** * Displays a view * diff --git a/src/Controller/ServerUsersController.php b/src/Controller/ServerUsersController.php index 7bc23e033..238090123 100644 --- a/src/Controller/ServerUsersController.php +++ b/src/Controller/ServerUsersController.php @@ -12,6 +12,13 @@ use App\Controller\AppController; */ class ServerUsersController extends AppController { + public function initialize() + { + parent::initialize(); + //$this->Auth->allow(['add', 'edit']); + $this->Auth->deny('index'); + } + /** * Index method * @@ -23,6 +30,23 @@ class ServerUsersController extends AppController $this->set(compact('serverUsers')); } + + public function login() + { + if ($this->request->is('post')) { + $user = $this->Auth->identify(); + if ($user) { + $this->Auth->setUser($user); + return $this->redirect($this->Auth->redirectUrl()); + } + $this->Flash->error(__('Invalid username or password, try again')); + } + } + + public function logout() + { + return $this->redirect($this->Auth->logout()); + } /** * View method diff --git a/src/Model/Entity/Operator.php b/src/Model/Entity/Operator.php index 681fd146c..2342897bd 100644 --- a/src/Model/Entity/Operator.php +++ b/src/Model/Entity/Operator.php @@ -8,6 +8,7 @@ use Cake\ORM\Entity; * * @property int $id * @property string $username + * @property int $operator_type_id * @property string $data_base64 */ class Operator extends Entity @@ -22,7 +23,9 @@ class Operator extends Entity * @var array */ protected $_accessible = [ - 'username' => true, - 'data_base64' => true + 'usernamePasswordHash' => true, + 'operator_type_id' => true, + 'data_base64' => true, + 'modified' => true ]; } diff --git a/src/Model/Entity/OperatorType.php b/src/Model/Entity/OperatorType.php new file mode 100644 index 000000000..0ca968f47 --- /dev/null +++ b/src/Model/Entity/OperatorType.php @@ -0,0 +1,31 @@ + true, + 'text' => true, + 'operators' => true + ]; +} diff --git a/src/Model/Entity/ServerUser.php b/src/Model/Entity/ServerUser.php index c180e060f..f1d47fd34 100644 --- a/src/Model/Entity/ServerUser.php +++ b/src/Model/Entity/ServerUser.php @@ -1,6 +1,7 @@ 0) { + return (new DefaultPasswordHasher)->hash($password); + } + } } diff --git a/src/Model/Table/OperatorTypesTable.php b/src/Model/Table/OperatorTypesTable.php new file mode 100644 index 000000000..8a3ce608c --- /dev/null +++ b/src/Model/Table/OperatorTypesTable.php @@ -0,0 +1,70 @@ +setTable('operator_types'); + $this->setDisplayField('name'); + $this->setPrimaryKey('id'); + + $this->hasMany('Operators', [ + 'foreignKey' => 'operator_type_id' + ]); + } + + /** + * Default validation rules. + * + * @param \Cake\Validation\Validator $validator Validator instance. + * @return \Cake\Validation\Validator + */ + public function validationDefault(Validator $validator) + { + $validator + ->integer('id') + ->allowEmptyString('id', null, 'create'); + + $validator + ->scalar('name') + ->maxLength('name', 25) + ->requirePresence('name', 'create') + ->notEmptyString('name'); + + $validator + ->scalar('text') + ->maxLength('text', 255) + ->requirePresence('text', 'create') + ->notEmptyString('text'); + + return $validator; + } +} diff --git a/src/Model/Table/OperatorsTable.php b/src/Model/Table/OperatorsTable.php index f47a96b9f..044f89799 100644 --- a/src/Model/Table/OperatorsTable.php +++ b/src/Model/Table/OperatorsTable.php @@ -9,6 +9,8 @@ use Cake\Validation\Validator; /** * Operators Model * + * @property &\Cake\ORM\Association\BelongsTo $OperatorTypes + * * @method \App\Model\Entity\Operator get($primaryKey, $options = []) * @method \App\Model\Entity\Operator newEntity($data = null, array $options = []) * @method \App\Model\Entity\Operator[] newEntities(array $data, array $options = []) @@ -31,8 +33,15 @@ class OperatorsTable extends Table parent::initialize($config); $this->setTable('operators'); - $this->setDisplayField('id'); + $this->setDisplayField('name'); $this->setPrimaryKey('id'); + + $this->belongsTo('OperatorTypes', [ + 'foreignKey' => 'operator_type_id', + 'joinType' => 'INNER' + ]); + + $this->addBehavior('Timestamp'); } /** @@ -48,11 +57,11 @@ class OperatorsTable extends Table ->allowEmptyString('id', null, 'create'); $validator - ->scalar('username') - ->maxLength('username', 128) - ->requirePresence('username', 'create') - ->notEmptyString('username') - ->add('username', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']); + ->scalar('usernamePasswordHash') + ->maxLength('usernamePasswordHash', 255) + ->requirePresence('usernamePasswordHash', 'create') + ->notEmptyString('usernamePasswordHash'); + //->add('usernamePasswordHash', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']); $validator ->scalar('data_base64') @@ -72,7 +81,8 @@ class OperatorsTable extends Table */ public function buildRules(RulesChecker $rules) { - $rules->add($rules->isUnique(['username'])); + //$rules->add($rules->isUnique(['usernamePasswordHash'])); + $rules->add($rules->existsIn(['operator_type_id'], 'OperatorTypes')); return $rules; } diff --git a/src/Model/Table/ServerUsersTable.php b/src/Model/Table/ServerUsersTable.php index 63b6fb2fe..0ad1fbacb 100644 --- a/src/Model/Table/ServerUsersTable.php +++ b/src/Model/Table/ServerUsersTable.php @@ -55,18 +55,18 @@ class ServerUsersTable extends Table ->scalar('username') ->maxLength('username', 50) ->requirePresence('username', 'create') - ->notEmptyString('username'); + ->notEmptyString('username', __('Please give a username')); $validator ->scalar('password') ->maxLength('password', 255) ->requirePresence('password', 'create') - ->notEmptyString('password'); + ->notEmptyString('password', __('Please give a password')); $validator ->email('email') ->requirePresence('email', 'create') - ->notEmptyString('email'); + ->notEmptyString('email', __('Please give a email')); $validator ->scalar('role') diff --git a/src/Template/Layout/default.ctp b/src/Template/Layout/default.ctp index caf014ebc..b5e38e309 100644 --- a/src/Template/Layout/default.ctp +++ b/src/Template/Layout/default.ctp @@ -42,8 +42,9 @@ $cakeDescription = 'CakePHP: the rapid development php framework';
diff --git a/src/Template/OperatorTypes/add.ctp b/src/Template/OperatorTypes/add.ctp new file mode 100644 index 000000000..1f9c10f6d --- /dev/null +++ b/src/Template/OperatorTypes/add.ctp @@ -0,0 +1,26 @@ + + +
+ Form->create($operatorType) ?> +
+ + Form->control('name'); + echo $this->Form->control('text'); + ?> +
+ Form->button(__('Submit')) ?> + Form->end() ?> +
diff --git a/src/Template/OperatorTypes/edit.ctp b/src/Template/OperatorTypes/edit.ctp new file mode 100644 index 000000000..3f4ab4d04 --- /dev/null +++ b/src/Template/OperatorTypes/edit.ctp @@ -0,0 +1,32 @@ + + +
+ Form->create($operatorType) ?> +
+ + Form->control('name'); + echo $this->Form->control('text'); + ?> +
+ Form->button(__('Submit')) ?> + Form->end() ?> +
diff --git a/src/Template/OperatorTypes/index.ctp b/src/Template/OperatorTypes/index.ctp new file mode 100644 index 000000000..71e01f6b3 --- /dev/null +++ b/src/Template/OperatorTypes/index.ctp @@ -0,0 +1,51 @@ + + +
+

+ + + + + + + + + + + + + + + + + + + +
Paginator->sort('id') ?>Paginator->sort('name') ?>Paginator->sort('text') ?>
Number->format($operatorType->id) ?>name) ?>text) ?> + Html->link(__('View'), ['action' => 'view', $operatorType->id]) ?> + Html->link(__('Edit'), ['action' => 'edit', $operatorType->id]) ?> + Form->postLink(__('Delete'), ['action' => 'delete', $operatorType->id], ['confirm' => __('Are you sure you want to delete # {0}?', $operatorType->id)]) ?> +
+
+
    + Paginator->first('<< ' . __('first')) ?> + Paginator->prev('< ' . __('previous')) ?> + Paginator->numbers() ?> + Paginator->next(__('next') . ' >') ?> + Paginator->last(__('last') . ' >>') ?> +
+

Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?>

+
+
diff --git a/src/Template/OperatorTypes/view.ctp b/src/Template/OperatorTypes/view.ctp new file mode 100644 index 000000000..383f146d3 --- /dev/null +++ b/src/Template/OperatorTypes/view.ctp @@ -0,0 +1,61 @@ + + +
+

name) ?>

+ + + + + + + + + + + + + +
name) ?>
text) ?>
Number->format($operatorType->id) ?>
+ +
diff --git a/src/Template/Operators/add.ctp b/src/Template/Operators/add.ctp index 8abf5271f..4d714f806 100644 --- a/src/Template/Operators/add.ctp +++ b/src/Template/Operators/add.ctp @@ -15,7 +15,8 @@
Form->control('username'); + echo $this->Form->control('usernamePasswordHash'); + echo $this->Form->control('operator_type_id'); echo $this->Form->control('data_base64'); ?>
diff --git a/src/Template/Operators/edit.ctp b/src/Template/Operators/edit.ctp index 2faaf7442..4e99f921f 100644 --- a/src/Template/Operators/edit.ctp +++ b/src/Template/Operators/edit.ctp @@ -21,7 +21,8 @@
Form->control('username'); + echo $this->Form->control('usernamePasswordHash'); + echo $this->Form->control('operator_type_id'); echo $this->Form->control('data_base64'); ?>
diff --git a/src/Template/Operators/index.ctp b/src/Template/Operators/index.ctp index 723a6b031..9b506d61e 100644 --- a/src/Template/Operators/index.ctp +++ b/src/Template/Operators/index.ctp @@ -16,17 +16,22 @@ Paginator->sort('id') ?> - Paginator->sort('username') ?> + Paginator->sort('usernamePasswordHash') ?> + Paginator->sort('operator_type_id') ?> Paginator->sort('data_base64') ?> + Paginator->sort('modified') ?> - + operator_type->name ?> Number->format($operator->id) ?> - username) ?> + usernamePasswordHash) ?> + Html->link(__($operator->operator_type->name), ['controller' => 'OperatorTypes', 'action' => 'view', $operator->operator_type_id]) ?> data_base64) ?> + modified) ?> Html->link(__('View'), ['action' => 'view', $operator->id]) ?> Html->link(__('Edit'), ['action' => 'edit', $operator->id]) ?> diff --git a/src/Template/Operators/view.ctp b/src/Template/Operators/view.ctp index a2bcd98e9..c3d745a5b 100644 --- a/src/Template/Operators/view.ctp +++ b/src/Template/Operators/view.ctp @@ -17,8 +17,8 @@

id) ?>

- - + + @@ -28,5 +28,9 @@ + + + +
username) ?>usernamePasswordHash) ?>
Number->format($operator->id) ?>
Number->format($operator->operator_type_id) ?>
diff --git a/src/Template/ServerUsers/add.ctp b/src/Template/ServerUsers/add.ctp index 668ea0fa0..1c3348452 100644 --- a/src/Template/ServerUsers/add.ctp +++ b/src/Template/ServerUsers/add.ctp @@ -19,8 +19,6 @@ echo $this->Form->control('password'); echo $this->Form->control('email'); echo $this->Form->control('role'); - echo $this->Form->control('activated'); - echo $this->Form->control('last_login', ['empty' => true]); ?> Form->button(__('Submit')) ?> diff --git a/src/Template/ServerUsers/index.ctp b/src/Template/ServerUsers/index.ctp index 25ff72dd6..0a771a858 100644 --- a/src/Template/ServerUsers/index.ctp +++ b/src/Template/ServerUsers/index.ctp @@ -17,7 +17,6 @@ Paginator->sort('id') ?> Paginator->sort('username') ?> - Paginator->sort('password') ?> Paginator->sort('email') ?> Paginator->sort('role') ?> Paginator->sort('activated') ?> @@ -32,7 +31,6 @@ Number->format($serverUser->id) ?> username) ?> - password) ?> email) ?> role) ?> activated) ?> diff --git a/src/Template/ServerUsers/login.ctp b/src/Template/ServerUsers/login.ctp new file mode 100644 index 000000000..42e7a2c55 --- /dev/null +++ b/src/Template/ServerUsers/login.ctp @@ -0,0 +1,19 @@ + +
+Flash->render() ?> +Form->create() ?> +
+ + Form->control('username') ?> + Form->control('password') ?> +
+Form->button(__('Login')); ?> +Form->end() ?> +
\ No newline at end of file diff --git a/webroot/css/style.css b/webroot/css/style.css index 40c1d2a9f..44429c9b2 100644 --- a/webroot/css/style.css +++ b/webroot/css/style.css @@ -522,3 +522,8 @@ table td { vertical-align: top; word-break: break-all; } + +.center { + margin-left:auto; + margin-right:auto; +}