Skip to content

Commit

Permalink
Merge branch 'pu/ccheng/user_adb_extra_field' into '2024.11'
Browse files Browse the repository at this point in the history
feature(Admin/User): make user contact configurable

See merge request tine20/tine20!5502
  • Loading branch information
ccheng-dev committed Dec 10, 2024
2 parents 43df55c + cb9ade6 commit 520a72c
Show file tree
Hide file tree
Showing 18 changed files with 375 additions and 30 deletions.
31 changes: 31 additions & 0 deletions tests/tine20/Admin/Controller/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,37 @@ public function testUpdateUserAdbContainer()
Addressbook_Controller_Contact::getInstance()->get($user->contact_id)->getIdFromProperty('container_id'));
}

public function testAddUserUpdateContact()
{
$userToCreate = TestCase::getTestUser();
$pw = Tinebase_Record_Abstract::generateUID(12);

$this->_usernamesToDelete[] = $userToCreate->accountLoginName;
$user = Admin_Controller_User::getInstance()->create($userToCreate, $pw, $pw);

$newContact = Addressbook_Controller_Contact::getInstance()->create(new Addressbook_Model_Contact([
'n_given' => 'foo',
'n_family' => 'test',
'email' => '[email protected]',
'tel_cell_private' => '+49TELCELLPRIVATE',
]));

$oldContactId = $user->contact_id;
$user->contact_id = $newContact->getId();
$user = Admin_Controller_User::getInstance()->update($user, $pw, $pw);
static::assertEquals($newContact->getId(), $user->contact_id);

$oldContact = Addressbook_Controller_Contact::getInstance()->get($oldContactId);
static::assertEquals($oldContact->type, Addressbook_Model_Contact::CONTACTTYPE_CONTACT, 'old user contact should switch type to contact');

$newContact = Addressbook_Controller_Contact::getInstance()->get($user->contact_id);
static::assertEquals($newContact->type, Addressbook_Model_Contact::CONTACTTYPE_USER, 'new user contact should switch type to user');
static::assertEquals($newContact->email, $user->accountEmailAddress, 'new contact email should be the same as user email');
static::assertEquals($newContact->n_fn, $user->accountFullName);
static::assertEquals($newContact->n_fileas, $user->accountDisplayName);

}

public function testUpdateUserWithEmailButNoPassword()
{
$this->_skipWithoutEmailSystemAccountConfig();
Expand Down
47 changes: 47 additions & 0 deletions tests/tine20/Admin/Frontend/Json/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,53 @@ public function testUpdateUserWithoutContainerACL()
self::assertEquals(2, $account['groups']['totalcount']);
}

/**
* testUpdateUserSendSMS
*
*/
public function testUpdateUserSendSMS()
{
Tinebase_Config::getInstance()->{Tinebase_Config::SMS}->{Tinebase_Config::SMS_ADAPTERS} = [
Tinebase_Model_Sms_AdapterConfigs::FLD_ADAPTER_CONFIGS => [
[
Tinebase_Model_Sms_AdapterConfig::FLD_NAME => 'sms1',
Tinebase_Model_Sms_AdapterConfig::FLD_ADAPTER_CLASS => Tinebase_Model_Sms_GenericHttpAdapter::class,
Tinebase_Model_Sms_AdapterConfig::FLD_ADAPTER_CONFIG => [
Tinebase_Model_Sms_GenericHttpAdapter::FLD_URL => 'https://shoo.tld/restapi/message',
Tinebase_Model_Sms_GenericHttpAdapter::FLD_BODY => '{"encoding":"auto","body":"{{ message }}","originator":"{{ app.branding.title }}","recipients":["{{ cellphonenumber }}"],"route":"2345"}',
Tinebase_Model_Sms_GenericHttpAdapter::FLD_METHOD => 'POST',
Tinebase_Model_Sms_GenericHttpAdapter::FLD_HEADERS => [
'Auth-Bearer' => 'unittesttokenshaaaaalalala'
],
],
],
],
];

$userArray = $this->_originalTestUser->toArray();

$userArray['send_password_via_sms'] = true;
$userArray['sms_phone_number'] = '777777777';
$userArray['accountPassword'] = 'tine20admin';

$smsConfig = Tinebase_Config::getInstance()->{Tinebase_Config::SMS}->{Tinebase_Config::SMS_ADAPTERS}
?->{Tinebase_Model_Sms_AdapterConfigs::FLD_ADAPTER_CONFIGS}->getFirstRecord();
$smsAdapterConfig = $smsConfig ? $smsConfig->{Tinebase_Model_Sms_AdapterConfig::FLD_ADAPTER_CONFIG} : null;
$smsAdapterConfig->setHttpClientConfig([
'adapter' => ($httpClientTestAdapter = new Tinebase_ZendHttpClientAdapter())
]);
$httpClientTestAdapter->writeBodyCallBack = function($body) {
Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' sms request body: ' . $body);
};
$httpClientTestAdapter->setResponse(new Zend_Http_Response(200, []));

$result = $this->_json->saveUser($userArray);

$this->assertStringContainsString('Instance: ' . Tinebase_Config::getInstance()->get(Tinebase_Config::BRANDING_TITLE) . ' , new password: ' . $userArray['accountPassword'],
$httpClientTestAdapter->lastRequestBody);
$this->assertTrue($result['sms']['777777777']);
}

/**
* @param Tinebase_Model_FullUser $account
* @return Tinebase_Model_Container
Expand Down
10 changes: 10 additions & 0 deletions tine20/Addressbook/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ protected function _handleEvent(Tinebase_Event_Abstract $_eventObject)
case 'Admin_Event_AddAccount':
$this->createPersonalFolder($_eventObject->account);
break;
case Admin_Event_UpdateAccount::class:
/** @var Admin_Event_UpdateAccount $_eventObject */
if ($_eventObject->account->contact_id !== $_eventObject->oldAccount->contact_id) {
$contact = Addressbook_Controller_Contact::getInstance()->get($_eventObject->oldAccount->contact_id);
$contact->type = Addressbook_Model_Contact::CONTACTTYPE_CONTACT;
$contact = Addressbook_Controller_Contact::getInstance()->update($contact);
if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->info(__METHOD__ . ' ' . __LINE__
. ' switch deprecated user contact type to contact : ' . $contact->getId());
}
break;
case 'Tinebase_Event_User_DeleteAccount':
/**
* @var Tinebase_Event_User_DeleteAccount $_eventObject
Expand Down
2 changes: 1 addition & 1 deletion tine20/Addressbook/Controller/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ protected function _inspectBeforeUpdate($_record, $_oldRecord)

$this->_checkAndSetShortName($_record, $_oldRecord);

if (isset($_oldRecord->type) && $_oldRecord->type == Addressbook_Model_Contact::CONTACTTYPE_USER) {
if (isset($_oldRecord->type) && $_oldRecord->type == Addressbook_Model_Contact::CONTACTTYPE_USER && empty($_record->type)) {
$_record->type = Addressbook_Model_Contact::CONTACTTYPE_USER;
}

Expand Down
61 changes: 61 additions & 0 deletions tine20/Admin/Frontend/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,67 @@ public function saveUser($recordData)
'totalcount' => count($userRoles)
);


if (!empty($password) && isset($recordData['sms_phone_number'])) {
$smsAdapterConfigs = Tinebase_Config::getInstance()->{Tinebase_Config::SMS}->{Tinebase_Config::SMS_ADAPTERS}
?->{Tinebase_Model_Sms_AdapterConfigs::FLD_ADAPTER_CONFIGS};

if (!$smsAdapterConfigs || count($smsAdapterConfigs) === 0) {
if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(
__METHOD__ . '::' . __LINE__ . ' sms adapter configs is not found , skip sending new password message');
} else {
$smsAdapterConfig = $smsAdapterConfigs->getFirstRecord();
$smsAdapterConfig = $smsAdapterConfig->{Tinebase_Model_Sms_AdapterConfig::FLD_ADAPTER_CONFIG};

if (empty($smsAdapterConfig->getHttpClientConfig())) {
$smsAdapterConfig->setHttpClientConfig([
'adapter' => ($genericHttpAdapter = new Tinebase_ZendHttpClientAdapter())
]);

$genericHttpAdapter->writeBodyCallBack = function($body) {
$colorGreen = "\033[43m";
$colorReset = "\033[0m";
Tinebase_Core::getLogger()->warn($colorGreen . __METHOD__ . '::' . __LINE__ . ' sms request body: ' . $body . $colorReset . PHP_EOL);
};
$genericHttpAdapter->setResponse(new Zend_Http_Response(200, []));
}

$template = Tinebase_Config::getInstance()->{Tinebase_Config::SMS}->{Tinebase_Config::SMS_MESSAGE_TEMPLATES}->get(Tinebase_Config::SMS_TEMPLATE_NEW_PASSWORD);
$locale = Tinebase_Core::getLocale();
if (! $locale) {
$locale = Tinebase_Translation::getLocale();
}
$twig = new Tinebase_Twig($locale, Tinebase_Translation::getTranslation(), [
Tinebase_Twig::TWIG_LOADER =>
new Tinebase_Twig_CallBackLoader(__METHOD__ . 'password', time() - 1, function () use ($template) {
return $template;
})
]);
$message = $twig->load(__METHOD__ . 'password')->render([
'instanceName' => Tinebase_Config::getInstance()->get(Tinebase_Config::BRANDING_TITLE),
'password' => $password,
]);

$smsSendConfig = new Tinebase_Model_Sms_SendConfig([
Tinebase_Model_Sms_SendConfig::FLD_MESSAGE => $message,
Tinebase_Model_Sms_SendConfig::FLD_RECIPIENT_NUMBER => $recordData['sms_phone_number'],
Tinebase_Model_Sms_SendConfig::FLD_ADAPTER_CLASS => Tinebase_Model_Sms_GenericHttpAdapter::class,
Tinebase_Model_Sms_SendConfig::FLD_ADAPTER_CONFIG => $smsAdapterConfig,
]);

if (Tinebase_Sms::send($smsSendConfig)) {
try {
$result['sms'] = [
$recordData['sms_phone_number'] => true,
];
} catch (Zend_Session_Exception $zse) {
if (Tinebase_Core::isLogLevel(Zend_Log::WARN))
Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' ' . $zse->getMessage()) ;
}
}
}
}

Tinebase_Core::setExecutionLifeTime($oldMaxExcecutionTime);

return $result;
Expand Down
Loading

0 comments on commit 520a72c

Please sign in to comment.