Permalink
Browse files

Add a command-line script to make an existing user an admin.

  • Loading branch information...
1 parent dd7dcc0 commit acb1fa70bc5d1ef4f34d27864fa2a59ce0fa9f37 @tseaver tseaver committed Nov 8, 2011
Showing with 59 additions and 0 deletions.
  1. +2 −0 TODO.txt
  2. +55 −0 cartouche/admin.py
  3. +2 −0 setup.py
View
@@ -154,3 +154,5 @@ User / Group Administration
* Key group name -> member UUIDs
* Key member UUID -> group names
+
+- [X] Add a command-line script to make an existing user an admin.
View
@@ -0,0 +1,55 @@
+import os
+import sys
+
+from pyramid.paster import bootstrap
+import transaction
+
+from cartouche.interfaces import IRegistrations
+from cartouche.persistence import ConfirmedRegistrations
+
+
+def add_admin_user():
+ __doc__ = """ Make an existing cartouche user a member of the 'admin' group.
+
+ Usage: %s config_uri login
+ """
+ try:
+ config_uri, login = sys.argv[1:]
+ except:
+ print __doc__ % sys.argv[0]
+ sys.exit[2]
+
+ ini_file = config_uri.split('#')[0]
+
+ if not os.path.isfile(ini_file):
+ print __doc__ % sys.argv[0]
+ print
+ print 'Invalid config file:', ini_file
+ print
+ sys.exit[2]
+
+ env = bootstrap(config_uri)
+ request, root = env['request'], env['root']
+ confirmed = request.registry.queryAdapter(root, IRegistrations,
+ name='confirmed')
+ if confirmed is None:
+ confirmed = ConfirmedRegistrations(root)
+
+ info = confirmed.get_by_login(login)
+ if info is None:
+ print __doc__ % sys.argv[0]
+ print
+ print 'Invalid login:', login
+ print
+ sys.exit[2]
+
+ admins = confirmed._getMapping('group_users').get('g:admin') or []
+ if info.uuid not in admins:
+ admins.append(info.uuid)
+ confirmed._getMapping('group_users')['g:admin'] = admins
+ groups = confirmed._getMapping('user_groups').get(info.uuid) or []
+ groups.append('g:admin')
+ confirmed._getMapping('user_groups')[info.uuid] = groups
+
+ transaction.commit()
+ env['closer']()
View
@@ -67,6 +67,8 @@
entry_points = """\
[paste.app_factory]
main = cartouche:main
+ [console_scripts]
+ add_cartouche_admin = cartouche.admin:add_admin_user
""",
paster_plugins=['pyramid'],
)

0 comments on commit acb1fa7

Please sign in to comment.