N°5102 - Allow to send emails using GSuite SMTP and OAuth - Add a flag to select OAuth client for SMTP usage

This commit is contained in:
Eric Espie
2022-07-07 16:34:19 +02:00
parent c25a4a7346
commit 24d19cd8d6
5 changed files with 98 additions and 18 deletions

View File

@@ -110,10 +110,9 @@
parent::DisplayBareHeader($oPage, $bEditMode);
if (!$bEditMode) {
$oConfig = utils::GetConfig();
$aScopes = $this->Get('scope')->GetValues();
if ($this->Get('status') == 'inactive') {
$oPage->p('<b>'.Dict::S('itop-oauth-client:Message:MissingToken').'</b>');
} elseif (in_array('SMTP', $aScopes) && $oConfig->Get('email_transport_smtp.username') == $this->Get('name')) {
} elseif ($this->Get('used_for_smtp') == 'yes' && $oConfig->Get('email_transport_smtp.username') == $this->Get('name')) {
$sLabel = Dict::S('itop-oauth-client:UsedForSMTP');
$sTestLabel = Dict::S('itop-oauth-client:TestSMTP');
$sTestURL = utils::GetAbsoluteUrlAppRoot().'setup/email.test.php';
@@ -290,12 +289,6 @@
</attributes>
</reconciliation>
<uniqueness_rules>
<rule id="name">
<attributes>
<attribute id="name"/>
</attributes>
<is_blocking>true</is_blocking>
</rule>
<rule id="server">
<attributes>
<attribute id="provider"/>
@@ -336,6 +329,16 @@
<attribute id="advanced_scope"/>
</dependencies>
</field>
<field id="used_for_smtp" xsi:type="AttributeEnum">
<always_load_in_tables>true</always_load_in_tables>
<values>
<value id="yes">yes</value>
<value id="no">no</value>
</values>
<sql>used_for_smtp</sql>
<default_value>no</default_value>
<is_null_allowed>true</is_null_allowed>
</field>
</fields>
<presentation>
<details>
@@ -389,6 +392,9 @@
<item id="advanced_scope">
<rank>30</rank>
</item>
<item id="used_for_smtp">
<rank>40</rank>
</item>
</items>
</item>
</items>
@@ -435,6 +441,30 @@
}
]]></code>
</method>
<method id="DoCheckToWrite">
<static>false</static>
<access>public</access>
<type>Overload-DBObject</type>
<code><![CDATA[ public function DoCheckToWrite()
{
parent::DoCheckToWrite();
$aChanges = $this->ListChanges();
if (array_key_exists('name', $aChanges) || array_key_exists('used_for_smtp', $aChanges))
{
$sNewName = $this->Get('name');
$sNewUseForSMTP = $this->Get('used_for_smtp');
if ($sNewUseForSMTP == 'yes') {
$oSearch = DBObjectSearch::FromOQL_AllData("SELECT OAuthClientGoogle WHERE name = :newname AND used_for_smtp = :newuseforsmtp AND id != :id UNION SELECT OAuthClientAzure WHERE name = :newname AND used_for_smtp = :newuseforsmtp AND id != :id");
$oSet = new DBObjectSet($oSearch, array(), ['id' => $this->GetKey(), 'newname' => $sNewName, 'newuseforsmtp' => $sNewUseForSMTP]);
if ($oSet->Count() > 0)
{
$this->m_aCheckIssues[] = Dict::Format('OAuthClient:Name/UseForSMTPMustBeUnique', $sNewName, $sNewUseForSMTP);
}
}
}
} ]]></code>
</method>
<method id="ComputeValues">
<static>false</static>
<access>public</access>
@@ -559,12 +589,6 @@
</attributes>
</reconciliation>
<uniqueness_rules>
<rule id="name">
<attributes>
<attribute id="name"/>
</attributes>
<is_blocking>true</is_blocking>
</rule>
<rule id="server">
<attributes>
<attribute id="provider"/>
@@ -605,6 +629,16 @@
<attribute id="advanced_scope"/>
</dependencies>
</field>
<field id="used_for_smtp" xsi:type="AttributeEnum">
<always_load_in_tables>true</always_load_in_tables>
<values>
<value id="yes">yes</value>
<value id="no">no</value>
</values>
<sql>used_for_smtp</sql>
<default_value>no</default_value>
<is_null_allowed>true</is_null_allowed>
</field>
</fields>
<presentation>
<details>
@@ -658,6 +692,9 @@
<item id="advanced_scope">
<rank>30</rank>
</item>
<item id="used_for_smtp">
<rank>40</rank>
</item>
</items>
</item>
</items>
@@ -704,6 +741,30 @@
}
]]></code>
</method>
<method id="DoCheckToWrite">
<static>false</static>
<access>public</access>
<type>Overload-DBObject</type>
<code><![CDATA[ public function DoCheckToWrite()
{
parent::DoCheckToWrite();
$aChanges = $this->ListChanges();
if (array_key_exists('name', $aChanges) || array_key_exists('used_for_smtp', $aChanges))
{
$sNewName = $this->Get('name');
$sNewUseForSMTP = $this->Get('used_for_smtp');
if ($sNewUseForSMTP == 'yes') {
$oSearch = DBObjectSearch::FromOQL_AllData("SELECT OAuthClientGoogle WHERE name = :newname AND used_for_smtp = :newuseforsmtp AND id != :id UNION SELECT OAuthClientAzure WHERE name = :newname AND used_for_smtp = :newuseforsmtp AND id != :id");
$oSet = new DBObjectSet($oSearch, array(), ['id' => $this->GetKey(), 'newname' => $sNewName, 'newuseforsmtp' => $sNewUseForSMTP]);
if ($oSet->Count() > 0)
{
$this->m_aCheckIssues[] = Dict::Format('OAuthClient:Name/UseForSMTPMustBeUnique', $sNewName, $sNewUseForSMTP);
}
}
}
} ]]></code>
</method>
<method id="ComputeValues">
<static>false</static>
<access>public</access>

View File

@@ -22,6 +22,8 @@ Dict::Add('EN US', 'English', 'English', [
'itop-oauth-client:Message:TokenCreated' => 'Access token created',
'itop-oauth-client:Message:TokenRecreated' => 'Access token regenerated',
'OAuthClient:Name/UseForSMTPMustBeUnique' => 'The combination Login (%1$s) and Use for SMTP (%2$s) has already be used for OAuth Client',
'OAuthClient:baseinfo' => 'Base Information',
'OAuthClient:scope' => 'Scope',
]);
@@ -81,6 +83,10 @@ Dict::Add('EN US', 'English', 'English', array(
'Class:OAuthClientAzure/Attribute:used_scope/Value:simple+' => '',
'Class:OAuthClientAzure/Attribute:used_scope/Value:advanced' => 'Advanced',
'Class:OAuthClientAzure/Attribute:used_scope/Value:advanced+' => '',
'Class:OAuthClientAzure/Attribute:used_for_smtp' => 'Used for SMTP',
'Class:OAuthClientAzure/Attribute:used_for_smtp+' => '',
'Class:OAuthClientAzure/Attribute:used_for_smtp/Value:yes' => 'Yes',
'Class:OAuthClientAzure/Attribute:used_for_smtp/Value:no' => 'No',
));
//
@@ -104,4 +110,8 @@ Dict::Add('EN US', 'English', 'English', array(
'Class:OAuthClientGoogle/Attribute:used_scope/Value:simple+' => '',
'Class:OAuthClientGoogle/Attribute:used_scope/Value:advanced' => 'Advanced',
'Class:OAuthClientGoogle/Attribute:used_scope/Value:advanced+' => '',
'Class:OAuthClientGoogle/Attribute:used_for_smtp' => 'Used for SMTP',
'Class:OAuthClientGoogle/Attribute:used_for_smtp+' => '',
'Class:OAuthClientGoogle/Attribute:used_for_smtp/Value:yes' => 'Yes',
'Class:OAuthClientGoogle/Attribute:used_for_smtp/Value:no' => 'No',
));

View File

@@ -5,6 +5,7 @@
* @copyright Copyright (C) 2013 XXXXX
* @license http://opensource.org/licenses/AGPL-3.0
*/
Dict::Add('FR FR', 'French', 'Français', [
'Menu:CreateMailbox' => 'Créer une boite mail...',
'Menu:OAuthClient' => 'Client OAuth',
@@ -21,6 +22,8 @@ Dict::Add('FR FR', 'French', 'Français', [
'itop-oauth-client:Message:TokenCreated' => 'Le jeton d\'accès à été créé',
'itop-oauth-client:Message:TokenRecreated' => 'Le jeton d\'accès à été renouvelé',
'OAuthClient:Name/UseForSMTPMustBeUnique' => 'La combinaison Login (%1$s) and Utilisé pour SMTP (%2$s) a déjà été utilisée pour OAuth Client',
'OAuthClient:baseinfo' => 'Information',
'OAuthClient:scope' => 'Scope',
]);
@@ -80,6 +83,10 @@ Dict::Add('FR FR', 'French', 'Français', array(
'Class:OAuthClientAzure/Attribute:used_scope/Value:simple+' => '',
'Class:OAuthClientAzure/Attribute:used_scope/Value:advanced' => 'Avancé',
'Class:OAuthClientAzure/Attribute:used_scope/Value:advanced+' => '',
'Class:OAuthClientAzure/Attribute:used_for_smtp' => 'Utilisé pour SMTP',
'Class:OAuthClientAzure/Attribute:used_for_smtp+' => '',
'Class:OAuthClientAzure/Attribute:used_for_smtp/Value:yes' => 'Oui',
'Class:OAuthClientAzure/Attribute:used_for_smtp/Value:no' => 'Non',
));
//
@@ -102,4 +109,8 @@ Dict::Add('FR FR', 'French', 'Français', array(
'Class:OAuthClientGoogle/Attribute:used_scope/Value:simple' => 'Simple',
'Class:OAuthClientGoogle/Attribute:used_scope/Value:advanced' => 'Avancé',
'Class:OAuthClientGoogle/Attribute:used_scope/Value:advanced+' => '',
'Class:OAuthClientGoogle/Attribute:used_for_smtp' => 'Utilisé pour SMTP',
'Class:OAuthClientGoogle/Attribute:used_for_smtp+' => '',
'Class:OAuthClientGoogle/Attribute:used_for_smtp/Value:yes' => 'Oui',
'Class:OAuthClientGoogle/Attribute:used_for_smtp/Value:no' => 'Non',
));

View File

@@ -19,10 +19,9 @@ class ApplicationUIExtension extends AbstractApplicationUIExtension
// Possible return values are:
// HILIGHT_CLASS_CRITICAL, HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
$oConfig = utils::GetConfig();
$aScopes = $oObject->Get('scope')->GetValues();
if ($oObject->Get('status') == 'inactive') {
return HILIGHT_CLASS_WARNING;
} elseif (in_array('SMTP', $aScopes) && $oConfig->Get('email_transport_smtp.username') == $oObject->Get('name')) {
} elseif ($oObject->Get('used_for_smtp') == 'yes' && $oConfig->Get('email_transport_smtp.username') == $oObject->Get('name')) {
return HILIGHT_CLASS_OK;
}
}

View File

@@ -43,8 +43,7 @@ class OAuthClientProviderFactory
throw new CoreException(Dict::Format('itop-oauth-client:MissingOAuthClient', $sUsername));
}
while ($oOAuthClient = $oSet->Fetch()) {
$aScopes = $oOAuthClient->Get('scope')->GetValues();
if (in_array('SMTP', $aScopes)) {
if ($oOAuthClient->Get('used_for_smtp') == 'yes') {
return $oOAuthClient;
}
}