Пример #1
0
    def post(self, *args, **kwargs):
        next = self.get_argument('next', '')
        account = self.get_argument('account', None)
        password = self.get_argument('password', '')
        rsa_encrypt = self.get_argument('rsa_encrypt', 0)

        if settings.login_pwd_rsa_encrypt and int(
                rsa_encrypt) == 1 and len(password) > 10:
            private_key = sys_config('sys_login_rsa_priv_key')
            password = RSAEncrypter.decrypt(password, private_key)

        if not account:
            return self.error('账号不能够为空')

        if Func.is_mobile(account):
            member = Member.Q.filter(Member.mobile == account).first()
        elif Func.is_email(account):
            member = Member.Q.filter(Member.email == account).first()
        else:
            member = Member.Q.filter(Member.username == account).first()

        if member is None:
            return self.error('用户名或者密码错误')

        if int(member.status) == 0:
            return self.error('用户被“禁用”,请联系客服')
        if check_password(password, member.password) is not True:
            return self.error('用户名或者密码错误')

        Member.login_success(member, self)

        self.clear_cookie(valid_code_key)

        return self.success(next=next)
Пример #2
0
    def post(self, *args, **kwargs):
        id = self.get_argument('id', None)

        params = self.params()

        params['status'] = params.get('status', 0)

        if not id:
            return self.error('用户ID不能为空')

        username = params.get('username', None)
        if username:
            count = Member.Q.filter(Member.id != id).filter(
                Member.username == username).count()
            if count > 0:
                return self.error('用户名已被占用')

        mobile = params.get('mobile', None)
        params.pop('mobile', None)
        if mobile:
            params['mobile'] = mobile
            if Func.is_mobile(mobile):
                count = Member.Q.filter(Member.id != id).filter(
                    Member.mobile == mobile).count()
                if count > 0:
                    return self.error('电话号码已被占用')

        email = params.get('email', None)
        params.pop('email', None)
        if email:
            params['email'] = email
            if Func.is_email(email):
                count = Member.Q.filter(Member.id != id).filter(
                    Member.email == email).count()
                if count > 0:
                    return self.error('Email已被占用')

        password = params.get('password', None)
        params.pop('password', None)
        if password:
            rsa_encrypt = params.get('rsa_encrypt', 0)
            if settings.login_pwd_rsa_encrypt and int(
                    rsa_encrypt) == 1 and len(password) > 10:
                private_key = sys_config('sys_login_rsa_priv_key')
                # print('password: '******'password'] = make_password(password)

        params.pop('_xsrf', None)
        params.pop('rsa_encrypt', None)
        Member.Q.filter(Member.id == id).update(params)
        Member.session.commit()

        # update member cache info
        member = Member.Q.filter(Member.id == id).first()
        cache_key = member.cache_info(self)

        return self.success(data=params)
Пример #3
0
    def post(self, *args, **kwargs):
        menu_tab = self.get_argument('menu_tab', 1)

        params = self.params()
        params['uuid'] = Func.uuid32()
        params.pop('user_id', None)
        params.pop('menu_tab', None)
        params.pop('_xsrf', None)

        if not params.get('code', None):
            return self.error('授权码不能够为空')

        count = AdminMenu.Q.filter(AdminMenu.code == params['code']).count()
        if count > 0:
            return self.error('Code已被占用')

        path = params.get('path', None)
        if path[0:4] != 'http' and path[0:1] != '/':
            params['path'] = '/' + path

        user_id = self.current_user.get('uuid')
        role_id = self.current_user.get('role_id')
        if not self.super_role():
            params['user_id'] = user_id

        menu = AdminMenu(**params)
        AdminMenu.session.add(menu)
        AdminMenu.session.commit()

        self.redirect('/admin/menu/index?#menu_tab=%s' % menu_tab)
Пример #4
0
    def post(self, *args, **kwargs):
        """上传图片"""
        current_user_id = self.current_user.get('user_id')

        user_id = self.get_argument('user_id', None)
        status = self.get_argument('status', None)
        authorized = self.get_argument('authorized', None)
        remark = self.get_argument('remark', '')

        member = Member.Q.filter(Member.id == user_id).first()
        if member is None:
            return self.error('用户不存在')

        params = {
            'user_id': user_id,
            'utc_updated_at': Func.utc_now(),
            'authorized_user_id': current_user_id,
        }
        if status is not None:
            params['status'] = status
        if authorized is not None:
            params['authorized'] = authorized
            params['remark'] = remark
            if member.authorized:
                return self.error('已经实名认证')

        MemberCertification.Q.filter(
            MemberCertification.user_id == user_id).update(params)
        MemberCertification.session.commit()

        return self.success()
Пример #5
0
    def post(self, *args, **kwargs):
        title = self.get_argument('title', None)
        key = self.get_argument('key', None)
        old_key = self.get_argument('old_key', None)

        params = self.params()
        params.pop('old_key', None)
        params.pop('_xsrf', None)

        count = Config.Q.filter(Config.key != old_key).filter(
            Config.key == key).count()
        if count > 0:
            return self.error('KEY已被占用')
        if title:
            count = Config.Q.filter(Config.key != old_key).filter(
                Config.title == title).count()
            if count > 0:
                return self.error('名称已被占用')

        config = Config.Q.filter(Config.key == old_key).first()
        if config:
            params = {**config.as_dict(), **params}
            params['utc_created_at'] = Func.str_to_datetime(
                params['utc_created_at'], 'UTC')

        Config.Q.filter(Config.key == old_key).delete()
        Config.session.add(Config(**params))
        Config.session.commit()
        params.pop('utc_created_at', None)
        return self.success(data=params)
Пример #6
0
class MemberOperationLog(BaseModel):
    """
    user model
    """
    __tablename__ = 'member_operation_log'

    uuid = Column(String(32),
                  primary_key=True,
                  nullable=False,
                  default=Func.uuid32())
    user_id = Column(String(32), ForeignKey('member.uuid'))
    # 用户账号: email or mobile or username
    account = Column(String(80), nullable=False)
    # 会员操作类型: email_reset_pwd mobile_reset_pwd username_reset_pwd activate_email
    action = Column(String(20), nullable=False)
    ip = Column(String(40), nullable=False)
    client = Column(String(20), nullable=True, default='web')
    utc_created_at = Column(TIMESTAMP, default=Func.utc_now)

    @property
    def created_at(self):
        return Func.dt_to_timezone(self.utc_created_at)

    @staticmethod
    def add_log(params):
        """激活邮件

        [description]

        Arguments:
            params {[type]} -- [description]
        """
        log = MemberOperationLog(**params)
        MemberOperationLog.session.add(log)
        MemberOperationLog.session.commit()
Пример #7
0
    def email_reset_pwd(self, email):
        """使用Email充值密码发送邮件功能
        """
        if not Func.is_email(email):
            return self.error('Email格式不正确')

        token = self.get_secure_cookie(settings.token_key)
        if token:
            return self.error('邮件已发送,30分钟后重试')

        member = Member.Q.filter(Member.email == email).first()
        if member is None:
            return self.error('账户没有注册')
        if member.status == 0:
            return self.error('账户被禁用')

        self.success()

        subject = '[%s]找回密码' % sys_config('site_name')
        token = Func.uuid32()
        action_url = sys_config(
            'site_url') + '/passport/forget.html?token=' + token

        localnow = Func.local_now() + datetime.timedelta(minutes=30)
        params = {
            'username': member.username,
            'expires': str(localnow),
            'action_url': action_url,
            'action_tips': '立即重置密码',
        }
        tmpl = 'common/email_content.html'
        content = self.render_string(tmpl, **params)
        # print('content', content)
        Func.sendmail({
            'to_addr': email,
            'subject': subject,
            'content': content
        })
        save = {
            'token': token,
            'account': email,
            'username': member.username,
            'action': 'email_reset_pwd',
        }
        expires = time.mktime(localnow.timetuple())
        self.set_secure_cookie(settings.token_key, str(save), expires=expires)
        return
Пример #8
0
    def activate_email(self, email):
        """激活邮箱发送邮件功能
        """
        if not Func.is_email(email):
            return self.error('Email格式不正确')

        user_id = self.current_user.get('id')
        member = Member.Q.filter(Member.id == user_id).first()

        if member.email_activated:
            return self.error('已经激活了,请不要重复操作')

        token = self.get_secure_cookie(settings.token_key)
        if token:
            return self.error('邮件已发送,10分钟后重试')

        self.success()

        subject = '[%s]激活邮件' % sys_config('site_name')
        token = Func.uuid32()
        action_url = sys_config(
            'site_url') + '/member/activate.html?token=' + token

        localnow = Func.local_now() + datetime.timedelta(minutes=10)
        params = {
            'username': member.username,
            'expires': str(localnow),
            'action_url': action_url,
            'action_tips': '立即激活邮箱',
        }
        tmpl = 'common/email_content.html'
        content = self.render_string(tmpl, **params)
        # print('content', content)
        Func.sendmail({
            'to_addr': email,
            'subject': subject,
            'content': content
        })
        save = {
            'token': token,
            'account': email,
            'username': member.username,
            'action': 'email_reset_pwd',
        }
        expires = time.mktime(localnow.timetuple())
        self.set_secure_cookie(settings.token_key, str(save), expires=expires)
        return
Пример #9
0
    def register(params):
        user_id = Func.uuid32()
        params['uuid'] = user_id
        member = Member(**params)
        Member.session.add(member)
        Member.session.commit()

        return member
Пример #10
0
    def post(self, *args, **kwargs):
        params = self.params()

        params['status'] = params.get('status', 0)

        if not params.get('username', None):
            return self.error('用户名不能为空')
        if not params.get('password', None):
            return self.error('密码不能为空')

        count = Member.Q.filter(Member.username == params['username']).count()
        if count > 0:
            return self.error('用户名已被占用')

        if Func.is_mobile(params.get('mobile', '')):
            count = Member.Q.filter(Member.mobile == params['mobile']).count()
            if count > 0:
                return self.error('电话号码已被占用', data=params)

        if Func.is_email(params.get('email', '')):
            count = Member.Q.filter(Member.email == params['email']).count()
            if count > 0:
                return self.error('Email已被占用')

        password = params.get('password')
        rsa_encrypt = params.get('rsa_encrypt', 0)
        if settings.login_pwd_rsa_encrypt and int(
                rsa_encrypt) == 1 and len(password) > 10:
            private_key = sys_config('sys_login_rsa_priv_key')
            password = RSAEncrypter.decrypt(password, private_key)
            params['password'] = make_password(password)

        params.pop('_xsrf', None)
        params.pop('rsa_encrypt', None)
        params['uuid'] = Func.uuid32()
        member = Member(**params)
        Member.session.add(member)
        Member.session.commit()

        return self.success(data=member.as_dict())
Пример #11
0
    def login_success(user, handler):
        # 设置登录用户cookie信息
        handler.set_curent_user(user)

        user_id = user.uuid
        login_count = user.login_count if user.login_count else 0
        params = {
            'login_count': login_count + 1,
            'utc_last_login_at': Func.utc_now(),
            'last_login_ip': handler.request.remote_ip,
        }
        User.Q.filter(User.uuid == user_id).update(params)

        params = {
            'uuid': Func.uuid32(),
            'user_id': user.uuid,
            'client': 'web',
            'ip': handler.request.remote_ip,
        }
        log = UserLoginLog(**params)
        UserLoginLog.session.add(log)
        UserLoginLog.session.commit()
        return True
Пример #12
0
    def login_success(member, handler, client='web'):
        # 设置登录用户cookiex信息
        handler.set_curent_user(member)

        user_id = member.uuid
        login_count = member.login_count if member.login_count else 0
        params = {
            'login_count': login_count + 1,
            'utc_last_login_at': Func.utc_now(),
            'last_login_ip': handler.request.remote_ip,
        }
        Member.Q.filter(Member.uuid == user_id).update(params)

        # 写登录日志
        params2 = {
            'uuid': Func.uuid32(),
            'user_id': user_id,
            'client': client,
            'ip': handler.request.remote_ip,
        }
        log = MemberLoginLog(**params2)
        MemberLoginLog.session.add(log)

        MemberLoginLog.session.commit()
Пример #13
0
class UserLoginLog(BaseModel):
    """
    user model
    """
    __tablename__ = 'sys_admin_user_login_log'

    uuid = Column(String(32),
                  primary_key=True,
                  nullable=False,
                  default=Func.uuid32())
    user_id = Column(String(32), ForeignKey('sys_admin_user.uuid'))
    ip = Column(String(40), nullable=False)
    client = Column(String(20), nullable=True)
    utc_created_at = Column(TIMESTAMP, default=Func.utc_now)

    @property
    def created_at(self):
        return Func.dt_to_timezone(self.utc_created_at)
Пример #14
0
class MemberFriend(BaseModel):
    """
    user model
    """
    __tablename__ = 'member_friend'

    uuid = Column(String(32),
                  primary_key=True,
                  nullable=False,
                  default=Func.uuid32())
    user_id = Column(String(32), ForeignKey('member.uuid'))
    ip = Column(String(40), nullable=False)
    client = Column(String(20), nullable=True, default='web')
    utc_created_at = Column(TIMESTAMP, default=Func.utc_now)

    @property
    def created_at(self):
        return Func.dt_to_timezone(self.utc_created_at)
Пример #15
0
 def get(self, *args, **kwargs):
     user_id = self.current_user.get('id')
     member = Member.Q.filter(Member.id == user_id).first()
     fields = ['id', 'username', 'avatar', 'sign']
     curr_user = member.as_dict(fields)
     items = Member.Q.filter(Member.status == 1).all()
     user_list = []
     if items:
         for row in items:
             user_list.append(
                 row.as_dict(['id', 'username', 'avatar', 'sign']))
     params = {
         'def_avator': self.static_url('image/default_avatar.jpg'),
         'timestamp': Func.unix_time(),
         # 'opponent_uid': opponent_uid,
         'limit': 20,
         'curr_user': curr_user,
         'user_list': user_list,
         'token': user_id,
     }
     self.render('chat/webrtc.html', **params)
Пример #16
0
class Role(BaseModel):
    """
    user model
    """
    __tablename__ = 'sys_admin_role'

    uuid = Column(String(32),
                  primary_key=True,
                  nullable=False,
                  default=Func.uuid32())
    rolename = Column(String(40), nullable=False)
    permission = Column(Text, default='')
    sort = Column(Integer, nullable=False, default=20)
    # 状态:( 0 禁用;1 启用, 默认1)
    status = Column(Integer, nullable=False, default=1)
    utc_created_at = Column(TIMESTAMP, default=Func.utc_now)

    @property
    def created_at(self):
        return Func.dt_to_timezone(self.utc_created_at)

    @classmethod
    def option_html(cls, role_id=None):
        query = cls.session.query(Role)
        query = query.filter(Role.status == 1)
        rows = query.order_by(Role.sort.asc()).all()
        SysLogger.debug(query.statement)
        option_str = ''
        for row in rows:
            selected = 'selected' if role_id == row.uuid else ''
            option_str += '<option value="%s" %s>%s</option>' % (
                row.uuid, selected, row.rolename)
        # SysLogger.debug('option_str: %s' % option_str)
        return option_str

    @classmethod
    def get_permission(cls, role_id):
        query = cls.session.query('permission')
        query = query.filter(Role.uuid == role_id)
        return query.scalar()
Пример #17
0
    def post(self, *args, **kwargs):
        """上传图片"""
        user_id = self.current_user.get('id')

        next = self.get_argument('next', '')
        imgfile = self.request.files.get('file')
        action = self.get_argument('action', None)
        path = self.get_argument('path', 'default_path')

        if action not in ['alipay', 'wechatpay', 'avatar']:
            return self.error('不支持的action')

        for img in imgfile:
            print('img', type(img))
            # 对文件进行重命名
            file_ext = FileUtil.file_ext(img['filename'])
            path = '%s/' % path
            save_name = img['filename']
            file_md5 = Func.md5(img['body'])
            if action == 'avatar':
                save_name = '%s.%s' % (user_id, file_ext)
            elif action in ['alipay', 'wechatpay']:
                save_name = '%s_%s.%s' % (user_id, action, file_ext)
            try:
                param = Uploader.upload_img(file_md5, img, save_name, path, {
                    'user_id': user_id,
                    'ip': self.request.remote_ip,
                })
                return self.success(data=param)
            except Exception as e:
                if settings.debug:
                    raise e
                SysLogger.error(e)
                return self.error('上传失败')

        return self.error('参数错误')
Пример #18
0
 def created_at(self):
     return Func.dt_to_timezone(self.utc_created_at)
Пример #19
0
 def last_login_at(self):
     return Func.dt_to_timezone(self.utc_last_login_at)
Пример #20
0
class Member(BaseModel):
    """
    user model
    """
    __tablename__ = 'member'

    uuid = Column(String(32),
                  primary_key=True,
                  nullable=False,
                  default=Func.uuid32())
    password = Column(String(128), nullable=False, default='')
    username = Column(String(40), nullable=False)
    mobile = Column(String(11), nullable=True)
    email = Column(String(80), nullable=True)
    level_id = Column(Integer, nullable=False, default=0)
    # 经验值
    experience = Column(Integer, nullable=False, default=0)
    # 性别(男 male ,女 female 隐藏 hide)
    sex = Column(String(10), nullable=False, default='hide')
    # 头像
    avatar = Column(String(255), nullable=True, default='')
    # 签名
    sign = Column(String(255), nullable=True, default='')
    login_count = Column(Integer, nullable=False, default=0)
    last_login_ip = Column(String(40), nullable=False, default='')
    deleted = Column(Integer, nullable=False, default=0)
    # 用户状态:(0 锁定, 1正常, 默认1)
    status = Column(Integer, nullable=False, default=1)
    utc_last_login_at = Column(TIMESTAMP, nullable=True)
    utc_created_at = Column(TIMESTAMP, default=Func.utc_now)
    ref_user_id = Column(String(32), default='')
    register_ip = Column(String(40), nullable=False, default='')
    # 客户端:web wechat android ios mobile
    register_client = Column(String(40), nullable=False, default='')

    sex_options = {
        'hide': '保密',
        'male': '男',
        'female': '女',
    }

    @property
    def sex_option(self):
        return self.sex_options.get(self.sex, '保密')

    @property
    def last_login_at(self):
        return Func.dt_to_timezone(self.utc_last_login_at)

    @property
    def created_at(self):
        return Func.dt_to_timezone(self.utc_created_at)

    @property
    def email_activated(self):
        return self.check_email_activated(self.uuid, self.email)

    @staticmethod
    def get_info(user_id, fields='username,avatar,sign'):
        query = "select %s from member where uuid='%s'" % (
            fields,
            user_id,
        )
        info = Member.session.execute(query).first()
        return dict(info)

    @staticmethod
    def check_email_activated(user_id, email):
        query = "select count(*) from member_operation_log where user_id='%s' and account='%s' and action='activate_email'" % (
            user_id, email)
        # print("query: ", query)
        value = Member.session.execute(query).scalar()
        return True if value > 0 else False

    @staticmethod
    def _friend_list(user_id, where=''):
        query = "select m.uuid as user_id,m.username,m.avatar,m.sign,f.group_id from member m left join member_friend f on m.uuid=f.to_user_id where f.from_user_id='%s' and m.status=1 and f.status=1 %s" % (
            user_id, where)
        rows = Member.session.execute(query).fetchall()
        items = []
        if rows:
            for row in rows:
                items.append(dict(row))
        return items

    @staticmethod
    def friends_no_grouping(user_id):
        where = " and f.group_id='0'"
        return Member._friend_list(user_id, where)

    @staticmethod
    def friends_by_group(user_id, static_url):
        """
        按分组获取好友
        """
        _friend_list = Member._friend_list(user_id)
        # print('_friend_list: ', _friend_list)
        query = "select uuid, groupname from member_friendgroup where owner_user_id='%s'" % user_id
        grows = Member.session.execute(query).fetchall()
        grows = grows if grows else []
        # print("grows: ", type(grows), grows)
        f_g_li = []
        try:
            if len(grows) > 0:
                f_g_li += [{
                    'id':
                    group_id,
                    'groupname':
                    groupname,
                    'list': [{
                        'id': fnd.get('user_id'),
                        'username': fnd.get('username'),
                        'status': Online.get_online(fnd.get('user_id')),
                        'sign': fnd.get('sign'),
                        'avatar': fnd.get('avatar')
                    } for fnd in _friend_list
                             if fnd.get('group_id') == group_id]
                } for (group_id, groupname) in grows]

            # Member.friends_no_grouping(user_id)
            f_g_li += [{
                'id':
                '0',
                'groupname':
                '未分组',
                'list': [{
                    'id': fnd.get('user_id'),
                    'username': fnd.get('username'),
                    'status': Online.get_online(fnd.get('user_id')),
                    'sign': fnd.get('sign'),
                    'avatar': static_url(fnd.get('avatar'))
                } for fnd in Member.friends_no_grouping(user_id)]
            }]
        except Exception as e:
            raise e

        return f_g_li

    @staticmethod
    def login_success(member, handler, client='web'):
        # 设置登录用户cookiex信息
        handler.set_curent_user(member)

        user_id = member.uuid
        login_count = member.login_count if member.login_count else 0
        params = {
            'login_count': login_count + 1,
            'utc_last_login_at': Func.utc_now(),
            'last_login_ip': handler.request.remote_ip,
        }
        Member.Q.filter(Member.uuid == user_id).update(params)

        # 写登录日志
        params2 = {
            'uuid': Func.uuid32(),
            'user_id': user_id,
            'client': client,
            'ip': handler.request.remote_ip,
        }
        log = MemberLoginLog(**params2)
        MemberLoginLog.session.add(log)

        MemberLoginLog.session.commit()

    @staticmethod
    def remove_avator(user_id, mavatar):
        try:
            query = "SELECT `file_md5` FROM `sys_attach_related` WHERE `related_table`='member' and `related_id`='%s';" % (
                user_id)
            old_file_md5 = Member.session.execute(query).scalar()
            if old_file_md5:
                pass
                delq = "DELETE FROM `sys_attach_related` WHERE `file_md5`='%s';"
                Member.session.execute(delq % old_file_md5)
                delq = "DELETE FROM `sys_attach` WHERE `file_md5`='%s';"
                Member.session.execute(delq % old_file_md5)
                old_avatar = settings.STATIC_PATH + '/' + mavatar
                os.remove(old_avatar)
        except Exception as e:
            raise e
        return True

    @staticmethod
    def register(params):
        user_id = Func.uuid32()
        params['uuid'] = user_id
        member = Member(**params)
        Member.session.add(member)
        Member.session.commit()

        return member
Пример #21
0
    def put(self, *args, **kwargs):
        user_id = self.current_user.get('id')
        username = self.get_argument('username', None)
        email = self.get_argument('email', None)
        mobile = self.get_argument('mobile', None)
        sex = self.get_argument('sex', None)
        sign = self.get_argument('sign', None)
        avatar = self.get_argument('avatar', None)
        file_md5 = self.get_argument('file_md5', None)

        params = {}

        if username:
            params['username'] = username
            count = Member.Q.filter(Member.id != user_id).filter(
                Member.username == username).count()
            if count > 0:
                return self.error('用户名已被占用')

        if mobile:
            params['mobile'] = mobile
            count = Member.Q.filter(Member.id != user_id).filter(
                Member.mobile == mobile).count()
            if count > 0:
                return self.error('电话号码已被占用')
        if email:
            params['email'] = email
            count = Member.Q.filter(Member.id != user_id).filter(
                Member.email == email).count()
            if count > 0:
                return self.error('Email已被占用')

        if sex:
            params['sex'] = sex
        if sign is not None:
            params['sign'] = sign

        if avatar and file_md5:
            params['avatar'] = avatar
            member = Member.Q.filter(Member.id == user_id).first()

            if avatar != member.avatar:
                Attach.remove_avatar(user_id, member.avatar)

            query = "REPLACE INTO `sys_attach_related` (`file_md5`, `related_table`, `related_id`, `ip`, `utc_created_at`) VALUES ('%s', '%s', '%d', '%s', '%s')" % (
                file_md5,
                'member',
                user_id,
                self.request.remote_ip,
                str(Func.utc_now())[0:-6],
            )
            # print('query ', query )
            Member.session.execute(query)
        Member.Q.filter(Member.id == user_id).update(params)
        Member.session.commit()

        # 设置登录用户cookie信息
        member = Member.Q.filter(Member.id == user_id).first()
        self.set_curent_user(member)

        return self.success()
Пример #22
0
class User(BaseModel):
    """
    user model
    """
    __tablename__ = 'sys_admin_user'

    uuid = Column(String(32),
                  primary_key=True,
                  nullable=False,
                  default=Func.uuid32())
    role_id = Column(String(32), ForeignKey('sys_admin_role.uuid'))
    password = Column(String(128), nullable=False, default='')
    username = Column(String(40), nullable=False)
    mobile = Column(String(11), nullable=True)
    email = Column(String(80), nullable=True)
    permission = Column(Text, default='')
    login_count = Column(Integer, nullable=False, default=0)
    last_login_ip = Column(String(128), nullable=False, default='')
    # 用户状态:(0 锁定, 1正常, 默认1)
    status = Column(Integer, nullable=False, default=1)
    utc_last_login_at = Column(TIMESTAMP, nullable=True)
    utc_created_at = Column(TIMESTAMP, default=Func.utc_now)

    @property
    def last_login_at(self):
        return Func.dt_to_timezone(self.utc_last_login_at)

    @property
    def created_at(self):
        return Func.dt_to_timezone(self.utc_created_at)

    @property
    def role_permission(self):
        query = "select permission from sys_admin_role where uuid='%s'" % self.role_id
        permission = User.session.execute(query).scalar()
        try:
            return json.loads(permission)
        except Exception as e:
            raise e
        return []

    @classmethod
    def get_permission(cls):
        try:
            return json.loads(cls.permission)
        except Exception as e:
            raise e
        return []

    @staticmethod
    def login_success(user, handler):
        # 设置登录用户cookie信息
        handler.set_curent_user(user)

        user_id = user.uuid
        login_count = user.login_count if user.login_count else 0
        params = {
            'login_count': login_count + 1,
            'utc_last_login_at': Func.utc_now(),
            'last_login_ip': handler.request.remote_ip,
        }
        User.Q.filter(User.uuid == user_id).update(params)

        params = {
            'uuid': Func.uuid32(),
            'user_id': user.uuid,
            'client': 'web',
            'ip': handler.request.remote_ip,
        }
        log = UserLoginLog(**params)
        UserLoginLog.session.add(log)
        UserLoginLog.session.commit()
        return True
Пример #23
0
class AdminMenu(BaseModel):
    """
    user group map model
    """
    __tablename__ = 'sys_admin_menu'

    uuid = Column(String(32),
                  primary_key=True,
                  nullable=False,
                  default=Func.uuid32())
    user_id = Column(String(32),
                     ForeignKey('sys_admin_user.uuid'),
                     nullable=False,
                     default='')
    parent_id = Column(String(32), nullable=False, default='top')
    code = Column(String(64), nullable=True)
    title = Column(String(20), nullable=False)
    icon = Column(String(20), nullable=False)
    path = Column(String(200), nullable=False)
    param = Column(String(200), nullable=False)
    target = Column(String(20), nullable=False, default='_self')
    nav = Column(Integer, nullable=False)
    sort = Column(Integer, nullable=False, default=20)
    system = Column(Integer, nullable=False)
    status = Column(Integer, nullable=False)
    utc_created_at = Column(TIMESTAMP, default=Func.utc_now)

    @property
    def created_at(self):
        return Func.dt_to_timezone(self.utc_created_at)

    @classmethod
    def info(cls, uuid=None, path=None):
        """获取当前访问节点信息

        [description]

        Keyword Arguments:
            uuid {str} -- [description] (default: {''})

        Returns:
            [type] -- [description]
        """
        query = cls.session.query(AdminMenu)
        if uuid:
            query = query.filter(AdminMenu.uuid == uuid)
        if path:
            path = path.split('?')[0]
            if path[-1:] == '/':
                path = path[0:-1]
            if path[-5:] == '.html':
                path = path[0:-5]

            query = query.filter(AdminMenu.path == path)

        row = query.first()
        row = row.as_dict() if row else None
        # SysLogger.debug(query.statement)
        return row

    @classmethod
    def brand_crumbs(cls, uuid):
        """获取当前节点的面包屑

        [description]

        Arguments:
            uuid {[type]} -- [description]

        Returns:
            [type] -- [description]
        """
        menu = []
        row = cls.info(uuid=uuid)
        if row['parent_id'] != '' and row['parent_id'] != 'top':
            menu.append(row)
            child = cls.brand_crumbs(row['parent_id'])
            if len(child):
                menu.extend(child)
        return menu

    @classmethod
    def main_menu(cls, parent_id='top', status=1, level=0):
        """获取后台主菜单(一级 > 二级 > 三级)
            后台顶部和左侧使用

            Keyword Arguments:
                parent_id {str} -- 父ID (default: {'0'})
                level {number} -- 层级数 (default: {0})
            Returns:
                [type] -- [description]
        """
        trees = []
        if not len(trees):
            filds = [
                'uuid', 'code', 'parent_id', 'title', 'path', 'param',
                'target', 'icon'
            ]
            query = cls.session.query(AdminMenu)
            if status is not None:
                query = query.filter(AdminMenu.status == status)
            query = query.filter(AdminMenu.nav == 1)
            rows = query.order_by(AdminMenu.sort.asc()).all()
            # print('query.statement: ', query.statement)
            for row in rows:
                row = row.as_dict(filds)
                if row.get('parent_id') != parent_id:
                    continue

                if level == 5:
                    return trees

                # 过滤没访问权限的节点
                # if (!RoleModel::checkAuth($v['id'])) {
                #     unset($data[$k]);
                #     continue;
                # }
                row['children'] = cls.main_menu(row.get('uuid'), status,
                                                level + 1)
                trees.append(row)
        return trees

    @staticmethod
    def children(parent_id='top', status=None, level=0, user_id=''):
        """获取指定节点下的所有子节点(不含快捷收藏的菜单)
        """
        trees = []
        if not len(trees):
            filds = [
                'uuid', 'code', 'parent_id', 'title', 'path', 'param',
                'target', 'icon', 'sort', 'status'
            ]
            query = AdminMenu.session.query(AdminMenu)
            if user_id:
                query = query.filter(AdminMenu.user_id == user_id)
            query = query.filter(AdminMenu.parent_id == parent_id)
            if status in [1, 0]:
                query = query.filter(AdminMenu.status == status)
            rows = query.order_by(AdminMenu.sort.asc()).all()
            data = []
            for row in rows:
                if level == 5:
                    return trees
                row = row.as_dict(filds)

                # 过滤没访问权限的节点
                # if (!RoleModel::checkAuth($v['id'])) {
                #     unset($data[$k]);
                #     continue;
                # }
                row['children'] = AdminMenu.children(row.get('uuid'), status,
                                                     level + 1)
                trees.append(row)
        return trees

    @staticmethod
    def menu_option(uuid=''):
        """菜单选项"""
        menus = AdminMenu.main_menu(status=None)
        if not len(menus) > 0:
            return ''
        option1 = '<option level="1" value="%s" %s>— %s</option>'
        option2 = '<option level="2" value="%s" %s>—— %s</option>'
        option3 = '<option level="3" value="%s" %s>——— %s</option>'
        html = ''
        for menu in menus:
            selected = 'selected' if uuid == menu.get('uuid', '') else ''
            title1 = menu.get('title', '')
            children1 = menu.get('children', [])
            html += option1 % (menu.get('uuid', ''), selected, title1)
            if not len(children1) > 0:
                continue
            for menu2 in children1:
                selected2 = 'selected' if uuid == menu2.get('uuid', '') else ''
                title2 = menu2.get('title', '')
                children2 = menu2.get('children', [])
                html += option2 % (menu2.get('uuid', ''), selected2, title2)
                if not len(children2) > 0:
                    continue
                for menu3 in children2:
                    selected3 = 'selected' if uuid == menu3.get('uuid',
                                                                '') else ''
                    title3 = menu3.get('title', '')
                    html += option3 % (menu3.get('uuid',
                                                 ''), selected3, title3)
        return html
Пример #24
0
    def post(self, *args, **kwargs):
        """重置密码
        """
        token = self.get_argument('token', None)
        next = self.get_argument('next', '')
        password = self.get_argument('password', None)
        repass = self.get_argument('repass', '')
        rsa_encrypt = self.get_argument('rsa_encrypt', 0)

        token2 = self.get_secure_cookie(self.token_key)
        if not (token and token2):
            return self.error('Token不存在或已经过期')

        token2 = str(token2, encoding='utf-8')
        token2 = token2.replace('\'', '"')
        token2 = json_decode(token2)

        action = token2.get('action', '')
        account = token2.get('account', '')
        # print('token2 ', token2.get('token', ''), token)
        if token2.get('token', '') != token:
            return self.error('Token不匹配')

        if not password:
            return self.error('新密码不能为空')

        if settings.login_pwd_rsa_encrypt and int(
                rsa_encrypt) == 1 and len(password) > 10:
            private_key = sys_config('sys_login_rsa_priv_key')
            password = RSAEncrypter.decrypt(password, private_key)
            repass = RSAEncrypter.decrypt(repass, private_key)

        if repass != password:
            msg = '两次输入的密码不一致,请重新输入'
            msg = "%s, %s" % (password, repass)
            return self.error(msg)

        member = None
        if action == 'email_reset_pwd':
            member = Member.Q.filter(Member.email == account).first()
        else:
            return self.error('不支持的action')

        if member is None:
            return self.error('用户不存在')

        if int(member.status) == 0:
            return self.error('用户被“禁用”,请联系客服')
        user_id = member.uuid
        params = {
            'password': make_password(password),
        }
        Member.Q.filter(Member.uuid == user_id).update(params)
        Member.session.commit()

        params = {
            'uuid': Func.uuid32(),
            'user_id': user_id,
            'account': account,
            'action': 'email_reset_pwd',
            'ip': self.request.remote_ip,
            'client': 'web',
        }
        MemberOperationLog.add_log(params)

        self.clear_cookie(self.token_key)
        return self.success(next=next)
Пример #25
0
 def read_at(self):
     return Func.dt_to_timezone(self.utc_read_at)
Пример #26
0
    def post(self, *args, **kwargs):
        next = self.get_argument('next', '')
        email = self.get_argument('email', None)
        mobile = self.get_argument('mobile', None)
        username = self.get_argument('username', None)
        sex = self.get_argument('sex', None)
        password = self.get_argument('password', None)
        repass = self.get_argument('repass', '')
        rsa_encrypt = self.get_argument('rsa_encrypt', 0)
        ref_user_id = self.get_argument('ref_user_id', '')
        code = self.get_argument('code', '')
        _ = self.locale.translate

        if self.invalid_img_captcha(code):
            return self.error(_('验证码错误'))

        if not email:
            return self.error('Email不能为空')

        if not password:
            return self.error('密码不能为空')

        if settings.login_pwd_rsa_encrypt and int(
                rsa_encrypt) == 1 and len(password) > 10:
            private_key = sys_config('sys_login_rsa_priv_key')
            password = RSAEncrypter.decrypt(password, private_key)
            repass = RSAEncrypter.decrypt(repass, private_key)

        if repass != password:
            msg = '两次输入的密码不一致,请重新输入'
            msg = "%s, %s" % (password, repass)
            return self.error(msg)

        count = Member.Q.filter(Member.username == username).count()
        if count > 0:
            return self.error('用户名已被占用')

        client = 'web'
        params = {
            'username': username,
            'password': make_password(password),
            'status': 1,
            'avatar': 'image/default_avatar.jpg',
            'register_ip': self.request.remote_ip,
            'register_client': client,
        }
        if email:
            params['email'] = email
            count = Member.Q.filter(Member.email == email).count()
            if count > 0:
                return self.error('Email已被占用')
        if mobile:
            if not Func.mobile(mobile):
                return self.error('电话号码格式有误')
            params['mobile'] = mobile
            count = Member.Q.filter(User.mobile == mobile).count()
            if count > 0:
                return self.error('电话号码已被占用')
        if sex:
            params['sex'] = sex
        if ref_user_id:
            params['ref_user_id'] = ref_user_id

        (code, member) = Member.register(params)
        if code == 0:
            Member.login_success(member, self, client=client)
            return self.success(next=next)
        else:
            return self.error(member)
Пример #27
0
 def updated_at(self):
     return Func.dt_to_timezone(self.utc_updated_at)