-
Notifications
You must be signed in to change notification settings - Fork 0
/
enrol.php
207 lines (173 loc) · 7.73 KB
/
enrol.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
<?php // $Id$
require_once($CFG->dirroot.'/enrol/enrol.class.php');
class enrolment_plugin_dbuserrel {
var $log;
/*
* For the given user, let's go out and look in an external database
* for an authoritative list of relationships, and then adjust the
* local Moodle assignments to match.
*/
function setup_enrolments(&$user=null) {
global $CFG;
// Store the field values in some shorter variable names to ease reading of the code.
$flocalsubject = $CFG->enrol_dbuserrel_localsubjectuserfield;
$flocalobject = $CFG->enrol_dbuserrel_localobjectuserfield;
$flocalrole = $CFG->enrol_dbuserrel_localrolefield;
$fremotesubject = $CFG->enrol_dbuserrel_remotesubjectuserfield;
$fremoteobject = $CFG->enrol_dbuserrel_remoteobjectuserfield;
$fremoterole = $CFG->enrol_dbuserrel_remoterolefield;
$dbtable = $CFG->enrol_dbuserrel_table;
// NOTE: if $this->enrol_connect() succeeds you MUST remember to call
// $this->enrol_disconnect() as it is doing some nasty vodoo with $CFG->prefix
$enroldb = $this->enrol_connect();
if (!$enroldb) {
error_log('Error: [ENROL_DBUSERREL] Could not make a connection');
return;
}
if ($user) {
$subjectfield = $enroldb->quote($user->{$flocalsubject});
$objectfield = $enroldb->quote($user->{$flocalobject});
$sql = "SELECT * FROM {$dbtable}
WHERE {$fremotesubject} = $subjectfield
OR {$fremoteobject} = $objectfield";
} else {
$sql = "SELECT * FROM {$dbtable}";
}
if ($rs = $enroldb->Execute($sql)) {
$uniqfield = sql_concat("r.$flocalrole", "'|'", "u1.$flocalsubject", "'|'", "u2.$flocalobject");
$sql = "SELECT $uniqfield AS uniq,
ra.*, r.{$flocalrole} ,
u1.{$flocalsubject} AS subjectid,
u2.{$flocalobject} AS objectid
FROM {$CFG->prefix}role_assignments ra
JOIN {$CFG->prefix}role r ON ra.roleid = r.id
JOIN {$CFG->prefix}context c ON c.id = ra.contextid
JOIN {$CFG->prefix}user u1 ON ra.userid = u1.id
JOIN {$CFG->prefix}user u2 ON c.instanceid = u2.id
WHERE c.contextlevel = " . CONTEXT_USER .
(!empty($user) ? " AND c.instanceid = {$user->id} OR ra.userid = {$user->id}" : '');
if (!$existing = get_records_sql($sql)) {
$existing = array();
}
if (!$rs->EOF) {
$roles = get_records('role', '', '', '', "$flocalrole, id");
$subjectusers = array(); // cache of mapping of localsubjectuserfield to mdl_user.id (for get_context_instance)
$objectusers = array(); // cache of mapping of localsubjectuserfield to mdl_user.id (for get_context_instance)
$contexts = array(); // cache
$rels = array();
while ($row = rs_fetch_next_record($rs)) {
// either we're assigning ON the current user, or TO the current user
$key = $row->{$fremoterole} . '|' . $row->{$fremotesubject} . '|' . $row->{$fremoteobject};
if (array_key_exists($key, $existing)) {
// exists in moodle db already, unset it (so we can delete everything left)
unset($existing[$key]);
error_log("Warning: [$key] exists in moodle already");
continue;
}
if (!array_key_exists($row->{$fremoterole}, $roles)) {
// role doesn't exist in moodle. skip.
error_log("Warning: role " . $row->{$fremoterole} . " wasn't found in moodle. skipping $key");
continue;
}
if (!array_key_exists($row->{$fremotesubject}, $subjectusers)) {
$subjectusers[$row->{$fremotesubject}] = get_field('user', 'id', $flocalsubject, $row->{$fremotesubject});
}
if ($subjectusers[$row->{$fremotesubject}] == false) {
error_log("Warning: [" . $row->{$fremotesubject} . "] couldn't find subject user -- skipping $key");
// couldn't find user, skip
continue;
}
if (!array_key_exists($row->{$fremoteobject}, $objectusers)) {
$objectusers[$row->{$fremoteobject}] = get_field('user', 'id', $flocalobject, $row->{$fremoteobject});
}
if ($objectusers[$row->{$fremoteobject}] == false) {
// couldn't find user, skip
error_log("Warning: [" . $row->{$fremoteobject} . "] couldn't find object user -- skipping $key");
continue;
}
$context = get_context_instance(CONTEXT_USER, $objectusers[$row->{$fremoteobject}]);
error_log("Information: [" . $row->{$fremotesubject} . "] assigning " . $row->{$fremoterole} . " to " . $row->{$fremotesubject}
. " on " . $row->{$fremoteobject});
role_assign($roles[$row->{$fremoterole}]->id, $subjectusers[$row->{$fremotesubject}], 0, $context->id, 0, 0, 0, 'dbuserrel');
}
// delete everything left in existing
foreach ($existing as $key => $assignment) {
if ($assignment->enrol == 'dbuserrel') {
error_log("Information: [$key] unassigning $key");
role_unassign($assignment->roleid, $assignment->userid, 0, $assignment->contextid);
}
}
} else {
error_log('Warning: [ENROL_DBUSERREL] Couldn\'t get rows from external db: '.$enroldb->ErrorMsg(). ' -- no relationships to assign');
}
}
$this->enrol_disconnect($enroldb);
}
/// Overide the get_access_icons() function
function get_access_icons($course) {
}
/// Overide the base config_form() function
function config_form($frm) {
global $CFG;
$vars = array(
'type', 'host', 'user', 'pass', 'name', 'userenroldatabase',
'useauthdb', 'table', 'localsubjectuserfield', 'localobjectuserfield',
'remotesubjectuserfield', 'remoteobjectuserfield', 'remoterolefield', 'localrolefield',
'useauthdb', 'useenroldatabase',
);
foreach ($vars as $var) {
$var = 'enrol_dbuserrel_' . $var;
if (!isset($frm->$var)) {
$frm->$var = '';
}
}
include("$CFG->dirroot/enrol/dbuserrel/config.html");
}
/// Override the base process_config() function
function process_config($config) {
foreach ((array)$config as $key => $value) {
if (strpos($key, 'enrol_dbuserrel_') !== 0) {
continue;
}
set_config($key, $value);
}
return true;
}
function enrol_connect() {
global $CFG;
// make up settings!
$type = '';
$host = '';
$user = '';
$pass = '';
$name = '';
$copyobj = $CFG;
$copyprefix = 'enrol_dbuserrel_';
if (!empty($CFG->enrol_dbuserrel_userenroldatabase)) {
$copyprefix = 'enrol_db';
}
else if (!empty($CFG->enrol_dbuserrel_useauthdb)) {
$copyobj = get_config('auth/db');
$copyprefix = '';
}
foreach (array('type', 'host', 'user', 'pass', 'name') as $setting) {
$$setting = $copyobj->{$copyprefix . $setting};
}
// Try to connect to the external database (forcing new connection)
$enroldb = &ADONewConnection($type);
if ($enroldb->Connect($host, $user, $pass, $name, true)) {
$enroldb->SetFetchMode(ADODB_FETCH_ASSOC); ///Set Assoc mode always after DB connection
return $enroldb;
} else {
trigger_error("Error connecting to enrolment DB backend with: "
. "$host,$user,$pass,$name");
return false;
}
}
/// DB Disconnect
function enrol_disconnect($enroldb) {
global $CFG;
$enroldb->Close();
}
} // end of class
?>