diff --git a/composer.json b/composer.json index 22dc228ae..ddd896fc7 100644 --- a/composer.json +++ b/composer.json @@ -57,7 +57,6 @@ "drupal/seckit": "^2.0", "drupal/security_review": "1.x-dev", "drupal/shield": "^1.2", - "drupal/slack_invite": "^2.0", "drupal/stage_file_proxy": "^1.0@RC", "drupal/token": "^1.5", "drupal/upgrade_status": "^3.0@beta", diff --git a/composer.lock b/composer.lock index a7119d2a0..c485bef44 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "777a905ed872ba8260f75187b94e050d", + "content-hash": "6d62439fa858dbb8f0d5b7c2f2ca570c", "packages": [ { "name": "acquia/memcache-settings", @@ -5498,50 +5498,6 @@ "source": "https://git.drupalcode.org/project/shield" } }, - { - "name": "drupal/slack_invite", - "version": "2.0.0", - "source": { - "type": "git", - "url": "https://git.drupalcode.org/project/slack_invite.git", - "reference": "2.0.0" - }, - "dist": { - "type": "zip", - "url": "https://ftp.drupal.org/files/projects/slack_invite-2.0.0.zip", - "reference": "2.0.0", - "shasum": "9d3ace0798782dc1e9421bd53d73c7222d5cce42" - }, - "require": { - "drupal/core": "^8.8 || ^9" - }, - "type": "drupal-module", - "extra": { - "drupal": { - "version": "2.0.0", - "datestamp": "1605030284", - "security-coverage": { - "status": "covered", - "message": "Covered by Drupal's security advisory policy" - } - } - }, - "notification-url": "https://packages.drupal.org/8/downloads", - "license": [ - "GPL-2.0-or-later" - ], - "authors": [ - { - "name": "BTMash", - "homepage": "https://www.drupal.org/user/60422" - } - ], - "description": "Invite your users to your slack team", - "homepage": "https://www.drupal.org/project/slack_invite", - "support": { - "source": "https://git.drupalcode.org/project/slack_invite" - } - }, { "name": "drupal/stage_file_proxy", "version": "1.2.0", @@ -11507,6 +11463,6 @@ "platform": { "php": ">=7.4" }, - "platform-dev": [], - "plugin-api-version": "2.3.0" + "platform-dev": {}, + "plugin-api-version": "2.9.0" } diff --git a/docroot/modules/custom/slack_invite/config/install/slack_invite.settings.yml b/docroot/modules/custom/slack_invite/config/install/slack_invite.settings.yml new file mode 100644 index 000000000..96dfc4883 --- /dev/null +++ b/docroot/modules/custom/slack_invite/config/install/slack_invite.settings.yml @@ -0,0 +1,6 @@ +token: "" +hostname: "" +twostep: + enabled: FALSE + channel: "" + message: "!email has requested an invitation to the Slack team. Click the following link to approve: !url" diff --git a/docroot/modules/custom/slack_invite/slack_invite.info.yml b/docroot/modules/custom/slack_invite/slack_invite.info.yml new file mode 100644 index 000000000..8895a611a --- /dev/null +++ b/docroot/modules/custom/slack_invite/slack_invite.info.yml @@ -0,0 +1,6 @@ +name: Slack Invite +type: module +description: 'Invite your users to your slack team' +package: Custom +core_version_requirement: ^9 || ^10 || ^11 + diff --git a/docroot/modules/custom/slack_invite/slack_invite.links.menu.yml b/docroot/modules/custom/slack_invite/slack_invite.links.menu.yml new file mode 100644 index 000000000..4fde6ca0e --- /dev/null +++ b/docroot/modules/custom/slack_invite/slack_invite.links.menu.yml @@ -0,0 +1,5 @@ +slack_invite.settingss: + title: 'Slack Invite' + parent: system.admin_config_services + description: 'Slack Invite Settings' + route_name: slack_invite.settings diff --git a/docroot/modules/custom/slack_invite/slack_invite.module b/docroot/modules/custom/slack_invite/slack_invite.module new file mode 100644 index 000000000..1b4ee3e2f --- /dev/null +++ b/docroot/modules/custom/slack_invite/slack_invite.module @@ -0,0 +1,42 @@ + 1, + 'email' => $email, + 'set_active' => 'true', + 'token' => variable_get('slack_invite_token', ''), + ]; + + $data['channels'] = variable_get('slack_invite_channels', ''); + if (empty($data['channels'])) { + unset($data['channels']); + } + + $data = drupal_http_build_query($data); + $options = [ + 'method' => 'POST', + 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'], + 'data' => $data, + ]; + + return drupal_http_request("{$api_url}", $options); +} diff --git a/docroot/modules/custom/slack_invite/slack_invite.permissions.yml b/docroot/modules/custom/slack_invite/slack_invite.permissions.yml new file mode 100644 index 000000000..47869f4a7 --- /dev/null +++ b/docroot/modules/custom/slack_invite/slack_invite.permissions.yml @@ -0,0 +1,7 @@ +administer slack invite: + title: 'Administer Slack Invite Settings.' + description: 'Administer the Slack Invite Settings.' + +approve slack invite: + title: 'Approve Slack Invites.' + description: 'Approve two-step Slack invitations.' diff --git a/docroot/modules/custom/slack_invite/slack_invite.routing.yml b/docroot/modules/custom/slack_invite/slack_invite.routing.yml new file mode 100644 index 000000000..b9e16fe59 --- /dev/null +++ b/docroot/modules/custom/slack_invite/slack_invite.routing.yml @@ -0,0 +1,15 @@ +slack_invite.settings: + path: '/admin/config/services/slack-invite' + defaults: + _form: 'Drupal\slack_invite\Form\SlackInviteSettingsForm' + _title: 'Slack Invite Settings' + requirements: + _permission: 'administer slack invite' + +slack_invite.twostep: + path: '/slack_invite/{email}/{token}' + defaults: + _form: 'Drupal\slack_invite\Form\SlackInviteTwoStepApproveForm' + _title: 'Approve Slack invitation' + requirements: + _custom_access: 'Drupal\slack_invite\Form\SlackInviteTwoStepApproveForm::access' diff --git a/docroot/modules/custom/slack_invite/slack_invite.services.yml b/docroot/modules/custom/slack_invite/slack_invite.services.yml new file mode 100644 index 000000000..389293e56 --- /dev/null +++ b/docroot/modules/custom/slack_invite/slack_invite.services.yml @@ -0,0 +1,3 @@ +services: + slack_invite: + class: Drupal\slack_invite\SlackInvite diff --git a/docroot/modules/custom/slack_invite/src/Form/SlackInviteForm.php b/docroot/modules/custom/slack_invite/src/Form/SlackInviteForm.php new file mode 100644 index 000000000..5fdcc0e8b --- /dev/null +++ b/docroot/modules/custom/slack_invite/src/Form/SlackInviteForm.php @@ -0,0 +1,68 @@ +config('slack_invite.settings'); + + $form['#action'] = Url::fromRoute('', ['query' => $this->getDestinationArray(), 'external' => FALSE])->toString(); + $form['slack_email'] = [ + '#type' => 'textfield', + '#title' => $this->t('Email'), + '#description' => $this->t('Enter email address for slack invite'), + '#required' => TRUE, + ]; + + $form['actions'] = [ + '#type' => 'actions', + ]; + $form['actions']['submit'] = [ + '#type' => 'submit', + '#value' => $this->t('Send') + ]; + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + $email = $form_state->getValue('slack_email'); + if (!\Drupal::service('email.validator')->isValid($email)) { + $form_state->setErrorByName('slack_email', $this->t('Enter email address in valid format (ex. example@example.com)')); + } + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $slack_invite = \Drupal::service('slack_invite'); + $slack_invite->send($form_state->getValue('slack_email')); + } +} diff --git a/docroot/modules/custom/slack_invite/src/Form/SlackInviteSettingsForm.php b/docroot/modules/custom/slack_invite/src/Form/SlackInviteSettingsForm.php new file mode 100644 index 000000000..d9801ec63 --- /dev/null +++ b/docroot/modules/custom/slack_invite/src/Form/SlackInviteSettingsForm.php @@ -0,0 +1,166 @@ +config('slack_invite.settings'); + + $form['slack_invite_token'] = [ + '#type' => 'textfield', + '#title' => $this->t('Slack Web API Token'), + '#description' => $this->t("Enter the Web API token you get from your team domain. Please ensure that the token has the required 'admin' scope to send out email invites."), + '#default_value' => $config->get('token'), + '#required' => TRUE, + ]; + + $form['slack_invite_hostname'] = [ + '#type' => 'textfield', + '#title' => $this->t('Slack Domain Hostname'), + '#description' => $this->t('Enter your slack team domain (ex. if your domain is https://drupal.slack.com, you would enter "drupal" minus the quotations).'), + '#default_value' => $config->get('hostname'), + '#required' => TRUE, + ]; + + $form['slack_bypass_check'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Bypass credential check'), + '#description' => $this->t('Bypass checking that token and hostname combination are valid'), + ]; + + $form['slack_twostep'] = [ + '#type' => 'details', + '#title' => $this->t('Two-step approval'), + '#open' => TRUE, + '#tree' => TRUE, + ]; + + $twostep = $config->get('twostep'); + $form['slack_twostep']['enabled'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Enable two-step approval'), + '#default_value' => $twostep['enabled'], + ]; + + $form['slack_twostep']['channel'] = [ + '#type' => 'textfield', + '#title' => $this->t('Channel'), + '#description' => $this->t('A slack channel name or id in which the invitation approval request will be sent'), + '#default_value' => $twostep['channel'], + '#states' => [ + 'invisible' => [ + ':input[name="slack_twostep[enabled]"]' => ['checked' => FALSE], + ], + ], + ]; + + $form['slack_twostep']['message'] = [ + '#type' => 'textfield', + '#title' => $this->t('Message'), + '#description' => $this->t('A message to sent to the above channel'), + '#default_value' => $twostep['message'], + '#states' => [ + 'invisible' => [ + ':input[name="slack_twostep[enabled]"]' => ['checked' => FALSE], + ], + ], + ]; + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + if ($form_state->getValue('slack_twostep')['enabled']) { + // Validate channel. + if (empty($form_state->getValue('slack_twostep')['channel'])) { + $form_state->setError($form['slack_twostep']['channel'], $this->t('Channel field is required when two-step approval is enabled.')); + } + + // Validate message. + if (empty($form_state->getValue('slack_twostep')['message'])) { + $form_state->setError($form['slack_twostep']['message'], $this->t('Message field is required when two-step approval is enabled.')); + } + } + + if ($form_state->getValue('slack_bypass_check')) { + return; + } + $team_hostname = $form_state->getValue('slack_invite_hostname'); + $token = $form_state->getValue('slack_invite_token'); + $api_url = "https://{$team_hostname}.slack.com/api/api.test"; + + $data = [ + 'form_params' => [ + '_attempts' => 1, + 'token' => $token, + ], + ]; + + try { + $client = \Drupal::httpClient(); + $response = $client->request('POST', $api_url, $data); + // Expected result. + $response_data = json_decode('' . $response->getBody()); + if ($response_data->ok !== TRUE) { + throw new Exception($this->t('Please check the token and hostname; unable to test request')); + } + // Ensure the correct scope is set. + if ($response->hasHeader('x-oauth-scopes')) { + $scopes = explode(',', $response->getHeader('x-oauth-scopes')[0]); + if (!in_array('admin', $scopes)) { + throw new Exception($this->t('The supplied token is missing the required scope: admin')); + } + } + } + catch (Exception $e) { + $form_state->setErrorByName('slack_invite_token', $e->getMessage()); + } + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $values = $form_state->getValues(); + + $config = $this->config('slack_invite.settings'); + $config->set('token', $values['slack_invite_token']) + ->set('hostname', $values['slack_invite_hostname']) + ->set('twostep', $values['slack_twostep']) + ->save(); + parent::submitForm($form, $form_state); + } + +} diff --git a/docroot/modules/custom/slack_invite/src/Form/SlackInviteTwoStepApproveForm.php b/docroot/modules/custom/slack_invite/src/Form/SlackInviteTwoStepApproveForm.php new file mode 100644 index 000000000..fb05c6323 --- /dev/null +++ b/docroot/modules/custom/slack_invite/src/Form/SlackInviteTwoStepApproveForm.php @@ -0,0 +1,72 @@ +email = \Drupal::routeMatch()->getParameter('email'); + $this->token = \Drupal::routeMatch()->getParameter('token'); + } + + /** + * Access check for form route. + */ + public function access(AccountInterface $account) { + $permission = $account->hasPermission('approve slack invite'); + + $slack_invite = \Drupal::service('slack_invite'); + $token = $this->token == $slack_invite->getEmailToken($this->email); + + return AccessResult::allowedIf($permission && $token); + } + + /** + * {@inheritdoc} + */ + public function getFormID() { + return 'slack_invite_twostep_approve_form'; + } + + /** + * @inheritdoc + */ + public function getCancelUrl() { + return Url::fromRoute(''); + } + + /** + * @inheritdoc + */ + public function getQuestion() { + return $this->t('Are you sure you want to invite %email to the Slack team?', ['%email' => $this->email]); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $slack_invite = \Drupal::service('slack_invite'); + $slack_invite->send(\Drupal::routeMatch()->getParameter('email'), TRUE); + $form_state->setRedirectUrl($this->getCancelUrl()); + } +} diff --git a/docroot/modules/custom/slack_invite/src/Plugin/Block/SlackInviteFormBlock.php b/docroot/modules/custom/slack_invite/src/Plugin/Block/SlackInviteFormBlock.php new file mode 100644 index 000000000..f186920eb --- /dev/null +++ b/docroot/modules/custom/slack_invite/src/Plugin/Block/SlackInviteFormBlock.php @@ -0,0 +1,30 @@ +getForm('Drupal\slack_invite\Form\SlackInviteForm'); + } + +} diff --git a/docroot/modules/custom/slack_invite/src/SlackInvite.php b/docroot/modules/custom/slack_invite/src/SlackInvite.php new file mode 100644 index 000000000..8c5018bba --- /dev/null +++ b/docroot/modules/custom/slack_invite/src/SlackInvite.php @@ -0,0 +1,172 @@ +email = $email; + $this->config = \Drupal::config('slack_invite.settings'); + $method = !$this->config->get('twostep')['enabled'] ? 'sendDirect' : 'sendTwoStep'; + if ($direct == TRUE) { + $method = 'sendDirect'; + } + call_user_func([$this, $method]); + } + + /** + * Send the invite directly to the user. + */ + protected function sendDirect() { + $team_hostname = $this->config->get('hostname'); + $api_url = "https://{$team_hostname}.slack.com/api/users.admin.invite?t=" . time(); + + $data = [ + 'form_params' => [ + '_attempts' => 1, + 'email' => $this->email, + 'set_active' => 'true', + 'token' => $this->config->get('token'), + ], + ]; + + try { + $client = \Drupal::httpClient(); + $response = $client->request('POST', $api_url, $data); + + $response_data = json_decode('' . $response->getBody()); + if ($response_data->ok == TRUE) { + \Drupal::messenger()->addStatus(t('You will receive an email notification inviting you to join the slack team shortly.')); + } + else { + $message = ''; + switch ($response_data->error) { + case SLACK_INVITE_ALREADY_IN_TEAM: + $message = $this->t('The user is already a member of the team'); + break; + + case SLACK_INVITE_SENT_RECENTLY: + $message = $this->t('The user was recently sent an invitation.'); + break; + + case SLACK_INVITE_ALREADY_INVITED: + $message = $this->t('The user is already invited.'); + break; + + default: + $message = $data['error']; + break; + } + \Drupal::messenger()->addStatus($this->t('There was an error sending your invite. Please contact the administrator with the following error details. The error message from slack was: @message', ['@message' => $message])); + } + } + catch (Exception $e) { + \Drupal::messenger()->addError($this->t('Something went wrong with the request. Please contact site administrator.')); + } + } + + /** + * Send a two-step request for invitation to the designated Slack channel. + */ + protected function sendTwoStep() { + $token = $this->getEmailToken($this->email); + $url = Url::fromRoute('slack_invite.twostep', [ + 'email' => $this->email, + 'token' => $token, + ], ['absolute' => TRUE]); + $url = $url->toString(); + + $team_hostname = $this->config->get('hostname'); + $api_url = "https://{$team_hostname}.slack.com/api/chat.postMessage?t=" . time(); + + $message = $this->t($this->config->get('twostep')['message'], [ + '!email' => $this->email, + '!url' => $url, + ]); + + $data = [ + 'form_params' => [ + '_attempts' => 1, + 'token' => $this->config->get('token'), + 'channel' => $this->config->get('twostep')['channel'], + 'text' => $message->render(), + ], + ]; + + try { + $client = \Drupal::httpClient(); + $response = $client->request('POST', $api_url, $data); + + $response_data = json_decode('' . $response->getBody()); + if ($response_data->ok == TRUE) { + \Drupal::messenger()->addStatus($this->t('Your slack invitation request has been made and you will receive an email notification inviting you to join the slack team pending approval.')); + } + else { + $message = ''; + switch ($response_data->error) { + default: + $message = $data['error']; + break; + } + \Drupal::messenger()->addStatus($this->t('There was an error sending your invitation request. Please contact the administrator with the following error details. The error message from slack was: @message', ['@message' => $message])); + } + } + catch (Exception $e) { + \Drupal::messenger()->addError($this->t('Something went wrong with the request. Please contact site administrator.')); + } + } + + /** + * @param $email + * @return string + */ + public function getEmailToken($email) { + // Return the first 8 characters. + return substr(Crypt::hmacBase64($email, $this->getPrivateKey() . $this->getHashSalt()), 0, 8); + } + + /** + * Gets the Drupal private key. + * + * @return string + * The Drupal private key. + */ + protected function getPrivateKey() { + return \Drupal::service('private_key')->get(); + } + + /** + * Gets a salt useful for hardening against SQL injection. + * + * @return string + * A salt based on information in settings.php, not in the database. + * + * @throws \RuntimeException + */ + protected function getHashSalt() { + return Settings::getHashSalt(); + } +}