def copy_dashboard(mapper, connection, target): dashboard_id = config.get('DASHBOARD_TEMPLATE_ID') if dashboard_id is None: return Session = sessionmaker(autoflush=False) session = Session(bind=connection) new_user = session.query(User).filter_by(id=target.id).first() # copy template dashboard to user template = session.query(Dashboard).filter_by(id=int(dashboard_id)).first() dashboard = Dashboard( dashboard_title=template.dashboard_title, position_json=template.position_json, description=template.description, css=template.css, json_metadata=template.json_metadata, slices=template.slices, owners=[new_user], ) session.add(dashboard) session.commit() # set dashboard as the welcome dashboard extra_attributes = UserAttribute( user_id=target.id, welcome_dashboard_id=dashboard.id, ) session.add(extra_attributes) session.commit()
def copy_dashboard( # pylint: disable=unused-argument mapper: Mapper, connection: Connection, target: "Dashboard") -> None: dashboard_id = config["DASHBOARD_TEMPLATE_ID"] if dashboard_id is None: return new_user = db.session.query(User).filter_by(id=target.id).first() # copy template dashboard to user template = db.session.query(Dashboard).filter_by( id=int(dashboard_id)).first() dashboard = Dashboard( dashboard_title=template.dashboard_title, position_json=template.position_json, description=template.description, css=template.css, json_metadata=template.json_metadata, slices=template.slices, owners=[new_user], ) db.session.add(dashboard) db.session.commit() # set dashboard as the welcome dashboard extra_attributes = UserAttribute(user_id=target.id, welcome_dashboard_id=dashboard.id) db.session.add(extra_attributes) db.session.commit()
def set_welcome_dashboard(id, user): # Make sure welcome dashboard exists dashboard = db.session.query(models.Dashboard).filter_by(id=id).first() # Set dashboard as default extra_attributes = UserAttribute( user_id=user.id, welcome_dashboard_id=dashboard.id, ) db.session.add(extra_attributes) db.session.commit()
def oauth_authorized(self, provider): log.debug("Authorized init") resp = self.appbuilder.sm.oauth_remotes[provider].authorized_response() if resp is None: flash(u"You denied the request to sign in.", "warning") return redirect("login") log.debug("OAUTH Authorized resp: {0}".format(resp)) # Retrieves specific user info from the provider try: self.appbuilder.sm.set_oauth_session(provider, resp) userinfo = self.appbuilder.sm.oauth_user_info(provider, resp) except Exception as e: log.error("Error returning OAuth user info: {0}".format(e)) user = None else: log.debug("User info retrieved from {0}: {1}".format( provider, userinfo)) # User email is not whitelisted if provider in self.appbuilder.sm.oauth_whitelists: whitelist = self.appbuilder.sm.oauth_whitelists[provider] allow = False for e in whitelist: if re.search(e, userinfo["email"]): allow = True break if not allow: flash(u"You are not authorized.", "warning") return redirect("login") else: log.debug("No whitelist for OAuth provider") user = self.appbuilder.sm.auth_user_oauth(userinfo) if user is None: flash(as_unicode(self.invalid_login_message), "warning") return redirect("login") else: login_user(user) try: state = jwt.decode( request.args["state"], self.appbuilder.app.config["SECRET_KEY"], algorithms=["HS256"], ) except jwt.InvalidTokenError: raise Exception("State signature is not valid!") try: next_url = state["next"][0] or self.appbuilder.get_url_for_index except (KeyError, IndexError): next_url = self.appbuilder.get_url_for_index #commit user_attributes from superset import app, db from superset.models.user_attributes import UserAttribute session = db.session() #find is exist user_attribute = session.query(UserAttribute).filter_by( user_id=user.id).one_or_none() if user_attribute is None: user_attribute = UserAttribute(user_id=user.id, tenant_code=provider) try: session.add(user_attribute) session.commit() except Exception as e: session.rollback() raise Exception("Error Save UserAttribute {0} ".format(e)) else: user_attribute.tenant_code = provider try: session.commit() except Exception as e: session.rollback() raise Exception("Error Save UserAttribute {0} ".format(e)) return redirect(next_url)