diff --git a/apiConfig.readonly.json b/apiConfig.readonly.json index ff64bfd..2c91a2b 100644 --- a/apiConfig.readonly.json +++ b/apiConfig.readonly.json @@ -1,5 +1,5 @@ { - "version": "2.0.14", + "version": "2.1.0", "tokenTimeout": 60, "apiRequestRatePerMinute": 256 } \ No newline at end of file diff --git a/composer.json b/composer.json index 7c978ac..1ff3b12 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "type": "library", "description": "LDC CRM Client V2", "license": "MIT", - "version": "2.0.14", + "version": "2.1.0", "authors": [ { "name": "Jérémy Munsch", diff --git a/src/CrmApiV2.php b/src/CrmApiV2.php index a46352f..acde0a5 100644 --- a/src/CrmApiV2.php +++ b/src/CrmApiV2.php @@ -71,7 +71,7 @@ class CrmApiV2 * to call its save method later on. * * @param string $identification The customer hashcode or email - * @return Customer + * @return \LDC\LDCCRM\Models\Customer|null Return null if the customer was not found * @throws \LDC\LDCCRM\Exceptions\CrmException * if an error occurs on server side or on getting response * @throws \LDC\LDCCRM\Exceptions\ApiErrorException diff --git a/src/Models/Customer.php b/src/Models/Customer.php index 0db48d4..98b916c 100644 --- a/src/Models/Customer.php +++ b/src/Models/Customer.php @@ -379,7 +379,7 @@ class Customer extends CrmClientModel * Find a customer using its hashcode in the crm * * @param string $hashcode - * @return self|null + * @return self|null Return null if the customer was not found * @throws \LDC\LDCCRM\Exceptions\CrmException * if an error occurs on server side or on getting response * @throws \LDC\LDCCRM\Exceptions\ApiErrorException diff --git a/src/WordPress/WPCrm.php b/src/WordPress/WPCrm.php index 55cd528..a19ab8a 100644 --- a/src/WordPress/WPCrm.php +++ b/src/WordPress/WPCrm.php @@ -77,7 +77,27 @@ class WPCrm public static function login($email, $password) { self::bootSessionIfNeeded(); - return self::loginUserWithApi($email, $password); + if ($user = self::loginUserWithApi($email, $password)) { + $_SESSION[self::$sessionKey] = json_encode($user); + } + return $user; + } + + /** + * Login user by setting user session + * + * @param string $email The customer email + * @param string $password The customer password + * + * @return \LDCCRM\WPCrmUser|null + */ + public static function forceLogin() + { + self::bootSessionIfNeeded(); + if ($user = self::getUser()) { + $_SESSION[self::$sessionKey] = json_encode($user); + } + return $user; } /** @@ -98,7 +118,7 @@ class WPCrm * * @param string $identification The customer hashcode or email * - * @return \LDCCRM\WPCrmUser|false + * @return \LDC\LDCCRM\Wordpress\WPCrmUser|false */ public static function getUserFromApi(string $identification) { @@ -376,7 +396,6 @@ class WPCrm self::$user = new WPCrmUser($customer->hashcode, $customer->email); } self::$user->setInfo($customer); - $_SESSION[self::$sessionKey] = json_encode(self::$user); } /** @@ -454,39 +473,58 @@ class WPCrm * * @param string $hashcode The customer hashcode * - * @return object|null + * @return array|null */ - public static function getUserFromTable(string $hashcode) + public static function getUserFromTable(string $hashcode): ?array { return self::getWpDb()->get_row( - 'SELECT * FROM `ldc_wp_customers` WHERE `hashcode` = "' . $hashcode . '"' + self::getWpDb()->prepare( + 'SELECT * FROM `ldc_wp_customers` WHERE `hashcode` = %s', + [ + 'hashcode' => $hashcode + ] + ), + 'ARRAY_A' ); } /** * Get a customer from table by an email * - * @param int $email The customer email + * @param string $email The customer email * - * @return object|null + * @return array|null */ - public static function getUserFromTableByEmail($email) + public static function getUserFromTableByEmail(string $email): ?array { return self::getWpDb()->get_row( - 'SELECT * FROM `ldc_wp_customers` WHERE `email` = "' . $email . '"' + self::getWpDb()->prepare( + 'SELECT * FROM `ldc_wp_customers` WHERE `email` = %s', + [ + 'email' => $email + ] + ), + 'ARRAY_A' ); } /** - * Get a customer from table + * Get a customer from table by an email * - * @return object|null + * @param string $uuid The customer transaction uuid + * + * @return array|null */ - public static function getUserToTable() + public static function getUserFromTableByUuid(string $uuid): ?array { return self::getWpDb()->get_row( - 'SELECT * FROM `ldc_wp_customers` WHERE `hashcode` = "' . - self::$user->hashcode . '"' + self::getWpDb()->prepare( + 'SELECT * FROM `ldc_wp_customers` WHERE `uuid` = %s', + [ + 'uuid' => $uuid + ] + ), + 'ARRAY_A' ); } @@ -497,11 +535,12 @@ class WPCrm */ private static function saveOrUpdateUserToTable() { - if (self::getUserToTable(self::$user->hashcode)) { + if (self::getUserFromTable(self::$user->hashcode)) { self::query( - 'UPDATE `ldc_wp_customers` SET `email` = %s WHERE `hashcode` = %s', + 'UPDATE `ldc_wp_customers` SET `email` = %s, `uuid` = %s WHERE `hashcode` = %s', [ 'email' => self::$user->email, + 'uuid' => self::$user->uuidToken, 'hashcode' => self::$user->hashcode ] ); @@ -511,7 +550,7 @@ class WPCrm [ 'hashcode' => self::$user->hashcode, 'email' => self::$user->email, - 'uuid' => '' + 'uuid' => self::$user->uuidToken ] ); } diff --git a/src/WordPress/WPCrmUser.php b/src/WordPress/WPCrmUser.php index 15c471f..dd62be8 100644 --- a/src/WordPress/WPCrmUser.php +++ b/src/WordPress/WPCrmUser.php @@ -18,6 +18,46 @@ use LDC\LDCCRM\Models\Customer; /** * Class de gestion des users en session avec Wordpress (ce truc immonde) * + * @property string $uuidToken — A local transaction token for this user + * + * + * @property string $hashcode — The crm hashcode this is a readonly field + * + * @property string $email — Must be a rfc valid email on a valid domain name + * + * @property string $password + * Required only on create, + * must have upper and lowers chars, + * at least 1 number, + * at leat 1 upper char, + * at least 1 lower char and + * at leat 1 special char + * + * @property string $firstname — The account first name + * + * @property string $lastname — The account last name + * + * @property string $birth_date — The account birth date 'yyyy-mm-dd' + * + * @property string $phone — The account phone + * + * @property string $address + * The account address (street number, name, build, flat number) + * + * @property string $address2 — The account phone (address complement) + * + * @property string $city — The account city name + * + * @property string $postcode — The account city postcode + * + * @property \LDC\LDCCRM\Enums\Country $country — The account country residence + * + * @property bool $opt_in — true if the account is opt in + * + * @property bool $status — true if the account is activated + * + * @property string $last_connection the last time user was connected 'yyyy-mm-dd HH:ii:ss' + * * @category API * @package LDCCrmClient * @subpackage Wordpress @@ -49,6 +89,13 @@ class WPCrmUser implements JsonSerializable */ protected $info = null; + /** + * The CRM customer temp uuidToken to validate transactions + * + * @var string UUID + */ + protected $uuidToken = null; + /** * WPCrmUser construct * @@ -70,6 +117,9 @@ class WPCrmUser implements JsonSerializable */ public function __get($prop) { + if ($prop === 'uuidToken') { + return $this->uuidToken; + } if (is_null($this->info)) { if (!is_null($this->pemail)) { WPCrm::getUserFromApi($this->pemail); @@ -89,6 +139,9 @@ class WPCrmUser implements JsonSerializable */ public function __set($prop, $value) { + if ($prop === 'uuidToken') { + return $this->uuidToken = $value; + } if (is_null($this->info)) { if (!is_null($this->pemail)) { WPCrm::getUserFromApi($this->pemail);