From a97988e9307bc5e7427960302cab5351372cf506 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Thu, 23 Jan 2014 18:52:09 -0500 Subject: Add infrastructure to configure server --- src/admin/__init__.py | 0 src/admin/common.py | 123 ++++++++++++++++++++++++++++++++++++++ src/login/common.py | 5 ++ src/root.py | 4 ++ src/util/data.py | 70 +++++++++++++++------- templates/admin/index.html | 41 +++++++++++++ templates/admin/login_plugin.html | 42 +++++++++++++ templates/index.html | 2 +- 8 files changed, 266 insertions(+), 21 deletions(-) create mode 100644 src/admin/__init__.py create mode 100755 src/admin/common.py create mode 100644 templates/admin/index.html create mode 100644 templates/admin/login_plugin.html diff --git a/src/admin/__init__.py b/src/admin/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/admin/common.py b/src/admin/common.py new file mode 100755 index 0000000..6c975b1 --- /dev/null +++ b/src/admin/common.py @@ -0,0 +1,123 @@ +#!/usr/bin/python +# +# Copyright (C) 2014 Simo Sorce +# +# see file 'COPYING' for use and warranty information +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from util import data +from util import page +from util import user +import cherrypy + +def admin_protect(fn): + + def check(*args, **kwargs): + if user.UserSession().get_user().is_admin: + return fn(*args, **kwargs) + + raise cherrypy.HTTPError(403) + + return check + +class LoginPluginPage(page.Page): + + def __init__(self, obj, site, baseurl): + super(LoginPluginPage, self).__init__(site) + self._obj = obj + self.url = '%s/%s' % (baseurl, obj.name) + + # Get the defaults + self.plugin_config = obj.get_config_desc() + if not self.plugin_config: + self.plugin_config = [] + + # Now overlay the actual config + for option in self.plugin_config: + self.plugin_config[option][2] = obj.get_config_value(option) + + @admin_protect + def GET(self, *args, **kwargs): + return self._template('admin/login_plugin.html', + title='%s plugin' % self._obj.name, + name='admin_login_%s_form' % self._obj.name, + action=self.url, + options=self.plugin_config) + + @admin_protect + def POST(self, *args, **kwargs): + + message = "Nothing was modified." + new_values = dict() + + for key, value in kwargs.iteritems(): + if key in self.plugin_config: + if value != self.plugin_config[key][2]: + cherrypy.log.error("Storing [%s]: %s = %s" % ( + self._obj.name, key, value)) + new_values[key] = value + + if len(new_values) != 0: + # First we try to save in the database + try: + store = data.Store() + store.save_login_plugin_config(self._obj.name, new_values) + message = "New configuration saved." + except Exception, e: + message = "Failed to save data!" + + # And only if it succeeds we change the live object + for name, value in new_values.items(): + self._obj.set_config_value(name, value) + self.plugin_config[name][2] = value + + return self._template('admin/login_plugin.html', + message=message, + title='%s plugin' % self._obj.name, + name='admin_login_%s_form' % self._obj.name, + action=self.url, + options=self.plugin_config) + + def root(self, *args, **kwargs): + cherrypy.log.error("method: %s" % cherrypy.request.method) + op = getattr(self, cherrypy.request.method, self.GET) + if callable(op): + return op(*args, **kwargs) + + +class LoginPlugins(page.Page): + def __init__(self, site, baseurl): + super(LoginPlugins, self).__init__(site) + self.url = '%s/login' % baseurl + + for plugin in self._site['login_plugins']['available']: + cherrypy.log.error('Admin login plugin: %s' % plugin) + obj = self._site['login_plugins']['available'][plugin] + self.__dict__[plugin] = LoginPluginPage(obj, self._site, self.url) + + +class Admin(page.Page): + + def __init__(self, *args, **kwargs): + super(Admin, self).__init__(*args, **kwargs) + self.url = '%s/admin' % self.basepath + self.login = LoginPlugins(self._site, self.url) + + def root(self, *args, **kwargs): + login_plugins = self._site['login_plugins'] + return self._template('admin/index.html', title='Administration', + available = login_plugins['available'], + enabled = login_plugins['enabled']) + diff --git a/src/login/common.py b/src/login/common.py index bd39c22..7a11f18 100755 --- a/src/login/common.py +++ b/src/login/common.py @@ -63,6 +63,11 @@ class LoginManagerBase(object): return value + def set_config_value(self, option, value): + if not self._config: + self._config = dict() + self._config[option] = value + def redirect_to_path(self, path): base = cherrypy.config.get('base.mount', "") raise cherrypy.HTTPRedirect('%s/login/%s' % (base, path)) diff --git a/src/root.py b/src/root.py index fb1a6ae..160e85e 100755 --- a/src/root.py +++ b/src/root.py @@ -19,6 +19,7 @@ from util import page from login import common as lc +from admin import common as ac import cherrypy sites = dict() @@ -36,5 +37,8 @@ class Root(page.Page): self.login = lc.Login(self._site) self.logout = lc.Logout(self._site) + # after all plugins are setup we can instantiate the admin pages + self.admin = ac.Admin(self._site) + def root(self): return self._template('index.html', title='Root') diff --git a/src/util/data.py b/src/util/data.py index c716ecf..3fda6d3 100755 --- a/src/util/data.py +++ b/src/util/data.py @@ -28,6 +28,24 @@ class Store(object): self._path = os.getcwd() else: self._path = path + self._admin_dbname = self._get_admin_dbname() + self._user_dbname = self._get_userprefs_dbname() + + def _get_admin_dbname(self): + path = None + if 'admin.config.db' in cherrypy.config: + path = cherrypy.config['admin.config.db'] + if not path: + path = os.path.join(self._path, 'adminconfig.sqlite') + return path + + def _get_userprefs_dbname(self): + path = None + if 'user.prefs.db' in cherrypy.config: + path = cherrypy.config['user.prefs.db'] + if not path: + path = os.path.join(self._path, 'userprefs.sqlite') + return path def _load_config(self, dbname): con = None @@ -64,13 +82,7 @@ class Store(object): return conf def get_admin_config(self): - path = None - if 'admin.config.db' in cherrypy.config: - path = cherrypy.config['admin.config.db'] - if not path: - path = os.path.join(self._path, 'adminconfig.sqlite') - - return self._load_config(path) + return self._load_config(self._admin_dbname) def _load_user_prefs(self, dbname, user): con = None @@ -103,13 +115,7 @@ class Store(object): return conf def _get_user_preferences(self, user): - path = None - if 'user.prefs.db' in cherrypy.config: - path = cherrypy.config['user.prefs.db'] - if not path: - path = os.path.join(self._path, 'userprefs.sqlite') - - return self._load_user_prefs(path, user) + return self._load_user_prefs(self._user_dbname, user) def _load_login_config(self, dbname): con = None @@ -157,11 +163,35 @@ class Store(object): return (lpo, plco); def get_login_config(self): - path = None - if 'admin.config.db' in cherrypy.config: - path = cherrypy.config['admin.config.db'] - if not path: - path = os.path.join(self._path, 'adminconfig.sqlite') + return self._load_login_config(self._admin_dbname) - return self._load_login_config(path) + def save_login_plugin_config(self, name, options): + con = None + try: + con = sqlite3.connect(self._admin_dbname) + cur = con.cursor() + curvals = dict() + for row in cur.execute( + "SELECT option, value FROM login_config WHERE name=?", + (name,)): + curvals[row[0]] = row[1] + + for o in options: + if o in curvals: + cur.execute( + "UPDATE login_config SET value=? WHERE name=? AND option=?", + (options[o], name, o)) + else: + cur.execute( + "INSERT INTO login_config VALUES(?,?,?)", + (name, o, options[o])) + con.commit() + except sqlite3.Error, e: + if con: + con.rollback() + cherrypy.log.error("Failed to store config: [%s]" % e) + raise + finally: + if con: + con.close() diff --git a/templates/admin/index.html b/templates/admin/index.html new file mode 100644 index 0000000..5af8497 --- /dev/null +++ b/templates/admin/index.html @@ -0,0 +1,41 @@ + + + + + {{ title }} + + + + +
+ +
+

Log Out

+
+
+ {% if user.is_admin %} +

Login plugins:

+
    + {% for p in available %} +
  • {{ p }} - + {% if p in enabled %} + Disable - + Configure + {% else %} + Enable + {% endif %} +
  • + {% endfor %} +
+

Plugins order: [list here and form button to change?]

+ {% endif %} +
+
+

Home

+
+
+ + + diff --git a/templates/admin/login_plugin.html b/templates/admin/login_plugin.html new file mode 100644 index 0000000..43adac6 --- /dev/null +++ b/templates/admin/login_plugin.html @@ -0,0 +1,42 @@ + + + + + {{ title }} + + + + +
+ +
+

Log Out

+
+
+

{{ message }}

+
+
+
+
    + {% for o in options %} +
  • {{ o }}: +

    {{ options[o][0] }}

    + +
  • + {% endfor %} +
+ +
+
+
+

+ Admin - + Home +

+
+
+ + + diff --git a/templates/index.html b/templates/index.html index c983af2..1e21948 100644 --- a/templates/index.html +++ b/templates/index.html @@ -13,7 +13,7 @@
{% if user.is_admin %} - admin + admin {% endif %}
-- cgit