1<?php
2/*
3 * Gallery - a web based photo album viewer and editor
4 * Copyright (C) 2000-2008 Bharat Mediratta
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21/**
22 * This controller will handle the creation of an user
23 * @package GalleryCore
24 * @subpackage UserInterface
25 * @author Bharat Mediratta <bharat@menalto.com>
26 * @version $Revision: 17580 $
27 */
28class AdminCreateUserController extends GalleryController {
29
30    /**
31     * @see GalleryController::handleRequest
32     */
33    function handleRequest($form) {
34	$ret = GalleryCoreApi::assertUserIsSiteAdministrator();
35	if ($ret) {
36	    return array($ret, null);
37	}
38
39	$results = $status = $error = array();
40	if (isset($form['action']['cancel'])) {
41
42	    /* Go back to the AdminUsers view */
43	    $redirect['view'] = 'core.SiteAdmin';
44	    $redirect['subView'] = 'core.AdminUsers';
45
46	} else if (isset($form['action']['create'])) {
47	    /*
48	     * If all the right fields are in place then go ahead and
49	     * create the user.
50	     */
51	    if (!empty($form['userName']) && !empty($form['email'])
52		    && !empty($form['password1']) && $form['password1'] == $form['password2']) {
53
54		list ($ret, $user) =
55		    GalleryCoreApi::newFactoryInstance('GalleryEntity', 'GalleryUser');
56		if ($ret) {
57		    return array($ret, null);
58		}
59
60		if (!isset($user)) {
61		    return array(GalleryCoreApi::error(ERROR_MISSING_OBJECT),
62				 null);
63		}
64
65		$ret = $user->create($form['userName']);
66		if ($ret) {
67		    if (!($ret->getErrorCode() & ERROR_COLLISION)) {
68			return array($ret, null);
69		    }
70
71		    /* Set our error status and fall back to the view */
72		    $error[] = 'form[error][userName][exists]';
73		} else {
74		    $user->setEmail($form['email']);
75		    $user->setFullName($form['fullName']);
76		    $user->setLanguage($form['language']);
77		    GalleryUtilities::unsanitizeInputValues($form['password1'], false);
78		    $user->changePassword($form['password1']);
79
80		    $ret = $user->save();
81		    if ($ret) {
82			return array($ret, null);
83		    }
84
85		    /* Request a redirect to the confirmation screen */
86		    $redirect['view'] = 'core.SiteAdmin';
87		    $redirect['subView'] = 'core.AdminUsers';
88		    $status['createdUser'] = $form['userName'];
89		}
90	    } else {
91		foreach (array('userName', 'email', 'password1', 'password2') as $key) {
92		    if (empty($form[$key])) {
93			$error[] = 'form[error][' . $key . '][missing]';
94		    }
95		}
96
97		if (!empty($form['password1']) && !empty($form['password2'])
98			&& $form['password1'] != $form['password2']) {
99		    $error[] = 'form[error][password2][mismatch]';
100		}
101	    }
102	}
103
104	if (!empty($redirect)) {
105	    $results['redirect'] = $redirect;
106	} else {
107	    $results['delegate']['view'] = 'core.SiteAdmin';
108	    $results['delegate']['subView'] = 'core.AdminCreateUser';
109	}
110	$results['status'] = $status;
111	$results['error'] = $error;
112
113	return array(null, $results);
114    }
115}
116
117/**
118 * This view will prompt for data to create a new user
119 */
120class AdminCreateUserView extends GalleryView {
121
122    /**
123     * @see GalleryView::loadTemplate
124     */
125    function loadTemplate(&$template, &$form) {
126	global $gallery;
127
128	$ret = GalleryCoreApi::assertUserIsSiteAdministrator();
129	if ($ret) {
130	    return array($ret, null);
131	}
132
133	if ($form['formName'] != 'AdminCreateUser') {
134	    /* First time around, set our defaults here. */
135	    $form['userName'] = '';
136	    $form['email'] = '';
137	    $form['fullName'] = '';
138	    $form['language'] = '';
139	    $form['formName'] = 'AdminCreateUser';
140	}
141
142	/* Set up our language selection list */
143	list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'core');
144	if ($ret) {
145	    return array($ret, null);
146	}
147	$languageList = array();
148	$languageList[''] = $module->translate('&lt;none&gt;');
149
150	$supportedLanguages = GalleryCoreApi::getSupportedLanguages();
151	foreach ($supportedLanguages as $language => $countryList) {
152	    foreach ($countryList as $country => $languageData) {
153		$languageList[$language . '_' . $country] =
154		    $languageData['description'];
155	    }
156	}
157
158	$AdminCreateUser = array();
159	$AdminCreateUser['languageList'] = $languageList;
160
161	$template->setVariable('AdminCreateUser', $AdminCreateUser);
162	$template->setVariable('controller', 'core.AdminCreateUser');
163
164	return array(null,
165		     array('body' => 'modules/core/templates/AdminCreateUser.tpl'));
166    }
167}
168?>
169