-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodebb.php
279 lines (234 loc) · 10.4 KB
/
nodebb.php
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
if (!defined('ABSPATH') || !defined('NODEBB_API_USER_ID')) exit;
require_once('external/php-jwt/src/JWT.php');
use Firebase\JWT\JWT;
class Castors_NodeBB {
public static function init() {
add_action('wp_login', [__CLASS__, 'login'], 10, 2);
add_action('wp_logout', [__CLASS__, 'logout']);
add_action('user_register', [__CLASS__, 'profile_saved'], 99);
add_action('profile_update', [__CLASS__, 'profile_saved'], 99);
}
public static function admin_init() {
add_action('wp_nav_menu_item_custom_fields', [__CLASS__, 'menu_item_fields'], 5);
add_action('wp_update_nav_menu_item', [__CLASS__, 'menu_item_update'], 10, 2);
add_action('wp_update_nav_menu', [__CLASS__, 'menu_update'], 10, 2);
add_settings_section(
'castors-nbb-section',
__("Authentification sur le forum NodeBB", 'castors'),
[__CLASS__, 'section_desc'],
'general'
);
add_settings_field(
'castors_nbb_secret',
__("JWT Secret", 'castors'),
[__CLASS__, 'secret'],
'general',
'castors-nbb-section',
[ 'label_for' => 'castors_nbb_secret' ]
);
add_settings_field(
'castors_nbb_api_token',
__("Jeton API", 'castors'),
[__CLASS__, 'api_token'],
'general',
'castors-nbb-section',
[ 'label_for' => 'castors_nbb_api_token' ]
);
register_setting('general', 'castors_nbb_secret');
register_setting('general', 'castors_nbb_api_token');
add_filter('groups_admin_groups_add_form_after_fields', [__CLASS__, 'add_group']);
add_filter('groups_admin_groups_edit_form_after_fields', [__CLASS__, 'edit_group']);
}
public static function login($user_login, $user) {
$expiration = time() + 14 * DAY_IN_SECONDS;
$payload = ['id' => $user->ID, 'username' => $user->user_login];
$secret = get_option('castors_nbb_secret');
$jwt = JWT::encode($payload, $secret, 'HS256');
setcookie('wp_nbb_login', $jwt, $expiration, '/', 'les-castors.fr', true, true);
}
public static function logout($user_id)
{
setcookie('wp_nbb_login', '', time() - 3600, '/', 'les-castors.fr', true);
}
public static function profile_saved($id) {
$user = get_user_by('id', $id);
static::updateAccount($user);
static::updateGroups($user);
}
public static function add_group($output) {
$output .= '<p class="description beware">';
$output .= esc_html__("ATTENTION : pensez à créer un groupe avec le même nom sur le forum NodeBB avant d'ajouter des membres aun nouveau groupe.", 'castors');
$output .= '</p>';
return $output;
}
public static function edit_group($output) {
$output .= '<p class="description beware">';
$output .= esc_html__("ATTENTION : Si un groupe avec le même nom existe sur le forum NodeBB, pensez à le mettre à jour également.", 'castors');
$output .= '</p>';
return $output;
}
public static function formatEndpoint($endpoint) {
return sprintf('%s?ts=%d&_uid=%d', $endpoint, current_time('timestamp'), NODEBB_API_USER_ID);
}
public static function parseArgs($args) {
$token = get_option('castors_nbb_api_token');
$defaults = [
'headers' => [
'Authorization' => "Bearer {$token}",
'Accept' => 'application/json',
],
];
return wp_parse_args($args, $defaults);
}
public static function read($endpoint, $args = []) {
$response = wp_remote_get(static::formatEndpoint($endpoint), static::parseArgs($args));
if (is_wp_error($response)) {
return $response;
}
if ($response['response']['code'] === 404) {
return null;
}
return json_decode($response['body']);
}
public static function create($endpoint, $body, $args = []) {
if ($body) {
$args['body'] = $body;
$args['Content-Type'] = 'application/json';
}
$response = wp_remote_post(static::formatEndpoint($endpoint), static::parseArgs($args));
if (is_wp_error($response)) {
return $response;
}
return json_decode($response['body']);
}
public static function update($endpoint, $body, $args = []) {
$args['method'] = 'PUT';
if ($body) {
$args['body'] = $body;
$args['Content-Type'] = 'application/json';
}
$response = wp_remote_request(static::formatEndpoint($endpoint), static::parseArgs($args));
if (is_wp_error($response)) {
return $response;
}
return json_decode($response['body']);
}
public static function delete($endpoint, $args = []) {
$args['method'] = 'DELETE';
$response = wp_remote_request(static::formatEndpoint($endpoint), static::parseArgs($args));
if (is_wp_error($response)) {
return $response;
}
return null;
}
public static function getAccountById($id) {
return static::read(get_site_url() . "/forum/api/v3/users/{$id}");
}
public static function getAccountByUsername($username) {
return static::read(get_site_url() . "/forum/api/user/{$username}");
}
public static function updateEmail($user, $nodeBBUser) {
if ($user->user_email !== $nodeBBUser->email) {
$result = static::create(get_site_url() . "/forum/api/v3/users/{$nodeBBUser->uid}/emails", ["email" => $user->user_email, "skipConfirmation" => 1]);
return $result;
}
return null;
}
public static function createAccount($user) {
$data = [
'username' => $user->user_login,
'fullname' => $user->display_name,
];
return static::create(get_site_url() . "/forum/api/v3/users", $data);
}
public static function updateAccount($user) {
$location_details = $user->castors_location_details;
$location = json_decode(htmlspecialchars_decode($location_details));
$nodeBBUser = static::getAccountByUsername($user->user_login);
$data = [
'fullname' => $user->display_name,
'website' => $user->user_url ?: '',
'location' => $location ? $location->value : '',
'aboutme' => $user->description ?: '',
];
if ($location) {
$data['fullname'] .= " ({$location->department})";
}
if (!$nodeBBUser) {
$nodeBBUser = static::createAccount($user);
}
if (!$nodeBBUser) {
return null;
}
static::updateEmail($user, $nodeBBUser);
return static::update(get_site_url() . "/forum/api/v3/users/{$nodeBBUser->uid}", $data);
}
public static function getGroups() {
return static::read(get_site_url() . "/forum/api/v3/groups");
}
public static function updateGroups($user) {
if (!class_exists('Groups_User')) {
// Groups extension is not active
return null;
}
$nodeBBUser = static::getAccountByUsername($user->user_login);
$groupsUser = new Groups_User($user->ID);
$nodeBBgroups = static::getGroups()->response->groups;
$nodeBBgroupNames = array_column($nodeBBgroups, 'name');
$groupNames = [];
foreach ($groupsUser->groups as $group) {
$groupNames[] = $group->group->name;
if (in_array($group->group->name, $nodeBBUser->groupTitleArray)) {
// Already in NodeBB group
continue;
}
$index = array_search($group->group->name, $nodeBBgroupNames);
if ($index === false) {
// Not a NodeBB group
}
// Add user to NodeBB group
$slug = $nodeBBgroups[$index]->slug;
static::update(get_site_url() . "/forum/api/v3/groups/{$slug}/membership/{$nodeBBUser->uid}", []);
}
foreach ($nodeBBUser->groups as $group) {
if (!in_array($group->name, $groupNames)) {
// User no longer in group
static::delete(get_site_url() . "/forum/api/v3/groups/{$group->slug}/membership/{$nodeBBUser->uid}");
}
}
}
public static function section_desc($args) {
echo '<p>' . __("<b>JWT Secret</b> doit être le même que celui enregistré dans le plugin <b>Session Sharing</b> du forum.", 'castors') . '</p>'
. '<p>' . __("<b>Jeton API</b> doit être déclaré dans la section <b>Gestion API</b> de l'administration du forum, associé à un compte administrateur.", 'castors') . '</p>';
}
public static function secret() {
echo '<input id="castors_nbb_secret" type="text" name="castors_nbb_secret" value="' . get_option('castors_nbb_secret') . '" class="regular-text">';
}
public static function api_token() {
echo '<input id="castors_nbb_api_token" type="text" name="castors_nbb_api_token" value="' . get_option('castors_nbb_api_token') . '" class="regular-text">';
}
public static function menu_item_fields($item_id) {
$label = __("Capacité", 'castors');
$value = get_post_meta($item_id, '_menu_item_capabilities', true);
$description = esc_html("Liste de capacités séparées par des virgules. Seuls les utilisateurs ayant au moins une de ces capacités verront cet élément de menu. Laisser vide pour l'afficher à tous les utilisateurs.", 'castors');
echo <<<EOF
<p class="field-capabilities description description-wide">
<label for="edit-menu-item-capabilities-{$item_id}">
{$label}<br />
<input type="text" id="edit-menu-item-capabilities-{$item_id}" class="widefat edit-menu-item-capabilities" name="menu_item_capabilities[{$item_id}]" value="{$value}" />
<span class="description">{$description}</span>
</label>
</p>
EOF;
}
public static function menu_item_update($menu_id, $item_id) {
if ($_POST['menu_item_capabilities']) {
$capabilities = $_POST['menu_item_capabilities'][$item_id] ?? '';
update_post_meta($item_id, '_menu_item_capabilities', $capabilities);
}
}
public static function menu_update($menu_id, $menu_data = []) {
//var_dump($menu_id, $menu_data); exit;
}
}