Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,52 @@
<?php

/**
* -------------------------------------------------------------------------
* {NAME} plugin for GLPI
* -------------------------------------------------------------------------
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* -------------------------------------------------------------------------
* @copyright Copyright (C) {YEAR} by the {NAME} plugin team.
* @license MIT https://opensource.org/licenses/mit-license.php
* @link https://github.com/pluginsGLPI/{LNAME}
* -------------------------------------------------------------------------
*/

use PhpCsFixer\Config;
use PhpCsFixer\Finder;

$finder = Finder::create()
->in(__DIR__)
->ignoreVCSIgnored(true)
->name('*.php');

$config = new Config();

$rules = [
'@PER-CS2.0' => true,
'trailing_comma_in_multiline' => ['elements' => ['arguments', 'array_destructuring', 'arrays']], // For PHP 7.4 compatibility
'@PER-CS' => true, // Latest PER rules.
];

return $config
->setRules($rules)
->setFinder($finder)
->setUsingCache(false);
->setCacheFile(__DIR__ . '/var/php-cs-fixer/.php-cs-fixer.cache')
;
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

- Show the specific OAuth authorization error instead of a generic message
- Ignore the connection host/port/security level entered on the authorization diagnostic form and always use the provider's own values
- Escape mail collector host and login when displayed on the application's "Mail collectors" tab

## [1.5.3] - 2026-06-24

Expand Down
1 change: 1 addition & 0 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function plugin_oauthimap_install()
}
}
}

$migration->executeMigration();

return true;
Expand Down
118 changes: 50 additions & 68 deletions inc/application.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public static function getMenuContent()
$menu['page'] = '/plugins/oauthimap/front/application.php';
$menu['icon'] = self::getIcon();
}
if (count($menu)) {

if ($menu !== []) {
return $menu;
}

Expand Down Expand Up @@ -169,6 +170,7 @@ public function displaySpecificTypeField($ID, $field = [], array $options = [])
$values[$provider_class] = $provider_class::getName();
$icons[$provider_class] = $provider_class::getIcon();
}

Dropdown::showFromArray(
$field_name,
$values,
Expand All @@ -186,7 +188,7 @@ public function displaySpecificTypeField($ID, $field = [], array $options = [])
$json_icons = json_encode($icons);
$js = <<<JAVASCRIPT
$(function() {
var icons = $json_icons;
var icons = {$json_icons};
var displayOptionIcon = function(item) {
if (!item.id || !icons[item.id]) {
return item.text;
Expand Down Expand Up @@ -234,14 +236,13 @@ public static function getSpecificValueToDisplay($field, $values, array $options
$values = [$field => $values];
}

switch ($field) {
case 'provider':
$value = $values[$field];
if (in_array($value, self::getSupportedProviders())) {
return '<i class="fab fa-lg ' . $value::getIcon() . '"></i> ' . $value::getName();
}
if ($field === 'provider') {
$value = $values[$field];
if (in_array($value, self::getSupportedProviders())) {
return '<i class="fab fa-lg ' . $value::getIcon() . '"></i> ' . $value::getName();
}

return $value;
return $value;
}

return parent::getSpecificValueToDisplay($field, $values, $options);
Expand All @@ -253,25 +254,24 @@ public static function getSpecificValueToSelect($field, $name = '', $values = ''
$values = [$field => $values];
}

switch ($field) {
case 'provider':
$selected = '';
$elements = ['' => Dropdown::EMPTY_VALUE];
foreach (self::getSupportedProviders() as $class) {
$elements[$class] = $class::getName();
if ($class === $values[$field]) {
$selected = $class;
}
if ($field === 'provider') {
$selected = '';
$elements = ['' => Dropdown::EMPTY_VALUE];
foreach (self::getSupportedProviders() as $class) {
$elements[$class] = $class::getName();
if ($class === $values[$field]) {
$selected = $class;
}
}

return Dropdown::showFromArray(
$name,
$elements,
[
'display' => false,
'value' => $selected,
],
);
return Dropdown::showFromArray(
$name,
$elements,
[
'display' => false,
'value' => $selected,
],
);
}

return parent::getSpecificValueToSelect($field, $name, $values, $options);
Expand All @@ -280,9 +280,7 @@ public static function getSpecificValueToSelect($field, $name = '', $values = ''
/**
* Displays form extra fields/scripts.
*
* @param int $id
*
* @return void
*/
public static function showFormExtra(int $id): void
{
Expand Down Expand Up @@ -343,7 +341,7 @@ public function prepareInputForUpdate($input)
*/
private function prepareInput($input)
{
if (array_key_exists('name', $input) && empty(trim($input['name']))) {
if (array_key_exists('name', $input) && in_array(trim($input['name']), ['', '0'], true)) {
Session::addMessageAfterRedirect(__s('Name cannot be empty', 'oauthimap'), false, ERROR);

return false;
Expand Down Expand Up @@ -400,8 +398,6 @@ public function post_updateItem($history = true)
*
* @param callable|null $callback_callable Callable to call on authorization callback
* @param array $callback_params Parameters to pass to callable
*
* @return void
*/
public function redirectToAuthorizationUrl(?callable $callback_callable = null, array $callback_params = []): void
{
Expand All @@ -412,7 +408,7 @@ public function redirectToAuthorizationUrl(?callable $callback_callable = null,
$provider = $this->getProvider();

$options = [
'scope' => self::getProviderScopes($this->fields['provider']),
'scope' => $this->getProviderScopes($this->fields['provider']),
];
switch ($this->fields['provider']) {
case Azure::class:
Expand All @@ -436,8 +432,6 @@ public function redirectToAuthorizationUrl(?callable $callback_callable = null,

/**
* Check if credentials are valid (i.e. all fields are correclty set).
*
* @return bool
*/
private function areCredentialsValid(): bool
{
Expand All @@ -452,8 +446,6 @@ private function areCredentialsValid(): bool

/**
* Get list of supported providers classnames.
*
* @return array
*/
private static function getSupportedProviders(): array
{
Expand Down Expand Up @@ -485,21 +477,21 @@ public function getProvider()
'clientId' => $this->fields['client_id'],
'clientSecret' => (new GLPIKey())->decrypt($this->fields['client_secret']),
'redirectUri' => self::getCallbackUrl(),
'scope' => self::getProviderScopes($this->fields['provider']),
'scope' => $this->getProviderScopes($this->fields['provider']),
];

if (!empty($CFG_GLPI['proxy_name'])) {
// Connection using proxy
$params['proxy'] = !empty($CFG_GLPI['proxy_user'])
$params['proxy'] = empty($CFG_GLPI['proxy_user'])
? sprintf(
'%s:%s@%s:%s',
rawurlencode($CFG_GLPI['proxy_user']),
rawurlencode((new GLPIKey())->decrypt($CFG_GLPI['proxy_passwd'])),
'%s:%s',
$CFG_GLPI['proxy_name'],
$CFG_GLPI['proxy_port'],
)
: sprintf(
'%s:%s',
'%s:%s@%s:%s',
rawurlencode($CFG_GLPI['proxy_user']),
rawurlencode((string) (new GLPIKey())->decrypt($CFG_GLPI['proxy_passwd'])),
$CFG_GLPI['proxy_name'],
$CFG_GLPI['proxy_port'],
);
Expand All @@ -512,6 +504,7 @@ public function getProvider()
if (!empty($this->fields['tenant_id'])) {
$params['tenant'] = $this->fields['tenant_id'];
}

break;
case Google::class:
$params['accessType'] = 'offline';
Expand All @@ -525,36 +518,27 @@ public function getProvider()
* Get required scopes for given provider.
*
* @param string $provider Provider classname
*
* @return array
*/
private static function getProviderScopes(string $provider): array
private function getProviderScopes(string $provider): array
{
$scopes = [];

switch ($provider) {
case Azure::class:
$scopes = [
'openid', 'email', // required to be able to fetch owner details
'offline_access',
'https://outlook.office.com/IMAP.AccessAsUser.All',
];
break;
case Google::class:
$scopes = [
'https://mail.google.com/',
];
break;
}

return $scopes;
return match ($provider) {
Azure::class => [
'openid', 'email', // required to be able to fetch owner details
'offline_access',
'https://outlook.office.com/IMAP.AccessAsUser.All',
],
Google::class => [
'https://mail.google.com/',
],
default => $scopes,
};
}

/**
* Get documentation URLs.
* Keys are providers classnames, values are URL.
*
* @return array
*/
private static function getProvidersDocumentationUrls(): array
{
Expand All @@ -566,8 +550,6 @@ private static function getProvidersDocumentationUrls(): array

/**
* Get callback URL used during authorization process.
*
* @return string
*/
private static function getCallbackUrl(): string
{
Expand Down Expand Up @@ -599,10 +581,10 @@ public static function install(Migration $migration)
$table = self::getTable();

if (!$DB->tableExists($table)) {
$migration->displayMessage("Installing $table");
$migration->displayMessage('Installing ' . $table);

$query = <<<SQL
CREATE TABLE IF NOT EXISTS `$table` (
CREATE TABLE IF NOT EXISTS `{$table}` (
`id` int {$default_key_sign} NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`is_active` tinyint NOT NULL DEFAULT '0',
Expand Down Expand Up @@ -640,7 +622,7 @@ public static function uninstall(Migration $migration)
global $DB;

$table = self::getTable();
$migration->displayMessage("Uninstalling $table");
$migration->displayMessage('Uninstalling ' . $table);
$migration->dropTable($table);

$DB->delete('glpi_displaypreferences', [
Expand Down
Loading