Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core\external;
18
 
19
use context_course;
20
use core\moodlenet\moodlenet_client;
21
use core\moodlenet\utilities;
22
use core\oauth2\api;
23
use core_external\external_api;
24
use core_external\external_function_parameters;
25
use core_external\external_single_structure;
26
use core_external\external_value;
27
use core_external\external_warnings;
28
use moodle_url;
29
 
30
/**
31
 * The external API to check whether a user has authorized for a given MoodleNet OAuth 2 issuer.
32
 *
33
 * @package    core
34
 * @copyright  2023 Huong Nguyen <huongnv13@gmail.com>
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class moodlenet_auth_check extends external_api {
38
 
39
    /**
40
     * Returns description of parameters.
41
     *
42
     * @return external_function_parameters
43
     * @since Moodle 4.2
44
     */
45
    public static function execute_parameters(): external_function_parameters {
46
        return new external_function_parameters([
47
            'issuerid' => new external_value(PARAM_INT, 'OAuth 2 issuer ID', VALUE_REQUIRED),
48
            'courseid' => new external_value(PARAM_INT, 'Course ID', VALUE_REQUIRED),
49
        ]);
50
    }
51
 
52
    /**
53
     * External function to check if the user is already authorized with MoodleNet.
54
     *
55
     * @param int $issuerid Issuer Id.
56
     * @param int $courseid The course ID that contains the activity which being shared
57
     * @return array
58
     * @since Moodle 4.2
59
     */
60
    public static function execute(int $issuerid, int $courseid): array {
61
        global $USER;
62
        [
63
            'issuerid' => $issuerid,
64
            'courseid' => $courseid,
65
        ] = self::validate_parameters(self::execute_parameters(), [
66
            'issuerid' => $issuerid,
67
            'courseid' => $courseid,
68
        ]);
69
 
70
        // Check capability.
71
        $coursecontext = context_course::instance($courseid);
72
        $usercanshareactivity = utilities::can_user_share($coursecontext, $USER->id, 'activity');
73
        $usercansharecourse = utilities::can_user_share($coursecontext, $USER->id, 'course');
74
        if (!$usercanshareactivity && !$usercansharecourse) {
75
            return self::return_errors($courseid, 'errorpermission',
76
                get_string('nopermissions', 'error', get_string('moodlenet:sharetomoodlenet', 'moodle')));
77
        }
78
 
79
        // Get the issuer.
80
        $issuer = api::get_issuer($issuerid);
81
        // Validate the issuer and check if it is enabled or not.
82
        if (!utilities::is_valid_instance($issuer)) {
83
            return self::return_errors($issuerid, 'errorissuernotenabled', get_string('invalidparameter', 'debug'));
84
        }
85
 
86
        $returnurl = new moodle_url('/admin/moodlenet_oauth2_callback.php');
87
        $returnurl->param('issuerid', $issuerid);
88
        $returnurl->param('callback', 'yes');
89
        $returnurl->param('sesskey', sesskey());
90
 
91
        // Get the OAuth Client.
92
        if (!$oauthclient = api::get_user_oauth_client($issuer, $returnurl, moodlenet_client::API_SCOPE_CREATE_RESOURCE, true)) {
93
            return self::return_errors($issuerid, 'erroroauthclient', get_string('invalidparameter', 'debug'));
94
        }
95
 
96
        $status = false;
97
        $warnings = [];
98
        $loginurl = '';
99
 
100
        if (!$oauthclient->is_logged_in()) {
101
            $loginurl = $oauthclient->get_login_url()->out(false);
102
        } else {
103
            $status = true;
104
        }
105
 
106
        return [
107
            'status' => $status,
108
            'loginurl' => $loginurl,
109
            'warnings' => $warnings,
110
        ];
111
    }
112
 
113
    /**
114
     * Describes the data returned from the external function.
115
     *
116
     * @return external_single_structure
117
     * @since Moodle 4.2
118
     */
119
    public static function execute_returns(): external_single_structure {
120
        return new external_single_structure([
121
            'loginurl' => new external_value(PARAM_RAW, 'Login url'),
122
            'status' => new external_value(PARAM_BOOL, 'status: true if success'),
123
            'warnings' => new external_warnings(),
124
        ]);
125
    }
126
 
127
    /**
128
     * Handle return error.
129
     *
130
     * @param int $itemid Item id
131
     * @param string $warningcode Warning code
132
     * @param string $message Message
133
     * @return array
134
     */
135
    protected static function return_errors(int $itemid, string $warningcode, string $message): array {
136
        $warnings[] = [
137
            'item' => $itemid,
138
            'warningcode' => $warningcode,
139
            'message' => $message,
140
        ];
141
 
142
        return [
143
            'status' => false,
144
            'loginurl' => '',
145
            'warnings' => $warnings,
146
        ];
147
    }
148
}