Ticket #147: account_changes_notifications.patch

File account_changes_notifications.patch, 7.0 kB (added by s0undt3ch, 8 months ago)
  • /dev/null

    old new  
     1 
     2from trac.core import * 
     3from trac.admin import IAdminPanelProvider 
     4from trac.config import Option, ListOption 
     5from trac.web.chrome import ITemplateProvider 
     6from trac.notification import NotifyEmail 
     7 
     8from api import IAccountChangeListener 
     9 
     10class 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 
     35class 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 
     65class 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')] 
  • /dev/null

    old new  
     1<!DOCTYPE html 
     2    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
     3    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
     4<html xmlns="http://www.w3.org/1999/xhtml" 
     5      xmlns:xi="http://www.w3.org/2001/XInclude" 
     6      xmlns:py="http://genshi.edgewall.org/"> 
     7  <xi:include href="admin.html" /> 
     8  <head> 
     9    <title>Accounts: Notifications Configuration</title> 
     10  </head> 
     11 
     12  <body> 
     13    <h2>Accounts: Notifications Configuration</h2> 
     14 
     15    <form id="accountsconfig" class="mod" method="post">      <fieldset> 
     16      <legend>Account Notifications</legend> 
     17      <p >Set the following options in order to be notified of 
     18        account creations, password resets and account deletions.</p> 
     19 
     20      <h3>Notification Actions</h3> 
     21        <p class="help">This is a list of actions which you can enable or 
     22          disable by <em>checking</em> the <em>checkboxes</em>.</p> 
     23        <input type="checkbox" name="notify_actions" value="new" 
     24          checked="${'new' in notify_actions or None}"> 
     25          Get notified of new accounts creation</input><br/> 
     26        <input type="checkbox" name="notify_actions" value="change" 
     27          checked="${'change' in notify_actions or None}"> 
     28          Get notified of password resets</input><br/> 
     29        <input type="checkbox" name="notify_actions" value="delete" 
     30          checked="${'delete' in notify_actions or None}"> 
     31          Get notified of accounts deletion</input><br/> 
     32 
     33      <h3>Notification Addresses</h3> 
     34      <p class="help">These are the addresses that get notified of the above 
     35        actions, besides<br/> 
     36        <b><tt>smtp_always_cc</tt></b> and <b><tt>smtp_always_bcc</tt></b> 
     37        under <b><tt>[notification]</tt></b> in <b><tt>trac.ini</tt></b>.</p> 
     38      <textarea cols="60" rows="2" id="notify_addresses" 
     39                name="notify_addresses">${ ' '.join(notify_addresses)}</textarea> 
     40    </fieldset> 
     41      <div class="buttons"> 
     42        <input type="submit" name="save" value="Save" /> 
     43      </div> 
     44    </form> 
     45  </body> 
     46</html> 
  • /dev/null

    old new  
     1${account.action} for user ${account.username} 
     2 
     3-- 
     4${project.name} <${project.url}> 
     5${project.descr} 
     6 
  • a/setup.py

    old new  
    3535            'acct_mgr.pwhash = acct_mgr.pwhash', 
    3636            'acct_mgr.svnserve = acct_mgr.svnserve', 
    3737            'acct_mgr.web_ui = acct_mgr.web_ui', 
     38            'acct_mgr.notification = acct_mgr.notification', 
    3839        ] 
    3940    }, 
    4041