def global_policies(self): """Set Global policies""" settings = get_policy_setting() if settings: c.form = PolicySettingsForm(request.POST, settings, csrf_context=session) else: c.form = PolicySettingsForm(request.POST, csrf_context=session) set_policy_form_opts(c.form) if request.method == 'POST' and c.form.validate(): save_policy_settings(c.form, settings, c.user, request.host, request.remote_addr) flash(_("The Global Policy Settings have been saved")) return self.render('/settings/policy_settings.html')
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() for sqlcmd in sqlparse.split(sql): if sqlcmd.strip(): try: Session.execute(text(sqlcmd.strip())) 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 sqlparse.split(sql): if sqlcmd.strip(): try: Session.execute(text(sqlcmd.strip())) Session.commit() except ProgrammingError: Session.rollback() policy = get_policy_setting() if not policy: print '-' * 100 log.info("Creating the policy settings") policy = PolicySettings() policy.archive_filename = 0 policy.archive_filetype = 0 policy.filename = 0 policy.filetype = 0 Session.add(policy) Session.commit() log.info("Policy settings created !") 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 !") admin = Session.query(User).filter(User.account_type == 1).all() if not admin: def timeout_handler(signum, frame): "Timeout exception" raise TimeoutException() old_handler = signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(30) try: create_user = raw_input('Do you want to configure ' 'an admin account? (Y/N): ') except (TimeoutException, EOFError): sys.exit(0) finally: signal.signal(signal.SIGALRM, old_handler) signal.alarm(0) if str(create_user).lower() == 'y': print '-' * 100 log.info("Creating initial admin account") value_map = {'username': True, 'password1': True, 'password2': True, 'firstname': False, 'lastname': False, 'email': True} values = {} def get_input(field, required): "Get user input" prompt = "Please enter the %s:" % field while 1: if field in ['password1', 'password2']: value = getpass.getpass(prompt=prompt) else: value = raw_input(prompt) if not required: break if required and value.strip() != "": if field not in ['email', 'password1', 'password2']: break if field == 'email': if not EMAIL_RE.match(value): print "Please provide a valid email address." else: break if field == 'password1': try: cracklib.VeryFascistCheck(value) except ValueError, message: print str(message) else: break if field == 'password2': if values['password1'] == value: break else: print 'password2 does not match password1' return value for attr in value_map: value = get_input(attr, value_map[attr]) values[attr] = value user = User(values['username'], values['email']) for name in ['firstname', 'lastname']: if values[name]: setattr(user, name, values[name]) user.internal = True user.active = True user.local = True user.account_type = 1 user.set_password(values['password1']) Session.add(user) Session.commit()