Merge branch 'MDL-23813_21' of git://github.com/timhunt/moodle into MOODLE_21_STABLE
[moodle.git] / auth / fc / auth.php
blob3717dc4bf7f982d2428f9cc44ed41db65fab7d02
1 <?php
2 /**
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';
22 /**
23 * FirstClass authentication plugin.
25 class auth_plugin_fc extends auth_plugin_base {
27 /**
28 * Constructor.
30 function auth_plugin_fc() {
31 $this->authtype = 'fc';
32 $this->config = get_config('auth/fc');
35 /**
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) {
44 global $CFG;
45 $retval = false;
47 // Don't allow blank usernames or passwords
48 if (!$username or !$password) {
49 return $retval;
52 $fpp = new fcFPP($this->config->host, $this->config->fppport);
53 if ($fpp->open()) {
54 if ($fpp->login($username, $password)) {
55 $retval = true;
58 $fpp->close();
60 return $retval;
63 /**
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 ------ -----------------------------------
71 firstname 1202
72 lastname 1204
73 email 1252
74 icq -
75 phone1 1206
76 phone2 1207 (Fax)
77 institution -
78 department -
79 address 1205
80 city -
81 country -
82 lang -
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.
89 $userinfo = array();
91 $fpp = new fcFPP($this->config->host, $this->config->fppport);
92 if ($fpp->open()) {
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);
102 $fpp->close();
104 foreach($userinfo as $key => $value) {
105 if (!$value) {
106 unset($userinfo[$key]);
110 return $userinfo;
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) {
119 return null;
122 $fcgroups = array();
124 $fpp = new fcFPP($this->config->host, $this->config->fppport);
125 if ($fpp->open()) {
126 if ($fpp->login($this->config->userid, $this->config->passwd)) {
127 $fcgroups = $fpp->getGroups($username);
130 $fpp->close();
132 if ((! $fcgroups)) {
133 return false;
136 $creators = explode(";", $this->config->creators);
138 foreach($creators as $creator) {
139 if (in_array($creator, $fcgroups)) {
140 return true;
144 return false;
147 function prevent_local_passwords() {
148 return true;
152 * Returns true if this authentication plugin is 'internal'.
154 * @return bool
156 function is_internal() {
157 return false;
161 * Returns true if this authentication plugin can change the user's
162 * password.
164 * @return bool
166 function can_change_password() {
167 return false;
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');
187 } else {
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 = '';
230 // save settings
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');
238 return true;