[2] | 1 | # -*- coding: utf-8 -*- |
---|
| 2 | # |
---|
| 3 | # Copyright (C) 2005 Matthew Good <trac@matt-good.net> |
---|
| 4 | # |
---|
| 5 | # "THE BEER-WARE LICENSE" (Revision 42): |
---|
| 6 | # <trac@matt-good.net> wrote this file. As long as you retain this notice you |
---|
| 7 | # can do whatever you want with this stuff. If we meet some day, and you think |
---|
| 8 | # this stuff is worth it, you can buy me a beer in return. Matthew Good |
---|
| 9 | # |
---|
| 10 | # Author: Matthew Good <trac@matt-good.net> |
---|
| 11 | |
---|
| 12 | import inspect |
---|
| 13 | |
---|
| 14 | from trac.core import * |
---|
| 15 | from trac.config import Option |
---|
| 16 | from trac.perm import PermissionSystem |
---|
| 17 | from trac.util import sorted |
---|
| 18 | from trac.util.datefmt import format_datetime |
---|
| 19 | from trac.web.chrome import ITemplateProvider |
---|
| 20 | from webadmin.web_ui import IAdminPageProvider |
---|
| 21 | |
---|
| 22 | from acct_mgr.api import AccountManager |
---|
| 23 | from acct_mgr.web_ui import _create_user |
---|
| 24 | |
---|
| 25 | def _getoptions(cls): |
---|
| 26 | if isinstance(cls, Component): |
---|
| 27 | cls = cls.__class__ |
---|
| 28 | return [(name, value) for name, value in inspect.getmembers(cls) |
---|
| 29 | if isinstance(value, Option)] |
---|
| 30 | |
---|
| 31 | class AccountManagerAdminPage(Component): |
---|
| 32 | |
---|
| 33 | implements(IAdminPageProvider) |
---|
| 34 | |
---|
| 35 | def __init__(self): |
---|
| 36 | self.account_manager = AccountManager(self.env) |
---|
| 37 | |
---|
| 38 | # IAdminPageProvider |
---|
| 39 | def get_admin_pages(self, req): |
---|
| 40 | if req.perm.has_permission('TRAC_ADMIN'): |
---|
| 41 | yield ('accounts', 'Comptes', 'config', 'Configuration') |
---|
| 42 | yield ('accounts', 'Comptes', 'users', 'Utilisateurs') |
---|
| 43 | |
---|
| 44 | def process_admin_request(self, req, cat, page, path_info): |
---|
| 45 | if page == 'config': |
---|
| 46 | return self._do_config(req) |
---|
| 47 | elif page == 'users': |
---|
| 48 | return self._do_users(req) |
---|
| 49 | |
---|
| 50 | def _do_config(self, req): |
---|
| 51 | if req.method == 'POST': |
---|
| 52 | selected_class = req.args.get('selected') |
---|
| 53 | self.config.set('account-manager', 'password_store', selected_class) |
---|
| 54 | selected = self.account_manager.password_store |
---|
| 55 | for attr, option in _getoptions(selected): |
---|
| 56 | newvalue = req.args.get('%s.%s' % (selected_class, attr)) |
---|
| 57 | if newvalue is not None: |
---|
| 58 | self.config.set(option.section, option.name, newvalue) |
---|
| 59 | self.config.save() |
---|
| 60 | try: |
---|
| 61 | selected = self.account_manager.password_store |
---|
| 62 | except AttributeError: |
---|
| 63 | selected = None |
---|
| 64 | sections = [ |
---|
| 65 | {'name': store.__class__.__name__, |
---|
| 66 | 'classname': store.__class__.__name__, |
---|
| 67 | 'selected': store is selected, |
---|
| 68 | 'options': [ |
---|
| 69 | {'label': attr, |
---|
| 70 | 'name': '%s.%s' % (store.__class__.__name__, attr), |
---|
| 71 | 'value': option.__get__(store, store), |
---|
| 72 | } |
---|
| 73 | for attr, option in _getoptions(store) |
---|
| 74 | ], |
---|
| 75 | } for store in self.account_manager.stores |
---|
| 76 | ] |
---|
| 77 | sections = sorted(sections, key=lambda i: i['name']) |
---|
| 78 | req.hdf['sections'] = sections |
---|
| 79 | return 'admin_accountsconfig.cs', None |
---|
| 80 | |
---|
| 81 | def _do_users(self, req): |
---|
| 82 | perm = PermissionSystem(self.env) |
---|
| 83 | listing_enabled = self.account_manager.supports('get_users') |
---|
| 84 | create_enabled = self.account_manager.supports('set_password') |
---|
| 85 | delete_enabled = self.account_manager.supports('delete_user') |
---|
| 86 | |
---|
| 87 | req.hdf['listing_enabled'] = listing_enabled |
---|
| 88 | req.hdf['create_enabled'] = create_enabled |
---|
| 89 | req.hdf['delete_enabled'] = delete_enabled |
---|
| 90 | |
---|
| 91 | if req.method == 'POST': |
---|
| 92 | if req.args.get('add'): |
---|
| 93 | if create_enabled: |
---|
| 94 | try: |
---|
| 95 | _create_user(req, self.env, check_permissions=False) |
---|
| 96 | except TracError, e: |
---|
| 97 | req.hdf['registration.error'] = e.message |
---|
| 98 | else: |
---|
| 99 | req.hdf['registration_error'] = u'L\'espace de stockage des ' \ |
---|
| 100 | u'mots de passe ne supporte ' \ |
---|
| 101 | u'pas la création des utilisateurs' |
---|
| 102 | elif req.args.get('remove'): |
---|
| 103 | if delete_enabled: |
---|
| 104 | sel = req.args.get('sel') |
---|
| 105 | sel = isinstance(sel, list) and sel or [sel] |
---|
| 106 | for account in sel: |
---|
| 107 | self.account_manager.delete_user(account) |
---|
| 108 | else: |
---|
| 109 | req.hdf['deletion_error'] = u'L\'espace de stockage des ' \ |
---|
| 110 | u'mots de passe ne supporte ' \ |
---|
| 111 | u'pas la suppression des utilisateurs' |
---|
| 112 | if listing_enabled: |
---|
| 113 | accounts = {} |
---|
| 114 | for username in self.account_manager.get_users(): |
---|
| 115 | accounts[username] = {'username': username} |
---|
| 116 | |
---|
| 117 | for username, name, email in self.env.get_known_users(): |
---|
| 118 | account = accounts.get(username) |
---|
| 119 | if account: |
---|
| 120 | account['name'] = name |
---|
| 121 | account['email'] = email |
---|
| 122 | |
---|
| 123 | db = self.env.get_db_cnx() |
---|
| 124 | cursor = db.cursor() |
---|
| 125 | cursor.execute("SELECT sid,last_visit FROM session WHERE authenticated=1") |
---|
| 126 | for username, last_visit in cursor: |
---|
| 127 | account = accounts.get(username) |
---|
| 128 | if account and last_visit: |
---|
| 129 | account['last_visit'] = format_datetime(last_visit) |
---|
| 130 | |
---|
| 131 | req.hdf['accounts'] = sorted(accounts.itervalues(), |
---|
| 132 | key=lambda acct: acct['username']) |
---|
| 133 | |
---|
| 134 | return 'admin_users.cs', None |
---|
| 135 | |
---|
| 136 | # ITemplateProvider |
---|
| 137 | |
---|
| 138 | def get_htdocs_dirs(self): |
---|
| 139 | """Return the absolute path of a directory containing additional |
---|
| 140 | static resources (such as images, style sheets, etc). |
---|
| 141 | """ |
---|
| 142 | return [] |
---|
| 143 | |
---|
| 144 | def get_templates_dirs(self): |
---|
| 145 | """Return the absolute path of the directory containing the provided |
---|
| 146 | ClearSilver templates. |
---|
| 147 | """ |
---|
| 148 | from pkg_resources import resource_filename |
---|
| 149 | return [resource_filename(__name__, 'templates')] |
---|
| 150 | |
---|