* @author Jacob T Paul * @author Paul Simon * @author Brady Miller * @author Tyler Wrenn * @author Stephen Nielson * @author Stephen Waite * @copyright Copyright (c) 2011 Z&H Consultancy Services Private Limited * @copyright Copyright (c) 2018-2019 Brady Miller * @copyright Copyright (c) 2020 Tyler Wrenn * @copyright Copyright (c) 2022 Discover and Change, Inc * @copyright Copyright (c) 2022 Stephen Waite * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3 */ require_once("../../globals.php"); $session = \OpenEMR\Common\Session\SessionWrapperFactory::getInstance()->getActiveSession(); $pid = $session->get('pid', 0); use OpenEMR\Common\{Csrf\CsrfUtils, Session\SessionWrapperFactory}; use OpenEMR\Common\Acl\AccessDeniedHelper; use OpenEMR\Common\Acl\AclMain; use OpenEMR\Services\PatientAccessOnsiteService; use OpenEMR\Core\OEGlobalsBag; // Portal credential management; align the permission check with the // `patientportal/portal` administration surface. if (!AclMain::aclCheckCore('patientportal', 'portal')) { AccessDeniedHelper::denyWithTemplate("ACL check failed for patientportal/portal: Create Portal Login", xl("Patient Portal")); } // The service layer accepts pid=0 and will read/write `patient_access_onsite` // against that value; require an open chart before reaching it. if (!is_numeric($pid) || (int) $pid <= 0) { AccessDeniedHelper::denyWithTemplate("No patient selected: Create Portal Login", xl("Patient Portal")); } $patientAccessOnSiteService = new PatientAccessOnsiteService(); $credentials = $patientAccessOnSiteService->getOnsiteCredentialsForPid($pid); $option = OEGlobalsBag::getInstance()->get('portal_force_credential_reset') ?? '0'; if ($option == '2') { $forced_reset_disable = PatientAccessOnsiteService::fetchUserSetting('portal_login.credential_reset_disable'); } elseif ($option == '0') { $forced_reset_disable = 0; // sets database to force reset on login } else { $forced_reset_disable = 1; // sets database to ignore force reset on login } $credPlainMessage = ''; $credEmailSent = false; $credEmailAddress = ''; if (isset($_POST['form_save']) && $_POST['form_save'] === 'submit') { CsrfUtils::checkCsrfInput(INPUT_POST, dieOnFail: true); $postForcedResetDisable = $_POST['forced_reset_disable'] ?? 0; $rawForcedResetDisable = is_numeric($postForcedResetDisable) ? intval($postForcedResetDisable) : 0; $forced_reset_disable = $option == '2' ? $rawForcedResetDisable : $option; $rawPwd = $_POST['pwd'] ?? ''; $postPwd = is_string($rawPwd) ? strip_tags($rawPwd) : ''; $rawUname = $_POST['uname'] ?? ''; $postUname = is_string($rawUname) ? strip_tags($rawUname) : ''; $rawLoginUname = $_POST['login_uname'] ?? ''; $postLoginUname = is_string($rawLoginUname) ? strip_tags($rawLoginUname) : ''; // TODO: @adunsulag do we clear the pwd variables here?? Hard to break it out into separate functions when we do that... $result = $patientAccessOnSiteService->saveCredentials($pid, $postPwd, $postUname, $postLoginUname, $forced_reset_disable); if (!empty($result)) { $emailResult = $patientAccessOnSiteService->sendCredentialsEmail($pid, $result['pwd'], $result['uname'], $result['login_uname'], $result['email_direct']); $credPlainMessage = strip_tags((string) $emailResult['plainMessage']); $credEmailSent = $emailResult['success']; $patientData = sqlQuery("SELECT `email` FROM `patient_data` WHERE `pid` = ?", [$pid]); $credEmailAddress = $patientData['email'] ?? ''; } } $trustedUserName = $patientAccessOnSiteService->getUniqueTrustedUsernameForPid($pid); $trustedEmail = $patientAccessOnSiteService->getTrustedEmailForPid($pid); echo $patientAccessOnSiteService->filterTwigTemplateData($pid, 'patient/portal_login/print.html.twig', [ 'credPlainMessage' => $credPlainMessage , 'credEmailSent' => $credEmailSent , 'credEmailAddress' => $credEmailAddress , 'csrfToken' => CsrfUtils::collectCsrfToken(session: $session) , 'fname' => $credentials['fname'] , 'portal_username' => $credentials['portal_username'] , 'id' => $credentials['id'] , 'uname' => $credentials['portal_username'] ?: $credentials['fname'] . $credentials['lname'] . $credentials['id'] , 'login_uname' => $credentials['portal_login_username'] ?? $trustedUserName , 'pwd' => $patientAccessOnSiteService->getRandomPortalPassword() , 'enforce_signin_email' => OEGlobalsBag::getInstance()->getBoolean('enforce_signin_email') , 'email_direct' => trim((string) $trustedEmail['email_direct']) , 'forced_reset_disable' => $forced_reset_disable , 'forced_reset_option' => $option // if someone wants to add additional data fields they can add this in as a // key => [...] property where the key is the template filename // which must exist inside a twig directory path of 'patient/partials/' and end with the '.html.twig' extension // the mapped value is the data array that will be passed to the twig template. , 'extensionsFormFields' => [] , 'extensionsJavascript' => [] ]);