3 // Allows the admin to control user logins from remote moodles.
5 require_once dirname(dirname(dirname(__FILE__
))) . '/config.php';
6 require_once($CFG->libdir
.'/adminlib.php');
7 include_once($CFG->dirroot
.'/mnet/lib.php');
9 $sort = optional_param('sort', 'username', PARAM_ALPHA
);
10 $dir = optional_param('dir', 'ASC', PARAM_ALPHA
);
11 $page = optional_param('page', 0, PARAM_INT
);
12 $perpage = optional_param('perpage', 30, PARAM_INT
);
13 $action = trim(strtolower(optional_param('action', '', PARAM_ALPHA
)));
16 $adminroot = admin_get_root();
19 admin_externalpage_setup('ssoaccesscontrol', $adminroot);
21 admin_externalpage_print_header($adminroot);
23 if (!extension_loaded('openssl')) {
24 print_error('requiresopenssl', 'mnet', '', NULL, true);
27 $sitecontext = get_context_instance(CONTEXT_SYSTEM
, SITEID
);
31 // grab the mnet hosts and remove the localhost
32 $mnethosts = get_records_menu('mnet_host', '', '', 'name', 'id, name');
33 if (array_key_exists($CFG->mnet_localhost_id
, $mnethosts)) {
34 unset($mnethosts[$CFG->mnet_localhost_id
]);
40 if (!empty($action) and confirm_sesskey()) {
42 // boot if insufficient permission
43 if (!has_capability('moodle/user:delete', $sitecontext)) {
44 error(get_string('nomodifyacl','mnet'));
47 // fetch the record in question
48 $id = required_param('id', PARAM_INT
);
49 if (!$idrec = get_record('mnet_sso_access_control', 'id', $id)) {
50 error(get_string('recordnoexists','mnet'), '/admin/mnet/access_control.php');
56 delete_records('mnet_sso_access_control', 'id', $id);
57 redirect('access_control.php', get_string('deleteuserrecord', 'mnet', array($idrec->username
, $mnethosts[$idrec->mnet_host_id
])));
62 // require the access parameter, and it must be 'allow' or 'deny'
63 $access = trim(strtolower(required_param('access', PARAM_ALPHA
)));
64 if ($access != 'allow' and $access != 'deny') {
65 error(get_string('invalidaccessparam', 'mnet') , '/admin/mnet/access_control.php');
68 if (mnet_update_sso_access_control($idrec->username
, $idrec->mnet_host_id
, $access)) {
69 if ($access == 'allow') {
70 redirect('access_control.php', get_string('ssl_acl_allow','mnet', array($idrec->username
, $mnethosts[$idrec->mnet_host_id
])));
71 } elseif ($access == 'deny') {
72 redirect('access_control.php', get_string('ssl_acl_deny','mnet', array($idrec->username
, $mnethosts[$idrec->mnet_host_id
])));
78 print_error('invalidactionparam', 'mnet', '/admin/mnet/access_control.php');
84 // process the form results
85 if ($form = data_submitted() and confirm_sesskey()) {
87 // check permissions and verify form input
88 if (!has_capability('moodle/user:delete', $sitecontext)) {
89 error(get_string('nomodifyacl','mnet'), '/admin/mnet/access_control.php');
91 if (empty($form->username
)) {
92 $formerror['username'] = get_string('enterausername','mnet');
94 if (empty($form->mnet_host_id
)) {
95 $formerror['mnet_host_id'] = get_string('selectahost','mnet');
97 if (empty($form->access
)) {
98 $formerror['access'] = get_string('selectaccesslevel','mnet'); ;
101 // process if there are no errors
102 if (count($formerror) == 0) {
104 // username can be a comma separated list
105 $usernames = explode(',', $form->username
);
107 foreach ($usernames as $username) {
108 $username = trim(moodle_strtolower($username));
109 if (!empty($username)) {
110 if (mnet_update_sso_access_control($username, $form->mnet_host_id
, $form->access
)) {
111 if ($form->access
== 'allow') {
112 redirect('access_control.php', get_string('ssl_acl_allow','mnet', array($username, $mnethosts[$form->mnet_host_id
])));
113 } elseif ($form->access
== 'deny') {
114 redirect('access_control.php', get_string('ssl_acl_deny','mnet', array($username, $mnethosts[$form->mnet_host_id
])));
124 print_box(get_string('ssoacldescr','mnet'));
125 // Are the needed bits enabled?
127 if (empty($CFG->mnet_dispatcher_mode
) ||
$CFG->mnet_dispatcher_mode
!== 'strict') {
128 $warn = '<p>' . get_string('mnetdisabled','mnet') .'</p>';
130 if (empty($CFG->auth_plugins_enabled
)) {
131 $warn .= '<p>' . get_string('authmnetdisabled','mnet').'</p>';
133 $auths = explode(',', $CFG->auth_plugins_enabled
);
134 if (!in_array('mnet', $auths)) {
135 $warn .= '<p>' . get_string('authmnetdisabled','mnet').'</p>';
139 if (get_config('auth/mnet', 'auto_add_remote_users') != true) {
140 $warn .= '<p>' . get_string('authmnetautoadddisabled','mnet').'</p>';
143 $warn = '<p>' . get_string('ssoaclneeds','mnet').'</p>' . $warn;
146 // output the ACL table
147 $columns = array("username", "mnet_host_id", "access", "delete");
149 $string = array('username' => get_string('username'),
150 'mnet_host_id' => get_string('remotehost', 'mnet'),
151 'access' => get_string('accesslevel', 'mnet'),
152 'delete' => get_string('delete'));
153 foreach ($columns as $column) {
154 if ($sort != $column) {
158 $columndir = $dir == "ASC" ?
"DESC" : "ASC";
159 $columnicon = $dir == "ASC" ?
"down" : "up";
160 $columnicon = " <img src=\"$CFG->pixpath/t/$columnicon.gif\" alt=\"\" />";
162 $headings[$column] = "<a href=\"?sort=$column&dir=$columndir&\">".$string[$column]."</a>$columnicon";
164 $headings['delete'] = '';
165 $acl = get_records('mnet_sso_access_control', '', '', "$sort $dir", '*'); //, $page * $perpage, $perpage);
166 $aclcount = count_records('mnet_sso_access_control');
169 print_heading(get_string('noaclentries','mnet'));
172 $table->head
= $headings;
173 $table->align
= array('left', 'left', 'center');
174 $table->width
= "95%";
175 foreach ($acl as $aclrecord) {
176 if ($aclrecord->access
== 'allow') {
177 $accesscolumn = get_string('allow', 'mnet')
178 . " (<a href=\"?id={$aclrecord->id}&action=acl&access=deny&sesskey={$USER->sesskey}\">"
179 . get_string('deny', 'mnet') . "</a>)";
181 $accesscolumn = get_string('deny', 'mnet')
182 . " (<a href=\"?id={$aclrecord->id}&action=acl&access=allow&sesskey={$USER->sesskey}\">"
183 . get_string('allow', 'mnet') . "</a>)";
185 $deletecolumn = "<a href=\"?id={$aclrecord->id}&action=delete&sesskey={$USER->sesskey}\">"
186 . get_string('delete') . "</a>";
187 $table->data
[] = array ($aclrecord->username
, $aclrecord->mnet_host_id
, $accesscolumn, $deletecolumn);
191 if (!empty($table)) {
193 echo '<p> </p>';
194 print_paging_bar($aclcount, $page, $perpage, "?sort=$sort&dir=$dir&perpage=$perpage&");
199 // output the add form
200 print_simple_box_start('center','90%','','20');
203 <div
class="mnetaddtoaclform">
204 <form id
="mnetaddtoacl" method
="post">
205 <input type
="hidden" name
="sesskey" value
="<?php echo $sesskey; ?>" />
209 echo get_string('username') . ":\n";
210 if (!empty($formerror['username'])) {
211 echo '<span class="error"> * </span>';
213 echo '<input type="text" name="username" size="20" maxlength="100" />';
215 // choose a remote host
216 echo " " . get_string('remotehost', 'mnet') . ":\n";
217 if (!empty($formerror['mnet_host_id'])) {
218 echo '<span class="error"> * </span>';
220 choose_from_menu($mnethosts, 'mnet_host_id');
222 // choose an access level
223 echo " " . get_string('accesslevel', 'mnet') . ":\n";
224 if (!empty($formerror['access'])) {
225 echo '<span class="error"> * </span>';
227 $accessmenu['allow'] = get_string('allow', 'mnet');
228 $accessmenu['deny'] = get_string('deny', 'mnet');
229 choose_from_menu($accessmenu, 'access');
232 echo '<input type="submit" value="' . get_string('addtoacl', 'mnet') . '" />';
233 echo "</form></div>\n";
236 foreach ($formerror as $error) {
237 echo "<br><span class=\"error\">$error<span>";
240 print_simple_box_end();
241 admin_externalpage_print_footer($adminroot);