def get_current_user(self): # 获取当前用户信息在redis中存储的信息 uid = request.cookies.get('admin_uid', None) uid_key = request.cookies.get('admin_key', None) if not (uid and uid_key): return None # 判断信息的有效性 today = datetime.strftime(datetime.today(), '%Y-%m-%d') login_at = self._get_login_cache(uid) if today != login_at: # 去SSO中校验用户的有效性 user_info = self._sso_verification(uid, uid_key) if not user_info: return None # 创建新用户 或者 获取用户的user_id user = self._get_user_id(user_info) self._account_valid(user) # 存储用户信息到Redis中 self._set_login_cache(uid, today) else: user = Account.query.filter_by(sso_id=int(uid)).first() if user: bussiness_id = UserBussinessCache().get(user.id) or 1 if self._is_bussiness_admin(user.id, bussiness=bussiness_id) and user.role != 'super_admin': user.role = 'admin' user.role = 'super_admin' if user.id == 1 else user.role return user
def post(self): input_json = request.get_json() bussiness_id = input_json['bussiness_id'] if not has_bussiness_permission(g.user, bussiness_id): return self.response_json(self.HttpErrorCode.FORBIDDEN) UserBussinessCache().set(g.user.id, bussiness_id) return self.response_json(self.HttpErrorCode.SUCCESS)
def debbuger_user(self): if not require_super_admin() or 'debugger' not in request.args: return g.user user = Account.query.filter_by(id=int(request.args['debugger'])).first() if user: bussiness_id = UserBussinessCache().get(user.id) or 1 if self._is_bussiness_admin(user.id, bussiness=bussiness_id): user.role = 'admin' return user if user else None
def get_current_user(self): session_token = request.cookies.get('session_token', '') user_id = SessionCache().get_user_id_by_token(session_token) if not user_id: return None user = Account.query.filter_by(id=user_id).first() if user: bussiness_id = UserBussinessCache().get(user.id) or 1 # TODO 需要修复db.model自动保存的问题 if self._is_bussiness_admin(user.id, bussiness=bussiness_id) and user.role != 'super_admin': user.role = 'admin' user.role = 'super_admin' if user.id == 1 else user.role return user
def login_user(): if self.app.config['AUTH_TYPE'] == 'SSO': auth = SSOAuth() else: auth = SessionAuth() g.user = auth.get_current_user() # debugger g.user = auth.debbuger_user() if g.user: g.bussiness_id = UserBussinessCache().get(g.user.id) or -1 else: g.bussiness_id = -1