Commits

Zhang Huangbin committed e2c8769

Code cleanup.
Don't query SQL in src/iredapd.py, just pass connection cursor to plugins.

Comments (0)

Files changed (3)

etc/iredapd.ini.sample

 db          = vmail
 user        = vmail
 password    = Psaf68wsuVctYSbj4PJzRqmFsE0rlQ
-alias_table = alias
 
 # Enabled plugins.
 #   - Plugin name is file name which placed under 'src/plugins/' directory.
 class MySQLModeler:
     def __init__(self):
         import web
+
+        # Turn off debug mode.
         web.config.debug = False
 
         self.db = web.database(
             self.plugins = [v.strip() for v in self.plugins.split(',')]
 
             # Get sender, recipient.
-            sender = map['sender']
-            sender_domain = sender.split('@', 1)[1]
-
-            recipient = map['recipient']
-            recipient_domain = recipient.split('@', 1)[1]
+            # Sender/recipient are used almost in all plugins, so store them
+            # a dict and pass to plugins.
+            senderReceiver = {
+                'sender': map['sender'],
+                'recipient': map['recipient'],
+                'sender_domain': map['sender'].split('@')[-1]
+                'recipient_domain': map['recipient'].split('@')[-1]
+            }
 
             if len(self.plugins) > 0:
-                # Get alias account from alias table.
-                vars = dict(recipient=recipient, recipient_domain=recipient_domain, )
-                result = self.db.select(
-                    cfg.get('mysql', 'alias_table', 'alias'),
-                    vars, where='address = $recipient AND domain = $recipient_domain',
-                )
-
-                # Return if recipient account doesn't exist.
-                if len(result) != 1:
-                    logging.debug('No alias found: %s' % recipient)
-                    return ACTION_DEFAULT
-
                 #
                 # Import plugin modules.
                 #
                         logging.debug('Apply plugin (%s).' % (module.__name__, ))
                         pluginAction = module.restriction(
                             dbConn=self.db,
-                            sqlRecord=result[0],
+                            senderReceiver=senderReceiver,
                             smtpSessionData=map,
                         )
 

src/plugins/sql_alias_access_policy.py

 #   - membersAndModeratorsOnly: Only members and moderators are allowed.
 
 import sys
-import logging
 
 ACTION_REJECT = 'REJECT Not Authorized'
 
 POLICY_ALLOWEDONLY = 'allowedOnly'      # Same as @POLICY_MODERATORSONLY
 POLICY_MEMBERSANDMODERATORSONLY = 'membersandmoderatorsonly'
 
-def restriction(dbConn, sqlRecord, smtpSessionData, **kargs):
+def restriction(dbConn, senderReceiver, smtpSessionData, **kargs):
+    # Get alias account from alias table.
+    result = dbConn.select('alias',
+        senderReceiver, where='address = $recipient AND domain = $recipient_domain',
+    )
+
+    # Return if recipient account doesn't exist.
+    if len(result) != 1:
+        return 'DUNNO'
+
+    # Use the first SQL record.
+    sqlRecord = result[0]
+
     policy = sqlRecord.get('accesspolicy', 'public').lower()
 
-    sender = smtpSessionData['sender'].lower()
-    sender_domain = sender.split('@')[1]
-
-    recipient = smtpSessionData['recipient'].lower()
-    recipient_domain = recipient.split('@')[1]
-
     members = [str(v.lower()) for v in sqlRecord.get('goto', '').split(',')]
     moderators = [str(v.lower()) for v in sqlRecord.get('moderators', '').split(',')]
 
         return 'DUNNO'
     elif policy == POLICY_DOMAIN:
         # Bypass all users under the same domain.
-        if sender_domain == recipient_domain:
+        if senderReceiver['sender_domain'] == senderReceiver['recipient_domain']:
             return 'DUNNO'
         else:
             return ACTION_REJECT
     elif policy == POLICY_SUBDOMAIN:
         # Bypass all users under the same domain or sub domains.
-        if sender.endswith('.' + recipient_domain):
+        if senderReceiver['sender'].endswith('.' + senderReceiver['recipient_domain']):
             return 'DUNNO'
         else:
             return ACTION_REJECT
     elif policy == POLICY_MEMBERSONLY:
         # Bypass all members.
-        if sender in members:
+        if senderReceiver['sender'] in members:
             return 'DUNNO'
         else:
             return ACTION_REJECT
     elif policy == POLICY_MODERATORSONLY or policy == POLICY_ALLOWEDONLY:
         # Bypass all moderators.
-        if sender in moderators:
+        if senderReceiver['sender'] in moderators:
             return 'DUNNO'
         else:
             return ACTION_REJECT
     elif policy == POLICY_MEMBERSANDMODERATORSONLY:
         # Bypass both members and moderators.
-        if sender in members or sender in moderators:
+        if senderReceiver['sender'] in members or senderReceiver['sender'] in moderators:
             return 'DUNNO'
         else:
             return ACTION_REJECT
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.