From abf81ef36d3c1bf088c94df7279a68c3d80cb2ea Mon Sep 17 00:00:00 2001 From: David Drapeau Date: Fri, 27 Nov 2020 14:10:57 +0100 Subject: [PATCH] clean code and fix bugs (to cont.) --- install/install-v1.0.0.sql | 33 +-- models/register.php | 50 +---- modules/ClassUser.php | 280 ++++++++++++++++++-------- modules/InterfaceConfig.php | 8 +- modules/InterfacePostgreSQL.php | 2 +- scripts/config.js | 2 +- scripts/logout.php | 4 + themes/materialize/views/register.php | 45 +---- 8 files changed, 215 insertions(+), 209 deletions(-) diff --git a/install/install-v1.0.0.sql b/install/install-v1.0.0.sql index 7f486d0..6c2974b 100644 --- a/install/install-v1.0.0.sql +++ b/install/install-v1.0.0.sql @@ -14,29 +14,6 @@ DROP TABLE IF EXISTS core_lang; DROP TABLE IF EXISTS core_currency; DROP TABLE IF EXISTS core_theme; ---create table erp_config to link user frontend with backend -DROP TABLE IF EXISTS erp_config; -CREATE TABLE erp_config( - id SERIAL, - create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - k VARCHAR(128) NOT NULL, - v VARCHAR(128) NOT NULL, - is_active BOOLEAN NOT NULL DEFAULT TRUE, - comment TEXT NOT NULL DEFAULT '', - CONSTRAINT erConfig_id_pk PRIMARY KEY(id), - CONSTRAINT erpConfig_k_uk UNIQUE(k) -); -INSERT INTO erp_config(k, v) -VALUES('url', 'http://localhost:8069/'), - ('db', 'wodoo_backend'), - ('admin_user', 'admin@backend'), - ('admin_password', 'admin'), - ('host', 'localhost'), - ('pg_user', 'odoo11'), - ('pg_password', 'odoo11'); - - - CREATE TABLE core_theme ( id SERIAL, create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, @@ -316,7 +293,7 @@ CREATE TABLE core_feature ( /* OFFICIAL DATA */ -INSERT INTO core_theme(code, name, is_prod_available) VALUES('default', 'Default', TRUE); +INSERT INTO core_theme(code, name, is_prod_available) VALUES('default', 'Default', TRUE),('materialize', 'MaterializeCSS', TRUE); INSERT INTO core_currency(code, name, symbol, rounding, priority) VALUES ('usd', 'USD', '$', '0.01', 3); INSERT INTO core_currency(code, name, symbol, rounding) VALUES ('vef', 'VEF', 'Bs.F', '0.0001'); @@ -861,10 +838,10 @@ CREATE TABLE user_user ( phone VARCHAR(32) NOT NULL DEFAULT '', postcode VARCHAR(32) NOT NULL DEFAULT '', city VARCHAR(128) NOT NULL DEFAULT '', - core_lang_id INTEGER, - core_country_id INTEGER, - core_currency_id INTEGER, - core_theme_id INTEGER, + core_lang_id INTEGER DEFAULT NULL, + core_country_id INTEGER DEFAULT NULL, + core_currency_id INTEGER DEFAULT NULL, + core_theme_id INTEGER DEFAULT NULL, is_backend_access BOOLEAN NOT NULL DEFAULT FALSE, is_active BOOLEAN NOT NULL DEFAULT FALSE, is_validated BOOLEAN DEFAULT FALSE, diff --git a/models/register.php b/models/register.php index 9fc330e..a08ef7f 100644 --- a/models/register.php +++ b/models/register.php @@ -8,16 +8,6 @@ $oTrans = new ClassTranslation(); $oUser = new ClassUser(); $oForm = new ClassForm(); -$url = $_SESSION['erp']['url']; -$db = $_SESSION['erp']['db']; -$username = $_SESSION['erp']['admin_user']; -$password = $_SESSION['erp']['admin_password']; - -$admin_connection = new ClassXMLRPC($url, $db, $username, $password); - -$countries = $admin_connection->execute('res.country', 'search_read', [[]], ['fields'=>['id', 'name', 'code']]); - - // Creating user in ERP and in WEFRA // .. if(isset($_POST['formRegisterButtonSubmit'])){ @@ -30,43 +20,9 @@ if(isset($_POST['formRegisterButtonSubmit'])){ } } - $name = $_POST['formRegisterFieldFirstname'].' '.$_POST['formRegisterFieldLastname']; - - //create user in ERP - $company_id = $admin_connection->execute('res.company', 'search', [[]], [])[0]; - $ext_id = $admin_connection->execute('res.users', 'create', - [['name'=>$name, - 'email'=>$_POST['formRegisterFieldEmail'], - 'login'=>$_POST['formRegisterFieldEmail'], //yes, the login IS the email, there is no mistake here - 'company_id'=>$company_id, - 'client'=>true - ]], - [] - ); - - //if user created in ERP, then create it in Wefra with password linked - if(isset($ext_id) and !is_array($ext_id)){ - //create user in Wefra and link the user to ERP - $user = $oUser->createUser($_POST, $ext_id); - - if($user){ - //update the password for user in ERP - //.. - $getUser = $oUser->getUser($user['user_id']); - $admin_connection->execute('res.users', 'write', [[$ext_id], ['password'=>$getUser['password'], 'password_uncrypted'=>$_POST['formRegisterFieldPassword']]], []); - //.. - //that way, ERP rights will be checked everytime Wodoo will use ORM to allow user to to actions which will have an influence in the ERP (ie. get a task which will be assigned to his ERP's user related) - - - } - //display a confirmation message that everything went well - $message['status']="teal"; - $message['content'] = "

Great job!



You are now part of JMDN Consulting Team.

We will come back to you very soon.

"; - } else { - //if user not created in ERP, displaying a warning message to ask for a new try - $message['status']="orange"; - $message['content'] = "we have failed to register your profile. Please try again or contact the team contact@jmdn-solutions.com with title: Cannot create account via Wodoo"; - } + //create user in Wefra and link the user to ERP + $res = $oUser->createUser($_POST); + print_r($res); } // .. // ENDOF creating user in ERP and in WeFra diff --git a/modules/ClassUser.php b/modules/ClassUser.php index eb428d5..1176646 100644 --- a/modules/ClassUser.php +++ b/modules/ClassUser.php @@ -19,13 +19,8 @@ class ClassUser extends ClassConfig { $execSQL = $oPDOLink->prepare($sql); $execSQL->execute(array(':user_id'=>$this->_userId, ':feature_code'=>$this->_featureCode)); } + //ENDOF _addFeatureToUser() - private function _setFeaturesToUser($user_id){ - $this->_userId = $user_id; - ClassUser::_addFeatureToUser($this->_userId, 'home'); - ClassUser::_addFeatureToUser($this->_userId, 'portfolio'); - ClassUser::_addFeatureToUser($this->_userId, 'logout'); - } public function getUserIdByLogin($user_login){ $this->_userLogin = $user_login; @@ -36,16 +31,18 @@ class ClassUser extends ClassConfig { $row = $execSQL->fetch(PDO::FETCH_ASSOC); return $row['id']; } + //ENDOF getUserIdByLogin() private function _checkIfVipOfferActive(){ $config = ClassConfig::getConfig(); - $oPDOLink = ClassConfig::databaseConnect(); + $oPDOLink = ClassConfig::databaseConnect(); if($config['is_vip_offer_active']==='true'){ return true; } else { return false; } } + //ENDOF _checkIfVipOfferActive() private function _checkIfEverythingForFreeActive(){ $config = ClassConfig::getConfig(); @@ -56,96 +53,203 @@ class ClassUser extends ClassConfig { return false; } } + //ENDOF _checkIfEverythingForFreeActive() - public function createUser($data, $ext_id){ - $this->_data = $data; - $this->_extId = $ext_id; - + private function _checkEmailConfirmation($email, $email_confirmation){ $config = ClassConfig::getConfig(); $oPDOLink = ClassConfig::databaseConnect(); //IF password and confirmPassword are not identical, displaying an error message - if($this->_data['formRegisterFieldPassword'] != $this->_data['formRegisterFieldConfirmPassword']){ - $message['state']='failed'; - $message['css_class']='failed'; - $message['translation_code'] = 'checkRegisterForm_notSamePassword'; - return $message; + if($email == $email_confirmation){ + return true; + } else { + return false; } - else { - $activation_code = rand(); - $sql=" - INSERT INTO user_user(email, password, activation_code, firstname, lastname, - core_lang_id, core_country_id, core_currency_id, core_theme_id, - is_backend_access, is_active, is_employee, - comment - ) VALUES ( - :email, :password, :activation_code, - :firstname, :lastname, - (SELECT id FROM core_lang WHERE code='en_gb'), - (SELECT id FROM core_country WHERE code='ch'), - (SELECT id FROM core_currency WHERE code='chf'), 1, - false, true, false, - 'user registered online via Wodoo frontend' - ) - "; - - $execSQL = $oPDOLink->prepare($sql); - if($execSQL->execute(array( - ':email'=>$this->_data['formRegisterFieldEmail'], - ':password'=>sha1($this->_data['formRegisterFieldPassword'].'-k3P[8x&'), - ':activation_code'=>$activation_code, - ':firstname'=>$this->_data['formRegisterFieldFirstname'], - ':lastname'=>$this->_data['formRegisterFieldLastname'] - ))){ - $newUserId = $oPDOLink->lastInsertId('user_user_id_seq'); - - //$isVipOfferActive = $this->_checkIfVipOfferActive(); - //$isEverythingForFreeActive = $this->_checkIfEverythingForFreeActive(); - // core user detail - $sql=" - INSERT INTO user_detail(user_id, ext_id) - VALUES (:user_id, :ext_id) - "; - $execSQL = $oPDOLink->prepare($sql); - $execSQL->execute(array(':user_id'=>$newUserId, ':ext_id'=>$this->_extId)); - - // get features for user - $sql="SELECT id FROM core_feature WHERE is_user_feature=TRUE;"; - $getFeatures = $oPDOLink->prepare($sql); - $getFeatures->execute(array()); - $features = $getFeatures->fetchAll(PDO::FETCH_ASSOC); - - $sql=" - INSERT INTO useruser_corefeature_rel(user_id, core_feature_id) - VALUES ( - (SELECT id FROM user_user WHERE email=:email LIMIT 1), - :feature_id - ); - "; - $execSQL = $oPDOLink->prepare($sql); - - //set features to user - foreach($features as $feature){ - $execSQL->execute(array(':email'=>$this->_data['formRegisterFieldEmail'], ':feature_id'=>$feature['id'])); - } - - $message['user_id'] = $newUserId; - $message['activation_code'] = $activation_code; - $message['status'] = "success"; - $message['css_class'] = 'success-message'; - //TODO change confirmation_registration by register_form_success when registrations will need an activation by clic on link in an email - //$message['translation_code'] = 'register_form_success'; //'confirmation_registration'; - - //TODO send an email to admin to indicate that there is a new registration - return $message; + } + //ENDOF _checkEmailConfirmation() + + private function _createUserUser($data){ + $this->_data = $data; + $config = ClassConfig::getConfig(); + $oPDOLink = ClassConfig::databaseConnect(); + $activation_code = rand(); + + $sql=" + INSERT INTO user_user(email, password, activation_code, + core_lang_id, core_country_id, core_currency_id, core_theme_id, + comment + ) VALUES ( + :email, :password, :activation_code, + (SELECT id FROM core_lang WHERE code='en_gb'), + (SELECT id FROM core_country WHERE code='ch'), + (SELECT id FROM core_currency WHERE code='chf'), + (SELECT id FROM core_theme WHERE code='materialize'), + 'user registered online via Wefra frontend' + ) + "; + $execSQL = $oPDOLink->prepare($sql); + $res = $execSQL->execute([ + ':email'=>$this->_data['formRegisterFieldEmail'], + ':password'=>sha1($this->_data['formRegisterFieldPassword'].'-k3P[8x&'), + ':activation_code'=>$activation_code + ]); + if($res == true || $res == 1){ + $user_id = $oPDOLink->lastInsertId('user_user_id_seq'); + return $user_id; + } else { + return false; + } + } + //ENDOF _createUserUser() + + private function _createUserDetail($user_id){ + $config = ClassConfig::getConfig(); + $oPDOLink = ClassConfig::databaseConnect(); + + $sql=" + INSERT INTO user_detail(user_id) + VALUES (:user_id) + "; + $execSQL = $oPDOLink->prepare($sql); + return $execSQL->execute(array(':user_id'=>$user_id)); + } + //ENDOF _createUserDetail() + + private function _setFeaturesToUser($user_id){ + $config = ClassConfig::getConfig(); + $oPDOLink = ClassConfig::databaseConnect(); + + // get features for user + $sql="SELECT id FROM core_feature WHERE is_user_feature=TRUE;"; + $getFeatures = $oPDOLink->prepare($sql); + $getFeatures->execute(array()); + $features = $getFeatures->fetchAll(PDO::FETCH_ASSOC); + + $sql=" + INSERT INTO useruser_corefeature_rel(user_id, core_feature_id) + VALUES ( + :user_id, + :feature_id + ); + "; + $execSQL = $oPDOLink->prepare($sql); + + //set features to user + foreach($features as $feature){ + $execSQL->execute(array(':user_id'=>$user_id, ':feature_id'=>$feature['id'])); + } + return true; + } + //ENDOF _setFeaturesToUser() + + public function createUser($data){ + $this->_data = $data; + $resEC = $this->_checkEmailConfirmation($this->_data['formRegisterFieldPassword'], $this->_data['formRegisterFieldConfirmPassword']); + + if($resEC == true){ + $user_id = $this->_createUserUser($this->_data); + if(is_numeric($user_id) == true){ + $resCUD = $this->_createUserDetail((integer) $user_id); + if($resCUD){ + $resSFTU = $this->_setFeaturesToUser($user_id); + return 'success_creation_user'; + } else { + return 'error_create_user_detail'; + } } else { - $message['status']='failed'; - $message['css_class'] = 'failed-message'; - //$message['translation_code'] = 'register_form_failed'; - return $message; + return 'error_create_user_user'; } + } else { + return 'email_diff_confirm_email'; } - } + } + //ENDOF createUser() + +// public function createUser($data){ +// $this->_data = $data; +// $config = ClassConfig::getConfig(); +// $oPDOLink = ClassConfig::databaseConnect(); +// +// +// //IF password and confirmPassword are not identical, displaying an error message +// if($this->_data['formRegisterFieldPassword'] != $this->_data['formRegisterFieldConfirmPassword']){ +// $message['state']='failed'; +// $message['css_class']='failed'; +// $message['translation_code'] = 'checkRegisterForm_notSamePassword'; +// return $message; +// } +// +// $activation_code = rand(); +// $sql=" +// INSERT INTO user_user(email, password, activation_code +// core_lang_id, core_country_id, core_currency_id, core_theme_id, +// comment +// ) VALUES ( +// :email, :password, :activation_code, +// (SELECT id FROM core_lang WHERE code='en_gb'), +// (SELECT id FROM core_country WHERE code='ch'), +// (SELECT id FROM core_currency WHERE code='chf'), +// (SELECT id FROM core_theme WHERE code='materialize'), +// 'user registered online via Wefra frontend' +// ) +// "; +// +// $execSQL = $oPDOLink->prepare($sql); +// if($execSQL->execute(array( +// ':email'=>$this->_data['formRegisterFieldEmail'], +// ':password'=>sha1($this->_data['formRegisterFieldPassword'].'-k3P[8x&'), +// ':activation_code'=>$activation_code, +// ))){ +// $newUserId = $oPDOLink->lastInsertId('user_user_id_seq'); +// +// //$isVipOfferActive = $this->_checkIfVipOfferActive(); +// //$isEverythingForFreeActive = $this->_checkIfEverythingForFreeActive(); +// // core user detail +// $sql=" +// INSERT INTO user_detail(user_id) +// VALUES (:user_id) +// "; +// $execSQL = $oPDOLink->prepare($sql); +// $execSQL->execute(array(':user_id'=>$newUserId)); +// +// // get features for user +// $sql="SELECT id FROM core_feature WHERE is_user_feature=TRUE;"; +// $getFeatures = $oPDOLink->prepare($sql); +// $getFeatures->execute(array()); +// $features = $getFeatures->fetchAll(PDO::FETCH_ASSOC); +// +// $sql=" +// INSERT INTO useruser_corefeature_rel(user_id, core_feature_id) +// VALUES ( +// (SELECT id FROM user_user WHERE email=:email LIMIT 1), +// :feature_id +// ); +// "; +// $execSQL = $oPDOLink->prepare($sql); +// +// //set features to user +// foreach($features as $feature){ +// $execSQL->execute(array(':email'=>$this->_data['formRegisterFieldEmail'], ':feature_id'=>$feature['id'])); +// } +// +// $message['user_id'] = $newUserId; +// $message['activation_code'] = $activation_code; +// $message['status'] = "success"; +// $message['css_class'] = 'success-message'; +// //TODO change confirmation_registration by register_form_success when registrations will need an activation by clic on link in an email +// //$message['translation_code'] = 'register_form_success'; //'confirmation_registration'; +// +// //TODO send an email to admin to indicate that there is a new registration +// return $message; +// } else { +// $message['user_id'] = "null"; +// $message['activation_code'] = "null"; +// $message['status']='error'; +// $message['css_class'] = 'failed-message'; +// //$message['translation_code'] = 'register_form_failed'; +// return $message; +// } +// } public function activateUser($activation_code){ diff --git a/modules/InterfaceConfig.php b/modules/InterfaceConfig.php index 3050ff7..6054896 100644 --- a/modules/InterfaceConfig.php +++ b/modules/InterfaceConfig.php @@ -1,10 +1,10 @@ getURLRoot()); diff --git a/themes/materialize/views/register.php b/themes/materialize/views/register.php index a2224b8..f4bb02e 100644 --- a/themes/materialize/views/register.php +++ b/themes/materialize/views/register.php @@ -1,18 +1,3 @@ - @@ -21,23 +6,13 @@ if(isset($message)){
- +
- Your profile has been created with success. You can now login and fully use Globsi platform. -
- -
- Please copy paste this message below and send it by email to clients@globsi.com -

Something went wrong on the FE side.

-
- -
- Please copy paste this message below and send it by email to clients@globsi.com -

Something went wrong on the BE side.

+ Your profile has been created with success.
- Please copy paste this message below and send it by email to clients@globsi.com + Please copy paste this message below and send it by email to

Something went wrong while trying to register.

@@ -48,7 +23,7 @@ if(isset($message)){
@@ -64,17 +39,7 @@ if( !isset($message) or $message['backend']['status']==false or $message['fronte
Contact
-
- account_circle - - -
-
- - - -
-
+
email