def create_model(self, form): if is_super_user(self): logging.info("UserManagementModelView.create_model(form=" + str(form) + ")") if form.password.data != form.password_confirm.data: flash('Password and confirm password does not match', 'error') return False try: user = User() # Added this line to create the user object since its required to create the passworduser object model = self.model(user) form.populate_obj(model) self.session.add(model) self._on_model_change(form, model, True) self.session.commit() except Exception as ex: if not self.handle_view_exception(ex): flash(gettext('Failed to create record. %(error)s', error=str(ex)), 'error') logging.exception('Failed to create record.') self.session.rollback() return False else: self.after_model_change(form, model, True) return model else: flash('Failed to create user. Only admin users can create new user', 'error') return False
def create_model(self, form): logging.info("UserManagementModelView.create_model(form=" + str(form) + ")") # Only the Admin can create new user from this plugin. if current_user.user.username != 'admin': flash('Failed to create user. Only admin user can create new user', 'error') logging.exception('Failed to create new user.') return False try: user = User( ) # Added this line to create the user object since its required to create the passworduser object model = self.model(user) form.populate_obj(model) self.session.add(model) self.on_model_change(form, model, True) self.session.commit() except Exception as ex: if not self.handle_view_exception(ex): flash( gettext('Failed to create record. %(error)s', error=str(ex)), 'error') logging.exception('Failed to create record.') self.session.rollback() return False else: self.after_model_change(form, model, True) return model
def authenticate(session, username, group): """ Authenticate a WebAuthUser with the specified username/password. :param session: An active SQLAlchemy session :param username: The username :param password: The password :raise AuthenticationError: if an error occurred :return: a WebAuthUser """ if not username: raise AuthenticationError() user = session.query(WebAuthUser).filter( WebAuthUser.username == username).first() if not user: user = WebAuthUser(User()) user.username = username user.group_name = group session.add(user) session.commit() # raise AuthenticationError() log.info("User %s successfully authenticated", username) return user