integration_moodle/lib/Controller/ConfigController.php
Julien Veyssier a5e3fc7298
almost there
Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
2020-09-02 19:57:18 +02:00

117 lines
4.2 KiB
PHP

<?php
/**
* Nextcloud - moodle
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Julien Veyssier <eneiluj@posteo.net>
* @copyright Julien Veyssier 2020
*/
namespace OCA\Moodle\Controller;
use OCP\App\IAppManager;
use OCP\Files\IAppData;
use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\IURLGenerator;
use OCP\IConfig;
use OCP\IServerContainer;
use OCP\IL10N;
use OCP\ILogger;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\IRequest;
use OCP\IDBConnection;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCA\Moodle\Service\MoodleAPIService;
use OCA\Moodle\AppInfo\Application;
class ConfigController extends Controller {
private $userId;
private $config;
private $dbconnection;
private $dbtype;
public function __construct($AppName,
IRequest $request,
IServerContainer $serverContainer,
IConfig $config,
IAppManager $appManager,
IAppData $appData,
IDBConnection $dbconnection,
IURLGenerator $urlGenerator,
IL10N $l,
ILogger $logger,
MoodleAPIService $moodleAPIService,
$userId) {
parent::__construct($AppName, $request);
$this->l = $l;
$this->userId = $userId;
$this->appData = $appData;
$this->serverContainer = $serverContainer;
$this->config = $config;
$this->dbconnection = $dbconnection;
$this->urlGenerator = $urlGenerator;
$this->logger = $logger;
$this->moodleAPIService = $moodleAPIService;
}
/**
* set config values
* @NoAdminRequired
*/
public function setConfig($values) {
foreach ($values as $key => $value) {
$this->config->setUserValue($this->userId, Application::APP_ID, $key, $value);
}
$response = new DataResponse(1);
return $response;
}
/**
* receive oauth code and get oauth access token
* @NoAdminRequired
* @NoCSRFRequired
*/
public function oauthRedirect($code = '') {
$moodleUrl = $this->config->getUserValue($this->userId, Application::APP_ID, 'url', '');
$clientID = $this->config->getUserValue($this->userId, Application::APP_ID, 'client_id', '');
$clientSecret = $this->config->getUserValue($this->userId, Application::APP_ID, 'client_secret', '');
if ($moodleUrl !== '' and $clientID !== '' and $clientSecret !== '' and $code !== '') {
$redirect_uri = $this->urlGenerator->linkToRouteAbsolute('integration_moodle.config.oauthRedirect');
$result = $this->moodleAPIService->requestOAuthAccessToken($moodleUrl, [
'client_id' => $clientID,
'client_secret' => $clientSecret,
'code' => $code,
'redirect_uri' => $redirect_uri,
'grant_type' => 'authorization_code',
'scope' => 'read write follow'
], 'POST');
if (is_array($result) and isset($result['access_token'])) {
$accessToken = $result['access_token'];
$this->config->setUserValue($this->userId, Application::APP_ID, 'token', $accessToken);
return new RedirectResponse(
$this->urlGenerator->linkToRoute('settings.PersonalSettings.index', ['section' => 'linked-accounts']) .
'?moodleToken=success'
);
}
$result = $this->l->t('Error getting OAuth access token') . ' ' . $result;
} else {
$result = $this->l->t('Error during OAuth exchanges');
}
return new RedirectResponse(
$this->urlGenerator->linkToRoute('settings.PersonalSettings.index', ['section' => 'linked-accounts']) .
'?moodleToken=error&message=' . urlencode($result)
);
}
}