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 public function __construct() {
41 $this->authtype
= 'fc';
42 $this->config
= get_config('auth/fc');
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
);
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) {
67 // Don't allow blank usernames or passwords
68 if (!$username or !$password) {
72 $fpp = new fcFPP($this->config
->host
, $this->config
->fppport
);
74 if ($fpp->login($username, $password)) {
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 ------ -----------------------------------
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.
111 $fpp = new fcFPP($this->config
->host
, $this->config
->fppport
);
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);
124 foreach($userinfo as $key => $value) {
126 unset($userinfo[$key]);
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
) {
144 $fpp = new fcFPP($this->config
->host
, $this->config
->fppport
);
146 if ($fpp->login($this->config
->userid
, $this->config
->passwd
)) {
147 $fcgroups = $fpp->getGroups($username);
156 $creators = explode(";", $this->config
->creators
);
158 foreach($creators as $creator) {
159 if (in_array($creator, $fcgroups)) {
167 function prevent_local_passwords() {
172 * Returns true if this authentication plugin is 'internal'.
176 function is_internal() {
181 * Returns true if this authentication plugin can change the user's
186 function can_change_password() {
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');
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
= '';
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');