From 6043b2f30f81cc56aaf466cecd2fd87e4fb0d548 Mon Sep 17 00:00:00 2001 From: gevorgmansuryan Date: Fri, 20 Sep 2024 02:23:01 +0400 Subject: [PATCH] Added new `resend` and `cancel` endpoints for user invite --- Events.php | 2 ++ controllers/user/InviteController.php | 29 ++++++++++++++++++++++++++- definitions/InviteDefinitions.php | 2 ++ docs/swagger/user.yaml | 27 +++++++++++++++++++++++++ module.json | 2 +- tests/config/test.php | 1 + 6 files changed, 61 insertions(+), 2 deletions(-) diff --git a/Events.php b/Events.php index e7ee16a..bb68b85 100644 --- a/Events.php +++ b/Events.php @@ -84,6 +84,8 @@ public static function onBeforeRequest($event) // User: Invite Controller ['pattern' => 'user/invite', 'route' => 'rest/user/invite/index', 'verb' => 'POST'], ['pattern' => 'user/invite', 'route' => 'rest/user/invite/list', 'verb' => 'GET'], + ['pattern' => 'user/invite/', 'route' => 'rest/user/invite/cancel', 'verb' => 'DELETE'], + ['pattern' => 'user/invite/', 'route' => 'rest/user/invite/resend', 'verb' => 'PATCH'], // User: Session Controller ['pattern' => 'user/session/all/', 'route' => 'rest/user/session/delete-from-user', 'verb' => 'DELETE'], diff --git a/controllers/user/InviteController.php b/controllers/user/InviteController.php index 2736826..51b2825 100644 --- a/controllers/user/InviteController.php +++ b/controllers/user/InviteController.php @@ -30,7 +30,7 @@ protected function getAccessRules() public function actionIndex() { - $emails = (array)Yii::$app->request->post('emails'); + $emails = (array) Yii::$app->request->post('emails'); if (!$emails) { return $this->returnError(404, 'Please provide an array of emails in the json format'); } @@ -70,6 +70,33 @@ public function actionList() return $this->returnPagination($query, $pagination, $results); } + public function actionResend($id) + { + $userInvite = Invite::find()->where(['id' => $id, 'source' => Invite::SOURCE_INVITE])->one(); + + if (!$userInvite) { + return $this->returnError(404, 'Invite not found!'); + } + + $userInvite->save(); + $userInvite->sendInviteMail(); + + return $this->returnSuccess('Invite has been resent.'); + } + + public function actionCancel($id) + { + $userInvite = Invite::find()->where(['id' => $id, 'source' => Invite::SOURCE_INVITE])->one(); + + if (!$userInvite) { + return $this->returnError(404, 'Invite not found!'); + } + + $userInvite->delete(); + + return $this->returnSuccess('Invite has been canceled.'); + } + protected function createInvite($email) { $userInvite = new Invite(); diff --git a/definitions/InviteDefinitions.php b/definitions/InviteDefinitions.php index bac3dec..568dbe3 100644 --- a/definitions/InviteDefinitions.php +++ b/definitions/InviteDefinitions.php @@ -3,6 +3,7 @@ namespace humhub\modules\rest\definitions; use humhub\modules\rest\models\Invite; +use yii\helpers\Url; class InviteDefinitions { @@ -15,6 +16,7 @@ public static function getInvite(Invite $invite) 'lastname' => $invite->lastname, 'language' => $invite->language, 'space' => $invite->space ? SpaceDefinitions::getSpaceShort($invite->space) : null, + 'invitationUrl' => Url::to(['/user/registration', 'token' => $invite->token], true), 'originator' => $invite->originator ? UserDefinitions::getUserShort($invite->originator) : null, 'createdBy' => $invite->createdBy ? UserDefinitions::getUserShort($invite->createdBy) : null, 'updatedBy' => $invite->updatedBy ? UserDefinitions::getUserShort($invite->updatedBy) : null, diff --git a/docs/swagger/user.yaml b/docs/swagger/user.yaml index 945d698..4429e84 100644 --- a/docs/swagger/user.yaml +++ b/docs/swagger/user.yaml @@ -516,6 +516,29 @@ paths: type: array items: $ref: "user.yaml#/definitions/Invite" + '/user/invite/{id}': + patch: + tags: + - Invite + summary: Resend invite + description: Resends invitation email + produces: + - application/json + - application/xml + responses: + '200': + description: Successful operation + delete: + tags: + - Invite + summary: Cancel invite + description: Cancels the invite, making the invitation URL no longer valid. + produces: + - application/json + - application/xml + responses: + '200': + description: Successful operation #----------------------------------------------------------------------------------------------------------------------- # Begin Session @@ -789,6 +812,10 @@ definitions: space: readOnly: true $ref: "space.yaml#/definitions/SpaceShort" + invitationUrl: + type: string + readOnly: true + example: "http://example.com/user/registration?token=cK4FCaopFHNP" originator: readOnly: true $ref: "user.yaml#/definitions/UserShort" diff --git a/module.json b/module.json index cfee703..41f7c1c 100644 --- a/module.json +++ b/module.json @@ -5,7 +5,7 @@ "keywords": [ "api", "rest" ], - "version": "0.10.3", + "version": "0.10.4", "homepage": "https://github.com/humhub/rest", "humhub": { "minVersion": "1.16" diff --git a/tests/config/test.php b/tests/config/test.php index 726273e..9970bed 100644 --- a/tests/config/test.php +++ b/tests/config/test.php @@ -8,6 +8,7 @@ return [ 'modules' => ['rest'], + 'humhub_root' => '/app/humhub', 'fixtures' => [ 'default', 'humhub\modules\rest\tests\codeception\fixtures\ActivityFixture',