Skip to content

Commit

Permalink
Edited the function to add users from the command line to work with s…
Browse files Browse the repository at this point in the history
…qlalchemy

Removed lxml from requries.txt as it throws errors when setting up the app
Removed users.py from app_globals
Modified auth_entry to handle super user
  • Loading branch information
Anusha Ranganathan committed Apr 14, 2012
1 parent 8c8a74c commit a41f574
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 25 deletions.
37 changes: 26 additions & 11 deletions add_user.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import sqlalchemy as sa
from rdfdatabank.model import init_model
from rdfdatabank.lib.auth_entry import add_user, add_user_groups
import sys
import ConfigParser
import sys, os

if __name__ == "__main__":
username = sys.argv[1]
password = sys.argv[2]
email = sys.argv[3]
user_details = {
#Initialize sqlalchemy
c = ConfigParser.ConfigParser()
f = '/var/lib/databank/production.ini'
c.read(f)
if not os.path.exists(f):
print "Config file not found"
elif not 'app:main' in c.sections():
print "Section app:main not found in config file"
else:
engine = sa.create_engine(c.get('app:main', 'sqlalchemy.url'))
init_model(engine)
#add user
username = sys.argv[1]
password = sys.argv[2]
email = sys.argv[3]
user_details = {
'username':u'%s'%username,
'password':u"%s"%password,
'name':u'Databank Administrator',
'email':u"%s"%email
}
add_user(user_details)

groups = []
groups.append(('*', 'administrator'))
add_user_groups(groups)
}
add_user(user_details)
#Add user membership
groups = []
groups.append(('*', 'administrator'))
add_user_groups(username, groups)

1 change: 0 additions & 1 deletion rdfdatabank.egg-info/requires.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ redis
repoze.who>=2.0a4
repoze.who_friendlyform
solrpy
lxml
rdflib==2.4.2
python-dateutil>=1.4.1
4 changes: 2 additions & 2 deletions rdfdatabank/lib/app_globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from rdfdatabank.lib.htpasswd import HtpasswdFile
from rdfdatabank.lib.broadcast import BroadcastToRedis

from rdfdatabank.config.users import _USERS
#from rdfdatabank.config.users import _USERS
from rdfdatabank.config.namespaces import NAMESPACES, PREFIXES

class Globals(object):
Expand All @@ -51,7 +51,7 @@ def __init__(self):
"""

self.authz = authz
self.users = _USERS
#self.users = _USERS
self.NAMESPACES = NAMESPACES
self.PREFIXES = PREFIXES

Expand Down
48 changes: 37 additions & 11 deletions rdfdatabank/lib/auth_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,34 @@
def add_silo(silo_name):
try:
p_q = meta.Session.query(Permission)

ga = Group()
ga.group_name = u'%s_administrator'%silo_name
if silo_name == '*':
ga.group_name = u'databank_administrator'
else:
ga.group_name = u'%s_administrator'%silo_name
ga.silo = u"%s"%silo_name
meta.Session.add(ga)

p_q_admin = p_q.filter(Permission.permission_name == u'administrator').one()
p_q_admin.groups.append(ga)

gb = Group()
gb.group_name = u'%s_manager'%silo_name
if silo_name == '*':
gb.group_name = u'databank_manager'
else:
gb.group_name = u'%s_manager'%silo_name
gb.silo = u"%s"%silo_name
meta.Session.add(gb)

p_q_manager = p_q.filter(Permission.permission_name == u'manager').one()
p_q_manager.groups.append(gb)

gc = Group()
gc.group_name = u'%s_submitter'%silo_name
if silo_name == '*':
gc.group_name = u'databank_submitter'
else:
gc.group_name = u'%s_submitter'%silo_name
gc.silo = u'%s'%silo_name
meta.Session.add(gc)

Expand All @@ -64,9 +73,14 @@ def add_silo(silo_name):
def delete_silo(silo_name):
try:
g_q = meta.Session.query(Group)
g_q_group1 = g_q.filter(Group.group_name == u'%s_administrator'%silo_name).one()
g_q_group2 = g_q.filter(Group.group_name == u'%s_manager'%silo_name).one()
g_q_group3 = g_q.filter(Group.group_name == u'%s_submitter'%silo_name).one()
if silo_name == '*':
g_q_group1 = g_q.filter(Group.group_name == u'databank_administrator').one()
g_q_group2 = g_q.filter(Group.group_name == u'databank_manager').one()
g_q_group3 = g_q.filter(Group.group_name == u'databank_submitter').one()
else:
g_q_group1 = g_q.filter(Group.group_name == u'%s_administrator'%silo_name).one()
g_q_group2 = g_q.filter(Group.group_name == u'%s_manager'%silo_name).one()
g_q_group3 = g_q.filter(Group.group_name == u'%s_submitter'%silo_name).one()
meta.Session.delete(g_q_group1)
meta.Session.delete(g_q_group2)
meta.Session.delete(g_q_group3)
Expand Down Expand Up @@ -154,7 +168,10 @@ def add_user_groups(username, groups):
u = u_q.filter(User.user_name == u'%s'%username).one()
g_q = meta.Session.query(Group)
for silo_name, permission_name in groups:
g_q_group = g_q.filter(Group.group_name == u'%s_%s'%(silo_name, permission_name)).one()
if silo_name =='*':
g_q_group = g_q.filter(Group.group_name == u'databank_%s'%permission_name).one()
else:
g_q_group = g_q.filter(Group.group_name == u'%s_%s'%(silo_name, permission_name)).one()
u.groups.append(g_q_group)
meta.Session.commit()
except IntegrityError:
Expand All @@ -171,7 +188,10 @@ def delete_user_groups(username, groups):
u = u_q.filter(User.user_name == u'%s'%username).one()
g_q = meta.Session.query(Group)
for silo_name, permission_name in groups:
g = g_q.filter(Group.group_name == u'%s_%s'%(silo_name, permission_name)).one()
if silo_name =='*':
g = g_q.filter(Group.group_name == u'databank_%s'%permission_name).one()
else:
g = g_q.filter(Group.group_name == u'%s_%s'%(silo_name, permission_name)).one()
query = "DELETE FROM user_group WHERE user_id=%d and group_id=%d"%(u.id, g.id)
meta.Session.execute(query)
meta.Session.commit()
Expand All @@ -190,7 +210,10 @@ def add_group_users(silo_name, user_groups):
for username, permission_name in user_groups:
u = u_q.filter(User.user_name == u'%s'%username).one()
if u:
g_q_group = g_q.filter(Group.group_name == u'%s_%s'%(silo_name, permission_name)).one()
if silo_name =='*':
g_q_group = g_q.filter(Group.group_name == u'databank_%s'%permission_name).one()
else:
g_q_group = g_q.filter(Group.group_name == u'%s_%s'%(silo_name, permission_name)).one()
u.groups.append(g_q_group)
meta.Session.commit()
except IntegrityError:
Expand All @@ -207,7 +230,10 @@ def delete_group_users(silo_name, user_groups):
g_q = meta.Session.query(Group)
for username, permission_name in user_groups:
u = u_q.filter(User.user_name == u'%s'%username).one()
g = g_q.filter(Group.group_name == u'%s_%s'%(silo_name, permission_name)).one()
if silo_name =='*':
g = g_q.filter(Group.group_name == u'databank_%s'%permission_name).one()
else:
g = g_q.filter(Group.group_name == u'%s_%s'%(silo_name, permission_name)).one()
query = "DELETE FROM user_group WHERE user_id=%d and group_id=%d"%(u.id, g.id)
meta.Session.execute(query)
meta.Session.commit()
Expand Down

0 comments on commit a41f574

Please sign in to comment.