/[drupal]/contributions/modules/ucreate/ucreate.module
ViewVC logotype

Contents of /contributions/modules/ucreate/ucreate.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Thu Aug 7 14:11:33 2008 UTC (15 months, 3 weeks ago) by yhahn
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
Changes since 1.2: +1 -8 lines
File MIME type: text/x-php
Fixing ucreate_og bug where users weren't automatically added to the first group.
1 <?php
2 // $Id: ucreate.module,v 1.2 2008/06/26 17:48:56 alexb Exp $
3 /**
4 * @file U create - allows non site admins create and block users.
5 */
6
7 /**
8 * Implementation of hook_help().
9 */
10 function ucreate_help($section) {
11 switch ($section) {
12 case 'user/add':
13 return '<p>'. t('Create a new user. A notification e-mail will be sent to the e-mail address specified.') .'</p>';
14 }
15 }
16
17 /**
18 * Implementation of hook_menu().
19 */
20 function ucreate_menu($may_cache) {
21 $items = array();
22 if ($may_cache) {
23 $items[] = array(
24 'path' => 'user/add',
25 'callback' => 'drupal_get_form',
26 'callback arguments' => array('ucreate_user_form'),
27 'title' => t('Add user'),
28 'description' => t('Add a user to this web site.'),
29 'access' => user_access('create users'),
30 );
31 $items[] = array(
32 'path' => 'admin/user/ucreate',
33 'callback' => 'drupal_get_form',
34 'callback arguments' => array('ucreate_settings_form'),
35 'title' => t('U create settings'),
36 'description' => t('Configure default roles for users created by U create module.'),
37 'access' => user_access('administer site configuration'),
38 );
39 }
40 else {
41 if (arg(0) == 'user' && is_numeric(arg(1))) {
42 global $user;
43 if (($user->uid != arg(1)) && user_access('block users')) {
44 $account = user_load(array('uid' => arg(1)));
45 if ($account->uid && ($account->uid != 1)) {
46 if ($account->status) {
47 $items[] = array(
48 'path' => 'user/'. arg(1) .'/block',
49 'callback' => 'drupal_get_form',
50 'callback arguments' => array('ucreate_block_form', arg(1), 'block'),
51 'title' => t('Suspend'),
52 'description' => t('Suspend this user\'s account.'),
53 'type' => MENU_LOCAL_TASK,
54 );
55 }
56 else {
57 $items[] = array(
58 'path' => 'user/'. arg(1) .'/activate',
59 'callback' => 'drupal_get_form',
60 'callback arguments' => array('ucreate_block_form', arg(1), 'activate'),
61 'title' => t('Activate'),
62 'description' => t('Activate this user\'s account.'),
63 'type' => MENU_LOCAL_TASK,
64 );
65 }
66 }
67 }
68 }
69 }
70 return $items;
71 }
72
73 /**
74 * Implementation of hook_perm().
75 */
76 function ucreate_perm() {
77 return array('create users', 'block users');
78 }
79
80 /**
81 * Implementation of hook_user().
82 */
83 function ucreate_user($op, &$edit, &$account, $category = NULL) {
84 if ($op == 'login') {
85 // On first login, welcome user and ask to reset password.
86 // @todo: make user form multi step.
87 if ($account->login == 0 && $account->uid) {
88 // Unset destination requests - otherwise drupal_goto does not yield results.
89 unset($_REQUEST['destination']);
90 unset($_REQUEST['edit']['destination']);
91 drupal_set_message(t('Welcome to !site, !name - please change your password', array('!site' => variable_get('site_name', 'Drupal'), '!name' => $account->name)), 'welcome');
92 drupal_goto('user/'. $account->uid .'/edit', 'destination='. variable_get('site_frontpage', 'node'));
93 }
94 }
95 }
96
97 /**
98 * Menu callback for settings form.
99 */
100 function ucreate_settings_form() {
101 $options = user_roles();
102 unset($options[1]);
103 unset($options[2]);
104 $form['ucreate_default_roles'] = array(
105 '#title' => t('Default roles'),
106 '#description' => t('Roles a new user should be assigned.'),
107 '#type' => 'checkboxes',
108 '#options' => $options,
109 '#default_value' => variable_get('ucreate_default_roles', array()),
110 );
111 // @todo: personalize e-mail message.
112 return system_settings_form($form);
113 }
114
115 /**
116 * Break out form for creating users.
117 */
118 function ucreate_user_form() {
119 $form['name'] = array(
120 '#type' => 'textfield',
121 '#title' => t('User name'),
122 '#required' => TRUE,
123 );
124 $form['mail'] = array(
125 '#type' => 'textfield',
126 '#title' => t('E-mail'),
127 '#required' => TRUE,
128 );
129 $form['mail_confirm'] = array(
130 '#type' => 'textfield',
131 '#title' => t('E-mail (confirm)'),
132 '#required' => TRUE,
133 );
134 $form['roles'] = array(
135 '#type' => 'value',
136 '#value' => drupal_map_assoc(variable_get('ucreate_default_roles', array())),
137 );
138 // The personal welcome message will be added to the top of the mail.
139 // @todo: Ideal would be offering the full notification message for edit
140 // * updated by ajax call back (we shouldn't show tokens to users)
141 // * or in a second step of the form
142 // Both approaches have ramifications for the use of the form in ajaxy popups.
143 $form['welcome_message_body'] = array(
144 '#type' => 'textarea',
145 '#title' => t('Personal welcome message'),
146 '#default_value' => '',
147 '#description' => t('This welcome message will appear at the top of the e-mail notification sent to the new user.')
148 );
149 $form['submit'] = array(
150 '#type' => 'submit',
151 '#value' => t('Add'),
152 '#weight' => 20,
153 );
154 return $form;
155 }
156
157 /**
158 * Validation handler for ucreate_user_form().
159 */
160 function ucreate_user_form_validate($form_id, $form_values, $form) {
161 if ($account = user_load(array('name' => $form_values['name']))) {
162 $name = user_access('access user profiles') ? l($account->name, 'user/'. $account->uid) : $account->name;
163 form_set_error('name', t('User !name already exists.', array('!name' => $name)));
164 }
165 if ($form_values['mail'] != $form_values['mail_confirm']) {
166 form_set_error('email_confirm', t('E-mail addresses don\'t match'));
167 }
168 else if (user_load(array('mail' => $form_values['mail']))) {
169 form_set_error('mail', t('User with this e-mail address already exists.'));
170 }
171 }
172
173 /**
174 * Submit handler for ucreate_user_form().
175 */
176 function ucreate_user_form_submit($form_id, $form_values) {
177 ucreate_user_create($form_values);
178 drupal_goto($_GET['q']);
179 }
180
181 /**
182 * Block user confirm dialog.
183 */
184 function ucreate_block_form($uid, $op) {
185 if ($account = user_load(array('uid'=>$uid))) {
186 $path = $_GET['destination'] ? $_GET['destination'] : '<front>';
187 $form = array(
188 'uid' => array(
189 '#type' => 'value',
190 '#value'=> $uid,
191 ),
192 'operation' => array(
193 '#type' => 'value',
194 '#value'=> $op,
195 ),
196 );
197 if ($op == 'block') {
198 $message = t('Are you sure you would like to suspend !user\'s account?', array('!user'=>theme('username', $account)));
199 }
200 else if ($op == 'activate') {
201 $message = t('Are you sure you would like to activate !user\'s account?', array('!user'=>theme('username', $account)));
202 }
203 $form = confirm_form($form, $message, $path, '');
204 return $form;
205 }
206 }
207
208 /**
209 * Submit handler for ucreate_block_form().
210 * @todo: send email if user is blocked/activated.
211 */
212 function ucreate_block_form_submit($form_id, $form_values) {
213 // print_r($form_values);
214 if ($account = user_load(array('uid' => $form_values['uid']))) {
215 if ($form_values['operation'] == 'block') {
216 $account = user_save($account, array('status' => 0));
217 if ($account->status == 0) {
218 drupal_set_message(t('The account !user was suspended.', array('!user'=>theme('username', $account))));
219 return;
220 }
221 }
222 else if ($form_values['operation'] == 'activate') {
223 $account = user_save($account, array('status' => 1));
224 if ($account->status == 1) {
225 drupal_set_message(t('The account !user was activated.', array('!user'=>theme('username', $account))));
226 return;
227 }
228 }
229 }
230 // Unlikely.
231 drupal_set_message(t('There was an error in changing the account status.'), 'error');
232 }
233
234 /**
235 * Create user
236 *
237 * @param array $edit
238 * Values in format accepted by user_save().
239 * Required values:
240 * $edit['name']
241 * $edit['mail']
242 *
243 */
244 function ucreate_user_create($edit) {
245 // Create account.
246 $account = new stdClass();
247 $password = user_password();
248 $edit['pass'] = $password;
249 $edit['status'] = 1;
250 $account = user_save($account, $edit);
251
252 // Notify user if successful.
253 if ($account->uid) {
254 drupal_set_message(t('You have created an account for !name, password and login instructions have been sent to the e-mail address !email.', array('!name' => $edit['name'], '!email' => l($edit['mail'], 'mailto:'. $edit['mail']))));
255
256 $subject = t('[!site_name] We have created an account for you', array('!site_name' => variable_get('site_name', 'Drupal')));
257 $variables = array(
258 '!name' => $edit['name'],
259 '!site' => variable_get('site_name', 'Drupal'),
260 '!login_url' => user_pass_reset_url($account) .'/login',
261 '!url' => trim(url(NULL, NULL, NULL, TRUE), '/'),
262 '!password' => $password,
263 );
264 if (trim($edit['welcome_message_body'])) {
265 $body .= $edit['welcome_message_body'];
266 $body .= "\n\n================================================\n";
267 }
268 else {
269 $body .= t("\nHello !name,\n", $variables);
270 }
271 // @todo: Would love to use one time login link here - alas it is only valid for 24 hrs and needs to be renewed then.
272 $body .= t("\nWe have created an account for you on !site\n!url.\n\nYou can log in to the site with the following username and password\n\n!name\n!password\n\nPlease change your password after the first time you log in.\n\nWelcome to !site", $variables);
273 if (!drupal_mail('ucreate-create', $edit['mail'], $subject, $body)) {
274 drupal_set_message(t('Error sending notification mail to user.'), 'error');
275 }
276 }
277 else {
278 drupal_set_message(t('Error creating user.'), 'error');
279 }
280 return $account;
281 }

  ViewVC Help
Powered by ViewVC 1.1.2