001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.faces.beans;
034:
035: import com.flexive.faces.messages.FxFacesMsgErr;
036: import com.flexive.faces.messages.FxFacesMsgInfo;
037: import com.flexive.faces.messages.FxFacesMsgWarn;
038: import com.flexive.shared.EJBLookup;
039: import com.flexive.shared.FxContext;
040: import com.flexive.shared.configuration.DBVendor;
041: import com.flexive.shared.configuration.DivisionData;
042: import com.flexive.shared.configuration.DivisionDataEdit;
043: import com.flexive.shared.exceptions.FxApplicationException;
044: import com.flexive.shared.interfaces.GlobalConfigurationEngine;
045: import org.apache.commons.lang.StringUtils;
046:
047: import java.util.ArrayList;
048: import java.util.List;
049:
050: /**
051: * Handles global configuration authentication and setup.
052: * Note that this beans runs completely outside standard flexive security, with a custom user name
053: * and password retrieved from the
054: * {@link com.flexive.shared.interfaces.GlobalConfigurationEngine GlobalConfigurationEngine}.
055: *
056: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
057: * @version $Rev: 1 $
058: */
059: public class GlobalConfigBean {
060: private String username;
061: private String password;
062: private boolean authenticated = false;
063:
064: // division table
065: private List<DivisionDataEdit> divisions;
066: private int editIndex = -1;
067:
068: // url checker
069: private String checkUrl;
070:
071: // update password
072: private String oldPassword;
073: private String newPassword;
074: private String repeatNewPassword;
075:
076: /**
077: * Perform the user login against the username and password delivered by the configuration engine.
078: *
079: * @return the outcome
080: */
081: public String login() {
082: final GlobalConfigurationEngine globalConfig = EJBLookup
083: .getGlobalConfigurationEngine();
084: authenticated = false;
085: try {
086: if (!globalConfig.getRootLogin().equals(username)
087: || !globalConfig.isMatchingRootPassword(password)) {
088: new FxFacesMsgErr("GlobalConfig.err.wrongLogin")
089: .addToContext();
090: return null;
091: }
092: authenticated = true;
093: return "success";
094: } catch (FxApplicationException e) {
095: new FxFacesMsgErr(e).addToContext();
096: return null;
097: }
098: }
099:
100: public String logout() {
101: authenticated = false;
102: divisions = null;
103: return "login";
104: }
105:
106: /**
107: * Add a division to the division table.
108: *
109: * @throws FxApplicationException if the divisions could not be initialized
110: */
111: public void addDivision() throws FxApplicationException {
112: try {
113: int maxId = 0;
114: for (DivisionData data : getDivisions()) {
115: if (data.getId() > maxId) {
116: maxId = data.getId();
117: }
118: }
119: getDivisions().add(
120: new DivisionDataEdit(new DivisionData(maxId + 1,
121: false, "", "", DBVendor.Unknown, "")));
122: editIndex = getDivisions().size() - 1;
123: } catch (FxApplicationException e) {
124: new FxFacesMsgErr(e).addToContext();
125: }
126: }
127:
128: /**
129: * Removes the division at editIndex.
130: */
131: public void removeDivision() {
132: try {
133: if (getDivisions().get(editIndex).getId() == FxContext
134: .get().getDivisionId()) {
135: new FxFacesMsgErr("GlobalConfig.err.removeOwnDivision")
136: .addToContext();
137: return;
138: }
139: final DivisionDataEdit removedDivision = getDivisions()
140: .remove(editIndex);
141: new FxFacesMsgInfo("GlobalConfig.nfo.removedDivision",
142: removedDivision.getId()).addToContext();
143: } catch (FxApplicationException e) {
144: new FxFacesMsgErr(e).addToContext();
145: } finally {
146: editIndex = -1;
147: }
148: }
149:
150: /**
151: * Tests the database connection of the division at editIndex.
152: */
153: public void testConnection() {
154: try {
155: final DivisionDataEdit division = getDivisions().get(
156: editIndex);
157: final DivisionData testedDivision = EJBLookup
158: .getGlobalConfigurationEngine().createDivisionData(
159: division.getId(), division.getDataSource(),
160: division.getDomainRegEx());
161: if (testedDivision.isAvailable()) {
162: new FxFacesMsgInfo(
163: "GlobalConfig.nfo.divisionTestSuccess",
164: testedDivision.getId()).addToContext();
165: } else {
166: new FxFacesMsgErr(
167: "GlobalConfig.err.divisionUnavailable",
168: testedDivision.getId()).addToContext();
169: }
170: getDivisions().set(editIndex, testedDivision.asEditable());
171: } catch (FxApplicationException e) {
172: new FxFacesMsgErr("GlobalConfig.err.divisionTest",
173: divisions.get(editIndex).getId(), e).addToContext();
174: } finally {
175: editIndex = -1;
176: }
177: }
178:
179: public void checkUrl() {
180: try {
181: if (checkUrl != null
182: && (checkUrl.toLowerCase().startsWith("http://") || checkUrl
183: .toLowerCase().startsWith("https://"))) {
184: new FxFacesMsgWarn("GlobalConfig.wng.url.protocol")
185: .addToContext();
186: checkUrl = checkUrl
187: .substring(checkUrl.indexOf("://") + 3);
188: }
189: int divisionId = -1;
190: for (DivisionData data : getDivisions()) {
191: if (data.isMatchingDomain(checkUrl)) {
192: if (divisionId == -1) {
193: divisionId = data.getId();
194: new FxFacesMsgInfo(
195: "GlobalConfig.nfo.divisionMatch", data
196: .getId()).addToContext();
197: } else {
198: new FxFacesMsgInfo(
199: "GlobalConfig.nfo.divisionMatchesTooLate",
200: data.getId(), divisionId)
201: .addToContext();
202: }
203: }
204: }
205: if (divisionId == -1) {
206: new FxFacesMsgInfo("GlobalConfig.nfo.divisionNoMatch")
207: .addToContext();
208: }
209: } catch (FxApplicationException e) {
210: new FxFacesMsgErr(e).addToContext();
211: }
212: }
213:
214: /**
215: * Return from edit mode.
216: */
217: public void leaveEditMode() {
218: editIndex = -1;
219: }
220:
221: public void resetDivisions() {
222: divisions = null;
223: editIndex = -1;
224: }
225:
226: public void updateDivisions() {
227: ensureLoggedIn();
228: FxContext.get().setGlobalAuthenticated(true);
229: try {
230: EJBLookup.getGlobalConfigurationEngine().saveDivisions(
231: getDivisions());
232: new FxFacesMsgInfo("GlobalConfig.nfo.updatedDivisions")
233: .addToContext();
234: } catch (Exception e) {
235: new FxFacesMsgErr(e).addToContext();
236: } finally {
237: FxContext.get().setGlobalAuthenticated(false);
238: }
239: }
240:
241: public void updatePassword() {
242: ensureLoggedIn();
243: FxContext.get().setGlobalAuthenticated(true);
244: try {
245: if (!EJBLookup.getGlobalConfigurationEngine()
246: .isMatchingRootPassword(oldPassword)) {
247: new FxFacesMsgErr("GlobalConfig.err.password.old")
248: .addToContext();
249: return;
250: }
251: if (!StringUtils.equals(newPassword, repeatNewPassword)) {
252: new FxFacesMsgErr("GlobalConfig.err.password.repeat")
253: .addToContext();
254: return;
255: }
256: if (StringUtils.isBlank(newPassword)) {
257: new FxFacesMsgErr("GlobalConfig.err.password.empty")
258: .addToContext();
259: return;
260: }
261: EJBLookup.getGlobalConfigurationEngine().setRootPassword(
262: newPassword);
263: new FxFacesMsgInfo("GlobalConfig.nfo.passwordUpdated")
264: .addToContext();
265: } catch (Exception e) {
266: new FxFacesMsgErr(e).addToContext();
267: } finally {
268: FxContext.get().setGlobalAuthenticated(false);
269: }
270: }
271:
272: public String getUsername() {
273: return username;
274: }
275:
276: public void setUsername(String username) {
277: this .username = username;
278: }
279:
280: public String getPassword() {
281: return password;
282: }
283:
284: public void setPassword(String password) {
285: this .password = password;
286: }
287:
288: public int getEditIndex() {
289: return editIndex;
290: }
291:
292: public void setEditIndex(int editIndex) {
293: this .editIndex = editIndex;
294: }
295:
296: public String getCheckUrl() {
297: return checkUrl;
298: }
299:
300: public void setCheckUrl(String checkUrl) {
301: this .checkUrl = checkUrl;
302: }
303:
304: public String getOldPassword() {
305: return oldPassword;
306: }
307:
308: public void setOldPassword(String oldPassword) {
309: this .oldPassword = oldPassword;
310: }
311:
312: public String getNewPassword() {
313: return newPassword;
314: }
315:
316: public void setNewPassword(String newPassword) {
317: this .newPassword = newPassword;
318: }
319:
320: public String getRepeatNewPassword() {
321: return repeatNewPassword;
322: }
323:
324: public void setRepeatNewPassword(String repeatNewPassword) {
325: this .repeatNewPassword = repeatNewPassword;
326: }
327:
328: public List<DivisionDataEdit> getDivisions()
329: throws FxApplicationException {
330: ensureLoggedIn();
331: if (divisions == null) {
332: divisions = new ArrayList<DivisionDataEdit>();
333: for (DivisionData data : EJBLookup
334: .getGlobalConfigurationEngine().getDivisions()) {
335: divisions.add(data.asEditable());
336: }
337: }
338: return divisions;
339: }
340:
341: private void ensureLoggedIn() {
342: if (!authenticated) {
343: throw new IllegalStateException(
344: "Please login first before accessing the global configuration pages.");
345: }
346: }
347: }
|