From 5781d3f736006dfc0650151e87f1c55046c09d51 Mon Sep 17 00:00:00 2001 From: James Edmonston Date: Wed, 10 Mar 2021 18:16:32 +0000 Subject: [PATCH] Add activateUser mutation --- src/models/Settings.php | 1 + src/services/UserService.php | 22 ++++++++++++++++++++++ src/templates/_sections/messages.twig | 7 +++++++ 3 files changed, 30 insertions(+) diff --git a/src/models/Settings.php b/src/models/Settings.php index ebf43a9..24e27f3 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -59,6 +59,7 @@ class Settings extends Model public $invalidRefreshToken = 'Invalid Refresh Token'; public $invalidEmailAddress = 'Invalid email address'; + public $userActivated = 'Successfully activated user'; public $passwordSaved = 'Successfully saved password'; public $passwordUpdated = 'Successfully updated password'; public $passwordResetSent = 'You will receive an email if it matches an account in our system'; diff --git a/src/services/UserService.php b/src/services/UserService.php index aea3481..c29327b 100644 --- a/src/services/UserService.php +++ b/src/services/UserService.php @@ -210,6 +210,28 @@ public function registerGqlMutations(Event $event) }, ]; + $event->mutations['activateUser'] = [ + 'description' => 'Activates user. Requires `code` and `id` from Craft activation email. Returns success message.', + 'type' => Type::nonNull(Type::string()), + 'args' => [ + 'code' => Type::nonNull(Type::string()), + 'id' => Type::nonNull(Type::string()), + ], + 'resolve' => function ($source, array $arguments) use ($users, $settings, $errorService) { + $code = $arguments['code']; + $id = $arguments['id']; + + $user = $users->getUserByUid($id); + + if (!$user || !$users->isVerificationCodeValidForUser($user, $code)) { + $errorService->throw($settings->invalidRequest, 'INVALID'); + } + + $users->activateUser($user); + return $settings->userActivated; + }, + ]; + $event->mutations['setPassword'] = [ 'description' => 'Sets password for unauthenticated user. Requires `code` and `id` from Craft reset password email. Returns success message.', 'type' => Type::nonNull(Type::string()), diff --git a/src/templates/_sections/messages.twig b/src/templates/_sections/messages.twig index 8fc4681..71a7428 100644 --- a/src/templates/_sections/messages.twig +++ b/src/templates/_sections/messages.twig @@ -10,6 +10,13 @@

Responses

+{{ forms.textField({ + label: 'User Activated', + name: 'userActivated', + value: settings.userActivated, + required: true, +}) }} + {{ forms.textField({ label: 'Password Saved', name: 'passwordSaved',