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
->port
);
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
->port
);
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)) {
148 * Returns true if this authentication plugin is 'internal'.
152 function is_internal() {
157 * Returns true if this authentication plugin can change the user's
162 function can_change_password() {
167 * Sync roles for this user
169 * @param $user object user object (without system magic quotes)
171 function sync_roles($user) {
172 $iscreator = $this->iscreator($user->username
);
173 if ($iscreator === null) {
174 return; //nothing to sync - creators not configured
177 if ($roles = get_roles_with_capability('moodle/legacy:coursecreator', CAP_ALLOW
)) {
178 $creatorrole = array_shift($roles); // We can only use one, let's use the first one
179 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
181 if ($iscreator) { // Following calls will not create duplicates
182 role_assign($creatorrole->id
, $user->id
, 0, $systemcontext->id
, 0, 0, 0, 'fc');
184 //unassign only if previously assigned by this plugin!
185 role_unassign($creatorrole->id
, $user->id
, 0, $systemcontext->id
, 'fc');
191 * Prints a form for configuring this authentication plugin.
193 * This function is called from admin/auth.php, and outputs a full page with
194 * a form for configuring this plugin.
196 * @param array $page An object containing all the data for this page.
198 function config_form($config, $err, $user_fields) {
199 include "config.html";
203 * Processes and stores configuration data for this authentication plugin.
205 function process_config($config) {
206 // set to defaults if undefined
207 if (!isset($config->host
)) {
208 $config->host
= "127.0.0.1";
210 if (!isset($config->fppport
)) {
211 $config->fppport
= "3333";
213 if (!isset($config->userid
)) {
214 $config->userid
= "fcMoodle";
216 if (!isset($config->passwd
)) {
217 $config->passwd
= "";
219 if (!isset($config->creators
)) {
220 $config->creators
= "";
222 if (!isset($config->changepasswordurl
)) {
223 $config->changepasswordurl
= '';
227 set_config('host', $config->user
, 'auth/fc');
228 set_config('fppport', $config->fppport
, 'auth/fc');
229 set_config('userid', $config->userid
, 'auth/fc');
230 set_config('passwd', $config->passwd
, 'auth/fc');
231 set_config('creators', $config->creators
, 'auth/fc');
232 set_config('changepasswordurl', $config->changepasswordurl
, 'auth/fc');