def active_cdkey(): '''激活激活码(企业级以下的用户的注册)''' validator = BaseValidator().get_json() uid = g.user.uid cdkey_code = validator['cdkey'] username = validator['username'] realname = validator['realname'] password = validator['password'] mobile = validator['mobile'] email = validator['email'] cdkey = CDKeyModel.objects.filter( cdkey=cdkey_code, state=True).first_or_404(msg='激活码无效,请联系相关负责人') employee_tuple = (ScopeEnum.CO_PROJECT, ScopeEnum.CO_OPERATE, ScopeEnum.CO_USER) if ScopeEnum(cdkey.auth) == ScopeEnum.CO_SUPER: # 注册成企业超级管理员 UserActive.active_co_super_or_admin(cdkey, uid, username, realname, password, mobile, email) cdkey.state = False cdkey.save() elif ScopeEnum(cdkey.auth) == ScopeEnum.CO_ADMIN: UserActive.active_co_super_or_admin(cdkey, uid, username, realname, password, mobile, email) elif ScopeEnum(cdkey.auth) in employee_tuple: UserActive.active_co_employee(cdkey, uid, username, realname, password, mobile, email) else: raise APIException(msg='激活码权限不足') return Success()
def match_user_scope(auth, type='en'): auth_scope_en = { ScopeEnum.USER: '******', ScopeEnum.ADMIN: 'AdminScope', ScopeEnum.SUPER: 'SuperScope' } auth_scope_cn = { ScopeEnum.USER: '******', ScopeEnum.ADMIN: '系统管理员', ScopeEnum.SUPER: '系统超级管理员' } if type == 'en': return auth_scope_en.get(ScopeEnum(auth), 'UserScope') elif type == 'cn': return auth_scope_cn.get(ScopeEnum(auth), '普通用户')
def verify_by_mobile(mobile, password): user = User.query.filter_by(mobile=mobile) \ .first_or_404(e=UserException(msg='该账号未注册')) if not user.check_password(password): raise AuthFailed(msg='密码错误') scope = 'AdminScope' if ScopeEnum( user.auth) == ScopeEnum.ADMIN else 'UserScope' return {'uid': user.id, 'scope': scope}
def verify_by_wx_open(code, *args): # 微信开放平台(第三方)登录 ot = OpenToken(code) user_info = ot.get() openid = user_info['openid'] # 用户唯一标识 user = User.query.filter_by(openid=openid).first() if not user: user = User.register_by_wx_open(user_info) scope = 'AdminScope' if ScopeEnum(user.auth) == ScopeEnum.Admin else 'UserScope' return {'uid': user.id, 'scope': scope}
def verify_by_wx_account(code, *args): ot = AccountToken(code) user_info = ot.get() unionid = user_info['unionid'] user = User.query.filter_by(unionid=unionid).first() if not user: user = User.register_by_wx_open(user_info) scope = 'AdminScope' if ScopeEnum( user.auth) == ScopeEnum.ADMIN else 'UserScope' return {'uid': user.id, 'scope': scope}
def is_permitted(scope, endpoint): key = str(ScopeEnum(scope).name).title() scope = globals()[key]() endpoint = endpoint.split('.')[1] if endpoint in scope.forbidden: return False if endpoint in scope.permitted: return True return False
def verify_by_wx_mina(code, *args): ut = WxToken(code) wx_result = ut.get() # wx_result = {session_key, expires_in, openid} openid = wx_result['openid'] user = User.query.filter_by(openid=openid).first() # 如果不在数据库,则新建用户 if not user: user = User.register_by_wx_mina(openid) scope = 'AdminScope' if ScopeEnum( user.auth) == ScopeEnum.ADMIN else 'UserScope' return {'uid': user.id, 'scope': scope}
def match_user_scope(auth, type='en'): ''' :param auth(int): 用户权限(1,2,...) :param type(str): en(英文) | cn(中文) :return: ''' auth_scope_en = { # System 系统(金峰) ScopeEnum.SYS_SUPER: 'SysSuperScope', ScopeEnum.SYS_ADMIN: 'SysAdminScope', # Company 企业 ScopeEnum.CO_SUPER: 'CoSuperScope', ScopeEnum.CO_ADMIN: 'CoAdminScope', ScopeEnum.CO_PROJECT: 'CoProjectScope', ScopeEnum.CO_OPERATE: 'CoOperateScope', ScopeEnum.CO_USER: '******', # Agent 代理商 ScopeEnum.AGENT: 'AgentScope', # guest 游客 ScopeEnum.GUEST: 'GuestScope' } auth_scope_cn = { # System 系统(金峰) ScopeEnum.SYS_SUPER: '系统超级管理员', ScopeEnum.SYS_ADMIN: '系统管理员', # Company 企业 ScopeEnum.CO_SUPER: '企业超级管理员', ScopeEnum.CO_ADMIN: '企业管理员', ScopeEnum.CO_PROJECT: '项目负责人', ScopeEnum.CO_OPERATE: '运维员工', ScopeEnum.CO_USER: '******', # Agent 代理商 ScopeEnum.AGENT: '代理商', # guest 游客 ScopeEnum.GUEST: '游客' } if type == 'en': return auth_scope_en.get(ScopeEnum(auth), 'GuestScope') elif type == 'cn': return auth_scope_cn.get(ScopeEnum(auth), '普通用户')
def is_admin(self): return ScopeEnum(self.auth) == ScopeEnum.ADMIN