3 * @author Martin Dougiamas
4 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
5 * @package moodle multiauth
7 * Authentication Plugin: FirstClass Authentication
9 * Authentication using a FirstClass server.
11 * 2006-08-28 File created.
14 if (!defined('MOODLE_INTERNAL')) {
15 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
18 require_once($CFG->libdir
.'/authlib.php');
20 require_once 'fcFPP.php';
23 * FirstClass authentication plugin.
25 class auth_plugin_fc
extends auth_plugin_base
{
30 function auth_plugin_fc() {
31 $this->authtype
= 'fc';
32 $this->config
= get_config('auth/fc');
36 * Returns true if the username and password work and false if they are
37 * wrong or don't exist.
39 * @param string $username The username
40 * @param string $password The password
41 * @return bool Authentication success or failure.
43 function user_login ($username, $password) {
47 // Don't allow blank usernames or passwords
48 if (!$username or !$password) {
52 $fpp = new fcFPP($this->config
->host
, $this->config
->fppport
);
54 if ($fpp->login($username, $password)) {
64 * Get user information from FirstCLass server and return it in an array.
65 * Localize this routine to fit your needs.
67 function get_userinfo($username) {
69 Moodle FirstCLass fieldID in UserInfo form
70 ------ -----------------------------------
83 timezone 8030 (Not used yet. Need to figure out how FC codes timezones)
85 description Get data from users resume. Pictures will be removed.
91 $fpp = new fcFPP($this->config
->host
, $this->config
->fppport
);
93 if ($fpp->login($this->config
->userid
, $this->config
->passwd
)) {
94 $userinfo['firstname'] = $fpp->getUserInfo($username,"1202");
95 $userinfo['lastname'] = $fpp->getUserInfo($username,"1204");
96 $userinfo['email'] = strtok($fpp->getUserInfo($username,"1252"),',');
97 $userinfo['phone1'] = $fpp->getUserInfo($username,"1206");
98 $userinfo['phone2'] = $fpp->getUserInfo($username,"1207");
99 $userinfo['description'] = $fpp->getResume($username);
104 foreach($userinfo as $key => $value) {
106 unset($userinfo[$key]);
114 * Get users group membership from the FirstClass server user and check if
115 * user is member of one of the groups of creators.
117 function iscreator($username) {
118 if (! $this->config
->creators
) {
124 $fpp = new fcFPP($this->config
->host
, $this->config
->fppport
);
126 if ($fpp->login($this->config
->userid
, $this->config
->passwd
)) {
127 $fcgroups = $fpp->getGroups($username);
136 $creators = explode(";", $this->config
->creators
);
138 foreach($creators as $creator) {
139 if (in_array($creator, $fcgroups)) {
147 function prevent_local_passwords() {
152 * Returns true if this authentication plugin is 'internal'.
156 function is_internal() {
161 * Returns true if this authentication plugin can change the user's
166 function can_change_password() {
171 * Sync roles for this user
173 * @param $user object user object (without system magic quotes)
175 function sync_roles($user) {
176 $iscreator = $this->iscreator($user->username
);
177 if ($iscreator === null) {
178 return; //nothing to sync - creators not configured
181 if ($roles = get_archetype_roles('coursecreator')) {
182 $creatorrole = array_shift($roles); // We can only use one, let's use the first one
183 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
185 if ($iscreator) { // Following calls will not create duplicates
186 role_assign($creatorrole->id
, $user->id
, $systemcontext->id
, 'auth_fc');
188 //unassign only if previously assigned by this plugin!
189 role_unassign($creatorrole->id
, $user->id
, $systemcontext->id
, 'auth_fc');
195 * Prints a form for configuring this authentication plugin.
197 * This function is called from admin/auth.php, and outputs a full page with
198 * a form for configuring this plugin.
200 * @param array $page An object containing all the data for this page.
202 function config_form($config, $err, $user_fields) {
203 include "config.html";
207 * Processes and stores configuration data for this authentication plugin.
209 function process_config($config) {
210 // set to defaults if undefined
211 if (!isset($config->host
)) {
212 $config->host
= "127.0.0.1";
214 if (!isset($config->fppport
)) {
215 $config->fppport
= "3333";
217 if (!isset($config->userid
)) {
218 $config->userid
= "fcMoodle";
220 if (!isset($config->passwd
)) {
221 $config->passwd
= "";
223 if (!isset($config->creators
)) {
224 $config->creators
= "";
226 if (!isset($config->changepasswordurl
)) {
227 $config->changepasswordurl
= '';
231 set_config('host', $config->host
, 'auth/fc');
232 set_config('fppport', $config->fppport
, 'auth/fc');
233 set_config('userid', $config->userid
, 'auth/fc');
234 set_config('passwd', $config->passwd
, 'auth/fc');
235 set_config('creators', $config->creators
, 'auth/fc');
236 set_config('changepasswordurl', $config->changepasswordurl
, 'auth/fc');