Exemplo n.º 1
0
    def user_create_unpersonal(self, operator,
                               account_name, group_name,
                               contact_address, account_type):
        """Bofh command: user create_unpersonal"""
        self.ba.can_create_user_unpersonal(operator.get_entity_id(),
                                           group=self._get_group(group_name))

        account_type = self._get_constant(self.const.Account, account_type,
                                          "account type")

        account_policy = AccountPolicy(self.db)
        try:
            account = account_policy.create_group_account(
                operator.get_entity_id(),
                account_name,
                self._get_group(group_name),
                contact_address,
                account_type
            )
        except InvalidAccountCreationArgument as e:
            raise CerebrumError(e)

        self._user_password(operator, account)

        # TBD: Better way of checking if email forwards are in use, by
        # checking if bofhd command is available?
        if hasattr(self, '_email_create_forward_target'):
            localaddr = '{}@{}'.format(
                account_name,
                Email.get_primary_default_email_domain())
            self._email_create_forward_target(localaddr, contact_address)

        return {'account_id': int(account.entity_id)}
Exemplo n.º 2
0
 def get_primary_maildomain(self):
     """Return correct `domain_id' for account's primary address."""
     dom = Email.EmailDomain(self._db)
     dom.find_by_domain(Email.get_primary_default_email_domain())
     entdom = Email.EntityEmailDomain(self._db)
     # Find OU and affiliation for this user's best-priority
     # account_type entry.
     for row in self.get_account_types():
         ou, aff = row['ou_id'], row['affiliation']
         # If a maildomain is associated with this (ou, aff)
         # combination, then that is the user's default maildomain.
         entdom.clear()
         try:
             entdom.find(ou, affiliation=aff)
             #
             # This if-test assumes that the cereconf.EMAIL_DEFAULT_DOMAIN
             # cannot be considered as a primary domain if another
             # valid domain is found for an account. The behaviour is wrong
             # for ØFK as quite av few of the accounts should have primary
             # addresses in default domain while they have other domains
             # Jazz
             #
             # If the default domain is specified, ignore this
             # affiliation.
             #     if entdom.entity_email_domain_id == dom.entity_id:
             #         continue
             return entdom.entity_email_domain_id
         except Errors.NotFoundError:
             pass
         # Otherwise, try falling back to tha maildomain associated
         # with (ou, None).
         entdom.clear()
         try:
             entdom.find(ou)
             if entdom.entity_email_domain_id == dom.entity_id:
                 continue
             return entdom.entity_email_domain_id
         except Errors.NotFoundError:
             pass
     # Still no proper maildomain association has been found; fall
     # back to default maildomain.
     return dom.entity_id
Exemplo n.º 3
0
 def get_primary_maildomain(self):
     """Return correct `domain_id' for account's primary address."""
     dom = Email.EmailDomain(self._db)
     dom.find_by_domain(Email.get_primary_default_email_domain())
     entdom = Email.EntityEmailDomain(self._db)
     # Find OU and affiliation for this user's best-priority
     # account_type entry.
     for row in self.get_account_types():
         ou, aff = row['ou_id'], row['affiliation']
         # If a maildomain is associated with this (ou, aff)
         # combination, then that is the user's default maildomain.
         entdom.clear()
         try:
             entdom.find(ou, affiliation=aff)
             #
             # This if-test assumes that the cereconf.EMAIL_DEFAULT_DOMAIN
             # cannot be considered as a primary domain if another
             # valid domain is found for an account. The behaviour is wrong
             # for ØFK as quite av few of the accounts should have primary
             # addresses in default domain while they have other domains
             # Jazz
             #
             # If the default domain is specified, ignore this
             # affiliation.
             #     if entdom.entity_email_domain_id == dom.entity_id:
             #         continue
             return entdom.entity_email_domain_id
         except Errors.NotFoundError:
             pass
         # Otherwise, try falling back to tha maildomain associated
         # with (ou, None).
         entdom.clear()
         try:
             entdom.find(ou)
             if entdom.entity_email_domain_id == dom.entity_id:
                 continue
             return entdom.entity_email_domain_id
         except Errors.NotFoundError:
             pass
     # Still no proper maildomain association has been found; fall
     # back to default maildomain.
     return dom.entity_id
Exemplo n.º 4
0
    def update_email_addresses(self, set_primary=False):
        # check if an e-mail spread is registered yet, if not don't
        # update
        email_spreads = (self.const.spread_exchange_account,
                         self.const.spread_exchange_acc_old,
                         self.const.spread_hia_email,
                         self.const.spread_uia_office_365,
                         self.const.spread_uia_forward)
        if not any([self.has_spread(spread) for spread in email_spreads]):
            # CRB-742: If spread_uia_office_365 is removed
            #  MailTarget targettype should be set as "deleted"
            try:
                et = Email.EmailTarget(self._db)
                et.find_by_email_target_attrs(target_entity_id=self.entity_id)
                if et.email_target_type != self.const.email_target_deleted:
                    et.email_target_type = self.const.email_target_deleted
                    et.write_db()
            except Errors.NotFoundError:
                pass
            return
        # Find, create or update a proper EmailTarget for this
        # account.
        et = Email.EmailTarget(self._db)
        target_type = self.const.email_target_account
        if self.has_spread(self.const.spread_uia_forward):
            target_type = self.const.email_target_forward
        if self.is_deleted() or self.is_reserved():
            target_type = self.const.email_target_deleted
        try:
            et.find_by_email_target_attrs(target_entity_id=self.entity_id)
            et.email_target_type = target_type
        except Errors.NotFoundError:
            # We don't want to create e-mail targets for reserved or
            # deleted accounts, but we do convert the type of existing
            # e-mail targets above.
            if target_type == self.const.email_target_deleted:
                return
            et.populate(target_type, self.entity_id, self.const.entity_account)
        et.write_db()
        # For deleted/reserved users, set expire_date for all of the
        # user's addresses, and don't allocate any new addresses.
        ea = Email.EmailAddress(self._db)
        if target_type == self.const.email_target_deleted:
            expire_date = self._db.DateFromTicks(time.time() +
                                                 60 * 60 * 24 * 1)
            for row in et.get_addresses():
                ea.clear()
                ea.find(row['address_id'])
                if ea.email_addr_expire_date is None:
                    ea.email_addr_expire_date = expire_date
                ea.write_db()
            return
        # if an account email_target without email_server is found assign
        # the appropriate server based on spread and account_type
        spread = None
        if not et.email_server_id:
            if self.get_account_types() or \
               self.owner_type == self.const.entity_group:
                for s in self.get_spread():
                    if s['spread'] in (int(self.const.spread_exchange_account),
                                       int(self.const.spread_exchange_acc_old),
                                       int(self.const.spread_hia_email)):
                        spread = s['spread']
                et = self._update_email_server(spread)
            else:
                # do not set email_server_target
                # until account_type is registered
                return
        # Figure out which domain(s) the user should have addresses
        # in.  Primary domain should be at the front of the resulting
        # list.
        # if the only address found is in EMAIL_DEFAULT_DOMAIN
        # don't set default address. This is done in order to prevent
        # adresses in default domain being sat as primary
        # TODO: account_types affiliated to OU's  without connected
        # email domain don't get a default address
        primary_set = False
        ed = Email.EmailDomain(self._db)
        ed.find(self.get_primary_maildomain())
        domains = [ed.email_domain_name]

        if ed.email_domain_name == Email.get_primary_default_email_domain():
            if not self.owner_type == self.const.entity_group:
                primary_set = True

        # Add the default domains if missing
        for domain in Email.get_default_email_domains():
            if domain not in domains:
                domains.append(domain)

        # Iterate over the available domains, testing various
        # local_parts for availability.  Set user's primary address to
        # the first one found to be available.
        # Never change any existing email addresses
        try:
            self.get_primary_mailaddress()
            primary_set = True
        except Errors.NotFoundError:
            pass
        epat = Email.EmailPrimaryAddressTarget(self._db)
        for domain in domains:
            if ed.email_domain_name != domain:
                ed.clear()
                ed.find_by_domain(domain)
            # Check for 'cnaddr' category before 'uidaddr', to prefer
            # 'cnaddr'-style primary addresses for users in
            # maildomains that have both categories.
            ctgs = [int(r['category']) for r in ed.get_categories()]
            local_parts = []
            if int(self.const.email_domain_category_cnaddr) in ctgs:
                local_parts.append(
                    self.get_email_cn_local_part(
                        given_names=1,
                        max_initials=1
                    )
                )
                local_parts.append(self.account_name)
            elif int(self.const.email_domain_category_uidaddr) in ctgs:
                local_parts.append(self.account_name)
            for lp in local_parts:
                lp = self.wash_email_local_part(lp)
                # Is the address taken?
                ea.clear()
                try:
                    ea.find_by_local_part_and_domain(lp, ed.entity_id)
                    if ea.email_addr_target_id != et.entity_id:
                        # Address already exists, and points to a
                        # target not owned by this Account.
                        continue
                    # Address belongs to this account; make sure
                    # there's no expire_date set on it.
                    ea.email_addr_expire_date = None
                except Errors.NotFoundError:
                    # Address doesn't exist; create it.
                    ea.populate(lp, ed.entity_id, et.entity_id,
                                expire=None)
                ea.write_db()
                if not primary_set:
                    epat.clear()
                    try:
                        epat.find(ea.email_addr_target_id)
                        epat.populate(ea.entity_id)
                    except Errors.NotFoundError:
                        epat.clear()
                        epat.populate(ea.entity_id, parent=et)
                    epat.write_db()
                    primary_set = True
                self.update_email_quota()
Exemplo n.º 5
0
    def update_email_addresses(self, set_primary=False):
        # check if an e-mail spread is registered yet, if not don't
        # update
        email_spreads = (self.const.spread_exchange_account,
                         self.const.spread_exchange_acc_old,
                         self.const.spread_hia_email,
                         self.const.spread_uia_office_365,
                         self.const.spread_uia_forward)
        if not any([self.has_spread(spread) for spread in email_spreads]):
            # CRB-742: If spread_uia_office_365 is removed
            #  MailTarget targettype should be set as "deleted"
            try:
                et = Email.EmailTarget(self._db)
                et.find_by_email_target_attrs(target_entity_id=self.entity_id)
                if et.email_target_type != self.const.email_target_deleted:
                    et.email_target_type = self.const.email_target_deleted
                    et.write_db()
            except Errors.NotFoundError:
                pass
            return
        # Find, create or update a proper EmailTarget for this
        # account.
        et = Email.EmailTarget(self._db)
        target_type = self.const.email_target_account
        if self.has_spread(self.const.spread_uia_forward):
            target_type = self.const.email_target_forward
        if self.is_deleted() or self.is_reserved():
            target_type = self.const.email_target_deleted
        try:
            et.find_by_email_target_attrs(target_entity_id=self.entity_id)
            et.email_target_type = target_type
        except Errors.NotFoundError:
            # We don't want to create e-mail targets for reserved or
            # deleted accounts, but we do convert the type of existing
            # e-mail targets above.
            if target_type == self.const.email_target_deleted:
                return
            et.populate(target_type, self.entity_id, self.const.entity_account)
        et.write_db()
        # For deleted/reserved users, set expire_date for all of the
        # user's addresses, and don't allocate any new addresses.
        ea = Email.EmailAddress(self._db)
        if target_type == self.const.email_target_deleted:
            expire_date = self._db.DateFromTicks(time.time() +
                                                 60 * 60 * 24 * 1)
            for row in et.get_addresses():
                ea.clear()
                ea.find(row['address_id'])
                if ea.email_addr_expire_date is None:
                    ea.email_addr_expire_date = expire_date
                ea.write_db()
            return
        # if an account email_target without email_server is found assign
        # the appropriate server based on spread and account_type
        spread = None
        if not et.email_server_id:
            if self.get_account_types() or \
               self.owner_type == self.const.entity_group:
                for s in self.get_spread():
                    if s['spread'] in (int(self.const.spread_exchange_account),
                                       int(self.const.spread_exchange_acc_old),
                                       int(self.const.spread_hia_email)):
                        spread = s['spread']
                et = self._update_email_server(spread)
            else:
                # do not set email_server_target
                # until account_type is registered
                return
        # Figure out which domain(s) the user should have addresses
        # in.  Primary domain should be at the front of the resulting
        # list.
        # if the only address found is in EMAIL_DEFAULT_DOMAIN
        # don't set default address. This is done in order to prevent
        # adresses in default domain being sat as primary
        # TODO: account_types affiliated to OU's  without connected
        # email domain don't get a default address
        primary_set = False
        ed = Email.EmailDomain(self._db)
        ed.find(self.get_primary_maildomain())
        domains = [ed.email_domain_name]

        if ed.email_domain_name == Email.get_primary_default_email_domain():
            if not self.owner_type == self.const.entity_group:
                primary_set = True

        # Add the default domains if missing
        for domain in Email.get_default_email_domains():
            if domain not in domains:
                domains.append(domain)

        # Iterate over the available domains, testing various
        # local_parts for availability.  Set user's primary address to
        # the first one found to be available.
        # Never change any existing email addresses
        try:
            self.get_primary_mailaddress()
            primary_set = True
        except Errors.NotFoundError:
            pass
        epat = Email.EmailPrimaryAddressTarget(self._db)
        for domain in domains:
            if ed.email_domain_name != domain:
                ed.clear()
                ed.find_by_domain(domain)
            # Check for 'cnaddr' category before 'uidaddr', to prefer
            # 'cnaddr'-style primary addresses for users in
            # maildomains that have both categories.
            ctgs = [int(r['category']) for r in ed.get_categories()]
            local_parts = []
            if int(self.const.email_domain_category_cnaddr) in ctgs:
                local_parts.append(
                    self.get_email_cn_local_part(given_names=1,
                                                 max_initials=1))
                local_parts.append(self.account_name)
            elif int(self.const.email_domain_category_uidaddr) in ctgs:
                local_parts.append(self.account_name)
            for lp in local_parts:
                lp = self.wash_email_local_part(lp)
                # Is the address taken?
                ea.clear()
                try:
                    ea.find_by_local_part_and_domain(lp, ed.entity_id)
                    if ea.email_addr_target_id != et.entity_id:
                        # Address already exists, and points to a
                        # target not owned by this Account.
                        continue
                    # Address belongs to this account; make sure
                    # there's no expire_date set on it.
                    ea.email_addr_expire_date = None
                except Errors.NotFoundError:
                    # Address doesn't exist; create it.
                    ea.populate(lp, ed.entity_id, et.entity_id, expire=None)
                ea.write_db()
                if not primary_set:
                    epat.clear()
                    try:
                        epat.find(ea.email_addr_target_id)
                        epat.populate(ea.entity_id)
                    except Errors.NotFoundError:
                        epat.clear()
                        epat.populate(ea.entity_id, parent=et)
                    epat.write_db()
                    primary_set = True
                self.update_email_quota()