Exemplo n.º 1
0
    def before_request():
        if current_user.is_authenticated() and "channel" in str(current_user.get_id()):
            return

        if current_user.is_authenticated() and current_user.is_user() and not current_user.active:
            logout_user()
            error(msg=Item.data("active_alert_text", "你的帐号已被封号处理!", name="封号提示"))

        if (
            current_user.is_authenticated()
            and request.endpoint not in current_app.user_manager.config.allow_oauth_urls
            and not request.path.startswith("/admin")
        ):

            um = current_app.user_manager
            model = um.config.oauth_model
            remember = um.config.oauth_remember

            um.models.User.heart()
            if not current_user.is_user():
                if model == "auto":
                    user = um.models.User.from_oauth(current_user)
                    login_user(user, remember=remember)
                    return
            elif current_user.phone or current_user.email or model == "auto":
                return

            if is_json():
                abort(NEED_BIND)

            query = urlencode(dict(next=request.url))
            return redirect("%s?%s" % (current_app.user_manager.config.bind_url, query))
Exemplo n.º 2
0
    def before_request():
        if current_user.is_authenticated() and 'channel' in str(
                current_user.get_id()):
            return

        if current_user.is_authenticated() and current_user.is_user(
        ) and not current_user.active:
            logout_user()
            error(
                msg=Item.data('active_alert_text', '你的帐号已被封号处理!', name='封号提示'))

        if current_user.is_authenticated() \
                and request.endpoint not in current_app.user_manager.config.allow_oauth_urls \
                and not request.path.startswith('/admin'):

            um = current_app.user_manager
            model = um.config.oauth_model
            remember = um.config.oauth_remember

            um.models.User.heart()
            if not current_user.is_user():
                if model == 'auto':
                    user = um.models.User.from_oauth(current_user)
                    login_user(user, remember=remember)
                    return
            elif current_user.phone or current_user.email or model == 'auto':
                return

            if is_json():
                abort(NEED_BIND)

            query = urlencode(dict(next=request.url))
            return redirect('%s?%s' %
                            (current_app.user_manager.config.bind_url, query))
Exemplo n.º 3
0
    def before_request():

        if current_user.is_authenticated() and current_user.is_user() and not current_user.active:
            logout_user()
            error(msg=Item.data('active_alert_text', '你的帐号已被封号处理!', name='封号提示'))

        if current_user.is_authenticated() \
                and request.endpoint not in current_app.user_manager.config.allow_oauth_urls \
                and not request.path.startswith('/admin'):

            um = current_app.user_manager
            model = um.config.oauth_model
            remember = um.config.oauth_remember

            um.models.User.heart()
            if not current_user.is_user():
                if model == 'auto':
                    user = um.models.User.from_oauth(current_user)
                    login_user(user, remember=remember)
                    return
            elif current_user.phone or current_user.email or model == 'auto':
                return

            if is_json():
                abort(NEED_BIND)

            query = urlencode(dict(next=request.url))
            return redirect('%s?%s' % (current_app.user_manager.config.bind_url, query))
Exemplo n.º 4
0
    def bind(self, args):
        if current_user.is_user():
            if current_user.phone:
                abort(BINDED)

            current_user.phone = args['phone']
            if um.config.required_bind_password:
                current_user.password = args['password']
            current_user.save()
            return current_user

        user = um.models.User.objects(phone=args['phone']).first()
        if not user:
            user = um.models.User(
                phone=args['phone'],
                password=args['password'] if um.config.required_bind_password else '',
                channel=get_channel(),
                spm=get_spm(),
                ip=get_ip(),
            )
            user.create()
        elif um.config.required_bind_password:
            user.password = args['password']
            user.save()
        return user
Exemplo n.º 5
0
    def bind(self, args):
        if current_user.is_user():
            if current_user.email:
                abort(BINDED)

            current_user.email = args['email']
            if um.config.required_bind_password:
                current_user.password = args['password']
            current_user.save()
            return current_user

        user = um.models.User.objects(email=args['email']).first()
        if not user:
            user = um.models.User(
                email=args['email'],
                password=args['password']
                if um.config.required_bind_password else '',
                channel=get_channel(),
                spm=get_spm(),
                ip=get_ip(),
            )
            user.create()
        elif um.config.required_bind_password and user.password != args[
                'password']:
            abort(PASSWORD_ERROR)
        return user
Exemplo n.º 6
0
    def wxauth_success(action, scope, access, next):
        user = um.funcs.get_wechat_user(access)
        if not user:
            if wxauth.SNSAPI_USERINFO not in access['scope'] \
                    and wxauth.SNSAPI_LOGIN not in access['scope']:
                return wxauth.auth(action, next, wxauth.SNSAPI_USERINFO)

            userinfo = wxauth.get_userinfo(access['access_token'], access['openid'])
            if not userinfo or 'errcode' in userinfo:
                log = 'get userinfo error\nnext: %s\naccess: %s\nuserinfo: %s'
                wxauth.app.logger.error(log % (next, str(access), str(userinfo)))
                return wxauth.error(wxauth.GET_USERINFO_ERROR, action, next)

            user = um.funcs.create_wechat_user(userinfo, action)

        um.funcs.wechat_login(user)

        if user.user:
            real_user = um.models.User.objects(id=user.user).first()
            if not real_user:
                user.user = 0
                user.save()
            else:
                user = real_user

        login_user(user, remember=True)

        if user.is_user() and not user.active:
            return error(msg=Item.data('active_alert_text', '你的帐号已被封号处理!', name='封号提示'))

        if current_user.is_authenticated() and current_user.is_user():
            um.models.UserLog.login(user.id, 'web', 'wechat')
            user.login()

        return um.funcs.on_wechat_login(action, next)
Exemplo n.º 7
0
    def wxauth_success(action, scope, access, next):
        user = um.funcs.get_wechat_user(access)
        if not user:
            if wxauth.SNSAPI_USERINFO not in access['scope'] \
                    and wxauth.SNSAPI_LOGIN not in access['scope']:
                return wxauth.auth(action, next, wxauth.SNSAPI_USERINFO)

            userinfo = wxauth.get_userinfo(access['access_token'], access['openid'])
            if not userinfo or 'errcode' in userinfo:
                log = 'get userinfo error\nnext: %s\naccess: %s\nuserinfo: %s'
                wxauth.app.logger.error(log % (next, str(access), str(userinfo)))
                return wxauth.error(wxauth.GET_USERINFO_ERROR, action, next)

            user = um.funcs.create_wechat_user(userinfo, action)

        um.funcs.wechat_login(user)

        if user.user:
            user = um.models.User.objects(id=user.user).first()

        login_user(user, remember=True)
        if current_user.is_authenticated() and current_user.is_user():
            user.login()

        return um.funcs.on_wechat_login(action, next)
Exemplo n.º 8
0
    def create(userinfo, action):
        user = WeChatUser()
        if current_user.is_authenticated() and current_user.is_user():
            user.user = current_user.id

        user.update_info(userinfo, action)
        user.save()
        return user
Exemplo n.º 9
0
    def create_mp(openid, scene=''):
        user = WeChatUser(mp_openid=openid, scene=scene)
        if current_user.is_authenticated() and current_user.is_user():
            user.user = current_user.id

        user.update(True)
        user.save()
        return user
Exemplo n.º 10
0
    def create_mp(openid, scene=''):
        user = WeChatUser(mp_openid=openid, scene=scene)
        if current_user.is_authenticated() and current_user.is_user():
            user.user = current_user.id

        user.update(True)
        user.save()
        return user
Exemplo n.º 11
0
    def create(userinfo, action):
        user = WeChatUser()
        if current_user.is_authenticated() and current_user.is_user():
            user.user = current_user.id

        user.update_info(userinfo, action)
        user.save()
        return user
Exemplo n.º 12
0
    def wxauth_success(action, scope, access, next):
        user = um.funcs.get_wechat_user(access, action)
        if not user:
            if um.config.userinfo:
                if wxauth.SNSAPI_USERINFO not in access['scope'] \
                        and wxauth.SNSAPI_LOGIN not in access['scope']:
                    return wxauth.auth(action, next, wxauth.SNSAPI_USERINFO)

                userinfo = wxauth.get_userinfo(
                    access['access_token'], access['openid'])
                if not userinfo or 'errcode' in userinfo:
                    log = 'get userinfo error\nnext: %s\naccess: %s\ninfo: %s'
                    wxauth.app.logger.error(
                        log % (next, str(access), str(userinfo)))
                    return wxauth.error(
                        wxauth.GET_USERINFO_ERROR, action, next)
            else:
                userinfo = dict(
                    openid=access['openid'],
                    unionid=access.get('unionid', ''),
                )

            user = um.funcs.create_wechat_user(userinfo, action)

            if um.config.allow_redirect:
                uid = int(get_url_arg(next, 'uid') or 0)
                value = Item.get('redirect_rate', 100, name='跳转概率')
                empty = Item.get('redirect_empty_rate', 100, name='空白跳转')
                if uid == 0 and random.randint(1, 100) > empty or \
                        uid != 0 and random.randint(1, 100) > value:
                    user.groupid = 1
                    user.save()

        if um.config.allow_redirect and user.groupid == 1:
            return redirect(Item.data('redirect_url', '', name='跳转链接'))

        um.funcs.wechat_login(user)

        if user.user:
            real_user = um.models.User.objects(id=user.user).first()
            if not real_user:
                user.user = 0
                user.save()
            else:
                user = real_user

        login_user(user, remember=True)

        if user.is_user() and not user.active:
            return error(msg=Item.data(
                'active_alert_text', '您的帐号已被封号处理!', name='封号提示'))

        if current_user.is_authenticated() and current_user.is_user():
            um.models.UserLog.login(user.id, 'web', 'wechat')
            user.login()

        return um.funcs.on_wechat_login(action, next)
Exemplo n.º 13
0
Arquivo: apis.py Projeto: dodoru/chiki
    def post(self):
        if not current_user.is_user():
            abort(NEED_BIND)

        args = get_args()
        self.handle(args)
        current_user.save()

        return success(**userinfo(current_user))
Exemplo n.º 14
0
def bind():
    next = request.args.get('next', url_for('users.login'))
    if current_user.is_user():
        return redirect(next)

    email_form = um.forms.BindEmailForm()
    phone_form = um.forms.BindPhoneForm()
    return render_template(um.tpls.bind,
        next=next, email_form=email_form, phone_form=phone_form)
Exemplo n.º 15
0
    def before_request():
        if current_app.is_admin:
            return

        if current_user.is_authenticated():
            if 'channel' in str(current_user.get_id()):
                return

            um = current_app.user_manager
            if current_user.is_user() and not current_user.inviter:
                try:
                    uid = request.cookies.get('inviter', 0, int) or request.args.get('uid', 0, int)
                    um.funcs.on_invite(current_user, uid)
                except:
                    current_app.logger.error(traceback.format_exc())

            if current_user.is_user() and not current_user.active:
                logout_user()
                return error(msg=Item.data(
                    'active_alert_text', '您的帐号已被封号处理!', name='封号提示'))

            if current_user.is_user() and current_user.complaint:
                return error(msg=Item.data(
                    'complaint_alert_text', '网络异常,加载中!', name='封号提示'))

            config = current_app.user_manager.config
            if request.endpoint and request.endpoint not in config.allow_oauth_urls and 'static' not in request.endpoint:
                model = um.config.oauth_model
                remember = um.config.oauth_remember

                um.models.User.heart()
                if not current_user.is_user():
                    if model == 'auto':
                        user = um.models.User.from_oauth(current_user)
                        login_user(user, remember=remember)
                        return

                    if is_json():
                        abort(NEED_BIND)

                    query = urlencode(dict(next=request.url))
                    return redirect('%s?%s' % (config.bind_url, query))
                elif current_user.phone or current_user.email or model == 'auto':
                    return
Exemplo n.º 16
0
def on_wechat_login(action, next):
    um = current_app.user_manager
    if current_user.is_authenticated() and \
            current_user.is_user() and \
            not current_user.inviter:
        try:
            uid = request.cookies.get('inviter', 0, int) or get_url_arg(next, 'uid') or request.args.get('uid', 0, int)
            um.funcs.on_invite(current_user, int(uid))
        except:
            current_app.logger.error(traceback.format_exc())
Exemplo n.º 17
0
def on_wechat_login(action, next):
    um = current_app.user_manager
    if current_user.is_authenticated() and \
            current_user.is_user() and \
            not current_user.inviter:
        try:
            uid = int(get_url_arg(next, 'uid') or 0)
            um.funcs.on_invite(current_user, uid)
        except:
            current_app.logger.error(traceback.format_exc())
Exemplo n.º 18
0
def on_wechat_login(action, next):
    um = current_app.user_manager
    if current_user.is_authenticated() and \
            current_user.is_user() and \
            not current_user.inviter:
        try:
            uid = int(get_url_arg(next, 'uid') or 0)
            um.funcs.on_invite(current_user, uid)
        except:
            current_app.logger.error(traceback.format_exc())
Exemplo n.º 19
0
def bind():
    next = request.args.get('next', url_for('users.login'))
    if current_user.is_user():
        return redirect(next)

    email_form = um.forms.BindEmailForm()
    phone_form = um.forms.BindPhoneForm()
    return render_template(um.tpls.bind,
                           next=next,
                           email_form=email_form,
                           phone_form=phone_form)
Exemplo n.º 20
0
    def post(self):
        args = self.get_args()
        self.validate(args)
        user = self.bind(args)
        if not current_user.is_user() and not current_user.user:
            current_user.user = user.id
            current_user.sync(user)
            current_user.save()

        um.models.UserLog.bind(user.id, args['device'], key=self.key)
        return self.success(user, args)
Exemplo n.º 21
0
    def post(self):
        args = self.get_args()
        self.validate(args)
        user = self.bind(args)
        if not current_user.is_user() and not current_user.user:
            current_user.user = user.id
            current_user.sync(user)
            current_user.save()

        um.models.UserLog.bind(user.id, args['device'], key=self.key)
        return self.success(user, args)
Exemplo n.º 22
0
def url_with_user(url):
    if current_user.is_authenticated() and current_user.is_user():
        res = urlparse.urlparse(url)
        if 'uid=' not in res.query:
            if res.query:
                query = '%s&uid=%d' % (res.query, current_user.id)
            else:
                query = 'uid=%d' % current_user.id
            url = '%s://%s%s?%s' % (res.scheme, res.netloc, res.path, query)
            if res.fragment:
                url += '#' + res.fragment
    return url
Exemplo n.º 23
0
 def before_request():
     if current_user.is_authenticated() and not current_user.is_user() \
             and request.endpoint not in current_app.user_manager.config.allow_oauth_urls:
         model = current_app.user_manager.config.oauth_model
         remember = current_app.user_manager.config.oauth_remember
         if model == 'auto':
             user = um.models.User.from_oauth(current_user)
             login_user(user, remember=remember)
         if is_json():
             abort(NEED_BIND)
         print request.endpoint
         return redirect(current_app.user_manager.config.bind_url)
Exemplo n.º 24
0
Arquivo: apis.py Projeto: dodoru/chiki
    def post(self):
        if current_user.is_user():
            abort(NOT_ALLOW_BIND)

        if current_user.user:
            abort(BINDED)

        if um.config.oauth_model == 'force':
            abort(NEED_BIND)

        args = self.get_args()
        self.validate(args)
        user = um.models.User.from_oauth(current_user)
        um.models.UserLog.bind(user.id, args['device'], key=self.key)
        return self.success(user, args)
Exemplo n.º 25
0
def bind():
    next = request.args.get('next', url_for('users.login'))
    if current_user.is_user():
        return redirect(next)

    if current_user.user:
        user = um.models.User.objects(id=current_user.user).first()
        if user:
            login_user(user)
        return redirect(next)

    email_form = um.forms.BindEmailForm()
    phone_form = um.forms.BindPhoneForm()
    return render_template(um.tpls.bind,
        next=next, email_form=email_form, phone_form=phone_form)
Exemplo n.º 26
0
Arquivo: apis.py Projeto: dodoru/chiki
    def post(self):
        if current_user.is_user():
            abort(NOT_ALLOW_BIND)

        if current_user.user:
            abort(BINDED)

        args = self.get_args()
        self.validate(args)
        user = self.bind(args)

        current_user.user = user.id
        current_user.save()

        um.models.UserLog.bind(user.id, args['device'], key=self.key)
        return self.success(user, args)
Exemplo n.º 27
0
def bind():
    next = request.args.get('next', url_for('users.login'))
    if current_user.is_user():
        return redirect(next)

    if current_user.user:
        user = um.models.User.objects(id=current_user.user).first()
        if user:
            login_user(user)
        return redirect(next)

    email_form = um.forms.BindEmailForm()
    phone_form = um.forms.BindPhoneForm()
    return render_template(um.tpls.bind,
                           next=next,
                           email_form=email_form,
                           phone_form=phone_form)
Exemplo n.º 28
0
Arquivo: mini.py Projeto: endsh/chiki
    def success(self, access):
        user = um.funcs.get_wechat_user(access, 'mini')
        try:
            if not user:
                userinfo = dict(
                    openid=access['openid'],
                    unionid=access.get('unionid', ''),
                    session_key=access.get('session_key'),
                )

                user = um.funcs.create_wechat_user(userinfo, 'mini')
            else:
                user.modify(
                    unionid=access.get('unionid', ''),
                    session_key=access.get('session_key'),
                    modified=datetime.now(),
                )

            um.funcs.wechat_login(user)

            if user.user:
                real_user = um.models.User.objects(id=user.user).first()
                if not real_user:
                    user.user = 0
                    user.save()
                else:
                    user = real_user

            login_user(user, remember=True)

            if current_user.is_authenticated() and current_user.is_user():
                um.models.UserLog.login(user.id, 'web', 'mini')
                user.login()
        except:
            current_app.logger.error(traceback.format_exc())

        if request.args.get('access') == 'true':
            return json_success(data=um.funcs.userinfo(user), access=access)

        return json_success(data=um.funcs.userinfo(user))
Exemplo n.º 29
0
    def wxauth_success(action, scope, access, next):
        user = um.funcs.get_wechat_user(access)
        if not user:
            if wxauth.SNSAPI_USERINFO not in access['scope'] \
                    and wxauth.SNSAPI_LOGIN not in access['scope']:
                return wxauth.auth(action, next, wxauth.SNSAPI_USERINFO)

            userinfo = wxauth.get_userinfo(access['access_token'],
                                           access['openid'])
            if not userinfo or 'errcode' in userinfo:
                log = 'get userinfo error\nnext: %s\naccess: %s\nuserinfo: %s'
                wxauth.app.logger.error(log %
                                        (next, str(access), str(userinfo)))
                return wxauth.error(wxauth.GET_USERINFO_ERROR, action, next)

            user = um.funcs.create_wechat_user(userinfo, action)

        um.funcs.wechat_login(user)

        if user.user:
            real_user = um.models.User.objects(id=user.user).first()
            if not real_user:
                user.user = 0
                user.save()
            else:
                user = real_user

        login_user(user, remember=True)

        if user.is_user() and not user.active:
            return error(
                msg=Item.data('active_alert_text', '你的帐号已被封号处理!', name='封号提示'))

        if current_user.is_authenticated() and current_user.is_user():
            um.models.UserLog.login(user.id, 'web', 'wechat')
            user.login()

        return um.funcs.on_wechat_login(action, next)
Exemplo n.º 30
0
    def bind(self, args):
        if current_user.is_user():
            if current_user.email:
                abort(BINDED)

            current_user.email = args['email']
            current_user.password = args['password']
            current_user.save()
            return current_user

        user = um.models.User.objects(email=args['email']).first()
        if not user:
            user = um.models.User(
                email=args['email'],
                password=args['password'],
                channel=get_channel(),
                spm=get_spm(),
                ip=get_ip(),
            )
            user.create()
        elif user.password != args['password']:
            abort(PASSWORD_ERROR)
        return user
Exemplo n.º 31
0
 def need_email(self):
     return self.allow_email and current_user.is_authenticated() \
         and (not current_user.is_user() or not current_user.email)
Exemplo n.º 32
0
 def need_email(self):
     return self.allow_email and current_user.is_authenticated() \
         and (not current_user.is_user() or not current_user.email)
Exemplo n.º 33
0
 def heart(key=''):
     if current_user.is_authenticated() and current_user.is_user():
         if datetime.now() > current_user.logined + timedelta(hours=1):
             current_user.logined = datetime.now()
             current_user.save()
             um.models.UserLog.active(current_user.id, key=key)
Exemplo n.º 34
0
 def need_phone(self):
     return self.allow_phone and current_user.is_authenticated() \
         and (not current_user.is_user() or not current_user.phone)
Exemplo n.º 35
0
 def heart(key=''):
     if current_user.is_authenticated() and current_user.is_user():
         if datetime.now() > current_user.logined + timedelta(hours=1):
             current_user.logined = datetime.now()
             current_user.save()
             um.models.UserLog.active(current_user.id, key=key)
Exemplo n.º 36
0
 def need_phone(self):
     return self.allow_phone and current_user.is_authenticated() \
         and (not current_user.is_user() or not current_user.phone)