Merge branch 'wip-mdl-50259' of https://github.com/rajeshtaneja/moodle
[moodle.git] / auth / fc / auth.php
blob75c8bd073b54913e72a09d05e7241bbe1a8cfebe
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 function auth_plugin_fc() {
41 $this->authtype = 'fc';
42 $this->config = get_config('auth/fc');
45 /**
46 * Returns true if the username and password work and false if they are
47 * wrong or don't exist.
49 * @param string $username The username
50 * @param string $password The password
51 * @return bool Authentication success or failure.
53 function user_login ($username, $password) {
54 global $CFG;
55 $retval = false;
57 // Don't allow blank usernames or passwords
58 if (!$username or !$password) {
59 return $retval;
62 $fpp = new fcFPP($this->config->host, $this->config->fppport);
63 if ($fpp->open()) {
64 if ($fpp->login($username, $password)) {
65 $retval = true;
68 $fpp->close();
70 return $retval;
73 /**
74 * Get user information from FirstCLass server and return it in an array.
75 * Localize this routine to fit your needs.
77 function get_userinfo($username) {
79 Moodle FirstCLass fieldID in UserInfo form
80 ------ -----------------------------------
81 firstname 1202
82 lastname 1204
83 email 1252
84 icq -
85 phone1 1206
86 phone2 1207 (Fax)
87 institution -
88 department -
89 address 1205
90 city -
91 country -
92 lang -
93 timezone 8030 (Not used yet. Need to figure out how FC codes timezones)
95 description Get data from users resume. Pictures will be removed.
99 $userinfo = array();
101 $fpp = new fcFPP($this->config->host, $this->config->fppport);
102 if ($fpp->open()) {
103 if ($fpp->login($this->config->userid, $this->config->passwd)) {
104 $userinfo['firstname'] = $fpp->getUserInfo($username,"1202");
105 $userinfo['lastname'] = $fpp->getUserInfo($username,"1204");
106 $userinfo['email'] = strtok($fpp->getUserInfo($username,"1252"),',');
107 $userinfo['phone1'] = $fpp->getUserInfo($username,"1206");
108 $userinfo['phone2'] = $fpp->getUserInfo($username,"1207");
109 $userinfo['description'] = $fpp->getResume($username);
112 $fpp->close();
114 foreach($userinfo as $key => $value) {
115 if (!$value) {
116 unset($userinfo[$key]);
120 return $userinfo;
124 * Get users group membership from the FirstClass server user and check if
125 * user is member of one of the groups of creators.
127 function iscreator($username) {
128 if (! $this->config->creators) {
129 return null;
132 $fcgroups = array();
134 $fpp = new fcFPP($this->config->host, $this->config->fppport);
135 if ($fpp->open()) {
136 if ($fpp->login($this->config->userid, $this->config->passwd)) {
137 $fcgroups = $fpp->getGroups($username);
140 $fpp->close();
142 if ((! $fcgroups)) {
143 return false;
146 $creators = explode(";", $this->config->creators);
148 foreach($creators as $creator) {
149 if (in_array($creator, $fcgroups)) {
150 return true;
154 return false;
157 function prevent_local_passwords() {
158 return true;
162 * Returns true if this authentication plugin is 'internal'.
164 * @return bool
166 function is_internal() {
167 return false;
171 * Returns true if this authentication plugin can change the user's
172 * password.
174 * @return bool
176 function can_change_password() {
177 return false;
181 * Sync roles for this user
183 * @param $user object user object (without system magic quotes)
185 function sync_roles($user) {
186 $iscreator = $this->iscreator($user->username);
187 if ($iscreator === null) {
188 return; //nothing to sync - creators not configured
191 if ($roles = get_archetype_roles('coursecreator')) {
192 $creatorrole = array_shift($roles); // We can only use one, let's use the first one
193 $systemcontext = context_system::instance();
195 if ($iscreator) { // Following calls will not create duplicates
196 role_assign($creatorrole->id, $user->id, $systemcontext->id, 'auth_fc');
197 } else {
198 //unassign only if previously assigned by this plugin!
199 role_unassign($creatorrole->id, $user->id, $systemcontext->id, 'auth_fc');
205 * Prints a form for configuring this authentication plugin.
207 * This function is called from admin/auth.php, and outputs a full page with
208 * a form for configuring this plugin.
210 * @param array $page An object containing all the data for this page.
212 function config_form($config, $err, $user_fields) {
213 include "config.html";
217 * Processes and stores configuration data for this authentication plugin.
219 function process_config($config) {
220 // set to defaults if undefined
221 if (!isset($config->host)) {
222 $config->host = "127.0.0.1";
224 if (!isset($config->fppport)) {
225 $config->fppport = "3333";
227 if (!isset($config->userid)) {
228 $config->userid = "fcMoodle";
230 if (!isset($config->passwd)) {
231 $config->passwd = "";
233 if (!isset($config->creators)) {
234 $config->creators = "";
236 if (!isset($config->changepasswordurl)) {
237 $config->changepasswordurl = '';
240 // save settings
241 set_config('host', $config->host, 'auth/fc');
242 set_config('fppport', $config->fppport, 'auth/fc');
243 set_config('userid', $config->userid, 'auth/fc');
244 set_config('passwd', $config->passwd, 'auth/fc');
245 set_config('creators', $config->creators, 'auth/fc');
246 set_config('changepasswordurl', $config->changepasswordurl, 'auth/fc');
248 return true;