<?php $source = get_input('source'); $user_guid = (int) get_input('user_guid'); if (empty($source) || empty($user_guid)) { register_error(elgg_echo('error:missing_data')); forward(REFERER); } $user = get_user($user_guid); if (empty($user) || !$user->canEdit()) { register_error(elgg_echo('actionunauthorized')); forward(REFERER); } $label = simplesaml_get_source_label($source); if (!simplesaml_is_enabled_source($source)) { register_error(elgg_echo('simplesaml:error:source_not_enabled', [$label])); forward(REFERER); } if (simplesaml_unlink_user($user, $source)) { system_message(elgg_echo('simplesaml:action:unlink:success', [$label])); } else { register_error(elgg_echo('simplesaml:action:unlink:error', [$label])); } forward(REFERER);
/** * Link a user to a Service Provider (SP), so in the future the user can login using this SP. * * @param ElggUser $user the user to link * @param string $saml_source the name of the SP * @param string $saml_uid the unique ID of the user on the IDentity Provider side * * @return bool true is the user is successfully linked, false on failure */ function simplesaml_link_user(ElggUser $user, $saml_source, $saml_uid) { $result = false; if (!empty($user) && elgg_instanceof($user, "user", null, "ElggUser") && !empty($saml_source) && !empty($saml_uid)) { if (simplesaml_is_enabled_source($saml_source)) { // remove links from other users $options = array("type" => "user", "limit" => false, "site_guids" => false, "plugin_id" => "simplesaml", "plugin_user_setting_name_value_pairs" => array($saml_source . "_uid" => $saml_uid)); $users = elgg_get_entities_from_plugin_user_settings($options); if (!empty($users)) { foreach ($users as $other_user) { simplesaml_unlink_user($other_user, $saml_source); } } // now save the setting for this user $result = elgg_set_plugin_user_setting($saml_source . "_uid", $saml_uid, $user->getGUID(), "simplesaml"); } } return $result; }
/** * Link a user to a Service Provider (SP), so in the future the user can login using this SP. * * @param ElggUser $user the user to link * @param string $saml_source the name of the SP * @param string $saml_uid the unique ID of the user on the IDentity Provider side * * @return bool */ function simplesaml_link_user(ElggUser $user, $saml_source, $saml_uid) { if (!$user instanceof ElggUser || empty($saml_source) || empty($saml_uid)) { return false; } if (!simplesaml_is_enabled_source($saml_source)) { return false; } // remove links from other users $options = ['type' => 'user', 'limit' => false, 'site_guids' => false, 'plugin_id' => 'simplesaml', 'plugin_user_setting_name_value_pairs' => [$saml_source . '_uid' => $saml_uid]]; $users = new ElggBatch('elgg_get_entities_from_plugin_user_settings', $options); $users->setIncrementOffset(false); foreach ($users as $other_user) { simplesaml_unlink_user($other_user, $saml_source); } // now save the setting for this user return elgg_set_plugin_user_setting("{$saml_source}_uid", $saml_uid, $user->getGUID(), 'simplesaml'); }