auth_plugin_ws::call_ws()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
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
/**
18
 * Authentication Plugin: External Webservice Authentication
19
 *
20
 * Checks against an external webservice.
21
 *
22
 * @package    auth_ws
23
 * @author     Daniel Neis Araujo
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
25
 */
26
27
defined('MOODLE_INTERNAL') || die();
28
29
require_once($CFG->libdir.'/authlib.php');
30
31
/**
32
 * External webservice authentication plugin.
33
 */
34
class auth_plugin_ws extends auth_plugin_base {
35
36
    /**
37
     * Constructor.
38
     */
39
    public function __construct() {
40
        $this->authtype = 'ws';
41
        $this->config = get_config('auth_ws');
42
43
        if (isset($this->config->default_params) && !empty($this->config->default_params)) {
44
            $params = explode(',', $this->config->default_params);
45
            $defaultparams = array();
46
            foreach ($params as $p) {
47
                list($paramname, $value) = explode(':', $p);
48
                $defaultparams[$paramname] = $value;
49
            }
50
            $this->config->ws_default_params = $defaultparams;
51
        } else {
52
            $this->config->ws_default_params = array();
53
        }
54
    }
55
56
    /**
57
     * Returns true if the username and password work and false if they are
58
     * wrong or don't exist.
59
     *
60
     * @param string $username The username
61
     * @param string $password The password
62
     * @return bool Authentication success or failure.
63
     */
64
    public function user_login($username, $password) {
65
66
        $functionname = $this->config->auth_function;
67
        $params  = array($this->config->auth_function_username_paramname => $username,
68
                         $this->config->auth_function_password_paramname => $password);
69
70
        $result = $this->call_ws($this->config->serverurl, $functionname, $params);
71
72
        return ($result->{$this->config->auth_function_resultClass}->{$this->config->auth_function_resultField} == true);
73
    }
74
75
    /**
76
     * This plugin is intended only to authenticate users.
77
     * User synchronization must be done by external service,
78
     * using Moodle's webservices.
79
     *
80
     * @param progress_trace $trace
81
     * @param bool $doupdates  Optional: set to true to force an update of existing accounts
82
     * @return int 0 means success, 1 means failure
83
     */
84
    public function sync_users(progress_trace $trace, $doupdates = false) {
85
        return true;
86
    }
87
88
    public function get_userinfo($username) {
89
        return array();
90
    }
91
92
    private function call_ws($serverurl, $functionname, $params = array()) {
93
94
        $serverurl = $serverurl . '?wsdl';
95
96
        $params = array_merge($this->config->ws_default_params, $params);
97
98
        $client = new SoapClient($serverurl);
99
        try {
100
            $resp = $client->__soapCall($functionname, array($params));
101
102
            return $resp;
103
        } catch (Exception $e) {
104
            echo "Exception:\n";
105
            echo $e->getMessage();
106
            echo "===\n";
107
            return false;
108
        }
109
    }
110
111
    public function prevent_local_passwords() {
112
        return true;
113
    }
114
115
    /**
116
     * Returns true if this authentication plugin is "internal".
117
     *
118
     * Internal plugins use password hashes from Moodle user table for authentication.
119
     *
120
     * @return bool
121
     */
122
    public function is_internal() {
123
        return false;
124
    }
125
126
    /**
127
     * Indicates if moodle should automatically update internal user
128
     * records with data from external sources using the information
129
     * from auth_plugin_base::get_userinfo().
130
     * The external service is responsible to update user records.
131
     *
132
     * @return bool true means automatically copy data from ext to user table
133
     */
134
    public function is_synchronised_with_external() {
135
        return false;
136
    }
137
138
    /**
139
     * Returns true if this authentication plugin can change the user's
140
     * password.
141
     *
142
     * @return bool
143
     */
144
    public function can_change_password() {
145
        return false;
146
    }
147
148
    /**
149
     * Returns the URL for changing the user's pw, or empty if the default can
150
     * be used.
151
     *
152
     * @return moodle_url
153
     */
154
    public function change_password_url() {
155
        if (isset($this->config->changepasswordurl) && !empty($this->config->changepasswordurl)) {
156
            return new moodle_url($this->config->changepasswordurl);
157
        } else {
158
            return null;
159
        }
160
    }
161
162
    /**
163
     * Returns true if plugin allows resetting of internal password.
164
     *
165
     * @return bool
166
     */
167
    public function can_reset_password() {
168
        return false;
169
    }
170
}
171