| | 1 | |
|---|
| | 2 | from trac.core import * |
|---|
| | 3 | from trac.admin import IAdminPanelProvider |
|---|
| | 4 | from trac.config import Option, ListOption |
|---|
| | 5 | from trac.web.chrome import ITemplateProvider |
|---|
| | 6 | from trac.notification import NotifyEmail |
|---|
| | 7 | |
|---|
| | 8 | from api import IAccountChangeListener |
|---|
| | 9 | |
|---|
| | 10 | class AccountChangesListner(Component): |
|---|
| | 11 | implements(IAccountChangeListener) |
|---|
| | 12 | |
|---|
| | 13 | _notify_actions = ListOption( |
|---|
| | 14 | 'account-manager', 'notify_actions', ['new', 'change', 'delete'], |
|---|
| | 15 | doc="""Comma separated list of actions to notify of. |
|---|
| | 16 | Available actions 'new', 'change', 'delete'.""") |
|---|
| | 17 | |
|---|
| | 18 | # IAccountChangeListener methods |
|---|
| | 19 | |
|---|
| | 20 | def user_created(self, username, password): |
|---|
| | 21 | if 'new' in self._notify_actions: |
|---|
| | 22 | notifier = AccountChangesNotification(self.env) |
|---|
| | 23 | notifier.notify(username, 'New user registration') |
|---|
| | 24 | |
|---|
| | 25 | def user_password_changed(self, username, password): |
|---|
| | 26 | if 'change' in self._notify_actions: |
|---|
| | 27 | notifier = AccountChangesNotification(self.env) |
|---|
| | 28 | notifier.notify(username, 'Password reset for user') |
|---|
| | 29 | |
|---|
| | 30 | def user_deleted(self, username): |
|---|
| | 31 | if 'delete' in self._notify_actions: |
|---|
| | 32 | notifier = AccountChangesNotification(self.env) |
|---|
| | 33 | notifier.notify(username, 'Deleted User') |
|---|
| | 34 | |
|---|
| | 35 | class AccountChangesNotification(NotifyEmail): |
|---|
| | 36 | template_name = 'user_changes_email.txt' |
|---|
| | 37 | |
|---|
| | 38 | _recipients = Option( |
|---|
| | 39 | 'account-manager', 'account_changes_notify_addresses', '', |
|---|
| | 40 | """List of email addresses that get notified of user changes, ie, |
|---|
| | 41 | new user, password change and delete user.""") |
|---|
| | 42 | |
|---|
| | 43 | def get_recipients(self, resid): |
|---|
| | 44 | recipients = self._recipients.split() |
|---|
| | 45 | return (recipients,[]) |
|---|
| | 46 | |
|---|
| | 47 | def notify(self, username, action): |
|---|
| | 48 | self.data.update({ |
|---|
| | 49 | 'account': { |
|---|
| | 50 | 'username': username, |
|---|
| | 51 | 'action': action |
|---|
| | 52 | }, |
|---|
| | 53 | 'login': { |
|---|
| | 54 | 'link': self.env.abs_href.login(), |
|---|
| | 55 | } |
|---|
| | 56 | }) |
|---|
| | 57 | |
|---|
| | 58 | projname = self.config.get('project', 'name') |
|---|
| | 59 | subject = '[%s] %s: %s' % (projname, action, username) |
|---|
| | 60 | |
|---|
| | 61 | NotifyEmail.notify(self, username, subject) |
|---|
| | 62 | |
|---|
| | 63 | |
|---|
| | 64 | |
|---|
| | 65 | class AccountChangesNotificationAdminPanel(Component): |
|---|
| | 66 | implements(IAdminPanelProvider, ITemplateProvider) |
|---|
| | 67 | |
|---|
| | 68 | # IAdminPageProvider |
|---|
| | 69 | def get_admin_panels(self, req): |
|---|
| | 70 | if req.perm.has_permission('TRAC_ADMIN'): |
|---|
| | 71 | yield ('accounts', 'Accounts', 'notification', 'Notification') |
|---|
| | 72 | |
|---|
| | 73 | def render_admin_panel(self, req, cat, page, path_info): |
|---|
| | 74 | if page == 'notification': |
|---|
| | 75 | return self._do_config(req) |
|---|
| | 76 | |
|---|
| | 77 | def _do_config(self, req): |
|---|
| | 78 | if req.method == 'POST': |
|---|
| | 79 | self.config.set( |
|---|
| | 80 | 'account-manager', 'account_changes_notify_addresses', |
|---|
| | 81 | ' '.join(req.args.get('notify_addresses').strip('\n').split())) |
|---|
| | 82 | |
|---|
| | 83 | self.config.set('account-manager', 'notify_actions', |
|---|
| | 84 | ','.join(req.args.getlist('notify_actions')) |
|---|
| | 85 | ) |
|---|
| | 86 | self.config.save() |
|---|
| | 87 | notify_addresses = self.config.get( |
|---|
| | 88 | 'account-manager', 'account_changes_notify_addresses').split() |
|---|
| | 89 | notify_actions = self.config.getlist('account-manager', |
|---|
| | 90 | 'notify_actions') |
|---|
| | 91 | data = dict(notify_actions=notify_actions, |
|---|
| | 92 | notify_addresses=notify_addresses) |
|---|
| | 93 | return 'admin_accountsnotification.html', data |
|---|
| | 94 | |
|---|
| | 95 | # ITemplateProvider |
|---|
| | 96 | |
|---|
| | 97 | def get_htdocs_dirs(self): |
|---|
| | 98 | """Return the absolute path of a directory containing additional |
|---|
| | 99 | static resources (such as images, style sheets, etc). |
|---|
| | 100 | """ |
|---|
| | 101 | return [] |
|---|
| | 102 | |
|---|
| | 103 | def get_templates_dirs(self): |
|---|
| | 104 | """Return the absolute path of the directory containing the provided |
|---|
| | 105 | ClearSilver templates. |
|---|
| | 106 | """ |
|---|
| | 107 | from pkg_resources import resource_filename |
|---|
| | 108 | return [resource_filename(__name__, 'templates')] |