Ejemplo n.º 1
0
 def register_by_wx_mina(openid: str):
     """小程序注册"""
     with db.auto_commit():
         user = User.create(commit=False)
         Identity.create(commit=False,
                         user_id=user.id,
                         type=ClientTypeEnum.WX_MINA.value,
                         identifier=openid,
                         verified=1)
     return user
Ejemplo n.º 2
0
 def update_identity(commit=True,
                     user_id=None,
                     identifier=None,
                     credential=None,
                     type=None):
     identity = Identity.get(user_id=user_id, type=type)
     if identity:
         identity.update(commit=commit,
                         identifier=identifier,
                         credential=credential)
     else:
         Identity.create(commit=commit,
                         user_id=user_id,
                         type=type,
                         identifier=identifier,
                         credential=credential)
Ejemplo n.º 3
0
 def create_identity(user_id, identifier, credential, type):
     # 判断是否是网站内部的身份类型
     if ClientTypeEnum(type) in current_app.config['CLINET_INNER_TYPES']:
         with db.auto_commit():
             user = User.get(id=user_id)
             attr = ClientTypeEnum(type).name.lower()
             setattr(user, attr, identifier)
             Identity.create(commit=False,
                             user_id=user_id,
                             type=type,
                             identifier=identifier,
                             credential=credential)
             user.save(commit=False)
     # 第三方平台,则无需修改用户信息
     else:
         Identity.create(user_id=user_id, type=type, identifier=identifier)
Ejemplo n.º 4
0
 def change_password(uid, old_password, new_password):
     identity = Identity.get_or_404(user_id=uid)  # 找到一个
     if identity.check_password(old_password):
         identity_list = Identity.query.filter(
             Identity.type.in_([
                 ClientTypeEnum.USERNAME.value, ClientTypeEnum.EMAIL.value,
                 ClientTypeEnum.MOBILE.value
             ]), Identity.user_id == uid).all()
         with db.auto_commit():
             for item in identity_list:
                 item.update(commit=False, password=new_password)
Ejemplo n.º 5
0
 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']
     identity = Identity.get(identifier=openid, type=ClientTypeEnum.WX_MINA.value)
     # 如果不在数据库,则新建用户
     if not identity:
         user = UserDao.register_by_wx_mina(openid=openid)
     else:
         user = User.get(id=identity.user_id)
     return {'uid': user.id, 'scope': user.auth_scope}
Ejemplo n.º 6
0
 def update_user(uid, form):
     # 第1步: 核对需修改的信息(用户名、手机号、邮箱)
     identity_infos = []
     if (hasattr(form, 'username')):
         identity_infos.append({
             'identifier': form.username,
             'type': ClientTypeEnum.USERNAME.value,
             'msg': '该用户名已被使用,请重新输入新的用户名'
         })
     if (hasattr(form, 'mobile')):
         identity_infos.append({
             'identifier': form.mobile,
             'type': ClientTypeEnum.MOBILE.value,
             'msg': '手机号已被使用,请重新输入新的手机号'
         })
     if (hasattr(form, 'email')):
         identity_infos.append({
             'identifier': form.email,
             'type': ClientTypeEnum.EMAIL.value,
             'msg': '邮箱已被使用,请重新输入新的邮箱号'
         })
     # 第2步: 修改用户信息
     with db.auto_commit():
         # 第2.1步: 获取用户信息
         user = User.query.filter_by(id=uid).first_or_404()
         credential = IdentityDao.get_credential(user_id=uid)
         # 第2.2步: 修改用户昵称
         if hasattr(form, 'nickname'):
             user.update(commit=False, nickname=form.nickname)
         # 第2.3步: 依次修改用户身份信息(用户名、手机号、邮箱)
         for item in identity_infos:
             Identity.abort_repeat(identifier=item['identifier'],
                                   msg=item['msg'])
             IdentityDao.update_identity(commit=False,
                                         user_id=uid,
                                         identifier=item['identifier'],
                                         credential=credential,
                                         type=item['type'])
Ejemplo n.º 7
0
 def delete_identity(user_id, type):
     '''删除时,需要判断type的类型
        因为username、mobile、email的解绑需要清除user表的对应字段
     '''
     user = User.get(id=user_id)
     with db.auto_commit():
         # 判断是否是网站内部的身份类型
         if ClientTypeEnum(
                 type) in current_app.config['CLINET_INNER_TYPES']:
             attr = ClientTypeEnum(type).name.lower()
             setattr(user, attr, None)
             user.save(commit=False)
         identity = Identity.get_or_404(user_id=user_id, type=type)
         identity.hard_delete(commit=False)  # 硬删除
Ejemplo n.º 8
0
 def email(self):
     identity = Identity.get(user_id=self.id, type=ClientTypeEnum.EMAIL.value)
     return identity.identifier if identity else None
Ejemplo n.º 9
0
 def mobile(self):
     identity = Identity.get(user_id=self.id, type=ClientTypeEnum.MOBILE.value)
     return identity.identifier if identity else None
Ejemplo n.º 10
0
 def username(self):
     identity = Identity.get(user_id=self.id, type=ClientTypeEnum.USERNAME.value)
     return identity.identifier if identity else None
Ejemplo n.º 11
0
 def create_user(form):
     with db.auto_commit():
         user = User.create(commit=False,
                            nickname=getattr(form, 'nickname', None),
                            auth=ScopeEnum.COMMON.value)
         if (hasattr(form, 'username')):
             Identity.abort_repeat(identifier=form.username,
                                   msg='该用户名已被使用,请重新输入新的用户名')
             Identity.create(commit=False,
                             user_id=user.id,
                             type=ClientTypeEnum.USERNAME.value,
                             verified=1,
                             identifier=form.username,
                             password=form.password)
         if (hasattr(form, 'mobile')):
             Identity.abort_repeat(identifier=form.mobile,
                                   msg='手机号已被使用,请重新输入新的手机号')
             Identity.create(commit=False,
                             user_id=user.id,
                             type=ClientTypeEnum.MOBILE.value,
                             identifier=form.mobile,
                             password=form.password)
         if (hasattr(form, 'email')):
             Identity.abort_repeat(identifier=form.email,
                                   msg='邮箱已被使用,请重新输入新的邮箱号')
             Identity.create(commit=False,
                             user_id=user.id,
                             type=ClientTypeEnum.EMAIL.value,
                             identifier=form.email,
                             password=form.password)
Ejemplo n.º 12
0
 def verify_by_mobile(mobile, password):
     identity = Identity.get_or_404(identifier=mobile, type=ClientTypeEnum.MOBILE.value,
                                    e=IdentityException(msg='该手机号未注册'))
     identity.check_password(password, e=AuthFailed(msg='密码错误'))
     user = User.get(id=identity.user_id)
     return {'uid': user.id, 'scope': user.auth_scope}
Ejemplo n.º 13
0
 def verify_by_email(email, password):
     identity = Identity.get_or_404(identifier=email, type=ClientTypeEnum.EMAIL.value,
                                    e=IdentityException(msg='该邮箱未注册'))
     identity.check_password(password, e=AuthFailed(msg='密码错误'))
     user = User.get(id=identity.user_id)
     return {'uid': user.id, 'scope': user.auth_scope}
Ejemplo n.º 14
0
 def verify_by_username(username, password):
     identity = Identity.get_or_404(identifier=username, type=ClientTypeEnum.USERNAME.value,
                                    e=IdentityException(msg='该用户名未注册'))
     identity.check_password(password, e=AuthFailed(msg='密码错误'))
     user = User.get(id=identity.user_id)
     return {'uid': user.id, 'scope': user.auth_scope}