MDL-10689:
[moodle-linuxchix.git] / auth / fc / auth.php
blob45049ad9f2a7b7def50ced63466f872946565c79
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->port);
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->port);
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;
148 * Returns true if this authentication plugin is 'internal'.
150 * @return bool
152 function is_internal() {
153 return false;
157 * Returns true if this authentication plugin can change the user's
158 * password.
160 * @return bool
162 function can_change_password() {
163 return false;
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');
183 } else {
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 = '';
226 // save settings
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');
234 return true;