2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Authentication Plugin: FirstClass Authentication
19 * Authentication using a FirstClass server.
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';
33 * FirstClass authentication plugin.
35 class auth_plugin_fc
extends auth_plugin_base
{
40 function auth_plugin_fc() {
41 $this->authtype
= 'fc';
42 $this->config
= get_config('auth/fc');
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) {
57 // Don't allow blank usernames or passwords
58 if (!$username or !$password) {
62 $fpp = new fcFPP($this->config
->host
, $this->config
->fppport
);
64 if ($fpp->login($username, $password)) {
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 ------ -----------------------------------
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.
101 $fpp = new fcFPP($this->config
->host
, $this->config
->fppport
);
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);
114 foreach($userinfo as $key => $value) {
116 unset($userinfo[$key]);
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
) {
134 $fpp = new fcFPP($this->config
->host
, $this->config
->fppport
);
136 if ($fpp->login($this->config
->userid
, $this->config
->passwd
)) {
137 $fcgroups = $fpp->getGroups($username);
146 $creators = explode(";", $this->config
->creators
);
148 foreach($creators as $creator) {
149 if (in_array($creator, $fcgroups)) {
157 function prevent_local_passwords() {
162 * Returns true if this authentication plugin is 'internal'.
166 function is_internal() {
171 * Returns true if this authentication plugin can change the user's
176 function can_change_password() {
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');
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
= '';
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');