Merge pull request #56 from jvalleroy/remove_old_user_db

Remove old user stuff
This commit is contained in:
Nick Daly 2013-11-24 20:43:35 -08:00
commit 0ec794aaa7
7 changed files with 9 additions and 122 deletions

View File

@ -12,8 +12,8 @@ ALL_BUT_GZ=$(subst $(wildcard *.tar.gz),,$(wildcard *))
DATADIR=/usr/share/plinth
PYDIR=$(DATADIR)/python/plinth
## Catch-all tagets
default: config dirs template css docs dbs
## Catch-all targets
default: config dirs template css docs
all: default
predepend:
@ -34,7 +34,7 @@ install: default apache-install freedombox-setup-install
cp share/init.d/plinth $(DESTDIR)/etc/init.d
install plinth $(DESTDIR)/usr/bin/
mkdir -p $(DESTDIR)/var/lib/plinth/cherrypy_sessions $(DESTDIR)/var/log/plinth $(DESTDIR)/var/run
cp -r data/* $(DESTDIR)/var/lib/plinth
mkdir -p $(DESTDIR)/var/lib/plinth/data
rm -f $(DESTDIR)/var/lib/plinth/users/sqlite3.distrib
freedombox-setup-install:
@ -47,11 +47,6 @@ uninstall:
rm -f $(DESTDIR)/usr/bin/plinth $(DESTDIR)/etc/init.d/plinth \
$(DESTDIR)/usr/share/man/man1/plinth.1.gz $(DESTDIR)/var/run/plinth.pid
dbs: data/users.sqlite3
data/users.sqlite3: data/users.sqlite3.distrib
cp data/users.sqlite3.distrib data/users.sqlite3
dirs:
@mkdir -p data/cherrypy_sessions

3
cfg.py
View File

@ -28,7 +28,6 @@ store_file = get_item(parser, 'Path', 'store_file')
user_db = get_item(parser, 'Path', 'user_db')
status_log_file = get_item(parser, 'Path', 'status_log_file')
access_log_file = get_item(parser, 'Path', 'access_log_file')
users_dir = get_item(parser, 'Path', 'users_dir')
pidfile = get_item(parser, 'Path', 'pidfile')
host = get_item(parser, 'Network', 'host')
port = int(get_item(parser, 'Network', 'port'))
@ -38,3 +37,5 @@ main_menu = Menu()
if store_file.endswith(".sqlite3"):
store_file = os.path.splitext(store_file)[0]
if user_db.endswith(".sqlite3"):
user_db = os.path.splitext(user_db)[0]

Binary file not shown.

View File

@ -1 +0,0 @@
{"username": "admin", "name": "Admin", "expert": "on", "groups": ["expert"], "passphrase": "5ebe2294ecd0e0f08eab7690d2a6ee69", "password": "", "email": "root@localhost"}

View File

@ -11,7 +11,6 @@ from withsqlite.withsqlite import sqlite_db
class UserStore(UserStoreModule, sqlite_db):
def __init__(self):
self.data_dir = cfg.users_dir
self.db_file = cfg.user_db
sqlite_db.__init__(self, self.db_file, autocommit=True, check_same_thread=False)
self.__enter__()
@ -64,110 +63,3 @@ class UserStore(UserStoreModule, sqlite_db):
def set(self,username=None,user=None):
sqlite_db.__setitem__(self,username, user)
class UserStoreOld():
#class UserStore(UserStoreModule):
"""The user storage is on disk. Rather than slurp the entire
thing, we read from the disk as needed. Writes are immediate,
though.
TODO: file locking"""
def __init__(self):
self.data_dir = cfg.users_dir
self.users = {}
def sanitize(username):
"""Return username with nonalphanumeric/underscore chars
removed.
TODO: allow international chars in usernames."""
pass
def attr(self, key, username):
"""Return field from username's record. If key does not
exist, don't raise attribute error, just return None.
User defaults to current. If no current user and none
specified, return none."""
try:
return self.get(username)[key]
except AttributeError:
return None
def current(self, name=False):
"""Return current user, if there is one, else None.
If name = True, return the username instead of the user."""
try:
username = cherrypy.session.get(cfg.session_key)
if name:
return username
else:
return self.get(username)
except AttributeError:
return None
def expert(self, username=None):
"""Return True if user username is an expert, else False.
If username is None, use the current user. If no such user exists, return False."""
user = self.get(username)
if not user:
return False
return 'expert' in user['groups']
def get(self, username=None, reload=False):
"""Returns a user instance with the user's info or else None if the user couldn't be found.
If reload is true, reload from disk, regardless of dict's contents
If username is None, try current user. If no current user, return None.
TODO: implement user_store.get reload"""
if not username:
username = self.current(name=True)
try:
return self.users[username]
except KeyError:
try:
IF = open(os.path.join(self.data_dir, username), "r")
except IOError:
return None
data = IF.read()
IF.close()
# We cache the result, and since we assume a system with
# relatively few users and small user data files, we never
# expire those results. If we revisit that assumption, we
# might need some cache expiry.
self.users[username] = User(json.loads(data))
return self.users[username]
def exists(self, username):
"""Return True if username exists, else False."""
return username in self.users or os.path.exists(os.path.join(cfg.users_dir, username))
def get_all(self):
"Returns a list of all the user objects"
usernames = os.listdir(self.data_dir)
for name in usernames:
self.get(name)
return self.users
def set(self, user):
"""Set the user data, both in memory and as needed in storage."""
OF = open(os.path.join(self.data_dir, user['username']), 'w')
OF.write(json.dumps(user))
OF.close()
def remove(self, user):
"""Delete the user from storage and RAM. User can be a user instance or a username."""
try:
name = user['name']
except TypeError:
if isinstance(user, basestring):
name = user
else:
raise TypeError
os.unlink(os.path.join(cfg.users_dir, name))
try:
del self.users[name]
except KeyError:
pass
cfg.log.info("%s deleted %s" % (cherrypy.session.get(cfg.session_key), name))

View File

@ -18,7 +18,8 @@ class users(PagePlugin):
@cherrypy.expose
@require()
def index(self):
return self.fill_template(title="Manage Users and Groups", sidebar_right="""<strong><a href="/sys/users/add">Add User</a></strong><br/><strong><a href="/sys/users/edit">Edit Users</a></strong>""")
sidebar_right = '<strong><a href="'+cfg.server_dir+'/sys/users/add">Add User</a></strong><br/><strong><a href="'+cfg.server_dir+'/sys/users/edit">Edit Users</a></strong>'
return self.fill_template(title="Manage Users and Groups", sidebar_right=sidebar_right)
class add(FormPlugin, PagePlugin):
url = ["/sys/users/add"]
@ -75,7 +76,7 @@ class edit(FormPlugin, PagePlugin):
user = User(uname[1])
add_form.html('<span class="indent">&nbsp;&nbsp;%s&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' %
add_form.get_checkbox(name=user['username']) +
'<a href="/sys/users/edit?username=%s">%s (%s)</a><br /></span>' %
'<a href="'+cfg.server_dir+'/sys/users/edit?username=%s">%s (%s)</a><br /></span>' %
(user['username'], user['name'], user['username']))
add_form.submit(label=_("Delete User"), name="delete")
return add_form.render()

View File

@ -9,12 +9,11 @@ data_dir = %(file_root)s/data
log_dir = %(data_dir)s
pid_dir = %(data_dir)s
python_root = %(file_root)s
users_dir = %(data_dir)s/users
server_dir = plinth/
# file locations
store_file = %(data_dir)s/store.sqlite3
user_db = %(users_dir)s
user_db = %(data_dir)s/users.sqlite3
status_log_file = %(log_dir)s/status.log
access_log_file = %(log_dir)s/access.log
pidfile = %(pid_dir)s/pidfile.pid