Example #1
0
 def adddestination(self, domainid):
     "Add a destination server"
     domain = self._get_domain(domainid)
     if not domain:
         abort(404)
     c.form = AddDeliveryServerForm(request.POST, csrf_context=session)
     c.id = domainid
     if request.POST and c.form.validate():
         server = DeliveryServer()
         for field in c.form:
             if field.name != "csrf_token":
                 setattr(server, field.name, field.data)
         try:
             domain.servers.append(server)
             Session.add(server)
             Session.add(domain)
             Session.commit()
             info = ADDDELSVR_MSG % dict(d=domain.name, ds=server.address)
             audit_log(c.user.username, 3, info, request.host, request.remote_addr, datetime.now())
             flash(_("The destination server has been created"))
             redirect(url(controller="domains", action="detail", domainid=domain.id))
         except IntegrityError:
             Session.rollback()
             flash_alert(_("The destination server %(dest)s already exists ") % dict(dest=server.address))
     return render("/domains/adddestination.html")
Example #2
0
 def editdestination(self, destinationid):
     "Edit destination server"
     server = self._get_server(destinationid)
     if not server:
         abort(404)
     c.form = AddDeliveryServerForm(request.POST, server, csrf_context=session)
     if request.POST and c.form.validate():
         updated = False
         kw = dict(domainid=server.domain_id)
         for field in c.form:
             if field.name != "csrf_token" and field.data != getattr(server, field.name):
                 setattr(server, field.name, field.data)
                 updated = True
         if updated:
             try:
                 Session.add(server)
                 Session.commit()
                 flash(_("The destination server has been updated"))
                 info = UPDATEDELSVR_MSG % dict(d=server.domains.name, ds=server.address)
                 audit_log(c.user.username, 2, info, request.host, request.remote_addr, datetime.now())
                 self.invalidate = 1
                 self._get_server(destinationid)
                 redirect(url("domain-detail", **kw))
             except IntegrityError:
                 Session.rollback()
                 flash_alert(_("The update failed"))
         else:
             flash_info(_("No changes were made to the destination server"))
             redirect(url("domain-detail", **kw))
     c.id = destinationid
     c.domainid = server.domain_id
     return render("/domains/editdestination.html")
Example #3
0
    def editaddress(self, addressid):
        "Edit address"
        address = get_address(addressid)
        if not address:
            abort(404)

        c.form = AddressForm(request.POST, address, csrf_context=session)
        if request.method == 'POST' and c.form.validate():
            try:
                if (address.address != c.form.address.data or
                    address.enabled != c.form.enabled.data):
                    update_address(c.form, address, c.user,
                                request.host, request.remote_addr)
                    flash(_('The alias address has been updated'))
                else:
                    flash_info(_('No changes were made to the address'))
            except IntegrityError:
                Session.rollback()
                msg = _('The address %(addr)s already exists') % \
                        dict(addr=c.form.address.data)
                flash_alert(msg)
                log.info(msg)
            except NoResultFound:
                domain = c.form.address.data.split('@')[1]
                msg = _('Domain: %(d)s does not belong to you') % \
                        dict(d=domain)
                flash(msg)
                log.info(msg)
            redirect(url(controller='accounts', action='detail',
            userid=address.user_id))
        c.id = addressid
        c.userid = address.user_id
        return self.render('/accounts/editaddress.html')
Example #4
0
    def add(self, orgid=None):
        "Add a domain"
        c.form = AddDomainForm(request.POST, csrf_context=session)
        c.form.organizations.query = self._get_organizations(orgid)
        if request.POST and c.form.validate():
            try:
                domain = Domain()
                for field in c.form:
                    if field.name != 'csrf_token':
                        setattr(domain, field.name, field.data)
                Session.add(domain)
                Session.commit()
                update_serial.delay()
                info = ADDDOMAIN_MSG % dict(d=domain.name)
                audit_log(c.user.username,
                        3, unicode(info), request.host,
                        request.remote_addr, now())
                flash(_('The domain: %(dom)s has been created') %
                    dict(dom=domain.name))
                redirect(url(controller='domains'))
            except IntegrityError:
                Session.rollback()
                flash_alert(_('The domain name %(dom)s already exists') %
                    dict(dom=domain.name))

        return render('/domains/new.html')
Example #5
0
    def add_domain_sigs(self, domainid):
        "Add domain signature"
        domain = self._get_domain(domainid)
        if not domain:
            abort(404)

        c.form = SigForm(request.POST, csrf_context=session)
        if request.POST and c.form.validate():
            try:
                sig = DomSignature()
                for field in c.form:
                    if field.name != 'csrf_token':
                        setattr(sig, field.name, field.data)
                domain.signatures.append(sig)
                Session.add(sig)
                Session.add(domain)
                Session.commit()
                save_dom_sig.apply_async(args=[sig.id], queue='msbackend')
                info = ADDDOMSIG_MSG % dict(d=domain.name)
                audit_log(c.user.username,
                        3, info, request.host,
                        request.remote_addr, datetime.now())
                flash(_('The signature has been created'))
                redirect(url('domain-settings-sigs', domainid=domainid))
            except IntegrityError:
                Session.rollback()
                flash(_('This signature type already exists'))
        c.domain = domain
        return render('/settings/domain_addsig.html')
Example #6
0
    def edit_account_sigs(self, sigid):
        "Edit account signatures"
        sign = self._get_usrsign(sigid)
        if not sign:
            abort(404)

        c.form = SigForm(request.POST, sign, csrf_context=session)
        del c.form['signature_type']
        if request.POST and c.form.validate():
            try:
                updated = False
                for field in c.form:
                    if (field.name != 'csrf_token' and
                        field.data != getattr(sign, field.name)):
                        updated = True
                        setattr(sign, field.name, field.data)
                if updated:
                    Session.add(sign)
                    Session.commit()
                    save_user_sig.apply_async(args=[sigid], queue='msbackend')
                    info = UPDATEACCSIG_MSG % dict(u=sign.user.username)
                    audit_log(c.user.username,
                            2, info, request.host,
                            request.remote_addr, datetime.now())
                    flash(_('The signature has been updated'))
                else:
                    flash(_('No changes made, signature not updated'))
                redirect(url('account-detail', userid=sign.user_id))
            except IntegrityError:
                Session.rollback()
                flash(_('Error occured updating the signature'))
        c.sign = sign
        return render('/settings/account_editsig.html')
Example #7
0
    def addalias(self, domainid):
        "Add alias domain"
        domain = self._get_domain(domainid)
        if not domain:
            abort(404)

        c.form = AddDomainAlias(request.POST, csrf_context=session)
        c.form.domain.query = Session.query(Domain)\
                                    .filter(Domain.id == domainid)
        if request.method == 'POST' and c.form.validate():
            alias = DomainAlias()
            alias.from_form(c.form)
            try:
                create_alias(domain, alias, c.user,
                            request.host, request.remote_addr)
                flash(_('The domain alias: %s has been created') % alias.name)
                redirect(url(controller='domains', action='detail',
                        domainid=domain.id))
            except IntegrityError:
                Session.rollback()
                msg = _('The domain alias: %s already exists') % alias.name
                flash_alert(msg)
                log.info(msg)

        c.domainid = domain.id
        c.domainname = domain.name
        return self.render('/domains/addalias.html')
Example #8
0
    def editalias(self, aliasid):
        "Edit alias domain"
        alias = self._get_alias(aliasid)
        if not alias:
            abort(404)

        c.form = EditDomainAlias(request.POST, alias, csrf_context=session)
        c.form.domain.query = Session.query(Domain).filter(Domain.id == alias.domain_id)
        if request.POST and c.form.validate():
            updated = False
            for field in c.form:
                if field.name != "csrf_token" and field.data != getattr(alias, field.name):
                    setattr(alias, field.name, field.data)
                    updated = True
            if updated:
                try:
                    Session.add(alias)
                    Session.commit()
                    update_serial.delay()
                    info = UPDATEDOMALIAS_MSG % dict(d=alias.name)
                    audit_log(c.user.username, 2, info, request.host, request.remote_addr, datetime.now())
                    flash(_("The domain alias: %s has been updated") % alias.name)
                    redirect(url("domain-detail", domainid=alias.domain_id))
                except IntegrityError:
                    Session.rollback()
                    flash_alert(_("The update failed"))
            else:
                flash_info(_("No changes were made to the domain alias"))
                redirect(url("domain-detail", domainid=alias.domain_id))

        c.aliasid = aliasid
        c.domainid = alias.domain_id
        c.domainname = alias.domain.name
        return render("/domains/editalias.html")
Example #9
0
 def add_auth(self, domainid):
     "Add auth server"
     domain = self._get_domain(domainid)
     if not domain:
         abort(404)
     c.form = AddAuthForm(request.POST, csrf_context=session)
     if request.method == 'POST' and c.form.validate():
         server = AuthServer()
         server.from_form(c.form)
         try:
             create_auth(domain, server, c.user,
                         request.host, request.remote_addr)
             flash(_('The authentication settings have been created'))
             redirect(url(controller='domains', action='detail',
                     domainid=domain.id))
         except IntegrityError:
             Session.rollback()
             auth = dict(AUTH_PROTOCOLS)[str(server.protocol)]
             msg = _('The host %(dest)s already configured for %(auth)s '
                     'authentication for this domain') % \
                     dict(dest=server.address, auth=auth)
             flash_alert(msg)
             log.info(msg)
     c.domainid = domainid
     c.domainname = domain.name
     return self.render('/domains/addauth.html')
Example #10
0
 def edit_auth(self, authid):
     "Edit auth server"
     server = self._get_authserver(authid)
     if not server:
         abort(404)
     c.form = AddAuthForm(request.POST, server, csrf_context=session)
     if request.method == 'POST' and c.form.validate():
         kwd = dict(domainid=server.domain_id)
         if auth_update_if_changed(c.form, server):
             try:
                 edit_auth(server, c.user, request.host,
                         request.remote_addr)
                 flash(_('The authentication settings have been updated'))
                 self.invalidate = 1
                 self._get_authserver(authid)
                 kwd['uc'] = 1
                 redirect(url('domain-detail', **kwd))
             except IntegrityError:
                 Session.rollback()
                 msg = _('The authentication settings update failed')
                 flash_alert(msg)
                 log.info(msg)
         else:
             msg = _('No changes were made to the authentication settings')
             flash_info(msg)
             log.info(msg)
             redirect(url('domain-detail', **kwd))
     c.domainid = server.domains.id
     c.domainname = server.domains.name
     c.authid = authid
     return self.render('/domains/editauth.html')
Example #11
0
 def editdestination(self, destinationid):
     "Edit destination server"
     server = self._get_server(destinationid)
     if not server:
         abort(404)
     c.form = AddDeliveryServerForm(request.POST,
                                     server,
                                     csrf_context=session)
     if request.method == 'POST' and c.form.validate():
         kwd = dict(domainid=server.domain_id)
         if update_if_changed(c.form, server):
             try:
                 update_destination(server, c.user, request.host,
                                     request.remote_addr)
                 self.invalidate = 1
                 self._get_server(destinationid)
                 kwd['uc'] = 1
                 flash(_('The destination server has been updated'))
                 redirect(url('domain-detail', **kwd))
             except IntegrityError:
                 Session.rollback()
                 msg = _('The update failed')
                 flash_alert(msg)
                 log.info(msg)
         else:
             msg = _('No changes were made to the destination server')
             flash_info(msg)
             log.info(msg)
             redirect(url('domain-detail', **kwd))
     c.id = destinationid
     c.domainid = server.domain_id
     return self.render('/domains/editdestination.html')
Example #12
0
    def edit(self, domainid):
        "Edit a domain"
        domain = self._get_domain(domainid)
        if not domain:
            abort(404)

        c.id = domainid
        c.form = domain_update_form(c.user, request.POST, domain,
                                get_organizations, session)
        if request.method == 'POST' and c.form.validate():
            kwd = dict(domainid=domain.id)
            if domain_update_if_changed(c.form, domain):
                try:
                    update_domain(domain, c.user, request.host,
                                    request.remote_addr)
                    flash(_('The domain: %(dom)s has been updated') %
                        dict(dom=domain.name))
                    kwd['uc'] = 1
                except IntegrityError:
                    Session.rollback()
                    msg = _('The domain %(dom)s could not be updated') % \
                            dict(dom=domain.name)
                    flash(msg)
                    log.info(msg)
            else:
                msg = _('No changes were made to the domain')
                flash_info(msg)
                log.info(msg)
            redirect(url('domain-detail', **kwd))
        return self.render('/domains/edit.html')
Example #13
0
 def edit_rule(self, rule_id):
     """Edit a policy rule"""
     rule = get_rule(rule_id)
     if not rule:
         abort(404)
     c.rule_id = rule_id
     c.policy_id = rule.policy_id
     c.policy_type = rule.policy.policy_type
     c.POLICY_NAME = POLICY_NAME
     c.POLICY_URL_MAP = POLICY_URL_MAP
     c.form = AddRuleForm(request.POST, rule, csrf_context=session)
     if rule.policy.policy_type in [1, 2]:
         c.form.action.choices = ARCHIVE_RULE_ACTIONS
     if request.method == 'POST' and c.form.validate():
         try:
             updated = update_rule(c.form, rule, c.user,
                         request.host, request.remote_addr)
             if updated:
                 flash(_('The rule has been updated'))
             else:
                 flash(_('No changes were made to the rule'))
         except IntegrityError:
             Session.rollback()
             flash(_('The rule could not be updated'))
         redirect(url('policy-rulesets', policy_id=rule.policy_id))
     return self.render('/settings/rules_edit.html')
Example #14
0
 def add_auth(self, domainid):
     "Add auth server"
     domain = self._get_domain(domainid)
     if not domain:
         abort(404)
     c.form = AddAuthForm(request.POST, csrf_context=session)
     if request.POST and c.form.validate():
         server = AuthServer()
         for field in c.form:
             if field.data and field.name != "csrf_token":
                 setattr(server, field.name, field.data)
         try:
             domain.authservers.append(server)
             Session.add(server)
             Session.add(domain)
             Session.commit()
             info = ADDAUTHSVR_MSG % dict(d=domain.name, ds=server.address)
             audit_log(c.user.username, 3, info, request.host, request.remote_addr, datetime.now())
             flash(_("The authentication settings have been created"))
             redirect(url(controller="domains", action="detail", domainid=domain.id))
         except IntegrityError:
             Session.rollback()
             auth = dict(AUTH_PROTOCOLS)[str(server.protocol)]
             flash_alert(
                 _("The host %(dest)s already configured for %(auth)s " "authentication for this domain")
                 % dict(dest=server.address, auth=auth)
             )
     c.domainid = domainid
     c.domainname = domain.name
     return render("/domains/addauth.html")
Example #15
0
    def editalias(self, aliasid):
        "Edit alias domain"
        alias = self._get_alias(aliasid)
        if not alias:
            abort(404)

        c.form = EditDomainAlias(request.POST, alias, csrf_context=session)
        c.form.domain.query = Session.query(Domain)\
                            .filter(Domain.id == alias.domain_id)
        if request.method == 'POST' and c.form.validate():
            if update_if_changed(c.form, alias):
                try:
                    edit_alias(alias, c.user, request.host,
                                request.remote_addr)
                    flash(_('The domain alias: %s has been updated') %
                            alias.name)
                    redirect(url('domain-detail', domainid=alias.domain_id))
                except IntegrityError:
                    Session.rollback()
                    msg = _('The update failed')
                    flash_alert(msg)
                    log.info(msg)
            else:
                msg = _('No changes were made to the domain alias')
                flash_info(msg)
                log.info(msg)
                redirect(url('domain-detail', domainid=alias.domain_id))

        c.aliasid = aliasid
        c.domainid = alias.domain_id
        c.domainname = alias.domain.name
        return self.render('/domains/editalias.html')
Example #16
0
    def addalias(self, domainid):
        "Add alias domain"
        domain = self._get_domain(domainid)
        if not domain:
            abort(404)

        c.form = AddDomainAlias(request.POST, csrf_context=session)
        c.form.domain.query = Session.query(Domain).filter(Domain.id == domainid)
        if request.POST and c.form.validate():
            alias = DomainAlias()
            for field in c.form:
                if field.data and field.name != "csrf_token":
                    setattr(alias, field.name, field.data)
            try:
                domain.aliases.append(alias)
                Session.add(alias)
                Session.add(domain)
                Session.commit()
                update_serial.delay()
                info = ADDDOMALIAS_MSG % dict(d=alias.name)
                audit_log(c.user.username, 3, info, request.host, request.remote_addr, datetime.now())
                flash(_("The domain alias: %s has been created") % alias.name)
                redirect(url(controller="domains", action="detail", domainid=domain.id))
            except IntegrityError:
                Session.rollback()
                flash_alert(_("The domain alias: %s already exists") % alias.name)

        c.domainid = domain.id
        c.domainname = domain.name
        return render("/domains/addalias.html")
Example #17
0
    def edit(self, userid):
        """GET /accounts/edit/id: Form to edit an existing item"""
        user = self._get_user(userid)
        if not user:
            abort(404)

        c.form = user_update_form(user, c.user, request.POST, session)
        if request.method == 'POST' and c.form.validate():
            kwd = dict(userid=user.id)
            if update_changed(c.form, FORM_FIELDS, user):
                try:
                    update_user(user, c.user, request.host,
                                request.remote_addr)
                    flash(_('The account has been updated'))
                    kwd['uc'] = 1
                except IntegrityError:
                    Session.rollback()
                    flash_alert(
                        _('The account: %(acc)s could not be updated') %
                        dict(acc=user.username))
                if (user.id == c.user.id and c.form.active and
                    c.form.active.data is False):
                    redirect(url('/logout'))
            else:
                flash_info(_('No changes made to the account'))
            redirect(url(controller='accounts', action='detail', **kwd))
        c.fields = FORM_FIELDS
        c.id = userid
        return self.render('/accounts/edit.html')
Example #18
0
    def edit_server(self, serverid):
        "Edit scan server"
        server = self._get_server(serverid)
        if not server:
            abort(404)

        c.form = ServerForm(request.POST, server, csrf_context=session)
        c.id = server.id
        if request.POST and c.form.validate():
            if (server.hostname != c.form.hostname.data or
                server.enabled != c.form.enabled.data):
                try:
                    server.hostname = c.form.hostname.data
                    server.enabled = c.form.enabled.data
                    Session.add(server)
                    Session.commit()
                    update_serial.delay()
                    info = HOSTUPDATE_MSG % dict(n=server.hostname)
                    audit_log(c.user.username,
                            2, info, request.host,
                            request.remote_addr, datetime.now())
                    flash(_('The scanning server has been updated'))
                except IntegrityError:
                    Session.rollback()
                    flash(_('Update of server failed'))
            else:
                flash_info(_('No changes were made to the server'))
            redirect(url(controller='settings'))
        return render('/settings/editserver.html')
Example #19
0
    def addaddress(self, userid):
        "Add address"
        user = self._get_user(userid)
        if not user:
            abort(404)

        c.form = AddressForm(request.POST, csrf_context=session)
        if request.method == 'POST' and c.form.validate():
            try:
                addr = create_address(c.form, user, c.user,
                                    request.host, request.remote_addr)
                flash(
                    _('The alias address %(address)s was successfully created.'
                    % dict(address=addr.address)))
            except IntegrityError:
                Session.rollback()
                msg = _('The address %(addr)s already exists') % \
                        dict(addr=c.form.address.data)
                flash_alert(msg)
                log.info(msg)
            except NoResultFound:
                domain = c.form.address.data.split('@')[1]
                msg = _('Domain: %(d)s does not belong to you') % \
                        dict(d=domain)
                flash(msg)
                log.info(msg)
            redirect(url(controller='accounts', action='detail',
                    userid=userid))
        c.id = userid
        return self.render('/accounts/addaddress.html')
Example #20
0
    def add_account_sigs(self, userid):
        "Add account signature"
        account = self._get_user(userid)
        if not account:
            abort(404)

        c.form = SigForm(request.POST, csrf_context=session)
        if request.POST and c.form.validate():
            try:
                sig = UserSignature()
                for field in c.form:
                    if field.name != 'csrf_token':
                        setattr(sig, field.name, field.data)
                account.signatures.append(sig)
                Session.add(sig)
                Session.add(account)
                Session.commit()
                save_user_sig.apply_async(args=[sig.id], queue='msbackend')
                info = ADDACCSIG_MSG % dict(u=account.username)
                audit_log(c.user.username,
                        3, info, request.host,
                        request.remote_addr, datetime.now())
                flash(_('The signature has been created'))
                redirect(url('account-detail', userid=userid))
            except IntegrityError:
                Session.rollback()
                flash(_('This signature type already exists'))
        c.account = account
        return render('/settings/account_addsig.html')
Example #21
0
 def add_auth(self, domainid):
     "Add auth server"
     domain = self._get_domain(domainid)
     if not domain:
         abort(404)
     c.form = AddAuthForm(request.POST, csrf_context=session)
     if request.method == 'POST' and c.form.validate():
         server = AuthServer()
         server.from_form(c.form)
         try:
             create_auth(domain, server, c.user, request.host,
                         request.remote_addr)
             flash(_('The authentication settings have been created'))
             redirect(
                 url(controller='domains',
                     action='detail',
                     domainid=domain.id))
         except IntegrityError:
             Session.rollback()
             auth = dict(AUTH_PROTOCOLS)[str(server.protocol)]
             msg = _('The host %(dest)s already configured for %(auth)s '
                     'authentication for this domain') % \
                     dict(dest=server.address, auth=auth)
             flash_alert(msg)
             log.info(msg)
     c.domainid = domainid
     c.domainname = domain.name
     return self.render('/domains/addauth.html')
Example #22
0
    def editaddress(self, addressid):
        "Edit address"
        address = get_address(addressid)
        if not address:
            abort(404)

        c.form = AddressForm(request.POST, address, csrf_context=session)
        if request.method == 'POST' and c.form.validate():
            try:
                if (address.address != c.form.address.data
                        or address.enabled != c.form.enabled.data):
                    update_address(c.form, address, c.user, request.host,
                                   request.remote_addr)
                    flash(_('The alias address has been updated'))
                else:
                    flash_info(_('No changes were made to the address'))
            except IntegrityError:
                Session.rollback()
                msg = _('The address %(addr)s already exists') % \
                        dict(addr=c.form.address.data)
                flash_alert(msg)
                log.info(msg)
            except NoResultFound:
                domain = c.form.address.data.split('@')[1]
                msg = _('Domain: %(d)s does not belong to you') % \
                        dict(d=domain)
                flash(msg)
                log.info(msg)
            redirect(
                url(controller='accounts',
                    action='detail',
                    userid=address.user_id))
        c.id = addressid
        c.userid = address.user_id
        return self.render('/accounts/editaddress.html')
Example #23
0
    def editalias(self, aliasid):
        "Edit alias domain"
        alias = self._get_alias(aliasid)
        if not alias:
            abort(404)

        c.form = EditDomainAlias(request.POST, alias, csrf_context=session)
        c.form.domain.query = Session.query(Domain)\
                            .filter(Domain.id == alias.domain_id)
        if request.method == 'POST' and c.form.validate():
            if update_if_changed(c.form, alias):
                try:
                    edit_alias(alias, c.user, request.host,
                               request.remote_addr)
                    flash(
                        _('The domain alias: %s has been updated') %
                        alias.name)
                    redirect(url('domain-detail', domainid=alias.domain_id))
                except IntegrityError:
                    Session.rollback()
                    msg = _('The update failed')
                    flash_alert(msg)
                    log.info(msg)
            else:
                msg = _('No changes were made to the domain alias')
                flash_info(msg)
                log.info(msg)
                redirect(url('domain-detail', domainid=alias.domain_id))

        c.aliasid = aliasid
        c.domainid = alias.domain_id
        c.domainname = alias.domain.name
        return self.render('/domains/editalias.html')
Example #24
0
    def edit(self, orgid):
        "Edit an organization"
        org = self._get_org(orgid)
        if not org:
            abort(404)

        c.form = OrgForm(request.POST, org, csrf_context=session)
        c.form.domains.query = Session.query(Domain)
        c.form.admins.query = Session.query(User).filter(
                                User.account_type == 2)
        c.id = org.id
        if request.POST and c.form.validate():
            updated = False
            for field in c.form:
                if (field.name != 'csrf_token' and
                    field.data != getattr(org, field.name)):
                    setattr(org, field.name, field.data)
                    updated = True
            if updated:
                try:
                    Session.add(org)
                    Session.commit()
                    info = UPDATEORG_MSG % dict(o=org.name)
                    audit_log(c.user.username,
                            2, info, request.host,
                            request.remote_addr, datetime.now())
                    flash(_('The organization has been updated'))
                except IntegrityError:
                    Session.rollback()
                    flash(_('The organization could not be updated'))
            else:
                flash_info(_('No changes made, Organization not updated'))
            redirect(url(controller='organizations'))
        return render('/organizations/edit.html')
Example #25
0
 def edit_auth(self, authid):
     "Edit auth server"
     server = self._get_authserver(authid)
     if not server:
         abort(404)
     c.form = AddAuthForm(request.POST, server, csrf_context=session)
     if request.method == 'POST' and c.form.validate():
         kwd = dict(domainid=server.domain_id)
         if auth_update_if_changed(c.form, server):
             try:
                 edit_auth(server, c.user, request.host,
                           request.remote_addr)
                 flash(_('The authentication settings have been updated'))
                 self.invalidate = 1
                 self._get_authserver(authid)
                 kwd['uc'] = 1
                 redirect(url('domain-detail', **kwd))
             except IntegrityError:
                 Session.rollback()
                 msg = _('The authentication settings update failed')
                 flash_alert(msg)
                 log.info(msg)
         else:
             msg = _('No changes were made to the authentication settings')
             flash_info(msg)
             log.info(msg)
             redirect(url('domain-detail', **kwd))
     c.domainid = server.domains.id
     c.domainname = server.domains.name
     c.authid = authid
     return self.render('/domains/editauth.html')
Example #26
0
    def addalias(self, domainid):
        "Add alias domain"
        domain = self._get_domain(domainid)
        if not domain:
            abort(404)

        c.form = AddDomainAlias(request.POST, csrf_context=session)
        c.form.domain.query = Session.query(Domain)\
                                    .filter(Domain.id == domainid)
        if request.method == 'POST' and c.form.validate():
            alias = DomainAlias()
            alias.from_form(c.form)
            try:
                create_alias(domain, alias, c.user, request.host,
                             request.remote_addr)
                flash(_('The domain alias: %s has been created') % alias.name)
                redirect(
                    url(controller='domains',
                        action='detail',
                        domainid=domain.id))
            except IntegrityError:
                Session.rollback()
                msg = _('The domain alias: %s already exists') % alias.name
                flash_alert(msg)
                log.info(msg)

        c.domainid = domain.id
        c.domainname = domain.name
        return self.render('/domains/addalias.html')
Example #27
0
 def editdestination(self, destinationid):
     "Edit destination server"
     server = self._get_server(destinationid)
     if not server:
         abort(404)
     c.form = AddDeliveryServerForm(request.POST,
                                    server,
                                    csrf_context=session)
     if request.method == 'POST' and c.form.validate():
         kwd = dict(domainid=server.domain_id)
         if update_if_changed(c.form, server):
             try:
                 update_destination(server, c.user, request.host,
                                    request.remote_addr)
                 self.invalidate = 1
                 self._get_server(destinationid)
                 kwd['uc'] = 1
                 flash(_('The destination server has been updated'))
                 redirect(url('domain-detail', **kwd))
             except IntegrityError:
                 Session.rollback()
                 msg = _('The update failed')
                 flash_alert(msg)
                 log.info(msg)
         else:
             msg = _('No changes were made to the destination server')
             flash_info(msg)
             log.info(msg)
             redirect(url('domain-detail', **kwd))
     c.id = destinationid
     c.domainid = server.domain_id
     return self.render('/domains/editdestination.html')
Example #28
0
 def edit_policy(self, policy_id):
     """Edit an existing Policy"""
     policy = get_policy(policy_id)
     if not policy:
         abort(404)
     c.policy_id = policy.id
     c.policy_type = policy.policy_type
     c.POLICY_NAME = POLICY_NAME
     c.POLICY_URL_MAP = POLICY_URL_MAP
     c.form = PolicyForm(request.POST, policy, csrf_context=session)
     rules = get_policy_rules(policy_id, True)
     if not rules:
         del c.form.enabled
     if request.method == 'POST' and c.form.validate():
         try:
             updated = update_policy(c.form, policy, c.user,
                         request.host, request.remote_addr)
             if updated:
                 flash(_('The policy has been updated'))
             else:
                 flash(_('No changes were made to the policy'))
         except IntegrityError:
             Session.rollback()
             flash(_('The update of the policy failed'))
         redirect(url(POLICY_URL_MAP[str(policy.policy_type)]))
     return self.render('/settings/policy_edit.html')
Example #29
0
    def edit(self, domainid):
        "Edit a domain"
        domain = self._get_domain(domainid)
        if not domain:
            abort(404)

        c.id = domainid
        c.form = domain_update_form(c.user, request.POST, domain,
                                    get_organizations, session)
        if request.method == 'POST' and c.form.validate():
            kwd = dict(domainid=domain.id)
            if domain_update_if_changed(c.form, domain):
                try:
                    update_domain(domain, c.user, request.host,
                                  request.remote_addr)
                    flash(
                        _('The domain: %(dom)s has been updated') %
                        dict(dom=domain.name))
                    kwd['uc'] = 1
                except IntegrityError:
                    Session.rollback()
                    msg = _('The domain %(dom)s could not be updated') % \
                            dict(dom=domain.name)
                    flash(msg)
                    log.info(msg)
            else:
                msg = _('No changes were made to the domain')
                flash_info(msg)
                log.info(msg)
            redirect(url('domain-detail', **kwd))
        return self.render('/domains/edit.html')
Example #30
0
    def adddestination(self, domainid):
        "Add a destination server"
        domain = self._get_domain(domainid)
        if not domain:
            abort(404)

        c.id = domainid
        c.form = AddDeliveryServerForm(request.POST, csrf_context=session)
        if request.method == 'POST' and c.form.validate():
            server = DeliveryServer()
            server.from_form(c.form)
            try:
                create_destination(domain, server, c.user, request.host,
                                   request.remote_addr)
                flash(_('The destination server has been created'))
                redirect(
                    url(controller='domains',
                        action='detail',
                        domainid=domain.id))
            except IntegrityError:
                Session.rollback()
                msg = _('The destination server %(dest)s already exists ') % \
                        dict(dest=server.address)
                flash_alert(msg)
                log.info(msg)
        return self.render('/domains/adddestination.html')
Example #31
0
    def add(self, orgid=None):
        "Add a domain"
        c.form = AddDomainForm(request.POST, csrf_context=session)
        c.form.organizations.query = get_organizations(orgid)
        if request.method == 'POST' and c.form.validate():
            try:
                domain = create_domain(c.form, c.user, request.host,
                                       request.remote_addr)
                try:
                    from baruwa.tasks.invite import create_mx_records
                    create_mx_records.apply_async(args=[domain.name])
                except ImportError:
                    pass
                flash(
                    _('The domain: %(dom)s has been created') %
                    dict(dom=domain.name))
                redirect(url(controller='domains'))
            except IntegrityError:
                Session.rollback()
                msg = _('The domain name %(dom)s already exists') % \
                        dict(dom=domain.name)
                flash_alert(msg)
                log.info(msg)

        return self.render('/domains/new.html')
Example #32
0
    def edit_relay(self, settingid):
        "Edit a mail relay"
        relay = self._get_setting(settingid)
        if not relay:
            abort(404)

        c.form = RelayEditForm(request.POST, relay, csrf_context=session)
        c.relayname = relay.address or relay.username
        c.relayid = relay.id
        c.orgid = relay.org_id
        if request.POST and c.form.validate():
            updated = False
            for field in c.form:
                if field.name == "csrf_token":
                    continue
                if not field.name in ["password1", "password2"] and field.data != getattr(relay, field.name):
                    setattr(relay, field.name, field.data)
                    updated = True
                if field.name == "password1" and field.data != "":
                    relay.set_password(field.data)
                    updated = True
            if updated:
                try:
                    Session.add(relay)
                    Session.commit()
                    info = UPDATERELAY_MSG % dict(r=c.relayname)
                    audit_log(c.user.username, 2, info, request.host, request.remote_addr, now())
                    flash(_("The outbound settings have been updated"))
                except IntegrityError:
                    Session.rollback()
                    flash(_("The outbound settings could not be updated"))
            else:
                flash(_("No changes made, The outbound settings not updated"))
            redirect(url("org-detail", orgid=relay.org_id))
        return render("/organizations/editrelay.html")
Example #33
0
    def edit_relay(self, settingid):
        "Edit a mail relay"
        relay = get_relay(settingid)
        if not relay:
            abort(404)

        c.relayname = relay.address or relay.username
        c.relayid = relay.id
        c.orgid = relay.org_id
        c.form = RelayEditForm(request.POST, relay, csrf_context=session)
        if request.method == 'POST' and c.form.validate():
            if relay_update_if_changed(c.form, relay):
                try:
                    edit_relay(relay, c.user, request.host,
                               request.remote_addr)
                    msg = _('The outbound settings have been updated')
                    flash(msg)
                    log.info(msg)
                except IntegrityError:
                    Session.rollback()
                    msg = _('The outbound settings could not be updated')
                    flash(msg)
                    log.info(msg)
            else:
                msg = _('No changes made, The outbound settings not updated')
                flash(msg)
                log.info(msg)
            redirect(url('org-detail', orgid=relay.org_id))
        return self.render('/organizations/editrelay.html')
Example #34
0
    def add_relay(self, orgid):
        "Add a mail relay"
        org = self._get_org(orgid)
        if not org:
            abort(404)

        c.form = RelayForm(request.POST, csrf_context=session)
        if request.POST and c.form.validate():
            try:
                outbound = Relay()
                outbound.address = c.form.address.data
                outbound.username = c.form.username.data
                outbound.enabled = c.form.enabled.data
                outbound.org = org
                if c.form.password1.data:
                    outbound.set_password(c.form.password1.data)
                Session.add(outbound)
                Session.commit()
                relay_name = c.form.address.data or c.form.username.data
                info = ADDRELAY_MSG % dict(r=relay_name)
                audit_log(c.user.username,
                        3, info, request.host,
                        request.remote_addr, datetime.now())
                flash(_('The outbound settings have been created'))
            except IntegrityError:
                Session.rollback()
                flash(_('The outbound settings could not created, Try again'))
            redirect(url('org-detail', orgid=orgid))
        c.orgid = org.id
        c.orgname = org.name
        return render('/organizations/addrelay.html')
Example #35
0
    def add_relay(self, orgid):
        "Add a mail relay"
        org = self._get_org(orgid)
        if not org:
            abort(404)

        c.form = RelayForm(request.POST, csrf_context=session)
        if request.POST and c.form.validate():
            try:
                outbound = Relay()
                outbound.address = c.form.address.data
                outbound.username = c.form.username.data
                outbound.enabled = c.form.enabled.data
                outbound.org = org
                if c.form.password1.data:
                    outbound.set_password(c.form.password1.data)
                Session.add(outbound)
                Session.commit()
                relay_name = c.form.address.data or c.form.username.data
                info = ADDRELAY_MSG % dict(r=relay_name)
                audit_log(c.user.username, 3, info, request.host,
                          request.remote_addr, datetime.now())
                flash(_('The outbound settings have been created'))
            except IntegrityError:
                Session.rollback()
                flash(_('The outbound settings could not created, Try again'))
            redirect(url('org-detail', orgid=orgid))
        c.orgid = org.id
        c.orgname = org.name
        return render('/organizations/addrelay.html')
Example #36
0
    def add_domain_sigs(self, domainid):
        "Add domain signature"
        domain = self._get_domain(domainid)
        if not domain:
            abort(404)

        c.form = SigForm(request.POST, csrf_context=session)
        if request.method == 'POST' and c.form.validate():
            try:
                sig = DomSignature()
                for field in c.form:
                    if field.name != 'csrf_token':
                        setattr(sig, field.name, field.data)
                domain.signatures.append(sig)
                Session.add(sig)
                Session.add(domain)
                Session.commit()
                save_dom_sig.apply_async(args=[sig.id], exchange=FANOUT_XCHG)
                backend_sig_update(c.form.signature_type.data)
                info = auditmsgs.ADDDOMSIG_MSG % dict(d=domain.name)
                audit_log(c.user.username,
                        3, unicode(info), request.host,
                        request.remote_addr, arrow.utcnow().datetime)
                flash(_('The signature has been created'))
                redirect(url('domain-settings-sigs', domainid=domainid))
            except IntegrityError:
                Session.rollback()
                msg = _('This signature type already exists')
                flash(msg)
                log.info(msg)
        c.domain = domain
        return self.render('/settings/domain_addsig.html')
Example #37
0
    def edit_account_sigs(self, sigid):
        "Edit account signatures"
        sign = self._get_usrsign(sigid)
        if not sign:
            abort(404)

        c.form = SigForm(request.POST, sign, csrf_context=session)
        del c.form['signature_type']
        if request.POST and c.form.validate():
            try:
                updated = False
                for field in c.form:
                    if (field.name != 'csrf_token'
                            and field.data != getattr(sign, field.name)):
                        updated = True
                        setattr(sign, field.name, field.data)
                if updated:
                    Session.add(sign)
                    Session.commit()
                    save_user_sig.apply_async(args=[sigid], queue='msbackend')
                    info = UPDATEACCSIG_MSG % dict(u=sign.user.username)
                    audit_log(c.user.username, 2, info, request.host,
                              request.remote_addr, now())
                    flash(_('The signature has been updated'))
                else:
                    flash(_('No changes made, signature not updated'))
                redirect(url('account-detail', userid=sign.user_id))
            except IntegrityError:
                Session.rollback()
                flash(_('Error occured updating the signature'))
        c.sign = sign
        return render('/settings/account_editsig.html')
Example #38
0
    def add_domain_sigs(self, domainid):
        "Add domain signature"
        domain = self._get_domain(domainid)
        if not domain:
            abort(404)

        c.form = SigForm(request.POST, csrf_context=session)
        if request.POST and c.form.validate():
            try:
                sig = DomSignature()
                for field in c.form:
                    if field.name != 'csrf_token':
                        setattr(sig, field.name, field.data)
                domain.signatures.append(sig)
                Session.add(sig)
                Session.add(domain)
                Session.commit()
                save_dom_sig.apply_async(args=[sig.id], queue='msbackend')
                info = ADDDOMSIG_MSG % dict(d=domain.name)
                audit_log(c.user.username, 3, info, request.host,
                          request.remote_addr, now())
                flash(_('The signature has been created'))
                redirect(url('domain-settings-sigs', domainid=domainid))
            except IntegrityError:
                Session.rollback()
                flash(_('This signature type already exists'))
        c.domain = domain
        return render('/settings/domain_addsig.html')
Example #39
0
    def addaddress(self, userid):
        "Add address"
        user = self._get_user(userid)
        if not user:
            abort(404)

        c.form = AddressForm(request.POST, csrf_context=session)
        if request.method == 'POST' and c.form.validate():
            try:
                addr = create_address(c.form, user, c.user, request.host,
                                      request.remote_addr)
                flash(
                    _('The alias address %(address)s was successfully created.'
                      % dict(address=addr.address)))
            except IntegrityError:
                Session.rollback()
                msg = _('The address %(addr)s already exists') % \
                        dict(addr=c.form.address.data)
                flash_alert(msg)
                log.info(msg)
            except NoResultFound:
                domain = c.form.address.data.split('@')[1]
                msg = _('Domain: %(d)s does not belong to you') % \
                        dict(d=domain)
                flash(msg)
                log.info(msg)
            redirect(url(controller='accounts', action='detail',
                         userid=userid))
        c.id = userid
        return self.render('/accounts/addaddress.html')
Example #40
0
    def edit(self, userid):
        """GET /accounts/edit/id: Form to edit an existing item"""
        user = self._get_user(userid)
        if not user:
            abort(404)

        c.form = user_update_form(user, c.user, request.POST, session)
        if request.method == 'POST' and c.form.validate():
            kwd = dict(userid=user.id)
            if update_changed(c.form, FORM_FIELDS, user):
                try:
                    update_user(user, c.user, request.host,
                                request.remote_addr)
                    flash(_('The account has been updated'))
                    kwd['uc'] = 1
                except IntegrityError:
                    Session.rollback()
                    flash_alert(
                        _('The account: %(acc)s could not be updated') %
                        dict(acc=user.username))
                if (user.id == c.user.id and c.form.active
                        and c.form.active.data is False):
                    redirect(url('/logout'))
            else:
                flash_info(_('No changes made to the account'))
            redirect(url(controller='accounts', action='detail', **kwd))
        c.fields = FORM_FIELDS
        c.id = userid
        return self.render('/accounts/edit.html')
Example #41
0
    def edit_server(self, serverid):
        "Edit scan server"
        server = self._get_server(serverid)
        if not server:
            abort(404)

        c.form = ServerForm(request.POST, server, csrf_context=session)
        c.id = server.id
        if request.POST and c.form.validate():
            if (server.hostname != c.form.hostname.data
                    or server.enabled != c.form.enabled.data):
                try:
                    server.hostname = c.form.hostname.data
                    server.enabled = c.form.enabled.data
                    Session.add(server)
                    Session.commit()
                    update_serial.delay()
                    info = HOSTUPDATE_MSG % dict(n=server.hostname)
                    audit_log(c.user.username, 2, info, request.host,
                              request.remote_addr, now())
                    flash(_('The scanning server has been updated'))
                except IntegrityError:
                    Session.rollback()
                    flash(_('Update of server failed'))
            else:
                flash_info(_('No changes were made to the server'))
            redirect(url(controller='settings'))
        return render('/settings/editserver.html')
Example #42
0
 def adddestination(self, domainid):
     "Add a destination server"
     domain = self._get_domain(domainid)
     if not domain:
         abort(404)
     c.form = AddDeliveryServerForm(request.POST, csrf_context=session)
     c.id = domainid
     if request.POST and c.form.validate():
         server = DeliveryServer()
         for field in c.form:
             if field.name != 'csrf_token':
                 setattr(server, field.name, field.data)
         try:
             domain.servers.append(server)
             Session.add(server)
             Session.add(domain)
             Session.commit()
             info = ADDDELSVR_MSG % dict(d=domain.name, ds=server.address)
             audit_log(c.user.username, 3, info, request.host,
                       request.remote_addr, now())
             flash(_('The destination server has been created'))
             redirect(
                 url(controller='domains',
                     action='detail',
                     domainid=domain.id))
         except IntegrityError:
             Session.rollback()
             flash_alert(
                 _('The destination server %(dest)s already exists ') %
                 dict(dest=server.address))
     return render('/domains/adddestination.html')
Example #43
0
 def save(self, filterid, format=None):
     "Save a temp filter"
     filters = session.get('filter_by', [])
     try:
         filt = filters[int(filterid)]
         filteritems = dict(FILTER_ITEMS)
         filterby = dict(FILTER_BY)
         saved = SavedFilter(name="%s %s %s" %
                             (filteritems[filt["field"]],
                              filterby[filt["filter"]], filt["value"]),
                             field=filt["field"],
                             option=filt["filter"],
                             user=c.user)
         saved.value = filt["value"]
         Session.add(saved)
         Session.commit()
         success = True
         error_msg = ''
         self.invalidate = True
     except IndexError:
         success = False
         error_msg = _("The filter does not exist")
     except IntegrityError:
         success = False
         error_msg = _("The filter already exists")
         Session.rollback()
     if format == 'json':
         response.headers['Content-Type'] = JSON_HEADER
         errors = dict(msg=error_msg)
         return json.dumps(self._get_data(format, success, errors))
     if success:
         flash(_("The filter has been saved"))
     else:
         flash(error_msg)
     redirect(url('toplevel', controller='reports'))
Example #44
0
    def edit(self, orgid):
        "Edit an organization"
        org = get_org(orgid)
        if not org:
            abort(404)

        c.id = org.id
        c.form = org_add_form(request.POST, session, org)
        if request.method == 'POST' and c.form.validate():
            if update_if_changed(c.form, org):
                try:
                    edit_org(org, c.user, request.host, request.remote_addr)
                    msg = _('The organization has been updated')
                    flash(msg)
                    log.info(msg)
                except IntegrityError:
                    Session.rollback()
                    msg = _('The organization could not be updated')
                    flash(msg)
                    log.info(msg)
            else:
                msg = _('No changes made, Organization not updated')
                flash_info(msg)
                log.info(msg)
            redirect(url(controller='organizations'))
        return self.render('/organizations/edit.html')
Example #45
0
 def add_auth(self, domainid):
     "Add auth server"
     domain = self._get_domain(domainid)
     if not domain:
         abort(404)
     c.form = AddAuthForm(request.POST, csrf_context=session)
     if request.POST and c.form.validate():
         server = AuthServer()
         for field in c.form:
             if field.data and field.name != 'csrf_token':
                 setattr(server, field.name, field.data)
         try:
             domain.authservers.append(server)
             Session.add(server)
             Session.add(domain)
             Session.commit()
             info = ADDAUTHSVR_MSG % dict(d=domain.name, ds=server.address)
             audit_log(c.user.username, 3, info, request.host,
                       request.remote_addr, now())
             flash(_('The authentication settings have been created'))
             redirect(
                 url(controller='domains',
                     action='detail',
                     domainid=domain.id))
         except IntegrityError:
             Session.rollback()
             auth = dict(AUTH_PROTOCOLS)[str(server.protocol)]
             flash_alert(
                 _('The host %(dest)s already configured for %(auth)s '
                   'authentication for this domain') %
                 dict(dest=server.address, auth=auth))
     c.domainid = domainid
     c.domainname = domain.name
     return render('/domains/addauth.html')
Example #46
0
    def edit(self, orgid):
        "Edit an organization"
        org = self._get_org(orgid)
        if not org:
            abort(404)

        c.form = OrgForm(request.POST, org, csrf_context=session)
        c.form.domains.query = Session.query(Domain)
        c.form.admins.query = Session.query(User).filter(
            User.account_type == 2)
        c.id = org.id
        if request.POST and c.form.validate():
            updated = False
            for field in c.form:
                if (field.name != 'csrf_token'
                        and field.data != getattr(org, field.name)):
                    setattr(org, field.name, field.data)
                    updated = True
            if updated:
                try:
                    Session.add(org)
                    Session.commit()
                    info = UPDATEORG_MSG % dict(o=org.name)
                    audit_log(c.user.username, 2, info, request.host,
                              request.remote_addr, datetime.now())
                    flash(_('The organization has been updated'))
                except IntegrityError:
                    Session.rollback()
                    flash(_('The organization could not be updated'))
            else:
                flash_info(_('No changes made, Organization not updated'))
            redirect(url(controller='organizations'))
        return render('/organizations/edit.html')
Example #47
0
    def edit_relay(self, settingid):
        "Edit a mail relay"
        relay = get_relay(settingid)
        if not relay:
            abort(404)

        c.relayname = relay.address or relay.username
        c.relayid = relay.id
        c.orgid = relay.org_id
        c.form = RelayEditForm(request.POST, relay, csrf_context=session)
        if request.method == 'POST' and c.form.validate():
            if relay_update_if_changed(c.form, relay):
                try:
                    edit_relay(relay, c.user, request.host,
                                request.remote_addr)
                    msg = _('The outbound settings have been updated')
                    flash(msg)
                    log.info(msg)
                except IntegrityError:
                    Session.rollback()
                    msg = _('The outbound settings could not be updated')
                    flash(msg)
                    log.info(msg)
            else:
                msg = _('No changes made, The outbound settings not updated')
                flash(msg)
                log.info(msg)
            redirect(url('org-detail', orgid=relay.org_id))
        return self.render('/organizations/editrelay.html')
Example #48
0
    def edit(self, orgid):
        "Edit an organization"
        org = get_org(orgid)
        if not org:
            abort(404)

        c.id = org.id
        c.form = org_add_form(request.POST, session, org)
        if request.method == 'POST' and c.form.validate():
            if update_if_changed(c.form, org):
                try:
                    edit_org(org, c.user, request.host, request.remote_addr)
                    msg = _('The organization has been updated')
                    flash(msg)
                    log.info(msg)
                except IntegrityError:
                    Session.rollback()
                    msg = _('The organization could not be updated')
                    flash(msg)
                    log.info(msg)
            else:
                msg = _('No changes made, Organization not updated')
                flash_info(msg)
                log.info(msg)
            redirect(url(controller='organizations'))
        return self.render('/organizations/edit.html')
Example #49
0
    def add(self, orgid=None):
        "Add a domain"
        c.form = AddDomainForm(request.POST, csrf_context=session)
        c.form.organizations.query = self._get_organizations(orgid)
        if request.POST and c.form.validate():
            try:
                domain = Domain()
                for field in c.form:
                    if field.name != 'csrf_token':
                        setattr(domain, field.name, field.data)
                Session.add(domain)
                Session.commit()
                update_serial.delay()
                info = ADDDOMAIN_MSG % dict(d=domain.name)
                audit_log(c.user.username, 3, info, request.host,
                          request.remote_addr, now())
                flash(
                    _('The domain: %(dom)s has been created') %
                    dict(dom=domain.name))
                redirect(url(controller='domains'))
            except IntegrityError:
                Session.rollback()
                flash_alert(
                    _('The domain name %(dom)s already exists') %
                    dict(dom=domain.name))

        return render('/domains/new.html')
Example #50
0
    def add_account_sigs(self, userid):
        "Add account signature"
        account = self._get_user(userid)
        if not account:
            abort(404)

        c.form = SigForm(request.POST, csrf_context=session)
        if request.POST and c.form.validate():
            try:
                sig = UserSignature()
                for field in c.form:
                    if field.name != 'csrf_token':
                        setattr(sig, field.name, field.data)
                account.signatures.append(sig)
                Session.add(sig)
                Session.add(account)
                Session.commit()
                save_user_sig.apply_async(args=[sig.id], queue='msbackend')
                info = ADDACCSIG_MSG % dict(u=account.username)
                audit_log(c.user.username, 3, info, request.host,
                          request.remote_addr, now())
                flash(_('The signature has been created'))
                redirect(url('account-detail', userid=userid))
            except IntegrityError:
                Session.rollback()
                flash(_('This signature type already exists'))
        c.account = account
        return render('/settings/account_addsig.html')
Example #51
0
    def edit(self, userid):
        """GET /accounts/edit/id: Form to edit an existing item"""
        user = self._get_user(userid)
        if not user:
            abort(404)

        c.form = EditUserForm(request.POST, user, csrf_context=session)
        c.form.domains.query = Session.query(Domain)
        if c.user.is_domain_admin:
            c.form.domains.query = Session.query(Domain).join(dom_owns,
                                    (oas, dom_owns.c.organization_id ==
                                    oas.c.organization_id))\
                                    .filter(oas.c.user_id == c.user.id)

        if user.account_type != 3 or c.user.is_peleb:
            del c.form.domains
        if c.user.is_peleb:
            del c.form.username
            del c.form.email
            del c.form.active

        if request.POST and c.form.validate():
            update = False
            kwd = dict(userid=userid)
            for attr in FORM_FIELDS:
                field = getattr(c.form, attr)
                if field and field.data != getattr(user, attr):
                    setattr(user, attr, field.data)
                    update = True
            if update:
                try:
                    Session.add(user)
                    Session.commit()
                    update_serial.delay()
                    flash(_('The account has been updated'))
                    kwd['uc'] = 1
                    info = UPDATEACCOUNT_MSG % dict(u=user.username)
                    audit_log(c.user.username,
                            2, unicode(info), request.host,
                            request.remote_addr, now())
                except IntegrityError:
                    Session.rollback()
                    flash_alert(
                    _('The account: %(acc)s could not be updated') %
                    dict(acc=user.username))
                if (user.id == c.user.id and c.form.active and
                    c.form.active.data == False):
                    redirect(url('/logout'))
            else:
                flash_info(_('No changes made to the account'))
            redirect(url(controller='accounts', action='detail',
                    **kwd))
        c.fields = FORM_FIELDS
        c.id = userid
        return render('/accounts/edit.html')
Example #52
0
def setup_app(command, conf, variables):
    """Place any commands to setup baruwa here"""
    # Don't reload the app if it was loaded under the testing environment
    if not pylons.test.pylonsapp:
        load_environment(conf.global_conf, conf.local_conf)

    # Create the tables if they don't already exist
    print '-' * 100
    log.info("Creating tables")
    Base.metadata.create_all(bind=Session.bind)
    basepath = os.path.dirname(os.path.dirname(__file__))
    # Create the custom functions
    print '-' * 100
    log.info("Creating custom functions")
    sqlfile = os.path.join(basepath, 'baruwa', 'config', 'sql',
                           'functions.sql')
    if os.path.exists(sqlfile):
        with open(sqlfile, 'r') as handle:
            sql = handle.read()
            try:
                conn = Session.connection()
                conn.execute(text(sql))
                Session.commit()
            except ProgrammingError:
                Session.rollback()
    defaultserver = Session.query(Server)\
                    .filter(Server.hostname == 'default')\
                    .all()
    # Create the Mailscanner SQL config views
    print '-' * 100
    log.info("Populating initial sql")
    sqlfile = os.path.join(basepath, 'baruwa', 'config', 'sql',
                           'integration.sql')
    if os.path.exists(sqlfile):
        with open(sqlfile, 'r') as handle:
            sql = handle.read()
        for sqlcmd in sql.split(';'):
            if sqlcmd:
                try:
                    sqlcmd = "%s;" % sqlcmd
                    Session.execute(text(sqlcmd))
                    Session.commit()
                except ProgrammingError:
                    Session.rollback()
    if not defaultserver:
        log.info("Creating the default settings node")
        dfls = Server('default', True)
        Session.add(dfls)
        confserial = ConfigSettings('confserialnumber', 'ConfSerialNumber', 0)
        confserial.value = 1
        confserial.server_id = 1
        Session.add(confserial)
        Session.commit()
        log.info("Default settings node created !")
Example #53
0
def savemodel(model, form, domainid=None):
    "save form data"
    for field in form:
        if field.name != 'csrf_token':
            setattr(model, field.name, field.data)
    if domainid:
        model.domain_id = domainid
    try:
        Session.add(model)
        Session.commit()
    except IntegrityError:
        Session.rollback()
Example #54
0
def savemodel(model, form, domainid=None):
    "save form data"
    for field in form:
        if field.name != 'csrf_token':
            setattr(model, field.name, field.data)
    if domainid:
        model.domain_id = domainid
    try:
        Session.add(model)
        Session.commit()
    except IntegrityError:
        Session.rollback()
Example #55
0
 def new(self):
     "Add an organization"
     c.form = org_add_form(request.POST, session)
     if request.method == 'POST' and c.form.validate():
         try:
             org = create_org(c.form, c.user, request.host,
                         request.remote_addr)
             msg = _('The organization: %s has been created') % org.name
             flash(msg)
             log.info(msg)
             redirect(url(controller='organizations'))
         except IntegrityError:
             Session.rollback()
             flash_alert(_('The organization already exists'))
     return self.render('/organizations/add.html')
Example #56
0
 def new(self):
     "Add an organization"
     c.form = org_add_form(request.POST, session)
     if request.method == 'POST' and c.form.validate():
         try:
             org = create_org(c.form, c.user, request.host,
                              request.remote_addr)
             msg = _('The organization: %s has been created') % org.name
             flash(msg)
             log.info(msg)
             redirect(url(controller='organizations'))
         except IntegrityError:
             Session.rollback()
             flash_alert(_('The organization already exists'))
     return self.render('/organizations/add.html')
Example #57
0
 def loggedin(self):
     "Landing page"
     came_from = (unquote(str(request.params.get('came_from', '')))
                  or url('/'))
     if not self.identity:
         if 'repoze.who.logins' in request.environ:
             login_counter = request.environ['repoze.who.logins'] + 1
         else:
             abort(409)
         redirect(
             url('/accounts/login',
                 came_from=came_from,
                 __logins=login_counter))
     userid = self.identity['repoze.who.userid']
     user = self.identity['user']
     if user is None:
         try:
             user, local_part, domain, domains = add_user(userid)
             msg = _('First time Login from external auth,'
                     ' your local account was created')
             user_address_update(user, local_part, domain, domains,
                                 self.identity)
         except IntegrityError:
             Session.rollback()
             redirect(url('/logout'))
         except ldap.LDAPError:
             pass
     else:
         if not user.active:
             redirect(url('/logout'))
         msg = _('Login successful, Welcome back %(username)s !' %
                 dict(username=userid))
     update_login(user)
     if user.is_peleb:
         for domain in user.domains:
             if check_language(domain.language):
                 session['lang'] = domain.language
                 session.save()
                 break
     session['taskids'] = []
     session.save()
     info = auditmsgs.ACCOUNTLOGIN_MSG % dict(u=user.username)
     audit_log(user.username, 6, unicode(info), request.host,
               request.remote_addr,
               arrow.utcnow().datetime)
     flash(msg)
     log.info(msg)
     redirect(url(came_from))
Example #58
0
def process_messages(last_date):
    "process messages table"
    params = dict(date=last_date)
    sql1 = text("""INSERT INTO archive
                SELECT * FROM messages WHERE timestamp <
                :date;""")
    try:
        Session.execute(sql1, params=params)
    except IntegrityError, error:
        Session.rollback()
        sql = text("""DELETE FROM archive WHERE id in
                    (SELECT id FROM messages WHERE timestamp < :date);""")
        Session.execute(sql, params=params)
        Session.execute(sql1, params=params)
        print >> sys.stderr, "Integrety error occured: %s" % str(error)
        sys.exit(2)
Example #59
0
 def add(self):
     """/accounts/new"""
     c.form = user_add_form(c.user, request.POST, session)
     if request.method == 'POST' and c.form.validate():
         try:
             user = create_user(c.form, c.user.username, request.host,
                                request.remote_addr)
             flash(
                 _('The account: %(user)s was created successfully') %
                 {'user': user.username})
             redirect(url('account-detail', userid=user.id))
         except IntegrityError:
             Session.rollback()
             flash_alert(
                 _('Either the username or email address already exist'))
     return self.render('/accounts/new.html')
Example #60
0
 def edit(self, domainid):
     "Edit a domain"
     domain = self._get_domain(domainid)
     if not domain:
         abort(404)
     c.form = AddDomainForm(request.POST, domain, csrf_context=session)
     if c.user.is_superadmin:
         c.form.organizations.query_factory = self._get_organizations
     else:
         del c.form.organizations
     c.id = domainid
     if request.POST and c.form.validate():
         updated = False
         kw = {'domainid': domain.id}
         for field in c.form:
             intfields = [
                 'spam_actions', 'highspam_actions', 'delivery_mode',
                 'report_every'
             ]
             if (field.name in intfields
                     and int(field.data) == getattr(domain, field.name)):
                 continue
             if (field.name != 'csrf_token'
                     and field.data != getattr(domain, field.name)):
                 setattr(domain, field.name, field.data)
                 updated = True
         if updated:
             try:
                 Session.add(domain)
                 Session.commit()
                 update_serial.delay()
                 info = UPDATEDOMAIN_MSG % dict(d=domain.name)
                 audit_log(c.user.username, 2, info, request.host,
                           request.remote_addr, now())
                 flash(
                     _('The domain: %(dom)s has been updated') %
                     dict(dom=domain.name))
                 kw['uc'] = 1
             except IntegrityError:
                 Session.rollback()
                 flash(
                     _('The domain %(dom)s could not be updated') %
                     dict(dom=domain.name))
         else:
             flash_info(_('No changes were made to the domain'))
         redirect(url('domain-detail', **kw))
     return render('/domains/edit.html')