Skip to content

Commit

Permalink
MSFTMPP-404: Split auth/oidc:manageconnection permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Verge authored and Akinsaya Delamarre committed May 20, 2016
1 parent e1f3164 commit 75107d4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 14 deletions.
14 changes: 13 additions & 1 deletion db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,16 @@
'contextlevel' => CONTEXT_USER,
'archetypes' => []
],
];
'auth/oidc:manageconnectionconnect' => [
'riskbitmask' => RISK_CONFIG,
'captype' => 'write',
'contextlevel' => CONTEXT_USER,
'archetypes' => []
],
'auth/oidc:manageconnectiondisconnect' => [
'riskbitmask' => RISK_CONFIG,
'captype' => 'write',
'contextlevel' => CONTEXT_USER,
'archetypes' => []
],
];
4 changes: 3 additions & 1 deletion lang/en/auth_oidc.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@
$string['eventuserloggedin'] = 'User Logged In with OpenID Connect';
$string['eventuserdisconnected'] = 'User disconnected from OpenID Connect';

$string['oidc:manageconnection'] = 'Manage OpenID Connect Connection';
$string['oidc:manageconnection'] = 'Allow OpenID Connection and Disconnection';
$string['oidc:manageconnectionconnect'] = 'Allow OpenID Connection';
$string['oidc:manageconnectiondisconnect'] = 'Allow OpenID Disconnection';

// In the following strings, $a refers to a customizable name for the identity manager. For example, this could be
// "Office 365", "OpenID Connect", etc.
Expand Down
41 changes: 41 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,44 @@ function auth_oidc_initialize_customicon($filefullname) {
theme_reset_all_caches();
}
}

/**
* Check for connection abilities.
*
* @param int $userid Moodle user id to check permissions for.
* @param string $mode Mode to check
* 'connect' to check for connect specific capability
* 'disconnect' to check for disconnect capability.
* 'both' to check for disconnect and connect capability.
* @param boolean $require Use require_capability rather than has_capability.
* @return boolean True if has capability.
*/
function auth_oidc_connectioncapability($userid, $mode = 'connect', $require = false) {
$check = 'has_capability';
if ($require) {
// If requiring the capability and user has manageconnection than checking connect and disconnect is not needed.
$check = 'require_capability';
if (has_capability('auth/oidc:manageconnection', \context_user::instance($userid), $userid)) {
return true;
}
} else if ($check('auth/oidc:manageconnection', \context_user::instance($userid), $userid)) {
return true;
}

$result = false;
switch ($mode) {
case "connect":
$result = $check('auth/oidc:manageconnectionconnect', \context_user::instance($userid), $userid);
break;
case "disconnect":
$result = $check('auth/oidc:manageconnectiondisconnect', \context_user::instance($userid), $userid);
break;
case "both":
$result = $check('auth/oidc:manageconnectionconnect', \context_user::instance($userid), $userid);
$result = $result && $check('auth/oidc:manageconnectiondisconnect', \context_user::instance($userid), $userid);
}
if ($require) {
return true;
}
return $result;
}
27 changes: 16 additions & 11 deletions ucp.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@

require_once(__DIR__.'/../../config.php');
require_once(__DIR__.'/auth.php');
require_once(__DIR__.'/lib.php');

require_login();

require_capability('auth/oidc:manageconnection', \context_user::instance($USER->id), $USER->id);

$action = optional_param('action', null, PARAM_TEXT);

$oidctoken = $DB->get_record('auth_oidc_token', ['username' => $USER->username]);
Expand All @@ -41,11 +40,13 @@
if (!is_enabled_auth('oidc')) {
throw new \moodle_exception('erroroidcnotenabled', 'auth_oidc');
}
auth_oidc_connectioncapability($USER->id, 'connect', true);
$auth = new \auth_oidc\loginflow\authcode;
$auth->set_httpclient(new \auth_oidc\httpclient());
$auth->initiateauthrequest();
} else if ($action === 'disconnectlogin' && $oidcloginconnected === true) {
if (is_enabled_auth('manual') === true) {
auth_oidc_connectioncapability($USER->id, 'disconnect', true);
$auth = new \auth_plugin_oidc;
$auth->set_httpclient(new \auth_oidc\httpclient());
$auth->disconnect();
Expand Down Expand Up @@ -77,18 +78,22 @@
if ($oidcloginconnected === true) {
echo \html_writer::tag('h4', get_string('ucp_status_enabled', 'auth_oidc'), ['class' => 'notifysuccess']);
if (is_enabled_auth('manual') === true) {
$connectlinkuri = new \moodle_url('/auth/oidc/ucp.php', ['action' => 'disconnectlogin']);
$strdisconnect = get_string('ucp_login_stop', 'auth_oidc', $opname);
$linkhtml = \html_writer::link($connectlinkuri, $strdisconnect);
echo \html_writer::tag('h5', $linkhtml);
echo \html_writer::span(get_string('ucp_login_stop_desc', 'auth_oidc', $opname));
if (auth_oidc_connectioncapability($USER->id, 'disconnect')) {
$connectlinkuri = new \moodle_url('/auth/oidc/ucp.php', ['action' => 'disconnectlogin']);
$strdisconnect = get_string('ucp_login_stop', 'auth_oidc', $opname);
$linkhtml = \html_writer::link($connectlinkuri, $strdisconnect);
echo \html_writer::tag('h5', $linkhtml);
echo \html_writer::span(get_string('ucp_login_stop_desc', 'auth_oidc', $opname));
}
}
} else {
echo \html_writer::tag('h4', get_string('ucp_status_disabled', 'auth_oidc'), ['class' => 'notifyproblem']);
$connectlinkuri = new \moodle_url('/auth/oidc/ucp.php', ['action' => 'connectlogin']);
$linkhtml = \html_writer::link($connectlinkuri, get_string('ucp_login_start', 'auth_oidc', $opname));
echo \html_writer::tag('h5', $linkhtml);
echo \html_writer::span(get_string('ucp_login_start_desc', 'auth_oidc', $opname));
if (auth_oidc_connectioncapability($USER->id, 'connect')) {
$connectlinkuri = new \moodle_url('/auth/oidc/ucp.php', ['action' => 'connectlogin']);
$linkhtml = \html_writer::link($connectlinkuri, get_string('ucp_login_start', 'auth_oidc', $opname));
echo \html_writer::tag('h5', $linkhtml);
echo \html_writer::span(get_string('ucp_login_start_desc', 'auth_oidc', $opname));
}
}
echo \html_writer::end_div();

Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2015111907;
$plugin->version = 2015111908;
$plugin->requires = 2015111600;
$plugin->component = 'auth_oidc';
$plugin->maturity = MATURITY_STABLE;
Expand Down

0 comments on commit 75107d4

Please sign in to comment.