def reset_password(): form = ResetPasswordForm() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user is None: flash(_('Please provide a valid email.'), 'error') return redirect(url_for('auth.reset_password')) if not user.active: flash(_('Your account is not active. Please contact the administrator.'), 'error') return redirect(url_for('auth.login')) if user.force_pwd_change: flash(_('You have already resetted your password!'), 'info') return redirect(url_for('auth.login')) password = password_generator(16) if current_app.config['DEBUG']: print(password) user.force_pwd_change = True user.password = password form.populate_obj(user) if user.update(): flash(_('Your password has been reset. Check your Email account.'), 'success') user_reset_password(user, password) return redirect(url_for('auth.login')) return render_template('auth/routes.reset_password.html.j2', form=form)
def before_app_request(): if current_user.is_authenticated: if current_user.force_pwd_change \ and request.endpoint[:5] != 'auth.' \ and request.endpoint[:7] != 'static.'\ and request.endpoint[:13] != 'debugtoolbar.': return redirect(url_for('auth.change_password'))
def login(): if current_user.is_authenticated: flash(_('You were already authenticated as %(name)s.', name=b(current_user.name)), 'info') if session['preferences']: session.pop('preferences', None) if session['locale']: session.pop('locale', None) logout_user() form = LoginForm() if not current_app.config['RUNE_AUTH_LOGIN_REMEMBER']: form.remember_me = None if form.validate_on_submit(): _fails = 0 user = User.query.filter_by(username=form.username.data).first() if user is None: flash(_('Invalid username or password.'), 'error') return redirect(url_for('auth.login')) _fails = user.failed_attempts if not user.verify_password(form.password.data): flash(_('Invalid username or password.'), 'error') return redirect(url_for('auth.login')) if _fails > 0: flash(ngettext('You have %(num)d failed login attempt.', 'You have %(num)d failed login attempts.', num=_fails), 'warning') login_user(user) session['locale'] = user.locale session['preferences'] = {} for preference in user.preferences: session['preferences'][preference.name] = preference.value return redirect(request.args.get('next') or url_for('main.index')) return render_template('auth/routes.login.html.j2', form=form)
def logout(): logout_user() session.pop('preferences', None) session.pop('locale', None) flash(_('You have been logged out.'), 'info') return redirect(url_for('auth.login'))
def main_message_clean(): messages = Notification.query.all() counter = 0 for message in messages: if message.expired: counter = counter + 1 message.delete() flash(_('%(counter)s messages have been removed.', counter=b(counter))) return redirect(url_for('admin.main_sysmsg_list'))
def main_sysmsg_edit(id): message = Notification.query.get_or_404(id) form = MessageForm(obj=message) if form.validate_on_submit(): message.author = cu form.populate_obj(message) if message.update(): flash(_('System Message updated sucessfuly'), 'success') return redirect(url_for('admin.main_sysmsg_list')) return render_template('main/admin.message.edit.html.j2', form=form)
def preference_reload(): session.pop('preferences', None) session['preferences'] = {} for pref in current_user.preferences: session['preferences'][pref.name] = pref.value flash(_('Preferences for %(name)s have been reloaded.', name=b(current_user.name or current_user.username)), 'success') return redirect(url_for('auth.preferences'))
def main_sysmsg_create(): form = MessageForm(locale=cu.locale) if form.validate_on_submit(): message = Notification() message.author = cu form.populate_obj(message) message.update() flash(_('Message created successfully.'), 'success') return redirect(url_for('admin.main_sysmsg_list')) return render_template('main/admin.message.edit.html.j2', form=form)
def change_password(): form = ChangePasswordForm() if form.validate_on_submit(): if current_user.verify_password(form.old_password.data): current_user.password = form.password.data current_user.force_pwd_change = False if current_user.update(): flash(_('Your password has been updated.'), 'success') return redirect(url_for('auth.login')) else: flash(_('Invalid password.'), 'danger') return render_template("auth/routes.change_password.html.j2", form=form)
def to_dict(self): """Export user to a dictionary.""" return { 'id': self.id, 'username': self.username, 'name': self.name, 'email': self.email, 'active': self.active, 'is_admin': self.is_admin, 'force_pwd_change': self.force_pwd_change, 'type': self.type, 'failed_attempts': self.failed_attempts, 'roles': [role for role in self.roles], 'permissions': self.permissions, 'links': [ { 'url': url_for('api.auth_user_details', username=self.username), 'text': 'Details' }, { 'url': url_for('api.auth_user_delete', username=self.username), 'text': 'Delete' }, ] }
def preference_create(): form = PreferenceForm() if form.validate_on_submit(): exists = AuthUserPreference.query.filter(and_( AuthUserPreference.name == form.name.data, AuthUserPreference.user_id == current_user.id)).first() if exists: preference = AuthUserPreference.query.get(exists.id) else: preference = AuthUserPreference() form.populate_obj(preference) preference.user_id = current_user.id preference.update() session.pop('preferences', None) session['preferences'] = {} for pref in current_user.preferences: session['preferences'][pref.name] = pref.value flash(_('Preference %(name)s is set to %(value)s', name=b(preference.name), value=b(preference.value or 'False')), 'success') return redirect(url_for('auth.preferences')) for field, errors in form.errors.items(): for error in errors: flash(_('%(field)s: %(msg)s', field=b(getattr(form, field).label.text), msg=error), 'danger') return render_template('auth/routes.preferences.html.j2', form=form, user=current_user)
def basis_apps_clean(): all_apps = current_app.rune_apps installable_apps = [] apps_perms = [] installed_perms = [perm.name for perm in Perm.query.all()] for app in all_apps: if all_apps[app]['installable']: installable_apps.append(app) for app_name in installable_apps: manage_mod = '.'.join([app_name, 'resources']) try: mod = importlib.import_module(manage_mod) except BaseException: current_app.logger.error(f'`{app_name}` has no `resources.py`') try: permissions = getattr(mod, 'PERMS') apps_perms.extend(permissions) except BaseException: current_app.logger.error(f'`{app_name}` permissions ... NOK') orphaned_perms = list(set(installed_perms) - set(apps_perms)) current_app.logger.warning(f'{cu.username} CLEAN {orphaned_perms}') for perm in orphaned_perms: Perm.query.filter_by(name=perm).first().delete() flash( _('Deleted %(count)s orphaned permissions', count=len(orphaned_perms)), 'success') return redirect(url_for('admin.basis_apps'))
def main_sysmsg_delete(id): message = Notification.query.get_or_404(id) message.delete() return redirect(url_for('admin.main_sysmsg_list'))