Merge branch 'stage2' of github.com:gradido/gradido into stage2

This commit is contained in:
einhornimmond 2021-04-06 13:09:44 +02:00
commit 9ea97bfb4d
18 changed files with 535 additions and 12 deletions

View File

@ -0,0 +1,17 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Author: einhornimmond
* Created: 06.04.2021
*/
CREATE TABLE `blockchain_types` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`text` varchar(255) NULL,
`symbol` varchar(10) NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

View File

@ -0,0 +1,5 @@
INSERT INTO `blockchain_types` (`id`, `name`, `text`, `symbol`) VALUES
(1, 'mysql', 'use mysql db as blockchain, work only with single community-server', NULL),
(2, 'hedera', 'use hedera for transactions', 'HBAR');

View File

@ -5,5 +5,6 @@ CREATE TABLE `transactions` (
`tx_hash` binary(48) DEFAULT NULL,
`memo` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`received` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`blockchain_type_id` bigint(20) unsigned NOT NULL DEFAULT 1,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

View File

@ -35,6 +35,7 @@ class AppController extends Controller
{
var $loginServerUrl = '';
var $blockchainType = 'mysql';
/**
* Initialization hook method.
*
@ -136,6 +137,23 @@ class AppController extends Controller
} else {
$this->loginServerUrl = Router::url('/', true);
}
/*
*
* 'GradidoBlockchain' => [
* // type:
* // - mysql: centralized blockchain in mysql db, no cross group transactions
* // - hedera: send transaction over hedera
* 'type' => 'hedera',
* // gradido nodes with blockchain (if type != mysql)
* 'nodes' => [
* ['host' => 'http://192.168.178.225', 'port' => 13702]
* ]
* ],
*/
$blockchain = Configure::read('GradidoBlockchain');
if($blockchain && isset($blockchain['type'])) {
$this->blockchainType = $blockchain['type'];
}
}
protected function requestLogin($session_id = 0, $redirect = true)

View File

@ -23,7 +23,7 @@ class AppRequestsController extends AppController
$this->loadComponent('JsonRequestClient');
$this->loadComponent('JsonRpcRequestClient');
//$this->Auth->allow(['add', 'edit']);
$this->Auth->allow('index');
$this->Auth->allow(['index', 'sendCoins']);
}
@ -52,6 +52,107 @@ class AppRequestsController extends AppController
return $this->returnJson(['state' => 'error', 'msg' => 'no post or get']);
}
private function checkRequiredFields($data, $fields) {
foreach($fields as $field) {
if(!isset($data[$field])) {
return ['state' => 'error', 'msg' => 'missing field', 'details' => $field . ' not found'];
}
}
return true;
}
public function sendCoins()
{
/*
* {
"session_id" : -127182,
"amount": 2000000,
"email": "max.musterman@gmail.de",
"memo":"Thank you :)",
"group": "gdd1",
"auto_sign": true
*/
$data = $this->request->input('json_decode');
$login_request_result = $this->requestLogin(0, false);
if($login_request_result !== true) {
return $this->returnJson($login_request_result);
}
$session = $this->getRequest()->getSession();
$required_fields = $this->checkRequiredFields($data, ['amount', 'email']);
if($required_fields !== true) {
return $this->returnJson($required_fields);
}
$amount = $data['amount'];
if(intval($amount) <= 0) {
return $this->returnJson(['state' => 'error', 'msg' => 'amount is invalid', 'details' => $amount]);
}
$email = $data['email'];
if($email == '') {
return $this->returnJson(['state' => 'error', 'msg' => 'email is empty']);
}
$memo = '';
if(isset($data['memo'])) {
$memo = $data['memo'];
}
$auto_sign = false;
if(isset($data['auto_sign'])) {
$auto_sign = boolval($data['auto_sign']);
}
$group = '';
if(isset($data['group'])) {
$group = $data['group'];
}
$requestAnswear = $this->JsonRequestClient->sendRequest(json_encode([
'session_id' => $session->read('session_id'),
'transaction_type' => 'transfer',
'memo' => $memo,
'amount' => $amount,
'target_group' => $group,
'target_email' => $email,
'auto_sign' => $auto_sign,
'blockchain_type' => $this->blockchainType
]), '/createTransaction');
if('success' == $requestAnswear['state'] && 'success' == $requestAnswear['data']['state']) {
$pendingTransactionCount = $session->read('Transactions.pending');
if($pendingTransactionCount == null) {
$pendingTransactionCount = 1;
} else {
$pendingTransactionCount++;
}
$session->write('Transactions.pending', $pendingTransactionCount);
//echo "pending: " . $pendingTransactionCount;
return $this->returnJson(['state' => 'success']);
} else {
/*
* if request contain unknown parameter format, shouldn't happen't at all
* {"state": "error", "msg": "parameter format unknown"}
* if json parsing failed
* {"state": "error", "msg": "json exception", "details":"exception text"}
* if session_id is zero or not set
* {"state": "error", "msg": "session_id invalid"}
* if session id wasn't found on login server, if server was restartet or user logged out (also per timeout, default: 15 minutes)
* {"state": "error", "msg": "session not found"}
* if session hasn't active user, shouldn't happen't at all, login-server should be checked if happen
* {"state": "code error", "msg":"user is zero"}
* if transaction type not known
* {"state": "error", "msg":"transaction_type unknown"}
* if receiver wasn't known to Login-Server
* {"state": "not found", "msg":"receiver not found"}
* if receiver account disabled, and therefor cannto receive any coins
* {"state": "disabled", "msg":"receiver is disabled"}
* if transaction was okay and will be further proccessed
* {"state":"success"}
*/
$answear_data = $requestAnswear['data'];
return $this->returnJson($answear_data);
}
}
private function acquireAccessToken($session_id)
{

View File

@ -0,0 +1,106 @@
<?php
namespace App\Controller;
use App\Controller\AppController;
/**
* BlockchainTypes Controller
*
* @property \App\Model\Table\BlockchainTypesTable $BlockchainTypes
*
* @method \App\Model\Entity\BlockchainType[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
*/
class BlockchainTypesController extends AppController
{
/**
* Index method
*
* @return \Cake\Http\Response|null
*/
public function index()
{
$blockchainTypes = $this->paginate($this->BlockchainTypes);
$this->set(compact('blockchainTypes'));
}
/**
* View method
*
* @param string|null $id Blockchain Type id.
* @return \Cake\Http\Response|null
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$blockchainType = $this->BlockchainTypes->get($id, [
'contain' => [],
]);
$this->set('blockchainType', $blockchainType);
}
/**
* Add method
*
* @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
*/
public function add()
{
$blockchainType = $this->BlockchainTypes->newEntity();
if ($this->request->is('post')) {
$blockchainType = $this->BlockchainTypes->patchEntity($blockchainType, $this->request->getData());
if ($this->BlockchainTypes->save($blockchainType)) {
$this->Flash->success(__('The blockchain type has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The blockchain type could not be saved. Please, try again.'));
}
$this->set(compact('blockchainType'));
}
/**
* Edit method
*
* @param string|null $id Blockchain 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)
{
$blockchainType = $this->BlockchainTypes->get($id, [
'contain' => [],
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$blockchainType = $this->BlockchainTypes->patchEntity($blockchainType, $this->request->getData());
if ($this->BlockchainTypes->save($blockchainType)) {
$this->Flash->success(__('The blockchain type has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The blockchain type could not be saved. Please, try again.'));
}
$this->set(compact('blockchainType'));
}
/**
* Delete method
*
* @param string|null $id Blockchain 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']);
$blockchainType = $this->BlockchainTypes->get($id);
if ($this->BlockchainTypes->delete($blockchainType)) {
$this->Flash->success(__('The blockchain type has been deleted.'));
} else {
$this->Flash->error(__('The blockchain type could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
}

View File

@ -15,7 +15,7 @@ use Cake\Core\Configure;
class JsonRequestClientComponent extends Component
{
public function sendTransaction($session_id, $base64Message, $user_balance = 0, $auto_sign = false) {
public function sendTransaction($session_id, $base64Message, $user_balance = 0, $auto_sign = false, $blockchain_type = 'mysql') {
if(!is_numeric($session_id)) {
return ['state' => 'error', 'type' => 'parameter error', 'msg' => 'session_id isn\'t numeric'];
}
@ -36,7 +36,8 @@ class JsonRequestClientComponent extends Component
'session_id' => $session_id,
'transaction_base64' => $base64Message,
'balance' => $user_balance,
'auto_sign' => $auto_sign
'auto_sign' => $auto_sign,
'blockchain_type' => $this->blockchainType
]), '/checkTransaction');
}

View File

@ -399,7 +399,8 @@ class TransactionCreationsController extends AppController
'memo' => $memo,
'amount' => $localAmountCent,
'target_pubkey' => $pubKeyHex,
'target_date' => $localTargetDateFrozen
'target_date' => $localTargetDateFrozen,
'blockchain_type' => $this->blockchainType
]), '/createTransaction');
if('success' != $requestAnswear['state']) {
@ -580,7 +581,8 @@ class TransactionCreationsController extends AppController
$session_id,
$transaction_base64,
$user['balance'],
$auto_sign
$auto_sign,
$this->blockchainType
);
if ($requestResult['state'] != 'success') {
$msg = 'error returned from login server';

View File

@ -185,7 +185,8 @@ class TransactionSendCoinsController extends AppController
'memo' => $requestData['memo'],
'amount' => $amountCent,
'target_group' => $known_groups['data']['data']['groups'][$requestData['group']],
'target_email' => $receiverEmail
'target_email' => $receiverEmail,
'blockchain_type' => $this->blockchainType
]), '/createTransaction');
if('success' == $requestAnswear['state'] && 'success' == $requestAnswear['data']['state']) {

View File

@ -0,0 +1,30 @@
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
/**
* BlockchainType Entity
*
* @property int $id
* @property string $name
* @property string|null $text
* @property string|null $symbol
*/
class BlockchainType extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array
*/
protected $_accessible = [
'name' => true,
'text' => true,
'symbol' => true,
];
}

View File

@ -39,6 +39,7 @@ class Transaction extends Entity
'tx_hash' => true,
'memo' => true,
'received' => true,
'blockchain_type_id' => true,
'state_group' => true,
'transaction_type' => true,
'state_created' => true,

View File

@ -0,0 +1,68 @@
<?php
namespace App\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* BlockchainTypes Model
*
* @method \App\Model\Entity\BlockchainType get($primaryKey, $options = [])
* @method \App\Model\Entity\BlockchainType newEntity($data = null, array $options = [])
* @method \App\Model\Entity\BlockchainType[] newEntities(array $data, array $options = [])
* @method \App\Model\Entity\BlockchainType|false save(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method \App\Model\Entity\BlockchainType saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method \App\Model\Entity\BlockchainType patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
* @method \App\Model\Entity\BlockchainType[] patchEntities($entities, array $data, array $options = [])
* @method \App\Model\Entity\BlockchainType findOrCreate($search, callable $callback = null, $options = [])
*/
class BlockchainTypesTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('blockchain_types');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->nonNegativeInteger('id')
->allowEmptyString('id', null, 'create');
$validator
->scalar('name')
->maxLength('name', 45)
->requirePresence('name', 'create')
->notEmptyString('name');
$validator
->scalar('text')
->maxLength('text', 255)
->allowEmptyString('text');
$validator
->scalar('symbol')
->maxLength('symbol', 10)
->allowEmptyString('symbol');
return $validator;
}
}

View File

@ -52,6 +52,10 @@ class TransactionsTable extends Table
'foreignKey' => 'transaction_type_id',
'joinType' => 'INNER'
]);
$this->belongsTo('BlockchainTypes', [
'foreignKey' => 'blockchain_type_id',
'joinType' => 'INNER'
]);
$this->hasMany('StateCreated', [
'foreignKey' => 'transaction_id'
]);
@ -114,6 +118,7 @@ class TransactionsTable extends Table
{
$rules->add($rules->existsIn(['state_group_id'], 'StateGroups'));
$rules->add($rules->existsIn(['transaction_type_id'], 'TransactionTypes'));
$rules->add($rules->existsIn(['blockchain_type_id'], 'BlockchainTypes'));
return $rules;
}

View File

@ -0,0 +1,25 @@
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\BlockchainType $blockchainType
*/
?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('List Blockchain Types'), ['action' => 'index']) ?></li>
</ul>
</nav>
<div class="blockchainTypes form large-9 medium-8 columns content">
<?= $this->Form->create($blockchainType) ?>
<fieldset>
<legend><?= __('Add Blockchain Type') ?></legend>
<?php
echo $this->Form->control('name');
echo $this->Form->control('text');
echo $this->Form->control('symbol');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>

View File

@ -0,0 +1,31 @@
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\BlockchainType $blockchainType
*/
?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Form->postLink(
__('Delete'),
['action' => 'delete', $blockchainType->id],
['confirm' => __('Are you sure you want to delete # {0}?', $blockchainType->id)]
)
?></li>
<li><?= $this->Html->link(__('List Blockchain Types'), ['action' => 'index']) ?></li>
</ul>
</nav>
<div class="blockchainTypes form large-9 medium-8 columns content">
<?= $this->Form->create($blockchainType) ?>
<fieldset>
<legend><?= __('Edit Blockchain Type') ?></legend>
<?php
echo $this->Form->control('name');
echo $this->Form->control('text');
echo $this->Form->control('symbol');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>

View File

@ -0,0 +1,51 @@
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\BlockchainType[]|\Cake\Collection\CollectionInterface $blockchainTypes
*/
?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('New Blockchain Type'), ['action' => 'add']) ?></li>
</ul>
</nav>
<div class="blockchainTypes index large-9 medium-8 columns content">
<h3><?= __('Blockchain Types') ?></h3>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th scope="col"><?= $this->Paginator->sort('id') ?></th>
<th scope="col"><?= $this->Paginator->sort('name') ?></th>
<th scope="col"><?= $this->Paginator->sort('text') ?></th>
<th scope="col"><?= $this->Paginator->sort('symbol') ?></th>
<th scope="col" class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($blockchainTypes as $blockchainType): ?>
<tr>
<td><?= $this->Number->format($blockchainType->id) ?></td>
<td><?= h($blockchainType->name) ?></td>
<td><?= h($blockchainType->text) ?></td>
<td><?= h($blockchainType->symbol) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $blockchainType->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $blockchainType->id]) ?>
<?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $blockchainType->id], ['confirm' => __('Are you sure you want to delete # {0}?', $blockchainType->id)]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->first('<< ' . __('first')) ?>
<?= $this->Paginator->prev('< ' . __('previous')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('next') . ' >') ?>
<?= $this->Paginator->last(__('last') . ' >>') ?>
</ul>
<p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p>
</div>
</div>

View File

@ -0,0 +1,36 @@
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\BlockchainType $blockchainType
*/
?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('Edit Blockchain Type'), ['action' => 'edit', $blockchainType->id]) ?> </li>
<li><?= $this->Form->postLink(__('Delete Blockchain Type'), ['action' => 'delete', $blockchainType->id], ['confirm' => __('Are you sure you want to delete # {0}?', $blockchainType->id)]) ?> </li>
<li><?= $this->Html->link(__('List Blockchain Types'), ['action' => 'index']) ?> </li>
<li><?= $this->Html->link(__('New Blockchain Type'), ['action' => 'add']) ?> </li>
</ul>
</nav>
<div class="blockchainTypes view large-9 medium-8 columns content">
<h3><?= h($blockchainType->name) ?></h3>
<table class="vertical-table">
<tr>
<th scope="row"><?= __('Name') ?></th>
<td><?= h($blockchainType->name) ?></td>
</tr>
<tr>
<th scope="row"><?= __('Text') ?></th>
<td><?= h($blockchainType->text) ?></td>
</tr>
<tr>
<th scope="row"><?= __('Symbol') ?></th>
<td><?= h($blockchainType->symbol) ?></td>
</tr>
<tr>
<th scope="row"><?= __('Id') ?></th>
<td><?= $this->Number->format($blockchainType->id) ?></td>
</tr>
</table>
</div>

View File

@ -391,18 +391,42 @@ return [
'Session' => [
'defaults' => 'php',
],
// ***************************************************
// Gradido specific configuration
// ***************************************************
// Login Server ip and port
'LoginServer' => [
'host' => 'http://login-server',
'port' => 1201
'port' => 1201,
'url' => 'http://localhost'
],
'API' => [
// login server for look up emails from other groups
// workaround for using multiple groups until every code is finished as planned
// normally node server are responsible for email look up from users from other groups
'NeighborLoginServers' => [
['host' => 'login-server', 'port' => 1201]
],
'GradidoBlockchain' => [
// type:
// - db: centralized blockchain in mysql db, no cross group transactions
// - hedera: send transaction over hedera
'type' => 'db',
// gradido nodes with blockchain (if type != db)
'nodes' => [
['host' => 'node', 'port' => 13702]
]
],
'GroupAlias' => 'docker',
'GDTServer' => [
//'host' => 'gdt'
],
'API' => [
'allowedCaller' => ['login-server']
],
'ServerAdminEmail' => 'info@gradido.net',
'ServerAdminEmail' => 'info@gradido.net',
'noReplyEmail' => 'no-reply@gradido.net',
'disableEmail' => true,
'GroupNode' => false
'disableEmail' => false
];