1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* MoodleNet authorization.
|
|
|
18 |
*
|
|
|
19 |
* @module core/moodlenet/authorize
|
|
|
20 |
* @copyright 2023 Huong Nguyen <huongnv13@gmail.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
* @since 4.3
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
import {alert as displayAlert, exception as displayException} from 'core/notification';
|
|
|
26 |
import * as MoodleNetService from 'core/moodlenet/service';
|
|
|
27 |
import {sendToMoodleNet} from 'core/moodlenet/send_resource';
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Handle authorization with MoodleNet server.
|
|
|
31 |
*
|
|
|
32 |
* @param {int} issuerId The OAuth 2 issuer ID.
|
|
|
33 |
* @param {int} courseId Course id.
|
|
|
34 |
* @param {int} resourceId Resource id.
|
|
|
35 |
* @param {int} shareFormat Share format.
|
|
|
36 |
* @return {promise}
|
|
|
37 |
*/
|
|
|
38 |
export const handleAuthorization = (issuerId, courseId, resourceId, shareFormat) => {
|
|
|
39 |
const windowSizeWidth = 550;
|
|
|
40 |
const windowSizeHeight = 550;
|
|
|
41 |
|
|
|
42 |
// Check if the user is authorized with MoodleNet or not.
|
|
|
43 |
return MoodleNetService.authorizationCheck(issuerId, courseId).then(async(data) => {
|
|
|
44 |
if (!data.status) {
|
|
|
45 |
// Not yet authorized.
|
|
|
46 |
// Declare moodleNetAuthorize variable, so we can call it later in the callback.
|
|
|
47 |
window.moodleNetAuthorize = (error, errorDescription) => {
|
|
|
48 |
// This will be called by the callback after the authorization is successful.
|
|
|
49 |
if (error === '') {
|
|
|
50 |
handleAuthorization(issuerId, courseId, resourceId, shareFormat);
|
|
|
51 |
} else if (error !== 'access_denied') {
|
|
|
52 |
displayAlert(
|
|
|
53 |
'Authorization error',
|
|
|
54 |
'Error: ' + error + '<br><br>Error description: ' + errorDescription,
|
|
|
55 |
'Cancel'
|
|
|
56 |
);
|
|
|
57 |
}
|
|
|
58 |
};
|
|
|
59 |
// Open the login url of the OAuth 2 issuer for user to login into MoodleNet and authorize.
|
|
|
60 |
return window.open(data.loginurl, 'moodlenet_auth',
|
|
|
61 |
`location=0,status=0,width=${windowSizeWidth},height=${windowSizeHeight},scrollbars=yes`);
|
|
|
62 |
} else {
|
|
|
63 |
// Already authorized.
|
|
|
64 |
return sendToMoodleNet(issuerId, resourceId, shareFormat);
|
|
|
65 |
}
|
|
|
66 |
}).catch(displayException);
|
|
|
67 |
};
|