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
This commit is contained in:
Yuriy Bakhtin 2024-09-27 15:18:22 +03:00 committed by GitHub
parent 590238d1a2
commit 74b57663f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 47 additions and 33 deletions

View File

@ -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)
--------------------------

View File

@ -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',

View File

@ -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,

View File

@ -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,

View File

@ -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())];
}
}