Revision [6318]
This is an old revision of RegisterAction made by DarTar on 2005-02-25 16:11:58.
Register Action
See also:
Documentation: RegisterActionInfo.I've started working on a new version of an action for user registration. The motivation behind this is to replace the current usersetting action with three distinct actions:
- register action - handling operations related to user registration and first login;
- login action - handling operations related to login/logout and password retrieval of registered users;
- usersettings action - handling user preferences (See UserSettingsPanel for more info).
The action
Current version: 0.2Done:
- Added labels and semantic markup for registration table as per JavaWoman's and IanAndolina's suggestion;
- Removed useless hidden fields (inherited from usersettings);
- Expanded elseif validations to allow further actions;
- Added welcome stuff for first login;
- Added done/failed icons (using menulets currently installed as beta features on this server);
To do:
- CSS to style form;
- (optionally) drop WikiName restriction on usernames;
- use core functions to validate fields;
- use central error handler for printing error messages;
- decide best strategy to link hardcoded login/logout page;
- define welcome page where new users must be redirected;
- (optionally) add option for email-confirmation of registered users.
The code
Save the following as ./actions/register.php and use it as {{register}}.
- <?php
- /**
- * Display a form for user registration.
- *
- * This action allows new users to register an account, if user registration is enabled.
- * All the required fields are validated before the new user is created.
- *
- * @package Actions
- * @name Register
- *
- * @author {@link http://wikka.jsnx.com/DarTar Dario Taraborelli}
- * @version 0.2
- * @since Wikka 1.1.X.X
- * @output form for user registration
- *
- * @todo
- * - CSS to style form;
- * - (optionally) drop WikiName restriction on usernames;
- * - use core functions to validate fields;
- * - use central error handler for printing error messages;
- * - decide best strategy to link hardcoded login/logout page;
- * - define welcome page where new users must be redirected;
- * - (optionally) add option for email-confirmation of registered users.
- */
- print $this->Format('===== Registration page =====');
- if ($this->GetConfigValue("allow_new_users") == "0") {
- // user registration is disabled
- print $this->Format('//User registration is disabled on this wiki//');
- } else {
- if ($user = $this->GetUser()){
- // user is logged in
- // is this the first time the user is logged in?
- if ($_GET['reg'] == 'ok') {
- // first login welcome stuff
- print $this->Format('--- **Registration successful!** --- --- You are currently logged in as '.$this->GetUserName());
- } else {
- // print user information
- print $this->Format('--- You are currently logged in as '.$this->GetUserName());
- }
- } else {
- // user is not logged in
- print "<script type=\"text/javascript\"><!-- \nfunction hov(loc,cls){ \n if(loc.className) loc.className=cls;\n}\n //-->\n</script>\n";
- // is user trying to register?
- if ($_POST) {
- // get POST values
- if ($_POST['password']) $password = $_POST['password'];
- if ($_POST['confpassword']) $confpassword = $_POST['confpassword'];
- // validate fields
- // note: all these validation checks should use core functions to preserve consistency
- // todo: add icons on non-valid fields
- if ($this->LoadUser($name)) {
- $error = 'Sorry, this username already exists. Please choose a different name.';
- $validname = $this->Action('failed');
- } elseif ($this->ExistsPage($name)) {
- $error = 'Sorry, this username is reserved for a page. Please choose a different name.';
- $validname = $this->Action('failed');
- } elseif (!$this->IsWikiName($name)) {
- $error = 'Please fill in a valid username (formatted as a ##""WikiName""##).';
- $validname = $this->Action('failed');
- $error = 'Please specify an email address.';
- $validname = $this->Action('done');
- $validemail = $this->Action('failed');
- $error = 'That does not quite look like an email address.';
- $validname = $this->Action('done');
- $validemail = $this->Action('failed');
- $error = 'Please choose your password.';
- $validname = $this->Action('done');
- $validemail = $this->Action('done');
- $validpassword = $this->Action('failed');
- $error = 'Sorry, password too short.';
- $validname = $this->Action('done');
- $validemail = $this->Action('done');
- $validpassword = $this->Action('failed');
- $error = 'Sorry, spaces are not allowed in passwords.';
- $validname = $this->Action('done');
- $validemail = $this->Action('done');
- $validpassword = $this->Action('failed');
- $error = 'You need to confirm your password.';
- $validname = $this->Action('done');
- $validemail = $this->Action('done');
- $validpassword = $this->Action('failed');
- $validconfpassword = $this->Action('failed');
- } elseif ($confpassword != $password) {
- $error = 'Sorry, passwords do not match.';
- $validname = $this->Action('done');
- $validemail = $this->Action('done');
- $validpassword = $this->Action('failed');
- $validconfpassword = $this->Action('failed');
- } else {
- // all required fields are valid and non-empty
- // create user
- $this->Query("insert into ".$this->config["table_prefix"]."users set ".
- "signuptime = now(), ".
- // log in
- $this->SetUser($this->LoadUser($name));
- // forward
- $this->Redirect($this->href('','','reg=ok'));
- }
- }
- $intro = $this->Format(' --- If you are a **new user** you can register an account using this form (if you already have an account, please go to the [[UserSettings login page]]). --- --- To register, the following fields are required:
- ~-your **username** (it must be formatted like a ##""WikiName""##, for example: ##""JuliusCaesar""##);
- ~-a **valid email address** (this will only be used to retrieve your password in case you lose it);
- ~-a **valid password** (min. 5 characters, no space allowed).
- --- ---');
- // build registration form
- $form = $this->FormOpen();
- $form .= ' <table summary="Form to provide registration data: username, email and password">';
- $form .= ' <caption>Registration form</caption>';
- $form .= ' <tbody>';
- $form .= '<tr><td colspan="3" align="center"><span class="error">'.$this->Format($error).'</span></td></tr>';
- }
- $form .= ' <tr>';
- $form .= ' <th align="right" scope="row"><label for="name">Your username:</label></th>';
- $form .= ' <td><input name="name" id="name" size="40" value="'.$name.'" title="Choose a valid username (formatted as a WikiName)" /></td>';
- $form .= ' <td>'.$validname.'</td>';
- $form .= ' </tr>';
- $form .= ' <tr>';
- $form .= ' <th align="right" scope="row"><label for="email">Your email address:</label></th>';
- $form .= ' <td><input name="email" id="email" size="40" value="'.$email.'" title="Fill in a valid email address"/></td>';
- $form .= ' <td align="left">'.$validemail.'</td>';
- $form .= ' </tr>';
- $form .= ' <tr>';
- $form .= ' <th align="right" scope="row"><label for="password">Your password:</label></th>';
- $form .= ' <td><input type="password" name="password" id="password" size="40" title="Choose a valid password (min. 5 chars, no space)" /></td>';
- $form .= ' <td align="left">'.$validpassword.'</td>';
- $form .= ' </tr>';
- $form .= ' <tr>';
- $form .= ' <th align="right" scope="row"><label for="confpassword">Confirm password:</label></th>';
- $form .= ' <td><input type="password" name="confpassword" id="confpassword" size="40" title="Type again your password for confirmation" /></td>';
- $form .= ' <td align="left">'.$validconfpassword.'</td>';
- $form .= ' </tr>';
- $form .= ' <tr>';
- $form .= ' <td></td>';
- $form .= ' <td><input type="submit" value="Register" size="40" title="Register" /></td>';
- $form .= ' </tr>';
- $form .= ' </tbody>';
- $form .= ' </table>';
- $form .= $this->FormClose();
- // output intro and form
- print $intro.$form;
- }
- }
- ?>
Implemented modifications
- Try to use SemanticMarkup if this is going to be rewritten anyway, instead of:
-
$form .= '<tr>';
$form .= ' <td align="right">Confirm password:</td>';
$form .= ' <td><input type="password" name="confpassword" size="40" title="Type again your password for confirmation" /></td>';
$form .= '</tr>';
- use:
-
$form .= '<label>Confirm password:<input type="password" name="confpassword" size="40" title="Type again your password for confirmation" /></label>';
- It is more elegant, semantically clean and frees some bytes to run free in forests! --IanAndolina
- While I agree that label should always be used for form control prompts, I don't agree with dropping the table. A form as a series of label-data constructs (i.e., name-value pairs) is semantically also a data table, especially since a form can be used not only to enter data but also to (re)view and modify it.
- But when a table is a data table, it should be marked up as a data table, with proper header cells related to the data cells, a caption, and a summary.
- The hidden "register" field is also superfluous, since the submit button can take care of that.
- We'd end up with something like this (this serves just as an example, not meant as the "final" code):
-
- // build registration form
- $form = $this->FormOpen();
- $form .= ' <table summary="form to provide registration data: username, email and password">';
- $form .= ' <caption>Registration form</caption>';
- $form .= ' <tbody>';
- $form .= '<tr><td></td><td><span class="error">'.$this->Format($error).'</span></td></tr>';
- }
- $form .= ' <tr>';
- $form .= ' <th align="right" scope="row"><label for="name">Your username:</label></th>';
- $form .= ' <td><input name="name" id="name" size="40" value="'.$name.'" title="Choose a valid username (formatted as a WikiName)" /></td>';
- $form .= ' </tr>';
- $form .= ' <tr>';
- $form .= ' <th align="right" scope="row"><label for="email">Your email address:</label></th>';
- $form .= ' <td><input name="email" id="email" size="40" value="'.$email.'" title="Fill in a valid email address"/></td>';
- $form .= ' </tr>';
- $form .= ' <tr>';
- $form .= ' <th align="right" scope="row"><label for="password">Your password:</label></th>';
- $form .= ' <td><input type="password" name="password" id="password" size="40" title="Choose a valid password (min. 5 chars, no space)" /></td>';
- $form .= ' </tr>';
- $form .= ' <tr>';
- $form .= ' <th align="right" scope="row"><label for="confpassword">Confirm password:</label></th>';
- $form .= ' <td><input type="password" name="confpassword" id="confpassword" size="40" title="Type again your password for confirmation" /></td>';
- $form .= ' </tr>';
- $form .= ' <tr>';
- $form .= ' <td></td>';
- $form .= ' <td><input type="submit" value="Register" size="40" title="Register" /></td>';
- $form .= ' </tr>';
- $form .= ' </tbody>';
- $form .= ' </table>';
- $form .= $this->FormClose();
- Note that I've also removed the if clauses for $name and $email - the fields should simply be initialized and can then directly be used in the form (moving towards a templating mindset :)).
- Preferably the align="right" on the header cells (and maybe other styling) should be taken care of by some special "form table" rules in the stylesheet (contextual rules will be all that's necessary, no need for extra classes or id - and this will enhance a consistent layout of forms). Both right-aligning labels and a consistent layout for all forms will be helpful for usability.
- Thanks for your feedback, guys! I'll patch the action with your suggestions asap. I was thinking that maybe we might want to add more options related to user registration in wikka.config.php.
- E.g.:
-
"allow_new_users" => "0",
-
"allow_new_users" => "1",
-
"allow_new_users" => "2",
- I'd also like to have Nils' opinion about this, since he had been working on an improved action for user registration with a confirmation code.
- -- DarTar
CategoryDevelopment