From 74b57663f0ebd5c4c5f0f16e13025f43841bde2a Mon Sep 17 00:00:00 2001 From: Yuriy Bakhtin Date: Fri, 27 Sep 2024 15:18:22 +0300 Subject: [PATCH] Add an event trigger after send an invitation email (#7229) * Add an event trigger after send an invitation email * Hide invitations with unknown sources * Fix resending to all core invitations --- CHANGELOG.md | 1 + .../PendingRegistrationsController.php | 52 ++++++++----------- .../admin/controllers/UserController.php | 3 +- .../models/PendingRegistrationSearch.php | 4 +- .../humhub/modules/user/models/Invite.php | 20 +++++++ 5 files changed, 47 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af3b081b9c..ebd00750cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ HumHub Changelog - Fix #7222: Fix rendering of checkbox on MacOS and iOS - Fix #7225: Fix module JS config initialisation on AJAX request - Fix #7227: Fix search reindexing after create new content +- Enh #7229: Hide invitations with unknown sources 1.16.2 (September 5, 2024) -------------------------- diff --git a/protected/humhub/modules/admin/controllers/PendingRegistrationsController.php b/protected/humhub/modules/admin/controllers/PendingRegistrationsController.php index 10e2e6331c..8e9e38ec1d 100644 --- a/protected/humhub/modules/admin/controllers/PendingRegistrationsController.php +++ b/protected/humhub/modules/admin/controllers/PendingRegistrationsController.php @@ -53,9 +53,6 @@ class PendingRegistrationsController extends Controller ]; } - /** - * @inheritdoc - */ public function actionIndex() { $searchModel = new PendingRegistrationSearch(); @@ -64,12 +61,7 @@ class PendingRegistrationsController extends Controller return $this->render('index', [ 'dataProvider' => $dataProvider, 'searchModel' => $searchModel, - 'types' => [ - null => null, - PendingRegistrationSearch::SOURCE_INVITE => Yii::t('AdminModule.base', 'Invite by email'), - PendingRegistrationSearch::SOURCE_INVITE_BY_LINK => Yii::t('AdminModule.base', 'Invite by link'), - PendingRegistrationSearch::SOURCE_SELF => Yii::t('AdminModule.base', 'Sign up'), - ], + 'types' => [null => null] + $searchModel->getAllowedSources(), ]); } @@ -100,7 +92,7 @@ class PendingRegistrationsController extends Controller } /** - * Resend a invite + * Resend an invitation * * @param int $id * @return string @@ -111,18 +103,18 @@ class PendingRegistrationsController extends Controller $this->forcePostRequest(); $invite = $this->findInviteById($id); if (Yii::$app->request->isPost) { - $invite->sendInviteMail(); - $this->view->success(Yii::t( - 'AdminModule.user', - 'Resend invitation email', - )); + if ($invite->sendInviteMail()) { + $this->view->success(Yii::t('AdminModule.user', 'Resend invitation email')); + } else { + $this->view->error(Yii::t('AdminModule.user', 'Cannot resend invitation email!')); + } return $this->redirect(['index']); } return $this->render('resend', ['model' => $invite]); } /** - * Delete an invite + * Delete an invitation * * @param int $id * @return string @@ -134,20 +126,20 @@ class PendingRegistrationsController extends Controller $this->forcePostRequest(); $invite = $this->findInviteById($id); if (Yii::$app->request->isPost) { - $invite->delete(); - $this->view->success(Yii::t( - 'AdminModule.user', - 'Deleted invitation', - )); + if ($invite->delete()) { + $this->view->success(Yii::t( + 'AdminModule.user', + 'Deleted invitation', + )); + } return $this->redirect(['index']); } return $this->render('delete', ['model' => $invite]); } /** - * Delete all invitations + * Resend all invitations * - * @param int $id * @return string * @throws HttpException * @throws Throwable @@ -155,7 +147,7 @@ class PendingRegistrationsController extends Controller public function actionResendAll() { if (Yii::$app->request->isPost) { - foreach (Invite::find()->each() as $invite) { + foreach (Invite::find()->where(Invite::filterSource())->each() as $invite) { $invite->sendInviteMail(); } @@ -178,7 +170,7 @@ class PendingRegistrationsController extends Controller public function actionDeleteAll() { if (Yii::$app->request->isPost) { - Invite::deleteAll(); + Invite::deleteAll(Invite::filterSource()); $this->view->success(Yii::t( 'AdminModule.user', @@ -189,9 +181,8 @@ class PendingRegistrationsController extends Controller } /** - * Delete all or selected invitation + * Resend all or selected invitation * - * @param int $id * @return string * @throws HttpException * @throws Throwable @@ -202,7 +193,7 @@ class PendingRegistrationsController extends Controller $ids = Yii::$app->request->post('id'); if (!empty($ids)) { - foreach (Invite::findAll(['id' => $ids]) as $invite) { + foreach (Invite::findAll(['id' => $ids] + Invite::filterSource()) as $invite) { $invite->sendInviteMail(); } $this->view->success(Yii::t( @@ -217,7 +208,6 @@ class PendingRegistrationsController extends Controller /** * Delete all or selected invitation * - * @param int $id * @return string * @throws HttpException * @throws Throwable @@ -227,7 +217,7 @@ class PendingRegistrationsController extends Controller if (Yii::$app->request->isPost) { $ids = Yii::$app->request->post('id'); if (!empty($ids)) { - foreach (Invite::findAll(['id' => $ids]) as $invite) { + foreach (Invite::findAll(['id' => $ids] + Invite::filterSource()) as $invite) { $invite->delete(); } $this->view->success(Yii::t( @@ -275,7 +265,7 @@ class PendingRegistrationsController extends Controller */ private function findInviteById($id) { - $invite = Invite::findOne(['id' => $id]); + $invite = Invite::findOne(['id' => $id] + Invite::filterSource()); if ($invite === null) { throw new HttpException(404, Yii::t( 'AdminModule.user', diff --git a/protected/humhub/modules/admin/controllers/UserController.php b/protected/humhub/modules/admin/controllers/UserController.php index f29d84e3c3..313ca37d95 100644 --- a/protected/humhub/modules/admin/controllers/UserController.php +++ b/protected/humhub/modules/admin/controllers/UserController.php @@ -85,7 +85,8 @@ class UserController extends Controller $searchModel = new UserSearch(); $searchModel->status = User::STATUS_ENABLED; $dataProvider = $searchModel->search(Yii::$app->request->queryParams); - $showPendingRegistrations = (Invite::find()->count() > 0 && Yii::$app->user->can([new ManageUsers(), new ManageGroups()])); + $showPendingRegistrations = Invite::find()->where(Invite::filterSource())->exists() && + Yii::$app->user->can([ManageUsers::class, ManageGroups::class]); return $this->render('list', [ 'dataProvider' => $dataProvider, diff --git a/protected/humhub/modules/admin/models/PendingRegistrationSearch.php b/protected/humhub/modules/admin/models/PendingRegistrationSearch.php index 9cc5afd025..b00964ce2b 100644 --- a/protected/humhub/modules/admin/models/PendingRegistrationSearch.php +++ b/protected/humhub/modules/admin/models/PendingRegistrationSearch.php @@ -50,7 +50,9 @@ class PendingRegistrationSearch extends Invite */ public function search($params = []) { - $query = self::find()->joinWith(['originator']); + $query = self::find() + ->joinWith(['originator']) + ->andWhere(self::filterSource()); $dataProvider = new ActiveDataProvider([ 'query' => $query, diff --git a/protected/humhub/modules/user/models/Invite.php b/protected/humhub/modules/user/models/Invite.php index 6f98fa49f5..bbe94ddc28 100644 --- a/protected/humhub/modules/user/models/Invite.php +++ b/protected/humhub/modules/user/models/Invite.php @@ -57,6 +57,8 @@ class Invite extends ActiveRecord */ public $skipCaptchaValidation = false; + protected ?array $allowedSources = null; + /** * @inheritdoc */ @@ -325,4 +327,22 @@ class Invite extends ActiveRecord { return $this->hasOne(User::class, ['id' => 'updated_by']); } + + public function getAllowedSources(): array + { + if ($this->allowedSources === null) { + $this->allowedSources = [ + self::SOURCE_INVITE => Yii::t('AdminModule.base', 'Invite by email'), + self::SOURCE_INVITE_BY_LINK => Yii::t('AdminModule.base', 'Invite by link'), + self::SOURCE_SELF => Yii::t('AdminModule.base', 'Sign up'), + ]; + } + + return $this->allowedSources; + } + + public static function filterSource(): array + { + return ['source' => array_keys((new static())->getAllowedSources())]; + } }