-
Notifications
You must be signed in to change notification settings - Fork 0
/
ding_user_consent.module
306 lines (293 loc) · 10.7 KB
/
ding_user_consent.module
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
* @file
* Ding user consent
*/
/**
* Implements hook_ding_provider_user().
*/
function ding_user_consent_ding_provider_user() {
return array(
'user_content' => array(
'required' => TRUE,
'install time setup' => TRUE,
),
);
}
/**
* Implements hook_menu().
*/
function ding_user_consent_menu() {
$items = array();
// User consent admin page.
$items['admin/config/ding/consent'] = array(
'title' => 'Ding consent',
'page callback' => 'drupal_get_form',
'page arguments' => array('ding_user_consent_admin_form'),
'access arguments' => array('administer ding provider'),
'file' => 'ding_user_consent.admin.inc',
);
// User consent form page.
$items['user/%user/consent'] = array(
'title' => 'Give consent',
'page callback' => 'drupal_get_form',
'page arguments' => array('ding_user_consent_form'),
'access callback' => 'ding_user_access',
'access arguments' => array(1),
'file' => 'ding_user_consent.pages.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Return the page to redirect user to in order to update consent
* Used by ding_provider.
*/
function ding_user_consent_page() {
global $user;
if (user_is_logged_in()) {
return 'user/' . $user->uid . '/consent';
}
return 'user/login';
}
/**
* Return the users consent if such exists.
* Should the censent be missing and have the required flag, the DingProviderConsentException is thrown.
*
* @param int $consent_id
* The id of the consent to check for, if null the complete list of consents the user has given will be returned if any.
* @param user $account
* The user account to check consent for, if null the current logged in user is used.
*
* @return
* This function returns either the specific consent as a 1 or 0, or the array of given consents.
*/
function ding_user_consent_get_consent($consent_id = NULL, $account = FALSE) {
// Set the account to the current user if it is not provided.
if (!$account) {
global $user;
$account = $user;
}
// Get consent info
$consents = ding_provider_invoke('user_consent', 'info');
if (!is_null($consent_id)) {
// Search for a specific consent type.
// Get the user specific consent.
$consent = ding_provider_invoke('user_consent', 'get_consent', $account);
// Throw a DingProviderConsentException if the user has not given consent yet or if consent is required and the user has denied consent.
if (empty($account->data['consent']) || isset($consent[$consent_id]) && $consents[$consent_id]['required'] && !$consent[$consent_id]) {
throw new DingProviderConsentException;
}
// Return the value if it is set.
if (!empty($consent[$consent_id])) {
return $consent[$consent_id];
}
}
else {
// Throw a DingProviderConsentException if the user has not received a consent notice yet.
if (empty($consent_id) && empty($account->data['consent'])) {
throw new DingProviderConsentException;
}
// Get user specific consent.
$consent = ding_provider_invoke('user_consent', 'get_consent', $account);
foreach ($consents as $id => $values) {
// Throw a DingProviderConsentException if any of the consent types is missing from the users consent list.
if (!isset($consent[$id])) {
throw new DingProviderConsentException;
}
}
// Return consent list.
return $consent;
}
// If all else fails throw a DingProviderConsentException, since we would like user to give consent.
throw new DingProviderConsentException;
}
/**
* Check if the users has given consent or not.
*
* @param int $consent_id
* The id of the consent to check for, if null the complete list of consents the user has given will be returned if any.
* @param user $account
* The user account to check consent for, if null the current logged in user is used.
*
* @return bool
* Returns TRUE is the user has given consent and FALSE if no consent has been given or is the consent is revoked.
*/
function ding_user_consent_has_consent($consent_id = NULL, $account = FALSE) {
// Set the account to the current user if it is not provided.
if (!$account) {
global $user;
$account = $user;
}
// Check if the user is a user with valid credentials
if(ding_user_is_provider_user($account)) {
try {
// Get consent info
$consents = ding_provider_invoke('user_consent', 'info');
if (!is_null($consent_id)) {
// Search for a specific consent type.
// Get the user specific consent.
$consent = ding_provider_invoke('user_consent', 'get_consent', $account);
// Return false if the user has not given consent yet or if consent is required and the user has denied consent.
if (empty($account->data['consent']) || isset($consent[$consent_id]) && $consents[$consent_id]['required'] && !$consent[$consent_id]) {
return FALSE;
}
// Return true if the consent id is set.
if (!empty($consent[$consent_id])) {
return TRUE;
}
}
else {
// Return false if the user has not received a consent notice yet.
if (empty($consent_id) && empty($account->data['consent'])) {
return FALSE;
}
// Get user specific consent.
$consent = ding_provider_invoke('user_consent', 'get_consent', $account);
// Return false if any of the consent types is missing from the users consent list.
foreach ($consents as $id => $values) {
if (!isset($consent[$id])) {
return FALSE;
}
}
// Return true
return TRUE;
}
} catch (Exception $e) {
// In case of an exception return false
return FALSE;
}
} else {
// Default to false
return FALSE;
}
}
/**
* Attach consent for to user profile form
*/
function ding_user_consent_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
$account = $form['#user'];
// Ensure that the user related to this profile form is a valid user and
if (ding_user_is_provider_user($account) && $account->uid == $GLOBALS['user']->uid) {
$consents = ding_provider_invoke('user_consent', 'info');
if($consents) {
$form['user_consent'] = array(
'#type' => 'fieldset',
'#title' => t('Legal consent'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$consent = ding_provider_invoke('user_consent', 'get_consent', $account);
foreach ($consents as $id => $values) {
if (!isset($values['title'])) {
$values['title'] = t('Legal consent');
}
$form['user_consent'][$id] = array(
'#type' => 'checkbox',
'#title' => check_plain($values['title']),
'#default_value' => !empty($consent[$id]) ? $consent[$id] : 0,
);
$form['user_consent'][$id]['#description'] = (!empty($values['description'])) ? check_plain($values['description']) : '';
if (variable_get($id . '_description_link', '')) {
$form['user_consent'][$id]['#description'] = format_string(
$form['user_consent'][$id]['#description'] . ' !br !url',
array(
'!br' => '<br>',
'!url' => l(t('Read more'), variable_get($id . '_description_link', ''))
)
);
}
}
$form['#validate'][] = 'ding_user_consent_form_user_profile_form_validate';
$form['#submit'][] = 'ding_user_consent_form_user_profile_form_submit';
}
}
}
/**
* Validate user consent before submitting user edit data.
*/
function ding_user_consent_form_user_profile_form_validate($form, &$form_state) {
$user = $form_state['user'];
if (empty($user->data['consent'])) {
// If this is the first time the user has been presented with the consent form no data is stored in the data['consent']
// and we therefore ensure that data['consent'] is set to prevent the consent form from reappearing when the user performs
// actions that would impacted consent, unless the functionality requires consent.
$form_state['input']['data']['consent'] = 1;
$form_state['values']['data']['consent'] = 1;
}
}
/**
* Submit user consent on user edit save.
*/
function ding_user_consent_form_user_profile_form_submit($form_id, &$form_state) {
$consents = ding_provider_invoke('user_consent', 'info');
$user = $form_state['user'];
$consent = ding_provider_invoke('user_consent', 'get_consent', $user);
$changed = array();
foreach ($consents as $id => $values) {
if ($form_state['input'][$id] <> $consent[$id]) {
if ($form_state['input'][$id]) {
$result = ding_provider_invoke('user_consent', 'add', $user, $id, $consent);
}
else {
$result = ding_provider_invoke('user_consent', 'remove', $user, $id, $consent);
}
$changed[$id] = ($form_state['input'][$id]) ? TRUE : FALSE;
}
}
module_invoke_all('ding_user_consent_changed', $changed);
}
/**
* Implements hook_form_alter
*/
function ding_user_consent_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'ding_reservation_reserve_form':
$form['#attributes']['class'][] = 'link-form';
//$form['#validate'][] = 'ding_user_consent_form_process';
break;
// Falling through on purpose.
case 'ding_user_consent_form':
if (isset($form_state['ajaxify'])) {
$form['actions']['submit']['#ajax'] = array(
'callback' => 'ding_user_consent_form_callback',
'wrapper' => drupal_html_id($form_id),
);
}
break;
}
}
/**
* Validate reserve form
*/
function ding_user_consent_form_process($form, &$form_state) {
ding_user_consent_get_consent();
}
/**
* Ajax command to authenticate. Used by ding_provider.
*/
function ajax_command_ding_user_consent($extra_data) {
global $user;
// @todo add support for user/login here.
module_load_include('inc', 'ding_user_consent', 'ding_user_consent.pages');
$title = t('Give consent');
$form_id = 'ding_user_consent_form';
$form_state = array(
'ajaxify' => TRUE,
);
$form = drupal_build_form($form_id, $form_state);
$consent_form = drupal_render($form);
return ajax_command_ding_popup('ding_user_consent', $title, $consent_form, array('resubmit' => TRUE, 'extra_data' => $extra_data));
}
/**
* Ajax callback.
*/
function ding_user_consent_form_callback($form, &$form_state) {
$response = array(
'#type' => 'ajax',
'#commands' => array(),
);
// Close dialog if successful.
$response['#commands'][] = ajax_command_ding_popup_close('ding_user_consent');
return $response;
}