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 send the user to the chosen subView in the AdminUsers View
23 * @package GalleryCore
24 * @subpackage UserInterface
25 * @author Bharat Mediratta <bharat@menalto.com>
26 * @version $Revision: 17580 $
27 */
28class AdminUsersController extends GalleryController {
29
30    /**
31     * @see GalleryController::handleRequest
32     */
33    function handleRequest($form) {
34	global $gallery;
35
36	$ret = GalleryCoreApi::assertUserIsSiteAdministrator();
37	if ($ret) {
38	    return array($ret, null);
39	}
40
41	$status = $error = $results = array();
42	$user = null;
43	if (!empty($form['text']['userName'])) {
44	    list ($ret, $user) = GalleryCoreApi::fetchUserByUserName($form['text']['userName']);
45	    if ($ret && !($ret->getErrorCode() & ERROR_MISSING_OBJECT)) {
46		return array($ret, null);
47	    }
48	}
49
50	if (isset($form['action']['filterClear'])) {
51
52	    /* Clear the filter */
53	    GalleryUtilities::putRequestVariable('form[list][filter]', null);
54
55	} else if (isset($form['action']['create'])) {
56
57	    /* Show the "create user" view */
58	    $redirect['view'] = 'core.SiteAdmin';
59	    $redirect['subView'] = 'core.AdminCreateUser';
60
61	} else if (isset($form['action']['editFromText'])) {
62
63	    if (empty($form['text']['userName'])) {
64		$error[] = 'form[error][text][noUserSpecified]';
65	    } else if ($user == null) {
66		$error[] = 'form[error][text][noSuchUser]';
67	    } else {
68		/* Show the "delete user" view */
69		$redirect['view'] = 'core.SiteAdmin';
70		$redirect['userId'] = $user->getId();
71		$redirect['subView'] = 'core.AdminEditUser';
72	    }
73
74	} else if (isset($form['action']['deleteFromText'])) {
75
76	    if (empty($form['text']['userName'])) {
77		$error[] = 'form[error][text][noUserSpecified]';
78	    } else if ($user == null) {
79		$error[] = 'form[error][text][noSuchUser]';
80	    } else {
81
82		/*
83		 * Check to see if we're trying to delete the anonymous user, or
84		 * ourself (can't do either of those).
85		 */
86		list ($ret, $anonymousUserId) =
87		    GalleryCoreApi::getPluginParameter('module', 'core', 'id.anonymousUser');
88		if ($ret) {
89		    return array($ret, null);
90		}
91
92		if ($user->getId() == $anonymousUserId) {
93		    $error[] = 'form[error][text][cantDeleteAnonymous]';
94		}
95
96		if ($user->getId() == $gallery->getActiveUserId()) {
97		    $error[] = 'form[error][text][cantDeleteSelf]';
98		}
99	    }
100
101	    if (empty($error)) {
102		/* Show the "delete user" view */
103		$redirect['view'] = 'core.SiteAdmin';
104		$redirect['subView'] = 'core.AdminDeleteUser';
105		$redirect['userId'] = $user->getId();
106	    }
107	}
108
109	if (!empty($redirect)) {
110	    $results['redirect'] = $redirect;
111	} else {
112	    $results['delegate']['view'] = 'core.SiteAdmin';
113	    $results['delegate']['subView'] = 'core.AdminUsers';
114	}
115	$results['status'] = $status;
116	$results['error'] = $error;
117
118	return array(null, $results);
119    }
120}
121
122/**
123 * This view will show available options to administer the users of Gallery
124 */
125class AdminUsersView extends GalleryView {
126
127    /**
128     * @see GalleryView::loadTemplate
129     */
130    function loadTemplate(&$template, &$form) {
131	global $gallery;
132
133	$ret = GalleryCoreApi::assertUserIsSiteAdministrator();
134	if ($ret) {
135	    return array($ret, null);
136	}
137
138	if ($form['formName'] != 'AdminUsers') {
139	    /* Set some defaults */
140	    $form['text']['userName'] = '';
141	    $form['formName'] = 'AdminUsers';
142	}
143
144	if (!isset($form['list']['filter'])) {
145	    $form['list']['filter'] = '';
146	}
147
148	if (!isset($form['list']['page']) || $form['list']['page'] < 1) {
149	    $form['list']['page'] = 1;
150	}
151
152	/* Fetch the user count every time we reload */
153	list ($ret, $totalUserCount) = GalleryCoreApi::fetchUserCount();
154	if ($ret) {
155	    return array($ret, null);
156	}
157
158	$form['list']['count'] = $totalUserCount;
159	$form['list']['pageSize'] = $totalUserCount > 10 ? 10 : $totalUserCount + 2;
160
161	/* If we have a filter, find out how many users match it */
162	if (!empty($form['list']['filter'])) {
163	    list ($ret, $form['list']['count']) =
164		GalleryCoreApi::fetchUserCount($form['list']['filter']);
165	    if ($ret) {
166		return array($ret, null);
167	    }
168	}
169
170	/* Figure out our max pages, make sure our current page fits in it */
171	$form['list']['maxPages'] = ceil($form['list']['count'] / $form['list']['pageSize']);
172	if ($form['list']['page'] > $form['list']['maxPages']) {
173	    $form['list']['page'] = $form['list']['maxPages'];
174	}
175
176	/* Calculate the next/back pages */
177	$form['list']['nextPage'] = min($form['list']['page']+1, $form['list']['maxPages']);
178	$form['list']['backPage'] = max(1, $form['list']['page']-1);
179
180	list ($ret, $userNames) = GalleryCoreApi::fetchUserNames(
181	    $form['list']['pageSize'],
182	    ($form['list']['page'] - 1) * $form['list']['pageSize'],
183	    $form['list']['filter']);
184	if ($ret) {
185	    return array($ret, null);
186	}
187
188	list ($ret, $anonymousUserId) =
189	    GalleryCoreApi::getPluginParameter('module', 'core', 'id.anonymousUser');
190	if ($ret) {
191	    return array($ret, null);
192	}
193
194	$myUserId = $gallery->getActiveUserId();
195
196	/* This is inefficient; we should extend GalleryCoreApi::fetchUserNames */
197	list ($ret, $users) =
198	    GalleryCoreApi::loadEntitiesById(array_keys($userNames), 'GalleryUser');
199	if ($ret) {
200	    return array($ret, null);
201	}
202
203	list ($ret, $searchResults) = GalleryCoreApi::getMapEntry(
204	    'FailedLoginsMap',
205	    array('userName', 'count'),
206	    array('userName' => array_values($userNames)));
207	if ($ret) {
208	    return array($ret, null);
209	}
210	while ($result = $searchResults->nextResult()) {
211	    $failedLogins[$result[0]] = $result[1];
212	}
213
214	$form['list']['userNames'] = array();
215	foreach ($users as $user) {
216	    $userId = $user->getId();
217	    $form['list']['userNames'][$userId] = (array)$user;
218	    if ($userId == $anonymousUserId || $userId == $myUserId) {
219		$form['list']['userNames'][$userId]['can']['delete'] = false;
220	    } else {
221		$form['list']['userNames'][$userId]['can']['delete'] = true;
222	    }
223	    $form['list']['userNames'][$userId]['failedLogins'] =
224		(isset($failedLogins[$user->getUserName()])) ?
225		$failedLogins[$user->getUserName()] : 0;
226	}
227
228	$template->setVariable('AdminUsers', array('totalUserCount' => $totalUserCount));
229	$template->setVariable('controller', 'core.AdminUsers');
230	return array(null, array('body' => 'modules/core/templates/AdminUsers.tpl'));
231    }
232}
233?>
234