From 0c7bb13b4e1e414b751d6ec12b1ccd349eb8eea3 Mon Sep 17 00:00:00 2001 From: Einhornimmond Date: Fri, 17 Sep 2021 21:13:14 +0200 Subject: [PATCH] change state_users table to user and rename colums in camelCase, adapt --- backend/src/index.ts | 2 +- .../src/Controller/AppController.php | 16 +++--- .../JsonRequestHandlerController.php | 6 +- .../Controller/StateUserRolesController.php | 4 +- .../src/Controller/StateUsersController.php | 18 +++--- .../TransactionCreationsController.php | 20 +++---- .../src/Model/Entity/StateUser.php | 14 ++--- .../src/Model/Table/StateUsersTable.php | 16 ++---- .../Model/Transactions/TransactionBase.php | 6 +- .../Transactions/TransactionCreation.php | 4 +- .../Transactions/TransactionTransfer.php | 4 +- database/migrations/0002-update_user.ts | 56 +++++++++++++++++++ 12 files changed, 106 insertions(+), 60 deletions(-) create mode 100644 database/migrations/0002-update_user.ts diff --git a/backend/src/index.ts b/backend/src/index.ts index b99e5bee4..07086c544 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -21,7 +21,7 @@ import { isAuthorized } from './auth/auth' // TODO implement // import queryComplexity, { simpleEstimator, fieldConfigEstimator } from "graphql-query-complexity"; -const DB_VERSION = '0001-init_db' +const DB_VERSION = '0002-update_user' const context = (args: any) => { const authorization = args.req.headers.authorization diff --git a/community_server/src/Controller/AppController.php b/community_server/src/Controller/AppController.php index e70b8e7bd..2c4b2b5e5 100644 --- a/community_server/src/Controller/AppController.php +++ b/community_server/src/Controller/AppController.php @@ -223,22 +223,22 @@ class AppController extends Controller $public_key_bin = hex2bin($json['user']['public_hex']); $stateUserQuery = $stateUserTable ->find('all') - ->where(['public_key' => $public_key_bin]) + ->where(['pubkey' => $public_key_bin]) ->contain('StateBalances', function ($q) { return $q->order(['record_date' => 'DESC']) ->limit(1); }); if ($stateUserQuery->count() == 1) { $stateUser = $stateUserQuery->first(); - if ($stateUser->first_name != $json['user']['first_name'] || - $stateUser->last_name != $json['user']['last_name'] || + if ($stateUser->firstName != $json['user']['first_name'] || + $stateUser->lastName != $json['user']['last_name'] || $stateUser->disabled != $json['user']['disabled'] || //$stateUser->username != $json['user']['username'] || // -> throws error $stateUser->email != $json['user']['email'] ) { - $stateUser->first_name = $json['user']['first_name']; - $stateUser->last_name = $json['user']['last_name']; + $stateUser->firstName = $json['user']['first_name']; + $stateUser->lastName = $json['user']['last_name']; $stateUser->disabled = intval($json['user']['disabled']); //$stateUser->username = $json['user']['username']; $stateUser->email = $json['user']['email']; @@ -250,9 +250,9 @@ class AppController extends Controller //echo $stateUser['id']; } else { $newStateUser = $stateUserTable->newEntity(); - $newStateUser->public_key = $public_key_bin; - $newStateUser->first_name = $json['user']['first_name']; - $newStateUser->last_name = $json['user']['last_name']; + $newStateUser->pubkey = $public_key_bin; + $newStateUser->firstName = $json['user']['first_name']; + $newStateUser->lastName = $json['user']['last_name']; $newStateUser->disabled = intval($json['user']['disabled']); //$newStateUser->username = $json['user']['username']; $newStateUser->email = $json['user']['email']; diff --git a/community_server/src/Controller/JsonRequestHandlerController.php b/community_server/src/Controller/JsonRequestHandlerController.php index 611984118..150cce4ae 100644 --- a/community_server/src/Controller/JsonRequestHandlerController.php +++ b/community_server/src/Controller/JsonRequestHandlerController.php @@ -308,7 +308,7 @@ class JsonRequestHandlerController extends AppController { $stateError = $stateErrorTable->newEntity(); // $pubkey = hex2bin($jsonData->public_key); - $user_query = $stateUsersTable->find('all')->select(['id'])->where(['public_key' => $pubkey]); + $user_query = $stateUsersTable->find('all')->select(['id'])->where(['pubkey' => $pubkey]); if($user_query->count() != 1) { return $this->returnJson(['state' => 'error', 'msg' => 'user not found', 'details' => 'user pubkey hex:' . $jsonData->public_key]); } @@ -412,7 +412,7 @@ class JsonRequestHandlerController extends AppController { //$pubkeys->sender //$pubkeys->receiver $stateUserTable = TableRegistry::getTableLocator()->get('StateUsers'); - $user = $stateUserTable->find('all')->where(['public_key' => hex2bin($pubkeys->sender)])->contain(['StateBalances']); + $user = $stateUserTable->find('all')->where(['pubkey' => hex2bin($pubkeys->sender)])->contain(['StateBalances']); if(!$user->count()) { return $this->returnJson(['state' => 'not found', 'msg' => 'user not found or empty balance']); } @@ -461,7 +461,7 @@ class JsonRequestHandlerController extends AppController { private function userDelete($userPubkeyHex) { $stateUserTable = TableRegistry::getTableLocator()->get('StateUsers'); - $user = $stateUserTable->find('all')->where(['public_key' => hex2bin($userPubkeyHex)]); + $user = $stateUserTable->find('all')->where(['pubkey' => hex2bin($userPubkeyHex)]); if(!$user || $user->count == 0) { return $this->returnJson(['state' => 'error', 'msg' => 'user not found']); } diff --git a/community_server/src/Controller/StateUserRolesController.php b/community_server/src/Controller/StateUserRolesController.php index 9238c7ad6..80b168f35 100644 --- a/community_server/src/Controller/StateUserRolesController.php +++ b/community_server/src/Controller/StateUserRolesController.php @@ -132,7 +132,7 @@ class StateUserRolesController extends AppController $public_hex = hex2bin($requestData['public_hex']); - $stateUser = $this->StateUsers->find('all')->where(['public_key' => $public_hex])->first(); + $stateUser = $this->StateUsers->find('all')->where(['pubkey' => $public_hex])->first(); foreach($requestData['role_id'] as $role_id) { @@ -158,7 +158,7 @@ class StateUserRolesController extends AppController $publichex = hex2bin($public_hex); - $stateUser = $this->StateUsers->find('all')->where(['public_key' => $publichex])->first(); + $stateUser = $this->StateUsers->find('all')->where(['pubkey' => $publichex])->first(); $stateUserRoles = $this->StateUserRoles->find('all')->where(['state_user_id' => $stateUser->id])->all(); diff --git a/community_server/src/Controller/StateUsersController.php b/community_server/src/Controller/StateUsersController.php index fc6ec8614..25171f462 100644 --- a/community_server/src/Controller/StateUsersController.php +++ b/community_server/src/Controller/StateUsersController.php @@ -182,15 +182,15 @@ class StateUsersController extends AppController if($account_state == 'email not activated') { if(count($pubkeySorted) > 0) { - $communityUsers->where(['hex(public_key) IN' => array_keys($pubkeySorted)]); + $communityUsers->where(['hex(pubkey) IN' => array_keys($pubkeySorted)]); } else { $communityUsers = null; } } else { $globalSearch = '%' . $searchString . '%'; $communityUsers->where(['OR' => [ - 'first_name LIKE' => $globalSearch, - 'last_name LIKE' => $globalSearch, + 'firstName LIKE' => $globalSearch, + 'lastName LIKE' => $globalSearch, 'email LIKE' => $globalSearch ]]); } @@ -199,7 +199,7 @@ class StateUsersController extends AppController //var_dump($communityUsers->toArray()); if($communityUsers) { foreach($communityUsers as $u) { - $pubkey_hex = bin2hex(stream_get_contents($u->public_key)); + $pubkey_hex = bin2hex(stream_get_contents($u->pubkey)); $u->public_hex = $pubkey_hex; if(!isset($pubkeySorted[$pubkey_hex])) { $pubkeySorted[$pubkey_hex] = ['login' => [], 'community' => []]; @@ -229,9 +229,9 @@ class StateUsersController extends AppController $color = 'danger'; if(count($user['community']) == 1) { $c_user = $user['community'][0]; - $finalUser['name'] = $c_user->first_name . ' ' . $c_user->last_name; - $finalUser['first_name'] = $c_user->first_name; - $finalUser['last_name'] = $c_user->last_name; + $finalUser['name'] = $c_user->firstName . ' ' . $c_user->lastName; + $finalUser['first_name'] = $c_user->firstName; + $finalUser['last_name'] = $c_user->lastName; $finalUser['email'] = $c_user->email; } } else if(count($user['login']) == 1) { @@ -415,7 +415,7 @@ class StateUsersController extends AppController //$user = $jsonData['user']; //var_dump($jsonData); $pubkey = hex2bin($jsonData['pubkeyhex']); - $stateUsers = $this->StateUsers->find('all')->where(['public_key' => $pubkey]); + $stateUsers = $this->StateUsers->find('all')->where(['pubkey' => $pubkey]); if($stateUsers->count() != 1) { return $this->returnJson(['state' => 'error', 'msg' => 'invalid result count']); } @@ -448,7 +448,7 @@ class StateUsersController extends AppController $pubkey = hex2bin($jsonData['pubkeyhex']); $stateUsers = $this->StateUsers ->find('all') - ->where(['public_key' => $pubkey]) + ->where(['pubkey' => $pubkey]) ->select(['id']); if($stateUsers->count() != 1) { return $this->returnJson(['state' => 'error', 'msg' => 'invalid result count']); diff --git a/community_server/src/Controller/TransactionCreationsController.php b/community_server/src/Controller/TransactionCreationsController.php index d02320d25..f024e5cb4 100644 --- a/community_server/src/Controller/TransactionCreationsController.php +++ b/community_server/src/Controller/TransactionCreationsController.php @@ -101,7 +101,7 @@ class TransactionCreationsController extends AppController $receiverProposal = []; foreach ($stateUsers as $stateUser) { $name = $stateUser->email; - $keyHex = bin2hex(stream_get_contents($stateUser->public_key)); + $keyHex = bin2hex(stream_get_contents($stateUser->pubkey)); if ($name === null) { $name = $stateUser->first_name . ' ' . $stateUser->last_name; } @@ -241,14 +241,14 @@ class TransactionCreationsController extends AppController $this->log("search for text: ".$requestData['searchText'], 'debug'); $stateUsers = $stateUserTable ->find('all') - ->select(['id', 'first_name', 'last_name', 'email']) - ->order(['first_name', 'last_name']) + ->select(['id', 'firstName', 'lastName', 'email']) + ->order(['firstName', 'lastName']) ->where( ['AND' => [ 'disabled' => 0, 'OR' => [ - 'LOWER(first_name) LIKE' => '%'.strtolower($requestData['searchText']).'%', - 'LOWER(last_name) LIKE' => '%'.strtolower($requestData['searchText']).'%', + 'LOWER(firstName) LIKE' => '%'.strtolower($requestData['searchText']).'%', + 'LOWER(lastName) LIKE' => '%'.strtolower($requestData['searchText']).'%', 'LOWER(email) LIKE' => '%'.strtolower($requestData['searchText']).'%' ] ] @@ -265,10 +265,10 @@ class TransactionCreationsController extends AppController } else { $stateUsers = $stateUserTable ->find('all') - ->select(['id', 'first_name', 'last_name', 'email']) + ->select(['id', 'firstName', 'lastName', 'email']) //->order(['id']) ->where(['disabled' => 0]) - ->order(['first_name', 'last_name']) + ->order(['firstName', 'lastName']) ->contain(['TransactionCreations' => [ 'fields' => [ 'TransactionCreations.amount', @@ -300,7 +300,7 @@ class TransactionCreationsController extends AppController //if($sumAmount < 20000000) { array_push($possibleReceivers, [ - 'name' => $stateUser->first_name . ' ' . $stateUser->last_name, + 'name' => $stateUser->firstName . ' ' . $stateUser->lastName, 'id' => $stateUser->id, 'email' => $stateUser->email, 'amount' => $sumAmount, @@ -353,7 +353,7 @@ class TransactionCreationsController extends AppController } $receiverUsers = $stateUserTable->find('all') ->where(['id IN' => array_keys($users)]) - ->select(['public_key', 'email', 'id']) + ->select(['pubkey', 'email', 'id']) ->contain(false); foreach ($receiverUsers as $receiverUser) { @@ -371,7 +371,7 @@ class TransactionCreationsController extends AppController } else { $pendings[$id] = $localAmountCent; } - $pubKeyHex = bin2hex(stream_get_contents($receiverUser->public_key)); + $pubKeyHex = bin2hex(stream_get_contents($receiverUser->pubkey)); $requestAnswear = $this->JsonRequestClient->sendRequest(json_encode([ 'session_id' => $session->read('session_id'), 'email' => $receiverUser->email, diff --git a/community_server/src/Model/Entity/StateUser.php b/community_server/src/Model/Entity/StateUser.php index 5fac7cda7..374e8d608 100644 --- a/community_server/src/Model/Entity/StateUser.php +++ b/community_server/src/Model/Entity/StateUser.php @@ -30,16 +30,14 @@ class StateUser extends Entity * @var array */ protected $_accessible = [ - 'index_id' => true, - 'state_group_id' => true, - 'public_key' => true, + 'groupId' => true, + 'pubkey' => true, 'email' => true, - 'first_name' => true, - 'last_name' => true, + 'firstName' => true, + 'lastName' => true, 'disabled' => true, 'username' => true, 'index' => true, - 'state_group' => true, 'state_balances' => true, 'state_created' => true, 'transaction_creations' => true, @@ -48,11 +46,11 @@ class StateUser extends Entity public function getEmailWithName() { - return $this->first_name . ' ' . $this->last_name . ' <' . $this->email . '>'; + return $this->firstName . ' ' . $this->lastName . ' <' . $this->email . '>'; } public function getNames() { - return $this->first_name . ' ' . $this->last_name; + return $this->firstName . ' ' . $this->lastName; } } diff --git a/community_server/src/Model/Table/StateUsersTable.php b/community_server/src/Model/Table/StateUsersTable.php index 5b01609f3..729b7800a 100644 --- a/community_server/src/Model/Table/StateUsersTable.php +++ b/community_server/src/Model/Table/StateUsersTable.php @@ -37,18 +37,10 @@ class StateUsersTable extends Table { parent::initialize($config); - $this->setTable('state_users'); + $this->setTable('user'); $this->setDisplayField('email'); $this->setPrimaryKey('id'); - /*$this->belongsTo('Indices', [ - 'foreignKey' => 'index_id', - 'joinType' => 'INNER' - ]);*/ - $this->belongsTo('StateGroups', [ - 'foreignKey' => 'state_group_id', - 'joinType' => 'INNER' - ]); $this->hasMany('StateBalances', [ 'foreignKey' => 'state_user_id' ]); @@ -80,8 +72,8 @@ class StateUsersTable extends Table ->allowEmptyString('id', null, 'create'); $validator - ->requirePresence('public_key', 'create') - ->notEmptyString('public_key'); + ->requirePresence('pubkey', 'create') + ->notEmptyString('pubkey'); return $validator; } @@ -124,7 +116,7 @@ class StateUsersTable extends Table $involvedUser = $this->find('all', [ 'contain' => [], 'where' => ['id IN' => $involvedUserIds], - 'fields' => ['id', 'first_name', 'last_name', 'email'], + 'fields' => ['id', 'firstName', 'lastName', 'email'], ]); //var_dump($involvedUser->toArray()); $involvedUserIndices = []; diff --git a/community_server/src/Model/Transactions/TransactionBase.php b/community_server/src/Model/Transactions/TransactionBase.php index 6b3817201..8c2709a96 100644 --- a/community_server/src/Model/Transactions/TransactionBase.php +++ b/community_server/src/Model/Transactions/TransactionBase.php @@ -49,13 +49,13 @@ class TransactionBase { protected function getStateUserId($publicKey) { $stateUsersTable = self::getTable('state_users'); - $stateUser = $stateUsersTable->find('all')->select(['id'])->where(['public_key' => $publicKey])->first(); + $stateUser = $stateUsersTable->find('all')->select(['id'])->where(['pubkey' => $publicKey])->first(); if($stateUser) { return $stateUser->id; } // create new entry $stateUserEntity = $stateUsersTable->newEntity(); - $stateUserEntity->public_key = $publicKey; + $stateUserEntity->pubkey = $publicKey; if($stateUsersTable->save($stateUserEntity)) { return $stateUserEntity->id; } else { @@ -77,7 +77,7 @@ class TransactionBase { protected function getStateUserFromPublickey($publicKey) { $stateUsersTable = self::getTable('state_users'); - $stateUser = $stateUsersTable->find('all')->where(['public_key' => $publicKey])->first(); + $stateUser = $stateUsersTable->find('all')->where(['pubkey' => $publicKey])->first(); if($stateUser) { return $stateUser; } diff --git a/community_server/src/Model/Transactions/TransactionCreation.php b/community_server/src/Model/Transactions/TransactionCreation.php index 87f2d2104..a8263c60d 100644 --- a/community_server/src/Model/Transactions/TransactionCreation.php +++ b/community_server/src/Model/Transactions/TransactionCreation.php @@ -95,7 +95,7 @@ class TransactionCreation extends TransactionBase { $existingCreations2 = $this->transactionCreationsTable ->find('all') ->select(['amount', 'state_user_id', 'target_date']) - ->contain(['StateUsers' => ['fields' => ['StateUsers.public_key']]]); + ->contain(['StateUsers' => ['fields' => ['StateUsers.pubkey']]]); $q = $existingCreations2; $targetDate = $this->protoTransactionCreation->getTargetDate(); @@ -230,7 +230,7 @@ class TransactionCreation extends TransactionBase { $receiverAmount = new \Proto\Gradido\TransferAmount(); - $receiverAmount->setPubkey(stream_get_contents($stateUser->public_key)); + $receiverAmount->setPubkey(stream_get_contents($stateUser->pubkey)); $receiverAmount->setAmount($transactionCreationEntity->amount); $protoCreation->setReceiver($receiverAmount); diff --git a/community_server/src/Model/Transactions/TransactionTransfer.php b/community_server/src/Model/Transactions/TransactionTransfer.php index dc1606f55..2bd470e73 100644 --- a/community_server/src/Model/Transactions/TransactionTransfer.php +++ b/community_server/src/Model/Transactions/TransactionTransfer.php @@ -96,7 +96,7 @@ class TransactionTransfer extends TransactionBase { $user = $stateUsersTable ->find('all') ->select(['id']) - ->where(['public_key' => $senderPublic]) + ->where(['pubkey' => $senderPublic]) ->contain(['StateBalances' => ['fields' => ['amount', 'state_user_id']]])->first(); if(!$user) { $this->addError($functionName, 'couldn\'t find sender in db' ); @@ -114,7 +114,7 @@ class TransactionTransfer extends TransactionBase { return false; } // check if receiver exist - $receiver_user = $stateUsersTable->find('all')->select(['id'])->where(['public_key' => $receiver_public_key])->first(); + $receiver_user = $stateUsersTable->find('all')->select(['id'])->where(['pubkey' => $receiver_public_key])->first(); if(!$receiver_user) { $this->addError($functionName, 'couldn\'t find receiver in db' ); return false; diff --git a/database/migrations/0002-update_user.ts b/database/migrations/0002-update_user.ts new file mode 100644 index 000000000..54e6acc7e --- /dev/null +++ b/database/migrations/0002-update_user.ts @@ -0,0 +1,56 @@ +/* FIRST MIGRATION + * + * This migration is special since it takes into account that + * the database can be setup already but also may not be. + * Therefore you will find all `CREATE TABLE` statements with + * a `IF NOT EXISTS`, all `INSERT` with an `IGNORE` and in the + * downgrade function all `DROP TABLE` with a `IF EXISTS`. + * This ensures compatibility for existing or non-existing + * databases. + */ + +export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { + // write upgrade logic as parameter of queryFn + + // rename table + await queryFn(` + ALTER TABLE state_users + RENAME TO user; + `) + // drop not used columns + await queryFn(` + ALTER TABLE user + DROP COLUMN index_id; + `) + // rename from snake case to camel case (cakePHP standard to typeorm standard) + await queryFn(` + ALTER TABLE user + CHANGE COLUMN group_id groupId int(10) unsigned NOT NULL DEFAULT '0', + CHANGE COLUMN public_key pubkey binary(32) NOT NULL, + CHANGE COLUMN first_name firstName varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + CHANGE COLUMN last_name lastName varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL; + `) + } + + export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { + // write downgrade logic as parameter of queryFn + // rename table + await queryFn(` + ALTER TABLE user + RENAME TO state_users; + `) + // put back dropped column + await queryFn(` + ALTER TABLE state_users + ADD index_id smallint(6) NOT NULL DEFAULT '0'; + `) + // rename from camel case to snake case (typeorm standard to cakePHP standard) + await queryFn(` + ALTER TABLE state_users + CHANGE COLUMN groupId group_id int(10) unsigned NOT NULL DEFAULT '0', + CHANGE COLUMN pubkey public_key binary(32) NOT NULL, + CHANGE COLUMN firstName first_name varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + CHANGE COLUMN lastName last_name varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL; + `) + } + \ No newline at end of file