Skip to content

Commit 3f0cce9

Browse files
Merge pull request #438 from Drupalcz/feature/slack_invite_part2_move
Slack invite move
2 parents bd8b471 + 35b3749 commit 3f0cce9

14 files changed

Lines changed: 595 additions & 48 deletions

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
"drupal/seckit": "^2.0",
5858
"drupal/security_review": "1.x-dev",
5959
"drupal/shield": "^1.2",
60-
"drupal/slack_invite": "^2.0",
6160
"drupal/stage_file_proxy": "^1.0@RC",
6261
"drupal/token": "^1.5",
6362
"drupal/upgrade_status": "^3.0@beta",

composer.lock

Lines changed: 3 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
token: ""
2+
hostname: ""
3+
twostep:
4+
enabled: FALSE
5+
channel: ""
6+
message: "!email has requested an invitation to the Slack team. Click the following link to approve: !url"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: Slack Invite
2+
type: module
3+
description: 'Invite your users to your slack team'
4+
package: Custom
5+
core_version_requirement: ^9 || ^10 || ^11
6+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
slack_invite.settingss:
2+
title: 'Slack Invite'
3+
parent: system.admin_config_services
4+
description: 'Slack Invite Settings'
5+
route_name: slack_invite.settings
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Primarily Drupal hooks and global API functions to manipulate slack invite.
6+
*
7+
* This is the main module file for Slack Invite.
8+
*/
9+
10+
11+
define('SLACK_INVITE_ALREADY_IN_TEAM', 'already_in_team');
12+
define('SLACK_INVITE_SENT_RECENTLY', 'sent_recently');
13+
define('SLACK_INVITE_ALREADY_INVITED', 'already_invited');
14+
15+
/**
16+
* Sends slack invite to email address.
17+
*/
18+
function _slack_invite_send_invite($email) {
19+
$team_hostname = variable_get('slack_invite_hostname', '');
20+
$api_url = "https://{$team_hostname}.slack.com/api/users.admin.invite?t=" . time();
21+
22+
$data = [
23+
'_attempts' => 1,
24+
'email' => $email,
25+
'set_active' => 'true',
26+
'token' => variable_get('slack_invite_token', ''),
27+
];
28+
29+
$data['channels'] = variable_get('slack_invite_channels', '');
30+
if (empty($data['channels'])) {
31+
unset($data['channels']);
32+
}
33+
34+
$data = drupal_http_build_query($data);
35+
$options = [
36+
'method' => 'POST',
37+
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
38+
'data' => $data,
39+
];
40+
41+
return drupal_http_request("{$api_url}", $options);
42+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
administer slack invite:
2+
title: 'Administer Slack Invite Settings.'
3+
description: 'Administer the Slack Invite Settings.'
4+
5+
approve slack invite:
6+
title: 'Approve Slack Invites.'
7+
description: 'Approve two-step Slack invitations.'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
slack_invite.settings:
2+
path: '/admin/config/services/slack-invite'
3+
defaults:
4+
_form: 'Drupal\slack_invite\Form\SlackInviteSettingsForm'
5+
_title: 'Slack Invite Settings'
6+
requirements:
7+
_permission: 'administer slack invite'
8+
9+
slack_invite.twostep:
10+
path: '/slack_invite/{email}/{token}'
11+
defaults:
12+
_form: 'Drupal\slack_invite\Form\SlackInviteTwoStepApproveForm'
13+
_title: 'Approve Slack invitation'
14+
requirements:
15+
_custom_access: 'Drupal\slack_invite\Form\SlackInviteTwoStepApproveForm::access'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
services:
2+
slack_invite:
3+
class: Drupal\slack_invite\SlackInvite
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* @file
4+
* Contains \Drupal\slack_invite\Form\SlackInviteForm.
5+
*/
6+
7+
namespace Drupal\slack_invite\Form;
8+
9+
use Drupal\Core\Http\Client;
10+
use Drupal\Core\Form\FormBase;
11+
use Drupal\Core\Form\FormStateInterface;
12+
use Exception;
13+
use Drupal\Core\Url;
14+
15+
/**
16+
* Builds the search form for the search block.
17+
*/
18+
class SlackInviteForm extends FormBase {
19+
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function getFormID() {
24+
return 'slack_invite_form';
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function buildForm(array $form, FormStateInterface $form_state) {
31+
$config = $this->config('slack_invite.settings');
32+
33+
$form['#action'] = Url::fromRoute('<current>', ['query' => $this->getDestinationArray(), 'external' => FALSE])->toString();
34+
$form['slack_email'] = [
35+
'#type' => 'textfield',
36+
'#title' => $this->t('Email'),
37+
'#description' => $this->t('Enter email address for slack invite'),
38+
'#required' => TRUE,
39+
];
40+
41+
$form['actions'] = [
42+
'#type' => 'actions',
43+
];
44+
$form['actions']['submit'] = [
45+
'#type' => 'submit',
46+
'#value' => $this->t('Send')
47+
];
48+
return $form;
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function validateForm(array &$form, FormStateInterface $form_state) {
55+
$email = $form_state->getValue('slack_email');
56+
if (!\Drupal::service('email.validator')->isValid($email)) {
57+
$form_state->setErrorByName('slack_email', $this->t('Enter email address in valid format (ex. example@example.com)'));
58+
}
59+
}
60+
61+
/**
62+
* {@inheritdoc}
63+
*/
64+
public function submitForm(array &$form, FormStateInterface $form_state) {
65+
$slack_invite = \Drupal::service('slack_invite');
66+
$slack_invite->send($form_state->getValue('slack_email'));
67+
}
68+
}

0 commit comments

Comments
 (0)