-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmeteor.module
More file actions
138 lines (121 loc) · 3.67 KB
/
meteor.module
File metadata and controls
138 lines (121 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* @file
* The Meteor SSO support module.
*
* @see OPERATION.md
*/
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\meteor\IdentityListener;
use Drupal\user\UserInterface;
/**
* Send a notification to a Meteor server.
*
* @param string $path
* The path reached in meteor.
* @param array $query
* Optional query params.
*/
function _meteor_notify($path, $query = []) {
/** @var \Drupal\meteor\Notifier $notifier */
$notifier = \Drupal::service('meteor.notifier');
$notifier->notify($path, $query);
}
/**
* Send a notification to a Meteor server about a user event.
*
* @param string $event
* The name of an event: an EventListener string constant.
* @param \Drupal\Core\Session\AccountInterface $account
* Optional. A user account.
* @param int $delay
* Optional. A delay to send the notification.
*
* @see \Drupal\meteor\IdentityListener
*/
function _meteor_notify_user_event($event, AccountInterface $account = NULL, $delay = 0) {
$query = [
'event' => strval($event),
];
$userId = $account instanceof AccountInterface ? intval($account->id()) : NULL;
if (isset($userId)) {
$query['userId'] = $userId;
}
if (isset($usDelay)) {
$query['delay'] = abs(intval($usDelay));
}
_meteor_notify('drupalUserEvent', $query);
}
/**
* Implements hook_field_storage_delete().
*
* One or more fields were deleted. If it happend on the user entity:
* - all user accounts need to refresh their "whoami" information,
* - they do not need to renew their login.
*/
function meteor_field_storage_config_delete(FieldStorageConfigInterface $entity) {
if ($entity->getTargetEntityTypeId() != 'user') {
return;
}
_meteor_notify_user_event(IdentityListener::FIELD_DELETE);
}
/**
* Implements hook_field_storage_insert().
*
* One or more fields were added. If it happend on the user entity:
* - all user accounts need to refresh their "whoami" information,
* - they do not need to renew their login.
*/
function meteor_field_storage_config_insert(FieldStorageConfigInterface $entity) {
if ($entity->getTargetEntityTypeId() != 'user') {
return;
}
_meteor_notify_user_event(IdentityListener::FIELD_INSERT);
}
/**
* Implements hook_field_storage_update().
*
* One or more fields were modified. If it happened on the user entity:
* - all user accounts need to refresh their "whoami" information,
* - they do not need to renew their login.
*/
function meteor_field_storage_config_update(FieldStorageConfigInterface $entity) {
if ($entity->getTargetEntityTypeId() != 'user') {
return;
}
_meteor_notify_user_event(IdentityListener::FIELD_UPDATE);
}
/**
* Implements hook_user_delete().
*
* Note: this is the hook used on user cancel-as-delete.
*/
function meteor_user_delete(AccountInterface $account) {
_meteor_notify_user_event(IdentityListener::USER_DELETE, $account);
}
/**
* Implements hook_user_login().
*/
function meteor_user_login(AccountInterface $account) {
_meteor_notify_user_event(IdentityListener::USER_LOGIN, $account, 1000);
}
/**
* Implements hook_user_logout().
*/
function meteor_user_logout(AccountProxyInterface $account) {
_meteor_notify_user_event(IdentityListener::USER_LOGOUT, $account->getAccount());
}
/**
* Implements hook_ENTITY_TYPE_update().
*
* Note: this is the hook used on user cancel-as-blocked.
*
* Deletion is not entirely complete at this point: wait for one second for a
* better chance for the transaction to be complete and user cache to be
* flushed.
*/
function meteor_user_update(UserInterface $account) {
_meteor_notify_user_event(IdentityListener::USER_UPDATE, $account, 1000);
}