2009-07-12 03:21:20 +00:00
|
|
|
package net.i2p.router.web;
|
|
|
|
|
2012-08-03 03:49:46 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2009-07-12 03:21:20 +00:00
|
|
|
/** set the theme */
|
|
|
|
public class ConfigUIHandler extends FormHandler {
|
|
|
|
private boolean _shouldSave;
|
2012-07-22 06:53:43 +00:00
|
|
|
private boolean _universalTheming;
|
2013-01-21 05:59:53 +00:00
|
|
|
private boolean _forceMobileConsole;
|
2009-07-12 03:21:20 +00:00
|
|
|
private String _config;
|
|
|
|
|
2009-08-15 16:08:33 +00:00
|
|
|
@Override
|
2009-07-12 03:21:20 +00:00
|
|
|
protected void processForm() {
|
2012-10-13 21:20:16 +00:00
|
|
|
if (_shouldSave) {
|
2009-07-12 03:21:20 +00:00
|
|
|
saveChanges();
|
2012-10-13 21:20:16 +00:00
|
|
|
} else if (_action.equals(_("Delete selected"))) {
|
|
|
|
delUser();
|
|
|
|
} else if (_action.equals(_("Add user"))) {
|
|
|
|
addUser();
|
|
|
|
}
|
2009-07-12 03:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setShouldsave(String moo) { _shouldSave = true; }
|
2012-07-22 06:53:43 +00:00
|
|
|
|
|
|
|
public void setUniversalTheming(String baa) { _universalTheming = true; }
|
|
|
|
|
2013-01-21 05:59:53 +00:00
|
|
|
public void setForceMobileConsole(String baa) { _forceMobileConsole = true; }
|
|
|
|
|
2009-07-12 03:21:20 +00:00
|
|
|
public void setTheme(String val) {
|
|
|
|
_config = val;
|
|
|
|
}
|
|
|
|
|
2009-10-26 21:48:46 +00:00
|
|
|
/** note - lang change is handled in CSSHelper but we still need to save it here */
|
2009-07-12 03:21:20 +00:00
|
|
|
private void saveChanges() {
|
2014-08-03 13:58:51 +00:00
|
|
|
if (_config == null || _config.length() <= 0)
|
2009-07-12 03:21:20 +00:00
|
|
|
return;
|
2014-08-03 13:58:51 +00:00
|
|
|
if (_config.replaceAll("[a-zA-Z0-9_-]", "").length() != 0) {
|
|
|
|
addFormError("Bad theme name");
|
|
|
|
return;
|
|
|
|
}
|
2013-11-21 11:31:50 +00:00
|
|
|
Map<String, String> changes = new HashMap<String, String>();
|
|
|
|
List<String> removes = new ArrayList<String>();
|
2012-08-01 01:50:59 +00:00
|
|
|
String oldTheme = _context.getProperty(CSSHelper.PROP_THEME_NAME, CSSHelper.DEFAULT_THEME);
|
2013-01-21 05:59:53 +00:00
|
|
|
boolean oldForceMobileConsole = _context.getBooleanProperty(CSSHelper.PROP_FORCE_MOBILE_CONSOLE);
|
2012-07-22 13:13:43 +00:00
|
|
|
if (_config.equals("default")) // obsolete
|
2012-08-03 03:49:46 +00:00
|
|
|
removes.add(CSSHelper.PROP_THEME_NAME);
|
2012-07-22 13:13:43 +00:00
|
|
|
else
|
2012-08-03 03:49:46 +00:00
|
|
|
changes.put(CSSHelper.PROP_THEME_NAME, _config);
|
|
|
|
if (_universalTheming)
|
|
|
|
changes.put(CSSHelper.PROP_UNIVERSAL_THEMING, "true");
|
|
|
|
else
|
|
|
|
removes.add(CSSHelper.PROP_UNIVERSAL_THEMING);
|
2013-01-21 05:59:53 +00:00
|
|
|
if (_forceMobileConsole)
|
|
|
|
changes.put(CSSHelper.PROP_FORCE_MOBILE_CONSOLE, "true");
|
|
|
|
else
|
|
|
|
removes.add(CSSHelper.PROP_FORCE_MOBILE_CONSOLE);
|
2012-08-03 03:49:46 +00:00
|
|
|
boolean ok = _context.router().saveConfig(changes, removes);
|
2012-01-18 01:54:34 +00:00
|
|
|
if (ok) {
|
2009-10-26 21:48:46 +00:00
|
|
|
if (!oldTheme.equals(_config))
|
|
|
|
addFormNotice(_("Theme change saved.") +
|
2010-11-17 22:26:31 +00:00
|
|
|
" <a href=\"configui\">" +
|
2009-10-26 21:48:46 +00:00
|
|
|
_("Refresh the page to view.") +
|
2013-01-21 05:59:53 +00:00
|
|
|
"</a>");
|
|
|
|
if (oldForceMobileConsole != _forceMobileConsole)
|
|
|
|
addFormNotice(_("Mobile console option saved.") +
|
|
|
|
" <a href=\"configui\">" +
|
|
|
|
_("Refresh the page to view.") +
|
2009-10-26 21:48:46 +00:00
|
|
|
"</a>");
|
|
|
|
} else {
|
2011-11-30 23:23:41 +00:00
|
|
|
addFormError(_("Error saving the configuration (applied but not saved) - please see the error logs."));
|
2009-10-26 21:48:46 +00:00
|
|
|
}
|
2009-07-12 03:21:20 +00:00
|
|
|
}
|
2012-10-13 21:20:16 +00:00
|
|
|
|
|
|
|
private void addUser() {
|
|
|
|
String name = getJettyString("name");
|
|
|
|
if (name == null || name.length() <= 0) {
|
|
|
|
addFormError(_("No user name entered"));
|
|
|
|
return;
|
|
|
|
}
|
2014-08-03 13:58:51 +00:00
|
|
|
String pw = getJettyString("nofilter_pw");
|
2012-10-13 21:20:16 +00:00
|
|
|
if (pw == null || pw.length() <= 0) {
|
|
|
|
addFormError(_("No password entered"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ConsolePasswordManager mgr = new ConsolePasswordManager(_context);
|
|
|
|
// rfc 2617
|
2012-10-15 12:28:45 +00:00
|
|
|
if (mgr.saveMD5(RouterConsoleRunner.PROP_CONSOLE_PW, RouterConsoleRunner.JETTY_REALM, name, pw)) {
|
2012-10-27 18:45:16 +00:00
|
|
|
if (!_context.getBooleanProperty(RouterConsoleRunner.PROP_PW_ENABLE))
|
|
|
|
_context.router().saveConfig(RouterConsoleRunner.PROP_PW_ENABLE, "true");
|
2012-10-13 21:20:16 +00:00
|
|
|
addFormNotice(_("Added user {0}", name));
|
2013-09-03 10:25:18 +00:00
|
|
|
addFormError(_("Restart required to take effect"));
|
2012-10-13 21:20:16 +00:00
|
|
|
} else {
|
|
|
|
addFormError(_("Error saving the configuration (applied but not saved) - please see the error logs."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void delUser() {
|
|
|
|
ConsolePasswordManager mgr = new ConsolePasswordManager(_context);
|
|
|
|
boolean success = false;
|
|
|
|
for (Object o : _settings.keySet()) {
|
|
|
|
if (!(o instanceof String))
|
|
|
|
continue;
|
|
|
|
String k = (String) o;
|
|
|
|
if (!k.startsWith("delete_"))
|
|
|
|
continue;
|
|
|
|
k = k.substring(7);
|
|
|
|
if (mgr.remove(RouterConsoleRunner.PROP_CONSOLE_PW, k)) {
|
|
|
|
addFormNotice(_("Removed user {0}", k));
|
|
|
|
success = true;
|
|
|
|
} else {
|
|
|
|
addFormError(_("Error saving the configuration (applied but not saved) - please see the error logs."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (success)
|
2013-09-03 10:25:18 +00:00
|
|
|
addFormError(_("Restart required to take effect"));
|
2012-10-13 21:20:16 +00:00
|
|
|
}
|
2009-07-12 03:21:20 +00:00
|
|
|
}
|