Skip to content

Commit

Permalink
Add possibility to profile .well-known files
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Jun 14, 2024
1 parent 73bd3e7 commit 82bde3a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
2.0.3 (Unreleased)
-----------------------
- Enh: Endpoint to test push status
- Enh: Add possibility to profile `.well-known` files

2.0.2 (May 28, 2024)
-----------------------
Expand Down
65 changes: 58 additions & 7 deletions models/ConfigureForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

namespace humhub\modules\fcmPush\models;

use Exception;
use humhub\modules\fcmPush\Module;
use Yii;
use yii\base\InvalidArgumentException;
use yii\base\Model;
use yii\helpers\FileHelper;
use yii\helpers\Json;

class ConfigureForm extends Model
{
public const FILE_ASSET_LINKS = 'assetlinks.json';
public const FILE_APPLE_ASSOCIATION = 'apple-app-site-association';

public $enableEmailGoService;

public $humhubInstallId;
Expand All @@ -24,6 +29,10 @@ class ConfigureForm extends Model

public $disableAuthChoicesIos;

public $fileAssetlinksJson;

public $fileAppleAppSiteAssociation;

/**
* Validate JSON field params
*
Expand All @@ -39,8 +48,7 @@ private function validateJsonParams($arrayPattern, $arrayCheck)
if (isset($arrayCheck[$key])) {
if (empty($arrayCheck[$key])) {
$errors["empty"] .= $errors["empty"] == "" ? "\"$key\"" : ", \"$key\"";
}
else {
} else {
$condition = false;
switch ($value['type']) {
case "string":
Expand Down Expand Up @@ -85,6 +93,7 @@ public function rules()
[['enableEmailGoService', 'disableAuthChoicesIos'], 'boolean'],
[['senderId'], 'number'],
[['serverKey', 'json', 'humhubApiKey'], 'safe'],
[['fileAssetlinksJson', 'fileAppleAppSiteAssociation'], 'string'],
['json', function ($attribute, $params, $validator) {
if (empty($this->$attribute)) {
return;
Expand Down Expand Up @@ -141,15 +150,17 @@ public function attributeLabels()
'json' => Yii::t('FcmPushModule.base', 'Service Account (JSON file)'),
'serverKey' => Yii::t('FcmPushModule.base', 'Cloud Messaging API (Legacy)'),
'disableAuthChoicesIos' => Yii::t('FcmPushModule.base', 'Disable AuthChoices on iOS App'),
'fileAssetlinksJson' => Yii::t('FcmPushModule.base', 'Well-known file {fileName}', ['fileName' => '"' . self::FILE_ASSET_LINKS . '"']),
'fileAppleAppSiteAssociation' => Yii::t('FcmPushModule.base', 'Well-known file {fileName}', ['fileName' => '"' . self::FILE_APPLE_ASSOCIATION . '"']),
];
}

public function attributeHints()
{
return [
'humhubInstallId' => 'Use this ID to register your API Key.',
'serverKey' => 'Please switch to the new "Firebase Cloud Messaging API (V1)" and enter a JSON file in the field above. The old legacy API is only temporarily available for existing installations and is no longer supported or maintained. ',
'json' => 'Paste the content of the service account JSON files here. You can find more information in the module instructions.'
'humhubInstallId' => Yii::t('FcmPushModule.base', 'Use this ID to register your API Key.'),
'serverKey' => Yii::t('FcmPushModule.base', 'Please switch to the new "Firebase Cloud Messaging API (V1)" and enter a JSON file in the field above. The old legacy API is only temporarily available for existing installations and is no longer supported or maintained.'),
'json' => Yii::t('FcmPushModule.base', 'Paste the content of the service account JSON files here. You can find more information in the module instructions.'),
];
}

Expand All @@ -170,7 +181,8 @@ public function loadSettings()
$this->serverKey = $settings->get('serverKey');
$this->humhubApiKey = $settings->get('humhubApiKey');
$this->disableAuthChoicesIos = $settings->get('disableAuthChoicesIos');

$this->fileAssetlinksJson = $this->getFileContent(self::FILE_ASSET_LINKS);
$this->fileAppleAppSiteAssociation = $this->getFileContent(self::FILE_APPLE_ASSOCIATION);

return true;
}
Expand All @@ -186,6 +198,8 @@ public function saveSettings()
$module->settings->set('serverKey', $this->serverKey);
$module->settings->set('humhubApiKey', $this->humhubApiKey);
$module->settings->set('disableAuthChoicesIos', $this->disableAuthChoicesIos);
$this->saveFile(self::FILE_ASSET_LINKS, $this->fileAssetlinksJson);
$this->saveFile(self::FILE_APPLE_ASSOCIATION, $this->fileAppleAppSiteAssociation);

return true;
}
Expand All @@ -197,9 +211,46 @@ public function getJsonAsArray()

public static function getInstance()
{
$config = new static;
$config = new static();
$config->loadSettings();

return $config;
}

protected function getFilePath(string $fileName): string
{
return Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . '.well-known' . DIRECTORY_SEPARATOR . $fileName;
}

protected function getFileContent(string $fileName): string
{
$filePath = $this->getFilePath($fileName);
return is_file($filePath) ? file_get_contents($filePath) : '';
}

protected function saveFile(string $fileName, ?string $content): bool
{
$filePath = $this->getFilePath($fileName);
if (!file_exists($filePath)) {
if (empty($content) && $content !== '0') {
// Don't create a file with empty content
return true;
}

$dirPath = dirname($filePath);
try {
FileHelper::createDirectory($dirPath);
} catch (Exception $ex) {
Yii::error('Cannot create the dir ' . $dirPath . ' Error: ' . $ex->getMessage(), 'fcm-push');
}
}

try {
return (bool) file_put_contents($filePath, $content);
} catch (Exception $ex) {
Yii::error('Cannot update the file ' . $filePath . ' Error: ' . $ex->getMessage(), 'fcm-push');
}

return false;
}
}
6 changes: 2 additions & 4 deletions views/admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,17 @@
<?= $form->beginCollapsibleFields('Advanced Settings'); ?>
<?= $form->field($model, 'disableAuthChoicesIos')->checkbox()
->label(Yii::t('FcmPushModule.base', 'Hide third-party login options for app users with iOS.')) ?>
<?= $form->field($model, 'fileAssetlinksJson')->textarea(['rows' => 10]) ?>
<?= $form->field($model, 'fileAppleAppSiteAssociation')->textarea(['rows' => 10]) ?>
<?= $form->endCollapsibleFields(); ?>
<br/>

<div class="form-group">
<?= Html::submitButton(Yii::t('base', 'Save'), ['class' => 'btn btn-primary', 'data-ui-loader' => '']) ?>
</div>


<?php ActiveForm::end(); ?>




<?= Html::a('Mobile App Debug', ['/fcm-push/mobile-app'], ['class' => 'pull-right']); ?>
</div>
</div>

0 comments on commit 82bde3a

Please sign in to comment.