Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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\router\parameters;
18
 
19
use core\exception\not_found_exception;
20
use core\param;
21
use core\user;
22
use core\router\schema\example;
23
use core\router\schema\parameters\mapped_property_parameter;
24
use core\router\schema\referenced_object;
25
use Psr\Http\Message\ServerRequestInterface;
26
 
27
/**
28
 * A parameter representing a user.
29
 *
30
 * @package    core
31
 * @copyright  Andrew Lyons <andrew@nicols.co.uk>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class path_user extends \core\router\schema\parameters\path_parameter implements
35
    mapped_property_parameter,
36
    referenced_object
37
{
38
    /**
39
     * Create a new instance of the path_user.
40
     *
41
     * @param string $name The name of the parameter to use for the identifier
42
     * @param mixed ...$extra Additional arguments
43
     */
44
    public function __construct(
45
        string $name = 'user',
46
        ...$extra,
47
    ) {
48
        $extra['name'] = $name;
49
        $extra['type'] = param::RAW;
50
        $extra['description'] = <<<EOF
51
        The user identifier.
52
 
53
        This can be the magic string 'current', or the user's id, idnumber, or username.
54
 
55
        If specifying an id, the value should be in the format `id:[id]`.
56
 
57
        If specifying an idnumber, the value should be in the format `idnumber:[idnumber]`.
58
 
59
        If specifying a username, the value should be in the format `username:[username]`.
60
        EOF;
61
        $extra['examples'] = [
62
            new example(
63
                name: 'The current user',
64
                value: 'current',
65
            ),
66
            new example(
67
                name: 'A user specified by their user id',
68
                value: '94853',
69
            ),
70
            new example(
71
                name: 'A user specified by their idnumber',
72
                value: 'idnumber:some-student-idnumber               ',
73
            ),
74
            new example(
75
                name: 'A user specified by their username',
76
                value: 'username:lyona1',
77
            ),
78
        ];
79
 
80
        parent::__construct(...$extra);
81
    }
82
 
83
    /**
84
     * Get the user object for the given identifier.
85
     *
86
     * @param string $value A user id, idnumber, or username
87
     * @return object
88
     * @throws not_found_exception If the user cannot be found
89
     */
90
    protected function get_user_for_value(string $value): mixed {
91
        global $USER;
92
 
93
        if ($value === 'current') {
94
            return $USER;
95
        }
96
 
97
        $data = null;
98
        if (is_numeric($value)) {
99
            $data = user::get_user($value);
100
        } else if (str_starts_with($value, 'idnumber:')) {
101
            $data = user::get_user_by_idnumber(substr($value, strlen('idnumber:')));
102
        } else if (str_starts_with($value, 'username:')) {
103
            $data = user::get_user_by_username(substr($value, strlen('username:')));
104
        }
105
 
106
        if ($data) {
107
            return $data;
108
        }
109
        throw new not_found_exception('user', $value);
110
    }
111
 
112
    #[\Override]
113
    public function add_attributes_for_parameter_value(
114
        ServerRequestInterface $request,
115
        string $value,
116
    ): ServerRequestInterface {
117
        $user = $this->get_user_for_value($value);
118
 
119
        $request = $request->withAttribute($this->name, $user);
120
 
121
        if ($user->id) {
122
            $request = $request->withAttribute("{$this->name}context", \core\context\user::instance($user->id));
123
        }
124
 
125
        return $request;
126
    }
127
 
128
    #[\Override]
129
    public function get_schema_from_type(param $type): \stdClass {
130
        $schema = parent::get_schema_from_type($type);
131
 
132
        $schema->pattern = "^(";
133
        $schema->pattern .= implode("|", [
134
            'current',
135
            '\d+',
136
            'idnumber:.+',
137
            'username:.+',
138
        ]);
139
        $schema->pattern .= ")$";
140
 
141
        return $schema;
142
    }
143
}