def role(uid): usr = User.query.filter_by(id=uid).first() if usr is not None: form = RoleChangeForm(request.form) form.role.choices = [(r.name, r.description) for r in Role.__iter__()] if form.validate_on_submit(): # Update role usr.role = Role.from_string(form.role.data) g.db.commit() return redirect(url_for('.users')) form.role.data = usr.role.name return {'form': form, 'view_user': usr} abort(404)
def role(uid): usr = User.query.filter_by(id=uid).first() if usr is not None: form = RoleChangeForm(request.form) form.role.choices = [(r.name, r.description) for r in Role] if form.validate_on_submit(): # Update role usr.role = Role.from_string(form.role.data) g.db.commit() return redirect(url_for('.users')) form.role.data = usr.role.name return { 'form': form, 'view_user': usr } abort(404)
def run(): from database import create_session from mod_auth.models import User, Role from mod_auth.models import Page db = create_session(sys.argv[1]) # Create pages if not existing pages = Page.query.all() if len(pages) == 0: page_entries = { 'report.dashboard': 'Dashboard', 'config.notifications': 'Notification services', 'config.data_processing': 'Data processing', 'config.services': 'Honeypot services', 'auth.users': 'User manager', 'auth.access': 'Access manager', 'honeypot.profiles': 'Profile management', 'honeypot.manage': 'Honeypot management' } for name, pretty_name in page_entries.iteritems(): page = Page(name, pretty_name) db.add(page) db.commit() # Add support pages db.add(Page('support.about', 'About', True)) db.add(Page('support.support', 'Support', True)) db.commit() # Create admin role, or check if it already exists existing = Role.query.filter(Role.is_admin).first() if existing is None: role = Role("Admin") db.add(role) db.commit() existing = role else: # Check if there's at least one admin user admin = User.query.filter(User.role_id == existing.id).first() if admin is not None: print("Admin already exists: %s" % admin.name) return user = User(existing.id, sys.argv[2], sys.argv[3], User.generate_hash(sys.argv[4])) db.add(user) db.commit() print("Admin user created with name: %s" % user.name)
def create_admin(self): # test if there is admin existed try: name, password, email = "admin", "adminpwd", "*****@*****.**" db = create_session(self.app.config['DATABASE_URI'], drop_tables=False) role = Role(name=name) db.add(role) db.commit() admin_user = User(role_id=role.id, name=name, password=password, email=email) db.add(admin_user) db.commit() finally: db.remove() return name, password, email
def access(): form = CreateRoleForm() if form.validate_on_submit(): # Create role role = Role(form.name.data) g.db.add(role) g.db.commit() redirect(url_for('.access')) return { 'roles': Role.query.filter( Role.id != Role.query.filter(Role.is_admin).first().id).order_by( Role.name.asc()), 'pages': Page.query.filter(not_(Page.is_global)).order_by(Page.name.asc()), 'form': form }
def role(uid): """ View and change user's role. :param uid: id of the user :type uid: int :return: role form and user view if valid response, appropriate error otherwise :rtype: dynamic """ usr = User.query.filter_by(id=uid).first() if usr is not None: form = RoleChangeForm(request.form) form.role.choices = [(r.name, r.description) for r in Role] if form.validate_on_submit(): usr.role = Role.from_string(form.role.data) g.db.commit() return redirect(url_for('.users')) form.role.data = usr.role.name return {'form': form, 'view_user': usr} g.log.debug(f'user with id: {uid} not found!') abort(404)