Root/
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 | <?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[ 'public_key' ] = new Pluf_Form_Field_Varchar( array ( 'required' => false, 'label' => __( 'Add a public key' ), 'initial' => '' , 'widget_attrs' => array ( 'rows' => 3, 'cols' => 40), 'widget' => 'Pluf_Form_Widget_TextareaInput' , 'help_text' => __( 'Paste a SSH or monotone public key. Be careful to not provide your private key here!' ) )); } /** * 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 public key as needed if ( '' !== $this ->cleaned_data[ 'public_key' ]) { $key = new IDF_Key(); $key ->user = $user ; $key ->content = $this ->cleaned_data[ 'public_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_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' ]; } public function clean_public_key() { $this ->cleaned_data[ 'public_key' ] = IDF_Form_UserAccount::checkPublicKey( $this ->cleaned_data[ 'public_key' ]); return $this ->cleaned_data[ 'public_key' ]; } } |