Indefero

Indefero Commit Details


Date:2010-02-24 03:43:28 (14 years 9 months ago)
Author:Loic d'Anterroches
Branch:develop, feature-issue_links, feature.better-home, feature.content-md5, feature.diff-whitespace, feature.download-md5, feature.issue-links, feature.issue-of-others, feature.issue-summary, feature.search-filter, feature.webrepos, feature.wiki-default-page, master, release-1.1, release-1.2, release-1.3
Commit:51842c02f65bca9c8988fa449296a7b3e1638508
Parents: cc6f1c7cd86266c79a46cfd2e87fa293026dfb6a
Message:Added the ability to manually create a user.

Changes:

File differences

src/IDF/Form/Admin/UserCreate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
81
82
83
84
85
86
87
88
89
90
91
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
122
123
124
125
126
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of InDefero, an open source project management application.
# Copyright (C) 2008 CĂ©ondo Ltd and contributors.
#
# InDefero 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 2 of the License, or
# (at your option) any later version.
#
# InDefero 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Allow an admin to create a user.
*/
class IDF_Form_Admin_UserCreate extends Pluf_Form
{
public $request = null;
public function initFields($extra=array())
{
$this->request = $extra['request'];
$this->fields['first_name'] = new Pluf_Form_Field_Varchar(
array('required' => false,
'label' => __('First name'),
'initial' => '',
'widget_attrs' => array(
'maxlength' => 50,
'size' => 15,
),
));
$this->fields['last_name'] = new Pluf_Form_Field_Varchar(
array('required' => true,
'label' => __('Last name'),
'initial' => '',
'widget_attrs' => array(
'maxlength' => 50,
'size' => 20,
),
));
$this->fields['login'] = new Pluf_Form_Field_Varchar(
array('required' => true,
'label' => __('Login'),
'max_length' => 15,
'min_length' => 3,
'initial' => '',
'help_text' => __('The login must be between 3 and 15 characters long and contains only letters and digits.'),
'widget_attrs' => array(
'maxlength' => 15,
'size' => 10,
),
));
$this->fields['email'] = new Pluf_Form_Field_Email(
array('required' => true,
'label' => __('Email'),
'initial' => '',
'help_text' => __('Double check the email address as the password is directly sent to the user.'),
));
$this->fields['language'] = new Pluf_Form_Field_Varchar(
array('required' => true,
'label' => __('Language'),
'initial' => '',
'widget' => 'Pluf_Form_Widget_SelectInput',
'widget_attrs' => array(
'choices' =>
Pluf_L10n::getInstalledLanguages()
),
));
$this->fields['ssh_key'] = new Pluf_Form_Field_Varchar(
array('required' => false,
'label' => __('Add a public SSH key'),
'initial' => '',
'widget_attrs' => array('rows' => 3,
'cols' => 40),
'widget' => 'Pluf_Form_Widget_TextareaInput',
'help_text' => __('Be careful to provide the public key and not the private key!')
));
}
/**
* Save the model in the database.
*
* @param bool Commit in the database or not. If not, the object
* is returned but not saved in the database.
* @return Object Model with data set from the form.
*/
function save($commit=true)
{
if (!$this->isValid()) {
throw new Exception(__('Cannot save the model from an invalid form.'));
}
$password = Pluf_Utils::getPassword();
$user = new Pluf_User();
$user->setFromFormData($this->cleaned_data);
$user->active = true;
$user->staff = false;
$user->administrator = false;
$user->setPassword($password);
$user->create();
/**
* [signal]
*
* Pluf_User::passwordUpdated
*
* [sender]
*
* IDF_Form_Admin_UserCreate
*
* [description]
*
* This signal is sent when a user is created
* by the staff.
*
* [parameters]
*
* array('user' => $user)
*
*/
$params = array('user' => $user);
Pluf_Signal::send('Pluf_User::passwordUpdated',
'IDF_Form_Admin_UserCreate', $params);
// Create the ssh key as needed
if ('' !== $this->cleaned_data['ssh_key']) {
$key = new IDF_Key();
$key->user = $user;
$key->content = $this->cleaned_data['ssh_key'];
$key->create();
}
// Send an email to the user with the password
Pluf::loadFunction('Pluf_HTTP_URL_urlForView');
$url = Pluf::f('url_base').Pluf_HTTP_URL_urlForView('IDF_Views::login', array(), array(), false);
$context = new Pluf_Template_Context(
array('password' => Pluf_Template::markSafe($password),
'user' => $user,
'url' => Pluf_Template::markSafe($url),
'admin' => $this->request->user,
));
$tmpl = new Pluf_Template('idf/gadmin/users/createuser-email.txt');
$text_email = $tmpl->render($context);
$email = new Pluf_Mail(Pluf::f('from_email'), $user->email,
__('Your details to access your forge.'));
$email->addTextMessage($text_email);
$email->sendMail();
return $user;
}
function clean_ssh_key()
{
$key = trim($this->cleaned_data['ssh_key']);
if (strlen($key) == 0) {
return '';
}
$key = str_replace(array("\n", "\r"), '', $key);
if (!preg_match('#^ssh\-[a-z]{3}\s(\S+)\s\S+$#', $key, $matches)) {
throw new Pluf_Form_Invalid(__('The format of the key is not valid. It must start with ssh-dss or ssh-rsa, a long string on a single line and at the end a comment.'));
}
return $key;
}
function clean_last_name()
{
$last_name = trim($this->cleaned_data['last_name']);
if ($last_name == mb_strtoupper($last_name)) {
return mb_convert_case(mb_strtolower($last_name),
MB_CASE_TITLE, 'UTF-8');
}
return $last_name;
}
function clean_first_name()
{
$first_name = trim($this->cleaned_data['first_name']);
if ($first_name == mb_strtoupper($first_name)) {
return mb_convert_case(mb_strtolower($first_name),
MB_CASE_TITLE, 'UTF-8');
}
return $first_name;
}
function clean_email()
{
$this->cleaned_data['email'] = mb_strtolower(trim($this->cleaned_data['email']));
$guser = new Pluf_User();
$sql = new Pluf_SQL('email=%s', array($this->cleaned_data['email']));
if ($guser->getCount(array('filter' => $sql->gen())) > 0) {
throw new Pluf_Form_Invalid(sprintf(__('The email "%s" is already used.'), $this->cleaned_data['email']));
}
return $this->cleaned_data['email'];
}
public function clean_login()
{
$this->cleaned_data['login'] = mb_strtolower(trim($this->cleaned_data['login']));
if (preg_match('/[^a-z0-9]/', $this->cleaned_data['login'])) {
throw new Pluf_Form_Invalid(sprintf(__('The login "%s" can only contain letters and digits.'), $this->cleaned_data['login']));
}
$guser = new Pluf_User();
$sql = new Pluf_SQL('login=%s', $this->cleaned_data['login']);
if ($guser->getCount(array('filter' => $sql->gen())) > 0) {
throw new Pluf_Form_Invalid(sprintf(__('The login "%s" is already used, please find another one.'), $this->cleaned_data['login']));
}
return $this->cleaned_data['login'];
}
}
src/IDF/Views/Admin.php
192192
193193
194194
195
195196
196197
197198
199
198200
199201
200202
201
202203
203204
204205
......
210211
211212
212213
213
214
215
216
214217
215218
216219
......
279282
280283
281284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
282317
283318
284319
if ($not_validated) {
$pag->forced_where = new Pluf_SQL('first_name = \'---\' AND active!='.$true);
$title = __('Not Validated User List');
$pag->action = 'IDF_Views_Admin::usersNotValidated';
} else {
$pag->forced_where = new Pluf_SQL('first_name != \'---\'');
$title = __('User List');
$pag->action = 'IDF_Views_Admin::users';
}
$pag->class = 'recent-issues';
$pag->summary = __('This table shows the users in the forge.');
$pag->action = 'IDF_Views_Admin::users';
$pag->edit_action = array('IDF_Views_Admin::userUpdate', 'id');
$pag->sort_order = array('login', 'ASC');
$list_display = array(
array('last_login', 'Pluf_Paginator_DateYMDHM', __('Last Login')),
);
$pag->extra_classes = array('', '', 'a-c', 'a-c', 'a-c', 'a-c');
$pag->configure($list_display, array(), array('login', 'last_login'));
$pag->configure($list_display,
array('login', 'last_name', 'email'),
array('login', 'last_login'));
$pag->items_per_page = 50;
$pag->no_results_text = __('No users were found.');
$pag->setFromRequest($request);
),
$request);
}
/**
* Create a new user.
*
* Only staff can add a user. The user can be added together with
* a public ssh key.
*/
public $userCreate_precond = array('Pluf_Precondition::staffRequired');
public function userCreate($request, $match)
{
$params = array(
'request' => $request,
);
if ($request->method == 'POST') {
$form = new IDF_Form_Admin_UserCreate($request->POST, $params);
if ($form->isValid()) {
$cuser = $form->save();
$request->user->setMessage(sprintf(__('The user %s has been created.'), (string) $cuser));
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Admin::users');
return new Pluf_HTTP_Response_Redirect($url);
}
} else {
$form = new IDF_Form_Admin_UserCreate(null, $params);
}
$title = __('Add User');
return Pluf_Shortcuts_RenderToResponse('idf/gadmin/users/create.html',
array(
'page_title' => $title,
'form' => $form,
),
$request);
}
}
function IDF_Views_Admin_bool($field, $item)
src/IDF/conf/urls.php
370370
371371
372372
373
374
375
376
377
373378
374379
375380
'model' => 'IDF_Views_Admin',
'method' => 'users');
$ctl[] = array('regex' => '#^/admin/users/create/$#',
'base' => $base,
'model' => 'IDF_Views_Admin',
'method' => 'userCreate');
$ctl[] = array('regex' => '#^/admin/users/notvalid/$#',
'base' => $base,
'model' => 'IDF_Views_Admin',
src/IDF/templates/idf/gadmin/users/base.html
44
55
66
7
7
8
89
910
<a {if $inIndex}class="active" {/if}href="{url 'IDF_Views_Admin::users'}">{trans 'User List'}</a>
{if $inUpdate} |
<a class="active" href="{url 'IDF_Views_Admin::userUpdate', array($cuser.id)}">{trans 'Update User'}</a>
{/if}
{/if} |
<a {if $inCreate}class="active" {/if}href="{url 'IDF_Views_Admin::userCreate'}">{trans 'Create User'}</a>
{/block}
src/IDF/templates/idf/gadmin/users/create.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
{extends "idf/gadmin/users/base.html"}
{block docclass}yui-t1{assign $inCreate=true}{/block}
{block body}
{if $form.errors}
<div class="px-message-error">
<p>{trans 'The form contains some errors. Please correct them to create the user.'}</p>
{if $form.get_top_errors}
{$form.render_top_errors|unsafe}
{/if}
</div>
{/if}
<form method="post" action=".">
<table class="form" summary="">
<tr>
<th><strong>{$form.f.login.labelTag}:</strong></th>
<td>{if $form.f.login.errors}{$form.f.login.fieldErrors}{/if}
{$form.f.login|unsafe}
</td>
</tr>
<tr>
<th>{$form.f.first_name.labelTag}:</th>
<td>{if $form.f.first_name.errors}{$form.f.first_name.fieldErrors}{/if}
{$form.f.first_name|unsafe}
</td>
</tr>
<tr>
<th><strong>{$form.f.last_name.labelTag}:</strong></th>
<td>{if $form.f.last_name.errors}{$form.f.last_name.fieldErrors}{/if}
{$form.f.last_name|unsafe}
</td>
</tr>
<tr>
<th><strong>{$form.f.email.labelTag}:</strong></th>
<td>{if $form.f.email.errors}{$form.f.email.fieldErrors}{/if}
{$form.f.email|unsafe}<br />
<span class="helptext">{$form.f.email.help_text}</span>
</td>
</tr>
<tr>
<th>{$form.f.language.labelTag}:</th>
<td>{if $form.f.language.errors}{$form.f.language.fieldErrors}{/if}
{$form.f.language|unsafe}
</td>
</tr>
<tr>
<th>{$form.f.ssh_key.labelTag}:</th>
<td>{if $form.f.ssh_key.errors}{$form.f.ssh_key.fieldErrors}{/if}
{$form.f.ssh_key|unsafe}<br />
<span class="helptext">{$form.f.ssh_key.help_text}</span>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="{trans 'Create User'}" name="submit" /> | <a href="{url 'IDF_Views_Admin::users'}">{trans 'Cancel'}</a>
</td>
</tr>
</table>
</form>
{/block}
{block context}
<div class="issue-submit-info">
<p>{trans 'The user password will be sent by email to the user.'}</p>
</div>{/block}
{block javascript}<script type="text/javascript">
document.getElementById('id_login').focus();
</script>{/block}
src/IDF/templates/idf/gadmin/users/createuser-email.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{blocktrans}Hello {$user},
An account on the forge has been created for you by
the administrator {$admin}.
Please find here your details to access the forge:
Address: {$url}
Login: {$user.login}
Password: {$password}
Yours faithfully,
The development team.
{/blocktrans}

Archive Download the corresponding diff file

Page rendered in 0.09831s using 13 queries.