From dff52ffea4f480a2f048ac7ef017487571be333c Mon Sep 17 00:00:00 2001 From: Hossein Azizabadi Farahani Date: Mon, 16 Dec 2024 10:12:59 +0330 Subject: [PATCH] Update account service --- src/Service/AccountService.php | 1716 +++++++++++++++++--------------- 1 file changed, 908 insertions(+), 808 deletions(-) diff --git a/src/Service/AccountService.php b/src/Service/AccountService.php index 5899649..5996048 100644 --- a/src/Service/AccountService.php +++ b/src/Service/AccountService.php @@ -99,19 +99,6 @@ class AccountService implements ServiceInterface /* @var array */ protected array $informationFields = [ - //'user_id', - //'name', - //'email', - //'identity', - //'mobile', - //'status', - //'multi_factor_status', - //'multi_factor_secret', - //'first_name', - //'last_name', - //'birthdate', - //'gender', - //'avatar', 'avatar_params', 'ip_register', 'register_source', @@ -239,47 +226,73 @@ public function login($params): array } /** - * @param $account + * @param $params * * @return array */ - public function canonizeAccount($account): array + public function loginOauth($params): array { - if (empty($account)) { - return []; - } + // Set login column + $this->identityColumn = 'email'; - if (is_object($account)) { - $account = [ - 'id' => (int)$account->getId(), - 'name' => $account->getName(), - 'identity' => $account->getIdentity(), - 'email' => $account->getEmail(), - 'mobile' => $account->getMobile(), - 'status' => (int)$account->getStatus(), - 'time_created' => $account->getTimeCreated(), - 'multi_factor_status' => (int)$account->getMultiFactorStatus(), - ]; + // Do log in + $authAdapter = $this->accountRepository->authenticationOauth($params); + + // Check login + if ($authAdapter->isValid()) { + // Get a user account + $account = $authAdapter->getIdentity(); + + // Canonize account + $account = $this->canonizeAccount($account); + + // Complete login + $result = $this->postLoginSuccess($account, $params); } else { - $account = [ - 'id' => (int)$account['id'], - 'name' => $account['name'] ?? '', - 'email' => $account['email'] ?? '', - 'identity' => $account['identity'] ?? '', - 'mobile' => $account['mobile'] ?? '', - 'status' => (int)$account['status'], - 'time_created' => $account['time_created'] ?? '', - 'multi_factor_status' => (int)$account['multi_factor_status'], - ]; + if (isset($this->config['oauth']['oauth_register']) && (int)$this->config['oauth']['oauth_register'] === 1) { + $this->addAccount($params); + $result = $this->loginOauth($params); + } else { + $result = $this->postLoginError($params); + } } - // Set time - $account['time_created_view'] = ' - '; - if (!empty($account['time_created']) && is_numeric($account['time_created'])) { - $account['time_created_view'] = $this->utilityService->date($account['time_created']); + return $result; + } + + /** + * @param $params + * + * @return array + */ + public function loginOauth2($params): array + { + // Set login column + $this->identityColumn = 'identity'; + + // Do log in + $authAdapter = $this->accountRepository->authenticationOauth2($params); + + // Check login + if ($authAdapter->isValid()) { + // Get a user account + $account = $authAdapter->getIdentity(); + + // Canonize account + $account = $this->canonizeAccount($account); + + // Complete login + $result = $this->postLoginSuccess($account, $params); + } else { + if (isset($this->config['oauth']['oauth_register']) && (int)$this->config['oauth']['oauth_register'] === 1) { + $this->addAccount($params); + $result = $this->loginOauth2($params); + } else { + $result = $this->postLoginError($params); + } } - return $account; + return $result; } /** @@ -439,75 +452,6 @@ public function postLoginSuccess($account, $params): array ]; } - /** - * @param $params - * - * @return array - */ - public function getProfile($params): array - { - $profile = $this->accountRepository->getProfile($params); - return $this->canonizeProfile($profile); - } - - /** - * @param $profile - * - * @return array - */ - public function canonizeProfile($profile): array - { - if (empty($profile)) { - return []; - } - - if (is_object($profile)) { - $profile = [ - 'user_id' => (int)$profile->getUserId(), - 'first_name' => $profile->getFirstName(), - 'last_name' => $profile->getLastName(), - 'birthdate' => $profile->getBirthdate(), - 'gender' => $profile->getGender(), - 'avatar' => $profile->getAvatar(), - 'information' => $profile->getInformation(), - ]; - } else { - $profile = [ - 'user_id' => (int)$profile['user_id'], - 'first_name' => $profile['first_name'], - 'last_name' => $profile['last_name'], - 'birthdate' => $profile['birthdate'], - 'gender' => $profile['gender'], - 'avatar' => $profile['avatar'], - 'information' => $profile['information'], - ]; - } - - // Set information - $profile['information'] = !empty($profile['information']) ? json_decode($profile['information'], true) : []; - - // Set avatar - $profile = $this->avatarService->createUri($profile); - - return $profile; - } - - /** - * @param $userId - * - * @return bool - */ - public function hasPassword($userId): bool - { - $hash = $this->accountRepository->getAccountPassword((int)$userId); - - if (empty($hash)) { - return false; - } - - return true; - } - /** * @param $params * @@ -568,74 +512,213 @@ public function postLoginError($params): array * * @return array */ - public function getAccount($params): array + public function perMobileLogin($params): array { - $account = $this->accountRepository->getAccount($params); - return $this->canonizeAccount($account); - } + // Set new password as OTP + $otpCode = Rand::getInteger(100000, 999999); + $otpExpire = (time() + 120); + $isNew = 0; - /** - * @param $params - * - * @return array - */ - public function loginOauth($params): array - { - // Set login column - $this->identityColumn = 'email'; + // Check account exist + $account = $this->getAccount(['mobile' => $params['mobile']]); - // Do log in - $authAdapter = $this->accountRepository->authenticationOauth($params); + // Create account if not exist + // Update OTP password if account exist + if (empty($account)) { + $account = $this->addAccount( + [ + 'mobile' => $params['mobile'], + 'first_name' => $params['first_name'] ?? null, + 'last_name' => $params['last_name'] ?? null, + 'source' => $params['source'] ?? null, + 'otp' => $otpCode, + ] + ); - // Check login - if ($authAdapter->isValid()) { - // Get a user account - $account = $authAdapter->getIdentity(); + // Set is new + $isNew = 1; + } else { + $otp = $this->generatePassword($otpCode); + $this->accountRepository->updateAccount((int)$account['id'], ['otp' => $otp]); + } - // Canonize account - $account = $this->canonizeAccount($account); + // Set otp data + $otp = [ + 'code' => $otpCode, + 'time_expire' => $otpExpire, + ]; - // Complete login - $result = $this->postLoginSuccess($account, $params); - } else { - if (isset($this->config['oauth']['oauth_register']) && (int)$this->config['oauth']['oauth_register'] === 1) { - $this->addAccount($params); - $result = $this->loginOauth($params); - } else { - $result = $this->postLoginError($params); - } + // Add or update user data to cache + $this->manageUserCache($account, [], [], [], $otp); + + // Set sms message + $message = 'Code: %s + لغو11'; + if ( + isset($params['source']) + && !empty($params['source']) + && isset($this->config['otp_sms']) + && in_array($params['source'], array_keys($this->config['otp_sms'])) + ) { + $message = $this->config['otp_sms'][$params['source']]; } - return $result; + // Send notification + $this->notificationService->send( + [ + 'sms' => [ + 'message' => sprintf($message, $otpCode), + 'mobile' => $account['mobile'], + 'source' => $params['source'] ?? '', + ], + ] + ); + + // Set result + return [ + 'result' => true, + 'data' => [ + 'message' => 'Verify code send to your mobile number !', + 'name' => $account['name'], + 'mobile' => $account['mobile'], + 'is_new' => $isNew, + 'otp_expire' => $otpExpire, + ], + 'error' => [], + ]; } /** * @param $params - * @param array $operator * * @return array */ - public function addAccount($params, array $operator = []): array + public function preMailLogin($params): array { - // Set name - if ( - isset($params['first_name']) - && !empty($params['first_name']) - && isset($params['last_name']) - && !empty($params['last_name']) - ) { - $params['name'] = sprintf('%s %s', $params['first_name'], $params['last_name']); + // Set new password as OTP + $otpCode = Rand::getInteger(100000, 999999); + $otpExpire = (time() + 180); + $isNew = 0; + + // Check account exist + $account = $this->getAccount(['email' => $params['email']]); + + // Create account if not exist + // Update OTP password if account exist + if (empty($account)) { + $account = $this->addAccount( + [ + 'email' => $params['email'], + 'source' => $params['source'] ?? null, + 'first_name' => $params['first_name'] ?? null, + 'last_name' => $params['last_name'] ?? null, + 'otp' => $otpCode, + ] + ); + + // Set is new + $isNew = 1; + } else { + $otp = $this->generatePassword($otpCode); + $this->accountRepository->updateAccount((int)$account['id'], ['otp' => $otp]); } - $otp = null; + // Set otp data + $otp = [ + 'code' => $otpCode, + 'time_expire' => $otpExpire, + ]; + + // Add or update user data to cache + $this->manageUserCache($account, [], [], [], $otp); + + // Send notification + $this->notificationService->send( + [ + 'email' => [ + 'to' => [ + 'email' => $account['email'], + 'name' => $account['name'], + ], + 'subject' => $this->config['otp_email']['subject'], + 'body' => sprintf($this->config['otp_email']['body'], $otpCode), + ], + ] + ); + + // Set result + return [ + 'result' => true, + 'data' => [ + 'message' => 'Verify code send to your email !', + 'name' => $account['name'], + 'email' => $account['email'], + 'is_new' => $isNew, + 'otp_expire' => $otpExpire, + ], + 'error' => [], + ]; + } + + /** + * @param $params + * + * @return array + */ + public function logout($params): array + { + // Set message + $message = 'You are logout successfully from this session !'; + + // Get and check user + $user = $this->cacheService->getUser($params['user_id']); + if (!empty($user)) { + // Save log + $this->historyService->logger('logout', ['request' => $params, 'account' => $user['account']]); + + // Check and clean user cache for logout + if (isset($params['all_session']) && (int)$params['all_session'] === 1) { + $this->cacheService->deleteUserItem($params['user_id'], 'all_keys', ''); + $message = 'You are logout successfully from all of your sessions !'; + } else { + $this->cacheService->deleteUserItem($params['user_id'], 'access_keys', $params['token_id']); + $this->cacheService->deleteUserItem($params['user_id'], 'multi_factor', $params['token_id']); + } + } + + return [ + 'result' => true, + 'data' => [ + 'message' => $message, + ], + 'error' => [], + ]; + } + + /** + * @param $params + * @param array $operator + * + * @return array + */ + public function addAccount($params, array $operator = []): array + { + // Set account name + $params['name'] = $this->setAccountName($params); + + // Set credential $credential = null; if (isset($params['credential']) && !empty($params['credential'])) { $credential = $this->generatePassword($params['credential']); } + + // Set otp + $otp = null; if (isset($params['otp']) && !empty($params['otp'])) { $otp = $this->generatePassword($params['otp']); } + // Set add account params $paramsAccount = [ 'name' => $params['name'] ?? null, 'email' => $params['email'] ?? null, @@ -647,17 +730,12 @@ public function addAccount($params, array $operator = []): array 'time_created' => time(), ]; + // add account $account = $this->accountRepository->addAccount($paramsAccount); $account = $this->canonizeAccount($account); - // Save log - $this->historyService->logger('register', ['request' => $params, 'account' => $account, 'operator' => $operator]); - - - // Clean up - $profileParams = [ - 'user_id' => (int)$account['id'], - ]; + // Clean up information data + $profileParams = ['user_id' => (int)$account['id']]; $informationParams = []; foreach ($params as $key => $value) { if (in_array($key, $this->profileFields)) { @@ -677,16 +755,40 @@ public function addAccount($params, array $operator = []): array } } + // Set information $profileParams['information'] = json_encode( $informationParams, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK ); + // add profile $profile = $this->accountRepository->addProfile($profileParams); $profile = $this->canonizeProfile($profile); + // merge account and profile data $account = array_merge($account, $profile); + // Save log + $this->historyService->logger('register', ['request' => $params, 'account' => $account, 'operator' => $operator]); + + // Send notification + $this->notificationService->send( + [ + 'information' => [ + 'sender_id' => (int)$operator['id'], + 'receiver_id' => (int)$params['user_id'], + 'type' => 'info', + 'title' => $this->translatorService->translate('add-account'), + 'body' => $this->translatorService->translate('add-account-message'), + 'source' => [ + 'module' => 'user', + 'section' => 'account', + 'item' => (int)$params['user_id'], + ], + ], + ] + ); + // Set user roles $this->roleService->addDefaultRoles($account, $operator); @@ -702,44 +804,25 @@ public function addAccount($params, array $operator = []): array } /** - * @param mixed $password + * @param $params * - * @return string + * @return array */ - protected function generatePassword(mixed $password): string + public function getAccount($params): array { - switch ($this->hashPattern) { - default: - case'bcrypt': - $hash = password_hash($password, PASSWORD_BCRYPT); - break; - - case'argon2id': - // Set option for a High-Security ARGON2ID - $options = [ - 'memory_cost' => 1 << 17, // 131072 KB (128 MB) - 'time_cost' => 4, // 4 iterations (same as default) - 'threads' => 2, // 2 parallel threads - ]; - - // Make a High-Security hash password - $hash = password_hash($password, PASSWORD_ARGON2ID /*, $options*/); - break; - - case'sha512': - $hash = hash('sha512', $password); - break; - } - - return $hash; + $account = $this->accountRepository->getAccount($params); + return $this->canonizeAccount($account); } /** - * @return int + * @param $params + * + * @return array */ - public function userRegisterStatus(): int + public function getProfile($params): array { - return $this->config['register']['status'] ?? 1; + $profile = $this->accountRepository->getProfile($params); + return $this->canonizeProfile($profile); } /** @@ -747,34 +830,9 @@ public function userRegisterStatus(): int * * @return array */ - public function loginOauth2($params): array + public function getAccountProfile($params): array { - // Set login column - $this->identityColumn = 'identity'; - - // Do log in - $authAdapter = $this->accountRepository->authenticationOauth2($params); - - // Check login - if ($authAdapter->isValid()) { - // Get a user account - $account = $authAdapter->getIdentity(); - - // Canonize account - $account = $this->canonizeAccount($account); - - // Complete login - $result = $this->postLoginSuccess($account, $params); - } else { - if (isset($this->config['oauth']['oauth_register']) && (int)$this->config['oauth']['oauth_register'] === 1) { - $this->addAccount($params); - $result = $this->loginOauth2($params); - } else { - $result = $this->postLoginError($params); - } - } - - return $result; + return $this->canonizeAccountProfile($this->accountRepository->getAccountProfile($params)); } /** @@ -782,80 +840,93 @@ public function loginOauth2($params): array * * @return array */ - public function perMobileLogin($params): array + public function getAccountList($params): array { - // Set new password as OTP - $otpCode = Rand::getInteger(100000, 999999); - $otpExpire = (time() + 120); - $isNew = 0; + $limit = $params['limit'] ?? 10; + $page = $params['page'] ?? 1; + $order = $params['order'] ?? ['time_created DESC', 'id DESC']; + $offset = ((int)$page - 1) * (int)$limit; - // Check account exist - $account = $this->getAccount(['mobile' => $params['mobile']]); + // Set params + $listParams = [ + 'page' => (int)$page, + 'limit' => (int)$limit, + 'order' => $order, + 'offset' => $offset, + ]; - // Create account if not exist - // Update OTP password if account exist - if (empty($account)) { - $account = $this->addAccount( - [ - 'mobile' => $params['mobile'], - 'first_name' => $params['first_name'] ?? null, - 'last_name' => $params['last_name'] ?? null, - 'source' => $params['source'] ?? null, - 'otp' => $otpCode, - ] + if (isset($params['name']) && !empty($params['name'])) { + $listParams['name'] = $params['name']; + } + if (isset($params['identity']) && !empty($params['identity'])) { + $listParams['identity'] = $params['identity']; + } + if (isset($params['email']) && !empty($params['email'])) { + $listParams['email'] = $params['email']; + } + if (isset($params['mobile']) && !empty($params['mobile'])) { + $listParams['mobile'] = $params['mobile']; + } + if (isset($params['mobiles']) && !empty($params['mobiles'])) { + $listParams['mobiles'] = $params['mobiles']; + } + if (isset($params['status']) && in_array($params['status'], [0, 1])) { + $listParams['status'] = $params['status']; + } + if (isset($params['status'])) { + $listParams['status'] = $params['status']; + } + if (isset($params['data_from']) && !empty($params['data_from'])) { + $listParams['data_from'] = strtotime( + ($params['data_from']) != null + ? sprintf('%s 00:00:00', $params['data_from']) + : sprintf('%s 00:00:00', date('Y-m-d', strtotime('-1 month'))) ); + } + if (isset($params['data_to']) && !empty($params['data_to'])) { + $listParams['data_to'] = strtotime( + ($params['data_to']) != null + ? sprintf('%s 00:00:00', $params['data_to']) + : sprintf('%s 23:59:59', date('Y-m-d')) + ); + } - // Set is new - $isNew = 1; - } else { - $otp = $this->generatePassword($otpCode); - $this->accountRepository->updateAccount((int)$account['id'], ['otp' => $otp]); + $filters = $this->prepareFilter($params); + if (!empty($filters)) { + foreach ($filters as $filter) { + $itemIdList = []; + $rowSet = $this->accountRepository->getIdFromFilter($filter); + foreach ($rowSet as $row) { + $itemIdList[] = $this->canonizeAccountId($row); + } + $listParams['id'] = $itemIdList; + } } - // Set otp data - $otp = [ - 'code' => $otpCode, - 'time_expire' => $otpExpire, - ]; - - // Add or update user data to cache - $this->manageUserCache($account, [], [], [], $otp); - - // Set sms message - $message = 'Code: %s - لغو11'; - if ( - isset($params['source']) - && !empty($params['source']) - && isset($this->config['otp_sms']) - && in_array($params['source'], array_keys($this->config['otp_sms'])) - ) { - $message = $this->config['otp_sms'][$params['source']]; + // Get list + $list = []; + $rowSet = $this->accountRepository->getAccountList($listParams); + foreach ($rowSet as $row) { + $list[$row->getId()] = $this->canonizeAccount($row); } - // Set notification params - $notificationParams = [ - 'sms' => [ - 'message' => sprintf($message, $otpCode), - 'mobile' => $account['mobile'], - 'source' => $params['source'] ?? '', - ], - ]; + // Get count + $count = $this->accountRepository->getAccountCount($listParams); - // Send notification - $this->notificationService->send($notificationParams); + // Get roles + $roleList = $this->roleService->getRoleAccountList(array_keys($list)); + foreach ($list as $id => $user) { + $list[$id]['roles'] = isset($roleList[$user['id']]) ? $roleList[$user['id']] : $this->emptyRoles; + } - // Set result return [ - 'result' => true, - 'data' => [ - 'message' => 'Verify code send to your mobile number !', - 'name' => $account['name'], - 'mobile' => $account['mobile'], - 'is_new' => $isNew, - 'otp_expire' => $otpExpire, + 'list' => array_values($list), + 'roles' => $roleList, + 'paginator' => [ + 'count' => $count, + 'limit' => $limit, + 'page' => $page, ], - 'error' => [], ]; } @@ -864,74 +935,130 @@ public function perMobileLogin($params): array * * @return array */ - public function preMailLogin($params): array + public function getAccountProfileList($params): array { - // Set new password as OTP - $otpCode = Rand::getInteger(100000, 999999); - $otpExpire = (time() + 180); - $isNew = 0; + $limit = $params['limit'] ?? 10; + $page = $params['page'] ?? 1; + $order = $params['order'] ?? ['time_created DESC', 'id DESC']; + $offset = ((int)$page - 1) * (int)$limit; - // Check account exist - $account = $this->getAccount(['email' => $params['email']]); + // Set params + $listParams = [ + 'page' => (int)$page, + 'limit' => (int)$limit, + 'order' => $order, + 'offset' => $offset, + ]; - // Create account if not exist - // Update OTP password if account exist - if (empty($account)) { - $account = $this->addAccount( - [ - 'email' => $params['email'], - 'source' => $params['source'] ?? null, - 'first_name' => $params['first_name'] ?? null, - 'last_name' => $params['last_name'] ?? null, - 'otp' => $otpCode, - ] + if (isset($params['name']) && !empty($params['name'])) { + $listParams['name'] = $params['name']; + } + if (isset($params['identity']) && !empty($params['identity'])) { + $listParams['identity'] = $params['identity']; + } + if (isset($params['email']) && !empty($params['email'])) { + $listParams['email'] = $params['email']; + } + if (isset($params['mobile']) && !empty($params['mobile'])) { + $listParams['mobile'] = $params['mobile']; + } + if (isset($params['mobiles']) && !empty($params['mobiles'])) { + $listParams['mobiles'] = $params['mobiles']; + } + if (isset($params['id']) && !empty($params['id'])) { + $listParams['id'] = $params['id']; + } + if (isset($params['status']) && in_array($params['status'], [0, 1])) { + $listParams['status'] = $params['status']; + } + if (isset($params['data_from']) && !empty($params['data_from'])) { + $listParams['data_from'] = strtotime( + ($params['data_from']) != null + ? sprintf('%s 00:00:00', $params['data_from']) + : sprintf('%s 00:00:00', date('Y-m-d', strtotime('-1 month'))) + ); + } + if (isset($params['data_to']) && !empty($params['data_to'])) { + $listParams['data_to'] = strtotime( + ($params['data_to']) != null + ? sprintf('%s 00:00:00', $params['data_to']) + : sprintf('%s 23:59:59', date('Y-m-d')) ); + } - // Set is new - $isNew = 1; - } else { - $otp = $this->generatePassword($otpCode); - $this->accountRepository->updateAccount((int)$account['id'], ['otp' => $otp]); + $filters = $this->prepareFilter($params); + if (!empty($filters)) { + foreach ($filters as $filter) { + $itemIdList = []; + $rowSet = $this->accountRepository->getIdFromFilter($filter); + foreach ($rowSet as $row) { + $itemIdList[] = $this->canonizeAccountId($row); + } + $listParams['id'] = $itemIdList; + } } - // Set otp data - $otp = [ - 'code' => $otpCode, - 'time_expire' => $otpExpire, - ]; + // Get list + $list = []; + $rowSet = $this->accountRepository->getAccountProfileList($listParams); + foreach ($rowSet as $row) { + $list[$row->getId()] = $this->canonizeAccountProfile($row); + } - // Add or update user data to cache - $this->manageUserCache($account, [], [], [], $otp); + // Get count + $count = $this->accountRepository->getAccountCount($listParams); - // Set notification params - $notificationParams = [ - 'email' => [ - 'to' => [ - 'email' => $account['email'], - 'name' => $account['name'], - ], - 'subject' => $this->config['otp_email']['subject'], - 'body' => sprintf($this->config['otp_email']['body'], $otpCode), + // Get roles + $roleList = $this->roleService->getRoleAccountList(array_keys($list)); + foreach ($list as $id => $user) { + $list[$id]['roles'] = isset($roleList[$user['id']]) ? $roleList[$user['id']] : $this->emptyRoles; + } + + return [ + 'list' => array_values($list), + 'paginator' => [ + 'count' => $count, + 'limit' => $limit, + 'page' => $page, ], ]; + } - // Send notification - $this->notificationService->send($notificationParams); + /** + * @param $id + * + * @return array + */ + public function getUserFromCache($id): array + { + $user = $this->cacheService->getUser($id); - // Set result return [ - 'result' => true, - 'data' => [ - 'message' => 'Verify code send to your email !', - 'name' => $account['name'], - 'email' => $account['email'], - 'is_new' => $isNew, - 'otp_expire' => $otpExpire, - ], - 'error' => [], + 'account' => $user['account'], + 'roles' => $user['roles'], ]; } + /** + * @param $id + * + * @return array + */ + public function getUserFromCacheFull($id): array + { + return $this->cacheService->getUser($id); + } + + /** + * @param array $params + * + * @return int + */ + public function getAccountCount(array $params = []): int + { + return $this->accountRepository->getAccountCount($params); + } + /** * @param $params * @param $account @@ -941,17 +1068,10 @@ public function preMailLogin($params): array */ public function updateAccount($params, $account, array $operator = []): array { - // Set name - if ( - isset($params['first_name']) - && !empty($params['first_name']) - && isset($params['last_name']) - && !empty($params['last_name']) - ) { - $params['name'] = sprintf('%s %s', $params['first_name'], $params['last_name']); - } + // Set account name + $params['name'] = $this->setAccountName($params); - // Clean up + // Clean up information data $accountParams = []; $profileParams = []; $informationParams = []; @@ -979,13 +1099,7 @@ public function updateAccount($params, $account, array $operator = []): array } } - //if (isset($params['device_type'])) { - // $informationParams['device_type'] = $params['device_type']; - //} - //if (isset($params['device_token'])) { - // $informationParams['device_token'] = $params['device_token']; - //} - + // Check information and set data if (!empty($informationParams)) { $profile = $this->getProfile(['user_id' => (int)$account['id']]); foreach ($profile['information'] as $key => $value) { @@ -996,23 +1110,19 @@ public function updateAccount($params, $account, array $operator = []): array } } - //if (isset($params['device_type'])) { - // $informationParams['device_type'] = $params['device_type']; - //} - //if (isset($params['device_token'])) { - // $informationParams['device_token'] = $params['device_token']; - //} - + // Set information $profileParams['information'] = json_encode( $informationParams, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK ); } + // Update account if data set if (!empty($accountParams)) { $this->accountRepository->updateAccount((int)$account['id'], $accountParams); } + // Update profile if data set if (!empty($profileParams)) { $this->accountRepository->updateProfile((int)$account['id'], $profileParams); } @@ -1029,92 +1139,52 @@ public function updateAccount($params, $account, array $operator = []): array $account['is_company_setup'] = false; $account['company_id'] = $user['authorization']['company_id'] ?? 0; $account['company_title'] = $user['authorization']['company']['title'] ?? ''; - if ($this->hasCompanyService()) { - $company = $this->companyService->getCompanyDetails((int)$account['id']); - if (!empty($company)) { - $account['company_id'] = $company['company_id']; - $account['company_title'] = $company['company_title']; - $account['is_company_setup'] = true; - } - } - - // Check company setup - if (!$account['is_company_setup'] && isset($this->config['login']['get_company']) && (int)$this->config['login']['get_company'] === 1) { - $isCompanySetup = false; - if (isset($user['authorization']['company']['is_company_setup'])) { - $isCompanySetup = $user['authorization']['company']['is_company_setup']; - } elseif (isset($user['account']['is_company_setup'])) { - $isCompanySetup = $user['account']['is_company_setup']; - } - - $account['is_company_setup'] = $isCompanySetup; - } - - // Add or update user data to cache - $this->manageUserCache($account); - - // Save log - $this->historyService->logger('update', ['request' => $params, 'account' => $account, 'operator' => $operator]); - - return $account; - } - - /** - * @param $params - * @param $account - * @param array $operator - * - * @return array - */ - public function addRoleAccountByAdmin($params, $account, array $operator = []): void - { - // Set user roles that receive from service - if (isset($params['roles'])) { - //$roles = explode(',', $params['roles']); - foreach ($params['roles'] as $role) { - if ($role != 'member') { - $this->roleService->addRoleAccount($account, $role, $role == 'admin' ? 'admin' : 'api', $operator); - } + if ($this->hasCompanyService()) { + $company = $this->companyService->getCompanyDetails((int)$account['id']); + if (!empty($company)) { + $account['company_id'] = $company['company_id']; + $account['company_title'] = $company['company_title']; + $account['is_company_setup'] = true; } + } - // Make user logout after edit role - $this->logout(['user_id' => (int)$account['id'], 'all_session' => 1]); + // Check company setup + if (!$account['is_company_setup'] && isset($this->config['login']['get_company']) && (int)$this->config['login']['get_company'] === 1) { + $isCompanySetup = false; + if (isset($user['authorization']['company']['is_company_setup'])) { + $isCompanySetup = $user['authorization']['company']['is_company_setup']; + } elseif (isset($user['account']['is_company_setup'])) { + $isCompanySetup = $user['account']['is_company_setup']; + } + + $account['is_company_setup'] = $isCompanySetup; } - } - /** - * @param $params - * - * @return array - */ - public function logout($params): array - { - // Set message - $message = 'You are logout successfully from this session !'; + // Add or update user data to cache + $this->manageUserCache($account); - // Get and check user - $user = $this->cacheService->getUser($params['user_id']); - if (!empty($user)) { - // Save log - $this->historyService->logger('logout', ['request' => $params, 'account' => $user['account']]); + // Save log + $this->historyService->logger('update', ['request' => $params, 'account' => $account, 'operator' => $operator]); - // Check and clean user cache for logout - if (isset($params['all_session']) && (int)$params['all_session'] === 1) { - $this->cacheService->deleteUserItem($params['user_id'], 'all_keys', ''); - $message = 'You are logout successfully from all of your sessions !'; - } else { - $this->cacheService->deleteUserItem($params['user_id'], 'access_keys', $params['token_id']); - $this->cacheService->deleteUserItem($params['user_id'], 'multi_factor', $params['token_id']); - } - } + // Send notification + $this->notificationService->send( + [ + 'information' => [ + 'sender_id' => (int)$operator['id'], + 'receiver_id' => (int)$params['user_id'], + 'type' => 'info', + 'title' => $this->translatorService->translate('update-account'), + 'body' => $this->translatorService->translate('update-account-message'), + 'source' => [ + 'module' => 'user', + 'section' => 'account', + 'item' => (int)$params['user_id'], + ], + ], + ] + ); - return [ - 'result' => true, - 'data' => [ - 'message' => $message, - ], - 'error' => [], - ]; + return $account; } /** @@ -1241,31 +1311,6 @@ public function addOrUpdateAccount($params): array return $account; } - /** - * @param $id - * - * @return array - */ - public function getUserFromCache($id): array - { - $user = $this->cacheService->getUser($id); - - return [ - 'account' => $user['account'], - 'roles' => $user['roles'], - ]; - } - - /** - * @param $id - * - * @return array - */ - public function getUserFromCacheFull($id): array - { - return $this->cacheService->getUser($id); - } - /** * @param $account * @@ -1351,128 +1396,133 @@ public function viewAccount($account): array } /** - * @param $roles - * @param $account - * @param string $section - * @param array $operator + * @param $userId * - * @return void + * @return bool */ - public function updateAccountRoles($roles, $account, string $section = 'api', array $operator = []): void + public function hasPassword($userId): bool { - $this->roleService->updateAccountRoles($roles, $account, $section, $operator); + $hash = $this->accountRepository->getAccountPassword((int)$userId); - // Make user logout after edit role - $this->logout(['user_id' => (int)$account['id'], 'all_session' => 1]); + if (empty($hash)) { + return false; + } + + return true; } /** - * @param $params - * @param $account + * @param mixed $password * - * @return void + * @return string */ - public function updatedDeviceToken($params, $account): void + private function generatePassword(mixed $password): string { - // Update cache - $this->cacheService->setUserItem($account['id'], 'device_tokens', $params['device_token']); + switch ($this->hashPattern) { + default: + case'bcrypt': + $hash = password_hash($password, PASSWORD_BCRYPT); + break; - // Save log - $this->historyService->logger('updatedDeviceToken', ['request' => $params, 'account' => $account]); + case'argon2id': + // ToDo: Fix it by minimum php 8.3 + // Set option for a High-Security ARGON2ID + $options = [ + 'memory_cost' => 1 << 17, // 131072 KB (128 MB) + 'time_cost' => 4, // 4 iterations (same as default) + 'threads' => 2, // 2 parallel threads + ]; + + // Make a High-Security hash password + $hash = password_hash($password, PASSWORD_ARGON2ID /*, $options*/); + break; + + case'sha512': + $hash = hash('sha512', $password); + break; + } + + return $hash; + } + + /** + * @return int + */ + public function userRegisterStatus(): int + { + return $this->config['register']['status'] ?? 1; } /** * @param $params + * @param $account + * @param array $operator * - * @return array + * @return void */ - public function getAccountProfileList($params): array + public function addRoleAccountByAdmin($params, $account, array $operator = []): void { - $limit = $params['limit'] ?? 10; - $page = $params['page'] ?? 1; - $order = $params['order'] ?? ['time_created DESC', 'id DESC']; - $offset = ((int)$page - 1) * (int)$limit; + // Set user roles that receive from service + if (isset($params['roles'])) { + //$roles = explode(',', $params['roles']); + foreach ($params['roles'] as $role) { + if ($role != 'member') { + $this->roleService->addRoleAccount($account, $role, $role == 'admin' ? 'admin' : 'api', $operator); + } + } - // Set params - $listParams = [ - 'page' => (int)$page, - 'limit' => (int)$limit, - 'order' => $order, - 'offset' => $offset, - ]; + // Make user logout after edit role + $this->logout(['user_id' => (int)$account['id'], 'all_session' => 1]); - if (isset($params['name']) && !empty($params['name'])) { - $listParams['name'] = $params['name']; - } - if (isset($params['identity']) && !empty($params['identity'])) { - $listParams['identity'] = $params['identity']; - } - if (isset($params['email']) && !empty($params['email'])) { - $listParams['email'] = $params['email']; - } - if (isset($params['mobile']) && !empty($params['mobile'])) { - $listParams['mobile'] = $params['mobile']; - } - if (isset($params['mobiles']) && !empty($params['mobiles'])) { - $listParams['mobiles'] = $params['mobiles']; - } - if (isset($params['id']) && !empty($params['id'])) { - $listParams['id'] = $params['id']; - } - if (isset($params['status']) && in_array($params['status'], [0, 1])) { - $listParams['status'] = $params['status']; - } - if (isset($params['data_from']) && !empty($params['data_from'])) { - $listParams['data_from'] = strtotime( - ($params['data_from']) != null - ? sprintf('%s 00:00:00', $params['data_from']) - : sprintf('%s 00:00:00', date('Y-m-d', strtotime('-1 month'))) - ); - } - if (isset($params['data_to']) && !empty($params['data_to'])) { - $listParams['data_to'] = strtotime( - ($params['data_to']) != null - ? sprintf('%s 00:00:00', $params['data_to']) - : sprintf('%s 23:59:59', date('Y-m-d')) + // Send notification + $this->notificationService->send( + [ + 'information' => [ + 'sender_id' => (int)$operator['id'], + 'receiver_id' => (int)$params['user_id'], + 'type' => 'info', + 'title' => $this->translatorService->translate('update-roles'), + 'body' => $this->translatorService->translate('update-roles-message'), + 'source' => [ + 'module' => 'user', + 'section' => 'account', + 'item' => (int)$params['user_id'], + ], + ], + ] ); } + } - $filters = $this->prepareFilter($params); - if (!empty($filters)) { - foreach ($filters as $filter) { - $itemIdList = []; - $rowSet = $this->accountRepository->getIdFromFilter($filter); - foreach ($rowSet as $row) { - $itemIdList[] = $this->canonizeAccountId($row); - } - $listParams['id'] = $itemIdList; - } - } - - // Get list - $list = []; - $rowSet = $this->accountRepository->getAccountProfileList($listParams); - foreach ($rowSet as $row) { - $list[$row->getId()] = $this->canonizeAccountProfile($row); - } + /** + * @param $roles + * @param $account + * @param string $section + * @param array $operator + * + * @return void + */ + public function updateAccountRoles($roles, $account, string $section = 'api', array $operator = []): void + { + $this->roleService->updateAccountRoles($roles, $account, $section, $operator); - // Get count - $count = $this->accountRepository->getAccountCount($listParams); + // Make user logout after edit role + $this->logout(['user_id' => (int)$account['id'], 'all_session' => 1]); + } - // Get roles - $roleList = $this->roleService->getRoleAccountList(array_keys($list)); - foreach ($list as $id => $user) { - $list[$id]['roles'] = isset($roleList[$user['id']]) ? $roleList[$user['id']] : $this->emptyRoles; - } + /** + * @param $params + * @param $account + * + * @return void + */ + public function updatedDeviceToken($params, $account): void + { + // Update cache + $this->cacheService->setUserItem($account['id'], 'device_tokens', $params['device_token']); - return [ - 'list' => array_values($list), - 'paginator' => [ - 'count' => $count, - 'limit' => $limit, - 'page' => $page, - ], - ]; + // Save log + $this->historyService->logger('updatedDeviceToken', ['request' => $params, 'account' => $account]); } /** @@ -1498,106 +1548,6 @@ public function prepareFilter($params): array return $filters; } - /** - * @param object|array $roleAccountList - * - * @return int|null - */ - public function canonizeAccountId(object|array $roleAccountList): int|null - { - if (empty($roleAccountList)) { - return 0; - } - - if (is_object($roleAccountList)) { - $accountId = $roleAccountList->getUserId(); - } else { - $accountId = $roleAccountList['user_id']; - } - - return $accountId; - } - - /** - * @param $account - * - * @return array - */ - public function canonizeAccountProfile($account): array - { - if (empty($account)) { - return []; - } - - if (is_object($account)) { - $account = [ - 'id' => (int)$account->getId(), - 'name' => $account->getName(), - 'identity' => $account->getIdentity(), - 'email' => $account->getEmail(), - 'mobile' => $account->getMobile(), - 'status' => (int)$account->getStatus(), - 'time_created' => $account->getTimeCreated(), - 'first_name' => $account->getFirstName(), - 'last_name' => $account->getLastName(), - 'birthdate' => $account->getBirthdate(), - 'gender' => $account->getGender(), - 'avatar' => $account->getAvatar(), - 'information' => $account->getInformation(), - ]; - } else { - $account = [ - 'id' => (int)$account['id'], - 'name' => $account['name'] ?? '', - 'email' => $account['email'] ?? '', - 'identity' => $account['identity'] ?? '', - 'mobile' => $account['mobile'] ?? '', - 'status' => (int)$account['status'], - 'time_created' => $account['time_created'] ?? '', - 'first_name' => $account['first_name'] ?? '', - 'last_name' => $account['last_name'] ?? '', - 'birthdate' => $account['birthdate'] ?? '', - 'gender' => $account['gender'] ?? '', - 'avatar' => $account['avatar'] ?? '', - 'information' => $account['information'] ?? '', - ]; - } - - // Set information - $account['information'] = !empty($account['information']) ? json_decode($account['information'], true) : []; - - // Set time - $account['time_created_view'] = ' - '; - if (!empty($account['time_created']) && is_numeric($account['time_created'])) { - $account['time_created_view'] = $this->utilityService->date($account['time_created']); - } - - // Set avatar - $account = $this->avatarService->createUri($account); - - return $account; - } - - /** - * @param array $params - * - * @return int - */ - public function getAccountCount(array $params = []): int - { - return $this->accountRepository->getAccountCount($params); - } - - /** - * @param $params - * - * @return array - */ - public function getAccountProfile($params): array - { - return $this->canonizeAccountProfile($this->accountRepository->getAccountProfile($params)); - } - /** * @param $params * @param array $account @@ -1675,7 +1625,7 @@ public function updatePassword($params, $account, array $operator = []): array * * @return boolean */ - protected function passwordEqualityCheck(mixed $credential, mixed $hash): bool + private function passwordEqualityCheck(mixed $credential, mixed $hash): bool { switch ($this->hashPattern) { default: @@ -1712,6 +1662,24 @@ public function updatePasswordByAdmin($params, array $operator = []): array ['request' => $params, 'account' => $this->getAccount(['id' => (int)$params['user_id']]), 'operator' => $operator] ); + // Send notification + $this->notificationService->send( + [ + 'information' => [ + 'sender_id' => (int)$operator['id'], + 'receiver_id' => (int)$params['user_id'], + 'type' => 'info', + 'title' => $this->translatorService->translate('update-password'), + 'body' => $this->translatorService->translate('update-password-message'), + 'source' => [ + 'module' => 'user', + 'section' => 'account', + 'item' => (int)$params['user_id'], + ], + ], + ] + ); + return [ 'result' => true, 'data' => [ @@ -1752,6 +1720,24 @@ public function updateStatusByAdmin($params, array $operator = []): array ['request' => $params, 'account' => $this->getAccount(['id' => (int)$params['user_id']]), 'operator' => $operator] ); + // Send notification + $this->notificationService->send( + [ + 'information' => [ + 'sender_id' => (int)$operator['id'], + 'receiver_id' => (int)$params['user_id'], + 'type' => 'info', + 'title' => $this->translatorService->translate('update-status'), + 'body' => $this->translatorService->translate('update-status-message'), + 'source' => [ + 'module' => 'user', + 'section' => 'account', + 'item' => (int)$params['user_id'], + ], + ], + ] + ); + return [ 'result' => true, 'data' => [ @@ -1781,6 +1767,24 @@ public function deleteUserByAdmin($params, array $operator = []): array ['request' => $params, 'account' => $this->getAccount(['id' => (int)$params['user_id']]), 'operator' => $operator] ); + // Send notification + $this->notificationService->send( + [ + 'information' => [ + 'sender_id' => (int)$operator['id'], + 'receiver_id' => (int)$params['user_id'], + 'type' => 'info', + 'title' => $this->translatorService->translate('delete-account'), + 'body' => $this->translatorService->translate('delete-account-message'), + 'source' => [ + 'module' => 'user', + 'section' => 'account', + 'item' => (int)$params['user_id'], + ], + ], + ] + ); + return [ 'result' => true, 'data' => [ @@ -1844,106 +1848,77 @@ public function isDuplicated($type, $value, int $id = 0): bool /** * @param $params + * @param array $operator * - * @return array + * @return void */ - public function getAccountListByOperator($params): array + public function resetAccount($params, array $operator = []): void { - $limit = $params['limit'] ?? 10; - $page = $params['page'] ?? 1; - $order = $params['order'] ?? ['time_created DESC', 'id DESC']; - $offset = ((int)$page - 1) * (int)$limit; + switch ($params['type']) { + case 'password': + // Update account + $this->accountRepository->updateAccount((int)$params['user_id'], ['credential' => null]); - // Set params - $listParams = [ - 'page' => (int)$page, - 'limit' => (int)$limit, - 'order' => $order, - 'offset' => $offset, - ]; + // Save log + $this->historyService->logger( + 'resetPasswordByOperator', + ['request' => $params, 'account' => $this->getAccount(['id' => (int)$params['user_id']]), 'operator' => $operator] + ); + break; - if (isset($params['name']) && !empty($params['name'])) { - $listParams['name'] = $params['name']; - } - if (isset($params['identity']) && !empty($params['identity'])) { - $listParams['identity'] = $params['identity']; - } - if (isset($params['email']) && !empty($params['email'])) { - $listParams['email'] = $params['email']; - } - if (isset($params['mobile']) && !empty($params['mobile'])) { - $listParams['mobile'] = $params['mobile']; - } - if (isset($params['status']) && in_array($params['status'], [0, 1])) { - $listParams['status'] = $params['status']; - } - if (isset($params['status'])) { - $listParams['status'] = $params['status']; - } - if (isset($params['data_from']) && !empty($params['data_from'])) { - $listParams['data_from'] = strtotime( - ($params['data_from']) != null - ? sprintf('%s 00:00:00', $params['data_from']) - : sprintf('%s 00:00:00', date('Y-m-d', strtotime('-1 month'))) - ); - } - if (isset($params['data_to']) && !empty($params['data_to'])) { - $listParams['data_to'] = strtotime( - ($params['data_to']) != null - ? sprintf('%s 00:00:00', $params['data_to']) - : sprintf('%s 23:59:59', date('Y-m-d')) - ); - } + case 'mfa': + // Update account + $this->accountRepository->updateAccount((int)$params['user_id'], ['multi_factor_status' => 0, 'multi_factor_secret' => null]); - $filters = $this->prepareFilter($params); - if (!empty($filters)) { - foreach ($filters as $filter) { - $itemIdList = []; - $rowSet = $this->accountRepository->getIdFromFilter($filter); - foreach ($rowSet as $row) { - $itemIdList[] = $this->canonizeAccountId($row); - } - $listParams['id'] = $itemIdList; - } - } + // Save log + $this->historyService->logger( + 'resetMfaByOperator', + ['request' => $params, 'account' => $this->getAccount(['id' => (int)$params['user_id']]), 'operator' => $operator] + ); + break; - $notAllow = $this->prepareFilter(['roles' => implode(',', $this->roleService->getAdminRoleList())]); - if (!empty($notAllow)) { - foreach ($notAllow as $filter) { - $notAllowItemIdList = []; - $rowSet = $this->accountRepository->getIdFromFilter($filter); - foreach ($rowSet as $row) { - $notAllowItemIdList[] = $this->canonizeAccountId($row); - } - $listParams['not_allowed_id'] = $notAllowItemIdList; - } - } + case 'avatar': + // Set avatar params + $avatar = [ + 'avatar' => '', + 'avatar_params' => [], + ]; - // Get list - $list = []; - $rowSet = $this->accountRepository->getAccountList($listParams); - foreach ($rowSet as $row) { - $list[$row->getId()] = $this->canonizeAccount($row); + // Set account + $account = ['id' => (int)$params['user_id']]; + + // Update profile + $this->updateAccount($avatar, $account, $operator); + + // Save log + $this->historyService->logger( + 'resetAvatarByOperator', + ['request' => $params, 'account' => $this->getAccount(['id' => (int)$params['user_id']]), 'operator' => $operator] + ); + break; } - // Get count - $count = $this->accountRepository->getAccountCount($listParams); + // Make user logout after edit role + $this->logout(['user_id' => (int)$params['user_id'], 'all_session' => 1]); + } - // Get roles - $roleList = $this->roleService->getRoleAccountList(array_keys($list)); - foreach ($list as $id => $user) { - $list[$id]['roles'] = isset($roleList[$user['id']]) ? $roleList[$user['id']] : $this->emptyRoles; + /** + * @param $params + * + * @return string|null + */ + public function setAccountName($params): string|null + { + if ( + isset($params['first_name']) + && !empty($params['first_name']) + && isset($params['last_name']) + && !empty($params['last_name']) + ) { + return sprintf('%s %s', $params['first_name'], $params['last_name']); } - return [ - 'list' => array_values($list), - 'roles' => $roleList, - 'paginator' => [ - 'count' => $count, - 'limit' => $limit, - 'page' => $page, - ], - ]; + return $params['name'] ?? null; } /** @@ -1951,7 +1926,7 @@ public function getAccountListByOperator($params): array * * @return array */ - public function getAccountList($params): array + public function getAccountListByOperator($params): array { $limit = $params['limit'] ?? 10; $page = $params['page'] ?? 1; @@ -1978,9 +1953,6 @@ public function getAccountList($params): array if (isset($params['mobile']) && !empty($params['mobile'])) { $listParams['mobile'] = $params['mobile']; } - if (isset($params['mobiles']) && !empty($params['mobiles'])) { - $listParams['mobiles'] = $params['mobiles']; - } if (isset($params['status']) && in_array($params['status'], [0, 1])) { $listParams['status'] = $params['status']; } @@ -2014,6 +1986,18 @@ public function getAccountList($params): array } } + $notAllow = $this->prepareFilter(['roles' => implode(',', $this->roleService->getAdminRoleList())]); + if (!empty($notAllow)) { + foreach ($notAllow as $filter) { + $notAllowItemIdList = []; + $rowSet = $this->accountRepository->getIdFromFilter($filter); + foreach ($rowSet as $row) { + $notAllowItemIdList[] = $this->canonizeAccountId($row); + } + $listParams['not_allowed_id'] = $notAllowItemIdList; + } + } + // Get list $list = []; $rowSet = $this->accountRepository->getAccountList($listParams); @@ -2179,62 +2163,6 @@ public function updatePasswordByOperator($params, array $operator = []): array ]; } - /** - * @param $params - * @param array $operator - * - * @return void - */ - public function resetAccount($params, array $operator = []): void - { - switch ($params['type']) { - case 'password': - // Update account - $this->accountRepository->updateAccount((int)$params['user_id'], ['credential' => null]); - - // Save log - $this->historyService->logger( - 'resetPasswordByOperator', - ['request' => $params, 'account' => $this->getAccount(['id' => (int)$params['user_id']]), 'operator' => $operator] - ); - break; - - case 'mfa': - // Update account - $this->accountRepository->updateAccount((int)$params['user_id'], ['multi_factor_status' => 0, 'multi_factor_secret' => null]); - - // Save log - $this->historyService->logger( - 'resetMfaByOperator', - ['request' => $params, 'account' => $this->getAccount(['id' => (int)$params['user_id']]), 'operator' => $operator] - ); - break; - - case 'avatar': - // Set avatar params - $avatar = [ - 'avatar' => '', - 'avatar_params' => [], - ]; - - // Set account - $account = ['id' => (int)$params['user_id']]; - - // Update profile - $this->updateAccount($avatar, $account, $operator); - - // Save log - $this->historyService->logger( - 'resetAvatarByOperator', - ['request' => $params, 'account' => $this->getAccount(['id' => (int)$params['user_id']]), 'operator' => $operator] - ); - break; - } - - // Make user logout after edit role - $this->logout(['user_id' => (int)$params['user_id'], 'all_session' => 1]); - } - /** * @param $account * @@ -2250,7 +2178,7 @@ public function requestMfa($account): array $mfa = $this->accountRepository->getMultiFactor((int)$account['id']); // Call MultiFactorAuth - $multiFactorAuth = new TwoFactorAuth($this->config['sitename'], 6, 30, Algorithm::Sha1, new EndroidQrCodeProvider()); + $multiFactorAuth = new TwoFactorAuth($this->config['sitename'], '', 6, 30, Algorithm::Sha1, new EndroidQrCodeProvider()); // Set data $secret = null; @@ -2282,11 +2210,11 @@ public function verifyMfa($account, $params, $tokenId): array // Set multi factor $multiFactorGlobal = (int)$this->config['multi_factor']['status'] ?? 0; - // Get multifactor information + // Get multifactorial information $mfa = $this->accountRepository->getMultiFactor((int)$account['id']); // Call MultiFactorAuth - $multiFactorAuth = new TwoFactorAuth($this->config['sitename'], 6, 30, Algorithm::Sha1, new EndroidQrCodeProvider()); + $multiFactorAuth = new TwoFactorAuth($this->config['sitename'], '', 6, 30, Algorithm::Sha1, new EndroidQrCodeProvider()); // check secret code and verify code $result = false; @@ -2348,9 +2276,16 @@ public function verifyMfa($account, $params, $tokenId): array ]; } - /** * Manage the user data cache by setting or updating it. + * + * @param array $account + * @param array $accessToken + * @param array $refreshToken + * @param array $multiFactor + * @param array $otp + * + * @return array */ public function manageUserCache(array $account, array $accessToken = [], array $refreshToken = [], array $multiFactor = [], array $otp = []): array { @@ -2450,4 +2385,169 @@ public function manageUserCache(array $account, array $accessToken = [], array $ return $cacheParams; } + /** + * @param $account + * + * @return array + */ + public function canonizeAccount($account): array + { + if (empty($account)) { + return []; + } + + if (is_object($account)) { + $account = [ + 'id' => (int)$account->getId(), + 'name' => $account->getName(), + 'identity' => $account->getIdentity(), + 'email' => $account->getEmail(), + 'mobile' => $account->getMobile(), + 'status' => (int)$account->getStatus(), + 'time_created' => $account->getTimeCreated(), + 'multi_factor_status' => (int)$account->getMultiFactorStatus(), + ]; + } else { + $account = [ + 'id' => (int)$account['id'], + 'name' => $account['name'] ?? '', + 'email' => $account['email'] ?? '', + 'identity' => $account['identity'] ?? '', + 'mobile' => $account['mobile'] ?? '', + 'status' => (int)$account['status'], + 'time_created' => $account['time_created'] ?? '', + 'multi_factor_status' => (int)$account['multi_factor_status'], + ]; + } + + // Set time + $account['time_created_view'] = ' - '; + if (!empty($account['time_created']) && is_numeric($account['time_created'])) { + $account['time_created_view'] = $this->utilityService->date($account['time_created']); + } + + return $account; + } + + /** + * @param $profile + * + * @return array + */ + public function canonizeProfile($profile): array + { + if (empty($profile)) { + return []; + } + + if (is_object($profile)) { + $profile = [ + 'user_id' => (int)$profile->getUserId(), + 'first_name' => $profile->getFirstName(), + 'last_name' => $profile->getLastName(), + 'birthdate' => $profile->getBirthdate(), + 'gender' => $profile->getGender(), + 'avatar' => $profile->getAvatar(), + 'information' => $profile->getInformation(), + ]; + } else { + $profile = [ + 'user_id' => (int)$profile['user_id'], + 'first_name' => $profile['first_name'], + 'last_name' => $profile['last_name'], + 'birthdate' => $profile['birthdate'], + 'gender' => $profile['gender'], + 'avatar' => $profile['avatar'], + 'information' => $profile['information'], + ]; + } + + // Set information + $profile['information'] = !empty($profile['information']) ? json_decode($profile['information'], true) : []; + + // Set avatar + $profile = $this->avatarService->createUri($profile); + + return $profile; + } + + /** + * @param object|array $roleAccountList + * + * @return int|null + */ + public function canonizeAccountId(object|array $roleAccountList): int|null + { + if (empty($roleAccountList)) { + return 0; + } + + if (is_object($roleAccountList)) { + $accountId = $roleAccountList->getUserId(); + } else { + $accountId = $roleAccountList['user_id']; + } + + return $accountId; + } + + /** + * @param $account + * + * @return array + */ + public function canonizeAccountProfile($account): array + { + if (empty($account)) { + return []; + } + + if (is_object($account)) { + $account = [ + 'id' => (int)$account->getId(), + 'name' => $account->getName(), + 'identity' => $account->getIdentity(), + 'email' => $account->getEmail(), + 'mobile' => $account->getMobile(), + 'status' => (int)$account->getStatus(), + 'time_created' => $account->getTimeCreated(), + 'first_name' => $account->getFirstName(), + 'last_name' => $account->getLastName(), + 'birthdate' => $account->getBirthdate(), + 'gender' => $account->getGender(), + 'avatar' => $account->getAvatar(), + 'information' => $account->getInformation(), + ]; + } else { + $account = [ + 'id' => (int)$account['id'], + 'name' => $account['name'] ?? '', + 'email' => $account['email'] ?? '', + 'identity' => $account['identity'] ?? '', + 'mobile' => $account['mobile'] ?? '', + 'status' => (int)$account['status'], + 'time_created' => $account['time_created'] ?? '', + 'first_name' => $account['first_name'] ?? '', + 'last_name' => $account['last_name'] ?? '', + 'birthdate' => $account['birthdate'] ?? '', + 'gender' => $account['gender'] ?? '', + 'avatar' => $account['avatar'] ?? '', + 'information' => $account['information'] ?? '', + ]; + } + + // Set information + $account['information'] = !empty($account['information']) ? json_decode($account['information'], true) : []; + + // Set time + $account['time_created_view'] = ' - '; + if (!empty($account['time_created']) && is_numeric($account['time_created'])) { + $account['time_created_view'] = $this->utilityService->date($account['time_created']); + } + + // Set avatar + $account = $this->avatarService->createUri($account); + + return $account; + } } \ No newline at end of file