Indefero

Indefero Git Source Tree

Root/src/IDF/Form/RegisterConfirmation.php

1<?php
2/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3/*
4# ***** BEGIN LICENSE BLOCK *****
5# This file is part of InDefero, an open source project management application.
6# Copyright (C) 2008-2011 Céondo Ltd and contributors.
7#
8# InDefero is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# InDefero is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21#
22# ***** END LICENSE BLOCK ***** */
23
24Pluf::loadFunction('Pluf_HTTP_URL_urlForView');
25
26/**
27 * Confirmation of the form.
28 *
29 */
30class IDF_Form_RegisterConfirmation extends Pluf_Form
31{
32 public $_user = null;
33
34 public function initFields($extra=array())
35 {
36 $this->_user = $extra['user'];
37
38 $this->fields['key'] = new Pluf_Form_Field_Varchar(
39 array('required' => true,
40 'label' => __('Your confirmation key'),
41 'initial' => $extra['key'],
42 'widget' => 'Pluf_Form_Widget_HiddenInput',
43 'widget_attrs' => array(
44 'readonly' => 'readonly',
45 ),
46
47 ));
48 $this->fields['first_name'] = new Pluf_Form_Field_Varchar(
49 array('required' => false,
50 'label' => __('First name'),
51 'initial' => '',
52 'widget_attrs' => array(
53 'maxlength' => 50,
54 'size' => 15,
55 ),
56 ));
57 $this->fields['last_name'] = new Pluf_Form_Field_Varchar(
58 array('required' => true,
59 'label' => __('Last name'),
60 'initial' => '',
61 'widget_attrs' => array(
62 'maxlength' => 50,
63 'size' => 15,
64 ),
65 ));
66
67 $this->fields['password'] = new Pluf_Form_Field_Varchar(
68 array('required' => true,
69 'label' => __('Your password'),
70 'initial' => '',
71 'widget' => 'Pluf_Form_Widget_PasswordInput',
72 'help_text' => __('Your password must be hard for other people to guess, but easy for you to remember.'),
73 'widget_attrs' => array(
74 'maxlength' => 50,
75 'size' => 15,
76 ),
77 ));
78 $this->fields['password2'] = new Pluf_Form_Field_Varchar(
79 array('required' => true,
80 'label' => __('Confirm your password'),
81 'initial' => '',
82 'widget' => 'Pluf_Form_Widget_PasswordInput',
83 'widget_attrs' => array(
84 'maxlength' => 50,
85 'size' => 15,
86 ),
87 ));
88
89
90
91 }
92
93 /**
94 * Just a simple control.
95 */
96 public function clean_key()
97 {
98 $this->cleaned_data['key'] = trim($this->cleaned_data['key']);
99 $error = __('We are sorry but this confirmation key is not valid. Maybe you should directly copy/paste it from your confirmation email.');
100 if (false === ($email_id=IDF_Form_RegisterInputKey::checkKeyHash($this->cleaned_data['key']))) {
101 throw new Pluf_Form_Invalid($error);
102 }
103 $guser = new Pluf_User();
104 $sql = new Pluf_SQL('email=%s AND id=%s', $email_id);
105 $users = $guser->getList(array('filter' => $sql->gen()));
106 if ($users->count() != 1) {
107 throw new Pluf_Form_Invalid($error);
108 }
109 if ($users[0]->active) {
110 throw new Pluf_Form_Invalid(__('This account has already been confirmed. Maybe should you try to recover your password using the help link.'));
111 }
112 $this->_user_id = $email_id[1];
113 return $this->cleaned_data['key'];
114 }
115
116 /**
117 * Check the passwords.
118 */
119 public function clean()
120 {
121 if ($this->cleaned_data['password'] != $this->cleaned_data['password2']) {
122 throw new Pluf_Form_Invalid(__('The two passwords must be the same.'));
123 }
124 return $this->cleaned_data;
125 }
126
127 /**
128 * Save the model in the database.
129 *
130 * @param bool Commit in the database or not. If not, the object
131 * is returned but not saved in the database.
132 * @return Object Model with data set from the form.
133 */
134 function save($commit=true)
135 {
136 if (!$this->isValid()) {
137 throw new Exception(__('Cannot save an invalid form.'));
138 }
139 $this->_user->setFromFormData($this->cleaned_data);
140 $this->_user->active = true;
141 $this->_user->administrator = false;
142 $this->_user->staff = false;
143 if ($commit) {
144 $this->_user->update();
145 /**
146 * [signal]
147 *
148 * Pluf_User::passwordUpdated
149 *
150 * [sender]
151 *
152 * IDF_Form_RegisterConfirmation
153 *
154 * [description]
155 *
156 * This signal is sent when the user updated his
157 * password from his account page.
158 *
159 * [parameters]
160 *
161 * array('user' => $user)
162 *
163 */
164 $params = array('user' => $this->_user);
165 Pluf_Signal::send('Pluf_User::passwordUpdated',
166 'IDF_Form_RegisterConfirmation', $params);
167 }
168 return $this->_user;
169 }
170}
171

Archive Download this file