add.php 9.17 KB
Newer Older
1
2
<?php
/**
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
5
 *
6
7
8
9
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
10
 *
11
12
13
14
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
15
 *
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20
 *
 * @package    mahara
 * @subpackage admin
21
 * @author     Catalyst IT Ltd
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
23
 * @copyright  (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
24
25
26
27
28
 *
 */

define('INTERNAL', 1);
define('INSTITUTIONALADMIN', 1);
29
define('MENUITEM', 'configusers/adduser');
30
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
31
define('TITLE', get_string('adduser', 'admin'));
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'admin');
require_once('pieforms/pieform.php');
require_once('institution.php');

// Site-wide account settings
$elements = array();
$elements['username'] = array(
    'type'    => 'text',
    'title'   => get_string('username'),
    'rules'   => array('required' => true),
);
$elements['firstname'] = array(
    'type'    => 'text',
    'title'   => get_string('firstname'),
    'rules'   => array('required' => true),
);
$elements['lastname'] = array(
    'type'    => 'text',
    'title'   => get_string('lastname'),
    'rules'   => array('required' => true),
);
$elements['email'] = array(
    'type'    => 'text',
    'title'   => get_string('email'),
    'rules'   => array('required' => true),
);
$elements['password'] = array(
    'type'    => 'text',
    'title'   => get_string('password'),
    'rules'   => array('required' => true),
);
if ($USER->get('admin')) {
    $elements['staff'] = array(
        'type'         => 'checkbox',
        'title'        => get_string('sitestaff','admin'),
    );
    $elements['admin'] = array(
        'type'         => 'checkbox',
        'title'        => get_string('siteadmin','admin'),
    );
}
$elements['quota'] = array(
    'type'         => 'bytes',
    'title'        => get_string('filequota','admin'),
    'rules'        => array('integer' => true),
    'defaultvalue' => get_config_plugin('artefact', 'file', 'defaultquota'),
);

81
82
83
84
85
86
87
88
89
90
91
if ($USER->get('admin')) {
    $authinstances = auth_get_auth_instances();
} else {
    $admininstitutions = $USER->get('admininstitutions');
    $authinstances = auth_get_auth_instances_for_institutions($admininstitutions);
    if (empty($authinstances)) {
        $SESSION->add_info_msg(get_string('configureauthplugin', 'admin'));
        redirect(get_config('wwwroot').'admin/users/institutions.php?i='.key($admininstitutions).'&amp;edit=1');
    }
}

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
if (count($authinstances) > 1) {
    $options = array();

    $external = false;
    foreach ($authinstances as $authinstance) {
        if ($USER->can_edit_institution($authinstance->name)) {
            $options[$authinstance->id] = $authinstance->displayname. ': '.$authinstance->instancename;
            if ($authinstance->authname != 'internal') {
                $external = true;
            }
        }
    }

    $elements['authinstance'] = array(
        'type'         => 'select',
        'title'        => get_string('institution'),
        'options'      => $options,
        'rules'        => array('required' => true),
    );
    $elements['institutionadmin'] = array(
        'type'         => 'checkbox',
        'title'        => get_string('institutionadministrator','admin'),
    );
    if ($external) {
        $elements['remoteusername'] = array(
            'type'         => 'text',
            'title'        => get_string('remoteusername', 'admin'),
            'description'  => get_string('remoteusernamedescription', 'admin'),
        );
    }
122
123
124
125
126
} else if (count($authinstances == 1)) {
    $elements['authinstance'] = array(
        'type'         => 'hidden',
        'value'        => $authinstances[0]->id,
    );
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
}

$elements['submit'] = array(
    'type'  => 'submit',
    'value' => get_string('createuser','admin'),
);

$form = pieform(array(
    'name'       => 'adduser',
    'renderer'   => 'table',
    'plugintype' => 'core',
    'pluginname' => 'admin',
    'elements'   => $elements,
));


function adduser_validate(Pieform $form, $values) {
    global $USER;

    $authobj = AuthFactory::create($values['authinstance']);

    $institution = $authobj->institution;

    // Institutional admins can only set their own institutions' authinstances
    if (!$USER->get('admin') && !$USER->is_institutional_admin($authobj->institution)) {
        $form->set_error('authinstance', get_string('notadminforinstitution', 'admin'));
        return;
    }

    $institution = new Institution($authobj->institution);

    // Don't exceed max user accounts for the institution
159
160
161
    if ($institution->isFull()) {
        $SESSION->add_error_msg(get_string('institutionmaxusersexceeded', 'admin'));
        redirect('/admin/users/add.php');
162
163
164
165
166
167
168
169
170
171
172
173
    }

    $username  = $values['username'];
    $firstname = $values['firstname'];
    $lastname  = $values['lastname'];
    $email     = $values['email'];
    $password  = $values['password'];

    if (method_exists($authobj, 'is_username_valid') && !$authobj->is_username_valid($username)) {
        $form->set_error('username', get_string('addusererrorinvalidusername', 'admin'));
        return;
    }
174
    if (!$form->get_error('username') && record_exists_select('usr', 'LOWER(username) = ?', strtolower($username))) {
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
        $form->set_error('username', get_string('usernamealreadytaken', 'auth.internal'));
        return;
    }

    if (!$form->get_error('firstname') && !preg_match('/\S/', $firstname)) {
        $form->set_error('firstname', $form->i18n('required'));
    }
    if (!$form->get_error('lastname') && !preg_match('/\S/', $lastname)) {
        $form->set_error('lastname', $form->i18n('required'));
    }

    if (record_exists('usr', 'email', $email)
        || record_exists('artefact_internal_profile_email', 'email', $email)) {
        $form->set_error('email', get_string('emailalreadytaken', 'auth.internal'));
    }

    if (method_exists($authobj, 'is_password_valid') && !$authobj->is_password_valid($password)) {
192
        $form->set_error('password', get_string('passwordinvalidform', 'auth.' . $authobj->type));
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
        return;
    }

}

function adduser_submit(Pieform $form, $values) {

    $user = new StdClass;
    $user->authinstance   = $values['authinstance'];
    $user->username       = $values['username'];
    $user->firstname      = $values['firstname'];
    $user->lastname       = $values['lastname'];
    $user->email          = $values['email'];
    $user->password       = $values['password'];
    $user->quota          = $values['quota'];
    $user->passwordchange = 1;

210
    global $USER, $SESSION;
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
    if ($USER->get('admin')) {  // Not editable by institutional admins
        $user->staff = (int) ($values['staff'] == 'on');
        $user->admin = (int) ($values['admin'] == 'on');
    }

    $authinstance = get_record('auth_instance', 'id', $values['authinstance']);
    $institution = new Institution($authinstance->institution);

    db_begin();

    $id = insert_record('usr', $user, 'id', true);
    $user->id = $id;

    if (isset($user->admin) && $user->admin) {
        activity_add_admin_defaults(array($user->id));
    }

    if ($institution->name != 'mahara') {
        $institution->addUserAsMember($user);
        if ($values['institutionadmin']) {
            set_field('usr_institution', 'admin', 1, 'usr', $user->id);
        }
    }

    if ($authinstance->authname != 'internal') {
        if (isset($values['remoteusername']) && strlen($values['remoteusername']) > 0) {
            $un = $values['remoteusername'];
        }
        else {
            $un = $user->username;
        }
        insert_record('auth_remote_user', (object) array(
            'authinstance'   => $authinstance->id,
            'remoteusername' => $un,
            'localusr'       => $user->id,
        ));
    }

    // Set profile fields
    foreach (array('firstname', 'lastname', 'email') as $field) {
        set_profile_field($id, $field, $user->{$field});
    }
Richard Mansfield's avatar
Richard Mansfield committed
253
    handle_event('createuser', $user);
254
255
    db_commit();

256
257
258
259
260
261
262
263
264
    try {
        email_user($user, $USER, get_string('accountcreated'),
            get_string('accountcreatedchangepasswordtext', 'mahara', $user->firstname, get_config('sitename'), $user->username, $user->password, get_config('sitename')),
            get_string('accountcreatedchangepasswordhtml', 'mahara', $user->firstname, get_config('sitename'), $user->username, $user->password, get_config('sitename'))
        );
    }
    catch (EmailException $e) {
        $SESSION->add_error_msg(get_string('newuseremailnotsent', 'admin'));
    }
265

266
267
268
269
270
271
272
273
    redirect('/admin/users/edit.php?id='.$id);
}

$smarty = smarty();
$smarty->assign('form', $form);
$smarty->display('admin/users/add.tpl');

?>