Skip to content

Commit

Permalink
dev/core#4411 fix issue with user language
Browse files Browse the repository at this point in the history
  • Loading branch information
jaapjansma committed Dec 3, 2023
1 parent ec3684f commit bbbdcd8
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
9 changes: 7 additions & 2 deletions ext/standaloneusers/CRM/Standaloneusers/BAO/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) {
}
}

public static function updateLastAccessed() {
public static function updateLastAccessed()
{
$sess = CRM_Core_Session::singleton();
$ufID = (int) $sess->get('ufID');
$ufID = (int)$sess->get('ufID');
CRM_Core_DAO::executeQuery("UPDATE civicrm_uf_match SET when_last_accessed = NOW() WHERE id = $ufID");
$sess->set('lastAccess', time());
}

public static function getPreferredLanguages(): array {
return CRM_Core_I18n::uiLanguages(FALSE);
}

}
8 changes: 7 additions & 1 deletion ext/standaloneusers/CRM/Standaloneusers/DAO/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from standaloneusers/xml/schema/CRM/Standaloneusers/User.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:0b0cd1d01573ad0e5adc1a3e92f9638b)
* (GenCodeChecksum:bb2c741901a689ff804ab65d05c7795b)
*/
use CRM_Standaloneusers_ExtensionUtil as E;

Expand Down Expand Up @@ -526,6 +526,12 @@ public static function &fields() {
'entity' => 'User',
'bao' => 'CRM_Standaloneusers_DAO_User',
'localizable' => 0,
'html' => [
'type' => 'Select',
],
'pseudoconstant' => [
'callback' => 'CRM_Standaloneusers_BAO_User::getPreferredLanguages',
],
'add' => '2.1',
],
'password_reset_token' => [
Expand Down
16 changes: 16 additions & 0 deletions ext/standaloneusers/Civi/Standalone/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realP
return FALSE;
}

$this->applyLocaleFromUser($user);

// Note: random_int is more appropriate for cryptographical use than mt_rand
// The long number is the max 32 bit value.
return [$user['contact_id'], $user['id'], random_int(0, 2147483647)];
Expand All @@ -235,6 +237,7 @@ public function loginAuthenticatedUserRecord(array $user, bool $withSession) {
])['values'][0]['contact_id'] ?? NULL;
// Confusingly, Civi stores it's *Contact* ID as *userID* on the session.
$session->set('userID', $contactID);
$this->applyLocaleFromUser($user);
}
}

Expand Down Expand Up @@ -452,4 +455,17 @@ public function preparePasswordResetWorkflow(array $user, string $token): ?CRM_S
return $workflowMessage;
}

/**
* Applies the locale from the user record.
*
* @param array $user
* @return void
*/
private function applyLocaleFromUser(array $user) {
$session = CRM_Core_Session::singleton();
if (!empty($user['language'])) {
$session->set('lcMessages', $user['language']);
}
}

}
2 changes: 1 addition & 1 deletion ext/standaloneusers/ang/afformEditUserAccount.aff.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<af-field name="uf_name" />
<af-field name="is_active" />
<af-field name="timezone" />
<af-field name="language" />
<af-field name="language" defn="{help_pre: ts('Set the user interface language of this user. You can also leave it empty to use the default system language.'), input_attrs: {placeholder: ts('System default language')}}" />
</fieldset>
<button class="af-button btn btn-primary" crm-icon="fa-check" ng-click="afform.submit()">Submit</button>
</af-form>
16 changes: 16 additions & 0 deletions ext/standaloneusers/standaloneusers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@
use CRM_Standaloneusers_ExtensionUtil as E;
// phpcs:enable

/**
* Hide the inherit CMS language on the Settings - Localization form.
*
* Implements hook_civicrm_buildForm().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildForm/
*/
function standaloneusers_civicrm_buildForm($formName, CRM_Core_Form $form) {
// Administer / Localization / Languages, Currency, Locations
if ($formName == 'CRM_Admin_Form_Setting_Localization') {
if ($inheritLocaleElement = $form->getElement('inheritLocale')) {
$inheritLocaleElement->freeze();
}
}
}

/**
* Implements hook_civicrm_config().
*
Expand Down
6 changes: 6 additions & 0 deletions ext/standaloneusers/xml/schema/CRM/Standaloneusers/User.xml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@
<type>varchar</type>
<length>5</length>
<comment>UI language preferred by the given user/contact</comment>
<pseudoconstant>
<callback>CRM_Standaloneusers_BAO_User::getPreferredLanguages</callback>
</pseudoconstant>
<html>
<type>Select</type>
</html>
<add>2.1</add>
</field>
<index>
Expand Down

0 comments on commit bbbdcd8

Please sign in to comment.