-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathos2intra_user_import.module
More file actions
355 lines (311 loc) · 13.7 KB
/
os2intra_user_import.module
File metadata and controls
355 lines (311 loc) · 13.7 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
*
* @author Thomas Thune Hansen <tth@bellcom.dk>
* @copyright bellcom open source aps
*/
/**
* Include inc files
*/
module_load_include('inc', 'os2intra_user_import', 'includes/activation_form');
module_load_include('inc', 'os2intra_user_import', 'includes/csv_mapping');
module_load_include('inc', 'os2intra_user_import', 'includes/departments');
module_load_include('inc', 'os2intra_user_import', 'includes/log');
module_load_include('inc', 'os2intra_user_import', 'includes/mail');
module_load_include('inc', 'os2intra_user_import', 'includes/og_roles');
module_load_include('inc', 'os2intra_user_import', 'includes/opus');
module_load_include('inc', 'os2intra_user_import', 'includes/settings_form');
module_load_include('inc', 'os2intra_user_import', 'includes/users');
module_load_include('inc', 'os2intra_user_import', 'includes/utilities');
// User activation default variable.
define('OS2INTRA_USER_IMPORT_ACTIVATE_IDENTIFICATION', NULL);
define('OS2INTRA_USER_IMPORT_ACTIVATE_BIRTHDAY', TRUE);
define('OS2INTRA_USER_IMPORT_ACTIVATE_EMAIL', TRUE);
/**
* Implements hook_menu()
*/
function os2intra_user_import_menu() {
$items = array();
$items['admin/config/os2intra/user_import'] = array(
'title' => 'OS2Intra User import',
'description' => 'Administer user import',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer os2intra user import'),
'file path' => drupal_get_path('module', 'system'),
'file' => 'system.admin.inc',
'weight' => -5,
);
$items['admin/config/os2intra/user_import/settings'] = array(
'title' => 'Settings',
'description' => 'Administer user import settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('os2intra_user_import_settings'),
'access arguments' => array('administer os2intra user import'),
'file' => 'includes/settings_form.inc',
);
$items['admin/config/os2intra/user_import/log'] = array(
'title' => 'Log',
'description' => 'View user import log',
'page callback' => 'os2intra_user_import_log',
'access arguments' => array('administer os2intra user import'),
'file' => 'includes/log.inc',
);
$items['admin/config/os2intra/user_import/overview'] = array(
'title' => 'Overview',
'description' => 'View imported users',
'page callback' => 'os2intra_user_import_overview',
'access arguments' => array('administer os2intra user import'),
);
$items['useractivation'] = array(
'title' => 'User activation',
'page callback' => 'os2intra_user_import_activate',
'access callback' => TRUE,
'file' => 'includes/activation_form.inc',
);
$items['admin/config/os2intra/dwe'] = array(
'title' => t('Departments without employees'),
'description' => t('A list of departments without employees'),
'page callback' => 'os2intra_user_import_departments_without_employees',
'access arguments' => array('administer os2intra'),
);
return $items;
}
/**
* Implements hook_permission()
*/
function os2intra_user_import_permission() {
return array(
'administer os2intra user import' => array(
'title' => t('Administer OS2Intra User import'),
'description' => t('Perform administration tasks for user import.'),
),
);
}
/**
* Implements hook_form_alter().
*/
function os2intra_user_import_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'edit_profile_user_profile_form') {
if (!user_access('administer os2intra user import')) {
$form[variable_get('os2intra_user_department_field')]['#access'] = 0;
$form['field_os2intra_employee_id']['#access'] = 0;
$form['field_os2intra_termination_date']['#access'] = 0;
}
}
// Use os2intra_user_import_add_managers_to_departments() for submits
if ($form_id == 'taxonomy_form_term') {
if (variable_get('os2intra_add_managers_to_departments')) {
if (is_array($form['#term']) && $form['#term']['vid'] == variable_get('os2intra_groups_vocabulary_vid')) {
$form['#submit'][] = "os2intra_user_import_add_managers_to_departments";
}
}
}
if ($form_id == 'user_login') {
// Add link for user activation to login form.
$form['activate_user'] = array(
'#type' => 'item',
'#markup' => l(t('Activate user'), 'useractivation'),
);
}
}
/**
* Page callback for overview
*/
function os2intra_user_import_overview() {
return views_embed_view('os2intra_user_import', 'block');
}
/**
* User import wrapper function.
*/
function os2intra_user_import_run_with_drush() {
$user_disable_method = variable_get('os2intra_user_import_user_disable_method', array('termination_date'=>1));
if (empty(variable_get('os2intra_user_import_activate_identification', OS2INTRA_USER_IMPORT_ACTIVATE_IDENTIFICATION))) {
$message = 'User activation mode is not setup. Please check user import configuration /admin/config/os2intra/user_import/settings';
os2intra_user_import_save_log('', $message);
drupal_set_message($message, 'error');
return;
}
if (variable_get('os2intra_user_import_enable', FALSE)) {
$current_timestamp = &drupal_static('os2intra_user_import_current_timestamp');
$current_timestamp = time();
$last_import_epoch = variable_get('os2intra_user_import_epoch');
if (!variable_get('os2intra_user_import_epoch')) {
variable_set('os2intra_user_import_epoch', $current_timestamp);
}
// Check if import has run successfully in 1.5 day.
$period = variable_get('os2intra_user_import_user_notification_period', 1.5) * 60 * 60 * 24;
if ($current_timestamp > $last_import_epoch + $period) {
// Send mail.
$params = array(
'subject' => t('1.5 day since last import'),
'body' => 'Check if the import is running correctly.',
);
$to = variable_get('os2intra_user_notification_email', variable_get('site_mail'), 'drift@bellcom.dk');
drupal_mail('os2intra_user_import', 'os2intra_user_notification_email', $to, language_default(), $params);
os2intra_user_import_save_log('', 'Email about last import older than 1.5 days sent to: ' . $to );
}
setlocale(LC_ALL, "en_DK.UTF-8");
$import_dir = variable_get('os2intra_user_import_dir', 'private://user_import');
$realpath = '';
$filename = variable_get('os2intra_user_import_file_name', 'FK-user_import');
$dir = reset(file_scan_directory($import_dir, '/' . $filename . '.(csv|CSV)$/'));
if ($dir) {
$realpath = drupal_realpath($dir->uri);
}
if (file_exists($realpath)) {
// Uncomment this to get extensive debugging in the Drupal user import log in admin
// http://[mydrupalsite]/admin/config/os2intra/user_import/log
variable_set('os2intra_user_import_save_extended_log', FALSE);
$last_import_temp_groups = variable_get('os2intra_user_import_epoch_groups');
$last_import_temp_users = variable_get('os2intra_user_import_epoch_users');
$last_import_temp_users_disable = variable_get('os2intra_user_import_epoch_users_disable');
os2intra_user_import_clear_log();
os2intra_user_import_save_log('', 'Last registered import: ' . $last_import_epoch);
os2intra_user_import_save_log('', 'Starting import');
// Parse file, map data accordingly
$users = _os2intra_user_import_process_file($realpath);
// Check and create groups.
if ($current_timestamp != $last_import_temp_groups) {
os2intra_user_import_save_log('', 'Starting groups. Last import different than the main import: ' . $current_timestamp . ' / ' . $last_import_temp_groups);
os2intra_user_import_save_log('', 'Starting Group Import Loop');
os2intra_user_import_groups($users);
os2intra_user_import_save_log('', 'Ending Group Import Loop');
variable_set('os2intra_user_import_epoch_groups', $current_timestamp);
}
else {
os2intra_user_import_save_log('', 'Skipping groups. Last import is the same as the running import: ' . $current_timestamp . ' / ' . $last_import_temp_groups);
}
// Check for users that already exist.
if ($current_timestamp != $last_import_temp_users) {
os2intra_user_import_save_log('', 'Starting User Loop');
$update_users = os2intra_user_import_check_users($users);
if ($user_disable_method['csv_file']) {
$active_users = os2intra_user_import_get_all_active_users();
}
// Loop over users and create.
os2intra_user_import_save_log('', 'Start Creating Users');
foreach ($users as $user) {
if (empty($user['department'])) {
os2intra_user_import_save_log('', 'Skipping user. department is missing. AD_ID:' . $user['ad_id'] . ' User ' . $user['first_name'] . ' ' . $user['last_name']);
continue;
}
os2intra_user_import_save_user($user);
}
os2intra_user_import_save_log('', 'End Creating Users');
// Update users
os2intra_user_import_save_log('', 'Start Update Users');
foreach ($update_users as $uid => $user) {
if ( $user_disable_method['csv_file'] && isset($active_users[$uid])) {
unset($active_users[$uid]);
}
if (empty($user['department'])) {
os2intra_user_import_save_log('', 'Skipping user. department is missing. AD_ID:' . $user['ad_id'] . ' User ' . $user['first_name'] . ' ' . $user['last_name']);
continue;
}
os2intra_user_import_save_user($user, $uid);
}
os2intra_user_import_save_log('', 'End Update Users');
os2intra_user_import_save_log('', 'Ending User Loop');
variable_set('os2intra_user_import_epoch_users', $current_timestamp);
}
else {
os2intra_user_import_save_log('', 'Skipping user update. Last import is the same as the running import: ' . $current_timestamp . ' / ' . $last_import_temp_users);
}
// Disable users.
if ($current_timestamp != $last_import_temp_users_disable) {
os2intra_user_import_save_log('', 'Start Disabling Users');
os2intra_user_import_disable_users($active_users);
os2intra_user_import_save_log('', 'End Disabling Users');
variable_set('os2intra_user_import_epoch_users_disable', $current_timestamp);
}
else {
os2intra_user_import_save_log('', 'Skipping user disable. Last import is the same as the running import: ' . $current_timestamp . ' / ' . $last_import_temp_users_disable);
}
// Logging
os2intra_user_import_save_log('', 'Import finished');
#variable_set('os2intra_user_import_epoch', time());
if ($current_timestamp >= $last_import_epoch + 86400) {
os2intra_user_import_save_log('', 'Last import updated changed - older than one day:');
// Timestamp to check if import has run successfully.
variable_set('os2intra_user_import_epoch', $current_timestamp);
}
else {
os2intra_user_import_save_log('', 'Last import time NOT updated - under one day old');
}
}
else {
os2intra_user_import_save_log('', 'File does not exist');
}
}
}
/**
* Returns a list of all departments without users with role 14 and role 16
*/
function os2intra_user_import_departments_without_employees() {
$departments_without_employees = array();
$gid_arrays_with = array();
$gid_arrays_without = array();
$gids_with = array();
$gids_without = array();
// Select all distinct group ID's where role ID is 14 and 16
$results_with = db_query('SELECT DISTINCT gid FROM og_users_roles WHERE rid=14 OR rid=16;');
// Select all group ID's where role ID is not 14 and not 16
$results_without = db_query('SELECT DISTINCT gid FROM og_users_roles WHERE rid != 14 AND rid != 16;');
while ($result_with = $results_with->fetchAssoc()) {
$gid_arrays_with[] = $result_with;
}
while ($result_without = $results_without->fetchAssoc()) {
$gid_arrays_without[] = $result_without;
}
// Create array with group ID's for those with rid 14 and 16
foreach ($gid_arrays_with as $key => $value) {
foreach ($value as $key => $gid) {
$gids_with[] = $gid;
}
}
// Create array with group ID's for those without rid 14 and 16
foreach ($gid_arrays_without as $key => $value) {
foreach ($value as $key => $gid) {
$gids_without[] = $gid;
}
}
// Load all nodes
$nids = array_diff($gids_without, $gids_with);
$nodes = node_load_multiple($nids);
// Loop through and return only ID's of type 'os2intra_org_group_unit' / department nodes
foreach ($nodes as $node) {
if ($node->type == 'os2intra_org_group_unit') {
$node_link = '/node/' . $node->nid;
$rows[] = array(
l($node->nid, $node_link),
l($node->title, $node_link),
l(t('Edit'), $node_link . '/edit'),
);
}
}
// Return render array with NID and title
$table['departments_without_employees'] = array(
'#theme' => 'table',
'#header' => array(t('ID'), t('Department'), t('Operations')),
'#rows' => $rows,
);
return $table;
}
function os2intra_user_import_user_insert(&$edit, $account, $category) {
//user create manually by admin
if (isset($_POST['form_id']) && $_POST['form_id'] == 'user_register_form' && field_info_instance('user', 'field_os2intra_ad_name', 'user')){
// Create authmap for user
$authmap = array(
'authname_simplesamlphp_auth' => $account->field_os2intra_ad_name[LANGUAGE_NONE][0]['value'] . variable_get('os2intra_user_import_authmap_name_suffix'),
);
if ( !user_get_authmaps($authmap) )
{
user_set_authmaps($account, $authmap);
}
}
}
function os2intra_user_import_user_presave(&$edit, $account, $category){
if ($edit['status'] === '1') {
unset($edit['field_os2intra_user_disable_date']['und'][0]);
}
}