def init_account_mail(self, use_mail_module): u""" Cache account mail addresses. This method builds a dict cache that maps account_id -> primary email address, and assigns the `dict.get` method to `self.account_mail`. NOTE: The LDAP_PERSON['mail_target_types'] setting decides which email target types are considered. :param bool use_mail_module: If True, Cerebrum.modules.Email will be used to populate this cache; otherwise the `self.account_mail` method will be None (not implemented). """ # Set self.account_mail = None if not use_mail_module, otherwise # function: account_id -> ('address' or None). if use_mail_module: timer = make_timer(self.logger, "Fetching account e-mail addresses...") # Get target types from config mail_target_types = [] for value in ldapconf('PERSON', 'mail_target_types', []): code = self.const.human2constant(value, self.const.EmailTarget) if code is None: self.logger.warn("Unknown EmailTarget %r in setting %s", value, "LDAP_PERSON['mail_target_types']") else: mail_target_types.append(code) # We don't want to import this if mod_email isn't present. from Cerebrum.modules.Email import EmailDomain, EmailTarget targets = EmailTarget(self.db).list_email_target_primary_addresses rewrite = EmailDomain(self.db).rewrite_special_domains # Look up target addrs. Note that the reversed order causes the # lesser prioritized target types to be overwritten by targets with # higher priority. mail = {} for code in reversed(mail_target_types): target_timer = make_timer(self.logger) for row in targets(target_type=code): try: mail[int(row['target_entity_id'])] = "@".join( (row['local_part'], rewrite(row['domain']))) except TypeError: continue target_timer("...target_type '{!s}' done".format(code)) self.account_mail = mail.get timer("...account e-mail addresses done.") else: self.account_mail = None
def __init__(self): """Initialize the Utils.""" self.db = Factory.get('Database')(client_encoding='UTF-8') self.en = Factory.get('Entity')(self.db) self.ac = Factory.get('Account')(self.db) self.pe = Factory.get('Person')(self.db) self.gr = Factory.get('Group')(self.db) self.co = Factory.get('Constants')(self.db) self.ed = EmailDomain(self.db) self.eq = EmailQuota(self.db) self.ef = EmailForward(self.db) self.et = Factory.get('EmailTarget')(self.db) self.dg = DistributionGroup(self.db)
def handle_person(database, source_system, affiliations, send_notifications, email_config, data): pe = Factory.get('Person')(database) ac = Factory.get('Account')(database) et = EmailTarget(database) ef = EmailForward(database) ed = EmailDomain(database) if (data.get('resourceType') == 'persons' and 'affiliation' in data.get( 'urn:ietf:params:event:SCIM:modify', {}).get('attributes', [])): ident = int(data.get('sub').split('/')[-1]) if not pe.list_affiliations(person_id=ident, source_system=source_system, affiliation=affiliations): return pe.clear() pe.find(ident) removed_forwards = defaultdict(list) for account_id in map(lambda x: x['account_id'], pe.get_accounts(filter_expired=False)): try: et.clear() et.find_by_target_entity(account_id) except Errors.NotFoundError: continue ef.clear() ef.find(et.entity_id) for forward in map(lambda x: x['forward_to'], ef.get_forward()): try: ed.clear() ed.find_by_domain(forward.split('@')[-1]) except Errors.NotFoundError: ac.clear() ac.find(account_id) ef.delete_forward(forward) removed_forwards[ac.get_primary_mailaddress()].append( forward) logger.info('Deleted forward {} from {}'.format( forward, ac.account_name)) if send_notifications: for k, v in removed_forwards.items(): sendmail(toaddr=k, fromaddr=email_config.sender, subject=email_config.subject, body=email_config.body_template.format('\n'.join(v))) database.commit()
def _fetch_primary_mail_addresses(self, user_dict): """ Fetch primary email addresses for users in user_dict. Add key, value pair 'mail', <primary address> if found. @param user_dict: account_id -> account info mapping @type user_dict: dict """ from Cerebrum.modules.Email import EmailDomain, EmailTarget etarget = EmailTarget(self.db) rewrite = EmailDomain(self.db).rewrite_special_domains for row in etarget.list_email_target_primary_addresses( target_type=self.co.email_target_account): v = user_dict.get(int(row['target_entity_id'])) if not v: continue try: v['mail'] = "@".join( (row['local_part'], rewrite(row['domain']))) except TypeError: pass # Silently ignore
def fetch_forward_info(self): """ Fetch forward info for all users with both AD and exchange spread. """ from Cerebrum.modules.Email import EmailDomain, EmailTarget, EmailForward etarget = EmailTarget(self.db) rewrite = EmailDomain(self.db).rewrite_special_domains eforward = EmailForward(self.db) # We need a email target -> entity_id mapping target_id2target_entity_id = {} for row in etarget.list_email_targets_ext(): if row['target_entity_id']: te_id = int(row['target_entity_id']) target_id2target_entity_id[int(row['target_id'])] = te_id # Check all email forwards for row in eforward.list_email_forwards(): te_id = target_id2target_entity_id.get(int(row['target_id'])) acc = self.get_account(account_id=te_id) # We're only interested in those with AD and exchange spread if acc.to_exchange: acc.add_forward(row['forward_to'])
import sys from Cerebrum.Utils import Factory from Cerebrum.modules.Email import EmailDomain from Cerebrum.modules.Email import EmailAddress from Cerebrum.modules.Email import EmailPrimaryAddressTarget db = Factory.get('Database')() db.cl_init(change_program='create_dg_moderator') ac = Factory.get('Account')(db) co = Factory.get('Constants')(db) dg = Factory.get('DistributionGroup')(db) gr = Factory.get('Group')(db) et = Factory.get('EmailTarget')(db) epat = EmailPrimaryAddressTarget(db) ed = EmailDomain(db) ea = EmailAddress(db) group_name = 'groupadmin' if len(sys.argv) > 1: group_domain = sys.argv[1] else: group_domain = 'groups.uio.no' ac.clear() ac.find_by_name('bootstrap_account') gr.clear() gr.populate(
def get_domainid(db, domain_part): domain = EmailDomain(db) domain.find_by_domain(domain_part) return domain.entity_id
def ed(self): if not hasattr(self, '_ed'): self._ed = EmailDomain(self._db) return self._ed