Merge branch 'MDL-53397_master' of git://github.com/dmonllao/moodle
[moodle.git] / auth / fc / auth.php
blobb9879923c7b99afef3648877b92910dbf3f0659d
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.
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/>.
17 /**
18 * Authentication Plugin: FirstClass Authentication
19 * Authentication using a FirstClass server.
21 * @package auth_fc
22 * @author Martin Dougiamas
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->libdir.'/authlib.php');
30 require_once 'fcFPP.php';
32 /**
33 * FirstClass authentication plugin.
35 class auth_plugin_fc extends auth_plugin_base {
37 /**
38 * Constructor.
40 public function __construct() {
41 $this->authtype = 'fc';
42 $this->config = get_config('auth/fc');
45 /**
46 * Old syntax of class constructor. Deprecated in PHP7.
48 * @deprecated since Moodle 3.1
50 public function auth_plugin_fc() {
51 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
52 self::__construct();
55 /**
56 * Returns true if the username and password work and false if they are
57 * wrong or don't exist.
59 * @param string $username The username
60 * @param string $password The password
61 * @return bool Authentication success or failure.
63 function user_login ($username, $password) {
64 global $CFG;
65 $retval = false;
67 // Don't allow blank usernames or passwords
68 if (!$username or !$password) {
69 return $retval;
72 $fpp = new fcFPP($this->config->host, $this->config->fppport);
73 if ($fpp->open()) {
74 if ($fpp->login($username, $password)) {
75 $retval = true;
78 $fpp->close();
80 return $retval;
83 /**
84 * Get user information from FirstCLass server and return it in an array.
85 * Localize this routine to fit your needs.
87 function get_userinfo($username) {
89 Moodle FirstCLass fieldID in UserInfo form
90 ------ -----------------------------------
91 firstname 1202
92 lastname 1204
93 email 1252
94 icq -
95 phone1 1206
96 phone2 1207 (Fax)
97 institution -
98 department -
99 address 1205
100 city -
101 country -
102 lang -
103 timezone 8030 (Not used yet. Need to figure out how FC codes timezones)
105 description Get data from users resume. Pictures will be removed.
109 $userinfo = array();
111 $fpp = new fcFPP($this->config->host, $this->config->fppport);
112 if ($fpp->open()) {
113 if ($fpp->login($this->config->userid, $this->config->passwd)) {
114 $userinfo['firstname'] = $fpp->getUserInfo($username,"1202");
115 $userinfo['lastname'] = $fpp->getUserInfo($username,"1204");
116 $userinfo['email'] = strtok($fpp->getUserInfo($username,"1252"),',');
117 $userinfo['phone1'] = $fpp->getUserInfo($username,"1206");
118 $userinfo['phone2'] = $fpp->getUserInfo($username,"1207");
119 $userinfo['description'] = $fpp->getResume($username);
122 $fpp->close();
124 foreach($userinfo as $key => $value) {
125 if (!$value) {
126 unset($userinfo[$key]);
130 return $userinfo;
134 * Get users group membership from the FirstClass server user and check if
135 * user is member of one of the groups of creators.
137 function iscreator($username) {
138 if (! $this->config->creators) {
139 return null;
142 $fcgroups = array();
144 $fpp = new fcFPP($this->config->host, $this->config->fppport);
145 if ($fpp->open()) {
146 if ($fpp->login($this->config->userid, $this->config->passwd)) {
147 $fcgroups = $fpp->getGroups($username);
150 $fpp->close();
152 if ((! $fcgroups)) {
153 return false;
156 $creators = explode(";", $this->config->creators);
158 foreach($creators as $creator) {
159 if (in_array($creator, $fcgroups)) {
160 return true;
164 return false;
167 function prevent_local_passwords() {
168 return true;
172 * Returns true if this authentication plugin is 'internal'.
174 * @return bool
176 function is_internal() {
177 return false;
181 * Returns true if this authentication plugin can change the user's
182 * password.
184 * @return bool
186 function can_change_password() {
187 return false;
191 * Sync roles for this user
193 * @param $user object user object (without system magic quotes)
195 function sync_roles($user) {
196 $iscreator = $this->iscreator($user->username);
197 if ($iscreator === null) {
198 return; //nothing to sync - creators not configured
201 if ($roles = get_archetype_roles('coursecreator')) {
202 $creatorrole = array_shift($roles); // We can only use one, let's use the first one
203 $systemcontext = context_system::instance();
205 if ($iscreator) { // Following calls will not create duplicates
206 role_assign($creatorrole->id, $user->id, $systemcontext->id, 'auth_fc');
207 } else {
208 //unassign only if previously assigned by this plugin!
209 role_unassign($creatorrole->id, $user->id, $systemcontext->id, 'auth_fc');
215 * Prints a form for configuring this authentication plugin.
217 * This function is called from admin/auth.php, and outputs a full page with
218 * a form for configuring this plugin.
220 * @param array $page An object containing all the data for this page.
222 function config_form($config, $err, $user_fields) {
223 include "config.html";
227 * Processes and stores configuration data for this authentication plugin.
229 function process_config($config) {
230 // set to defaults if undefined
231 if (!isset($config->host)) {
232 $config->host = "127.0.0.1";
234 if (!isset($config->fppport)) {
235 $config->fppport = "3333";
237 if (!isset($config->userid)) {
238 $config->userid = "fcMoodle";
240 if (!isset($config->passwd)) {
241 $config->passwd = "";
243 if (!isset($config->creators)) {
244 $config->creators = "";
246 if (!isset($config->changepasswordurl)) {
247 $config->changepasswordurl = '';
250 // save settings
251 set_config('host', $config->host, 'auth/fc');
252 set_config('fppport', $config->fppport, 'auth/fc');
253 set_config('userid', $config->userid, 'auth/fc');
254 set_config('passwd', $config->passwd, 'auth/fc');
255 set_config('creators', $config->creators, 'auth/fc');
256 set_config('changepasswordurl', $config->changepasswordurl, 'auth/fc');
258 return true;