ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilOrgUnitSimpleUserImport.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3require_once('./Modules/OrgUnit/classes/class.ilOrgUnitImporter.php');
4
14
18 public function simpleUserImport($file_path) {
19 $this->stats = array( 'created' => 0, 'removed' => 0 );
20 $a = file_get_contents($file_path, 'r');
21 $xml = new SimpleXMLElement($a);
22
23 if (!count($xml->Assignment)) {
24 $this->addError('no_assignment', NULL, NULL);
25
26 return;
27 }
28
29 foreach ($xml->Assignment as $a) {
30 $this->simpleUserImportElement($a);
31 }
32 }
33
34
38 public function simpleUserImportElement(SimpleXMLElement $a) {
39 global $DIC;
40 $rbacadmin = $DIC['rbacadmin'];
41
42 $attributes = $a->attributes();
43 $action = $attributes->action;
44 $user_id_type = $a->User->attributes()->id_type;
45 $user_id = (string)$a->User;
46 $org_unit_id_type = $a->OrgUnit->attributes()->id_type;
47 $org_unit_id = (string)$a->OrgUnit;
48 $role = (string)$a->Role;
49
50 if (!$user_id = $this->buildUserId($user_id, $user_id_type)) {
51 $this->addError('user_not_found', $a->User);
52
53 return;
54 }
55
56 if (!$org_unit_id = $this->buildRef($org_unit_id, $org_unit_id_type)) {
57 $this->addError('org_unit_not_found', $a->OrgUnit);
58
59 return;
60 }
61 $org_unit = new ilObjOrgUnit($org_unit_id);
62
63 if ($role == 'employee') {
64 $role_id = $org_unit->getEmployeeRole();
65 } elseif ($role == 'superior') {
66 $role_id = $org_unit->getSuperiorRole();
67 } else {
68 $this->addError('not_a_valid_role', $user_id);
69
70 return;
71 }
72
73 if ($action == 'add') {
74 $rbacadmin->assignUser($role_id, $user_id);
75 $this->stats['created'] ++;
76 } elseif ($action == 'remove') {
77 $rbacadmin->deassignUser($role_id, $user_id);
78 $this->stats['removed'] ++;
79 } else {
80 $this->addError('not_a_valid_action', $user_id);
81 }
82 }
83
84
91 private function buildUserId($id, $type) {
92 global $DIC;
93 $ilDB = $DIC['ilDB'];
94 if ($type == 'ilias_login') {
95 $user_id = ilObjUser::_lookupId($id);
96
97 return $user_id ? $user_id : false;
98 } elseif ($type == 'external_id') {
100
101 return $user_id ? $user_id : false;
102 } elseif ($type == 'email') {
103 $q = 'SELECT usr_id FROM usr_data WHERE email = ' . $ilDB->quote($id, 'text');
104 $set = $ilDB->query($q);
105 $user_id = $ilDB->fetchAssoc($set);
106
107 return $user_id ? $user_id : false;
108 } elseif ($type == 'user_id') {
109 return $id;
110 } else {
111 return false;
112 }
113 }
114}
115
116?>
An exception for terminatinating execution or to throw for unit testing.
Class ilObjOrgUnit.
static _lookupId($a_user_str)
Lookup id by login.
static _lookupObjIdByImportId($a_import_id)
Class ilOrgUnitImporter.
addError($lang_var, $import_id, $action=NULL)
Class ilOrgUnitSimpleUserImport.
global $ilDB
global $DIC