Beispiel #1
0
    def test_can_vote(self):

        assert not self.post.permissions.vote.allows(AnonymousIdentity())

        identity = Identity(self.user.id)
        identity.provides.update(self.user.provides)
        assert not self.post.permissions.vote.allows(identity)

        user = User(username="******", email="*****@*****.**")

        db.session.add(user)
        db.session.commit()

        identity = Identity(user.id)
        identity.provides.update(user.provides)

        assert self.post.permissions.vote.allows(identity)

        votes = self.post.votes
        votes.add(user.id)
        self.post.votes = votes

        del self.post.permissions

        assert not self.post.permissions.vote.allows(identity)
Beispiel #2
0
    def test_can_delete(self):

        assert not self.comment.permissions.delete.allows(AnonymousIdentity())

        identity = Identity(self.user.id)
        identity.provides.update(self.user.provides)
        assert self.comment.permissions.delete.allows(identity)

        user = User(username="******", email="*****@*****.**")

        db.session.add(user)
        db.session.commit()

        identity = Identity(user.id)
        assert not self.comment.permissions.delete.allows(identity)

        user.role = User.MODERATOR

        identity.provides.update(user.provides)
        assert self.comment.permissions.delete.allows(identity)

        user.role = User.ADMIN
        del user.provides

        identity.provides.update(user.provides)
        assert self.comment.permissions.delete.allows(identity)
Beispiel #3
0
    def test_can_receive_mail(self):

        user = User(username="******", email="*****@*****.**")

        db.session.add(user)

        assert user.get_following().count() == 0

        user2 = User(username="******", email="*****@*****.**")

        db.session.add(user2)

        db.session.commit()

        id1 = Identity(user.id)
        id2 = Identity(user2.id)

        id1.provides.update(user.provides)
        id2.provides.update(user2.provides)

        assert not user.permissions.send_message.allows(id2)
        assert not user2.permissions.send_message.allows(id1)

        user.follow(user2)

        db.session.commit()

        del user.permissions
        del user2.permissions

        assert not user.permissions.send_message.allows(id2)
        assert not user2.permissions.send_message.allows(id1)

        user2.follow(user)
        user.receive_email = True

        del user.permissions
        del user2.permissions

        assert user.permissions.send_message.allows(id2)
        assert not user2.permissions.send_message.allows(id1)

        user2.receive_email = True

        del user.permissions
        del user2.permissions

        assert user.permissions.send_message.allows(id2)
        assert user2.permissions.send_message.allows(id1)

        user.unfollow(user2)

        del user.permissions
        del user2.permissions

        assert not user.permissions.send_message.allows(id2)
        assert not user2.permissions.send_message.allows(id1)
Beispiel #4
0
def signup():

    if 'openid' not in session:
        abort(403)

    form = OpenIdSignupForm(next=request.args.get("next"),
                            username=request.args.get("name"),
                            email=request.args.get("email"))

    if form.validate_on_submit():

        user = User(openid=session.pop('openid'))
        form.populate_obj(user)

        db.session.add(user)
        db.session.commit()

        session.permanent = True

        identity_changed.send(current_app._get_current_object(),
                              identity=Identity(user.id))

        flash(_("Welcome, %%s") % user.username, "success")

        next_url = form.next.data or \
            url_for("user.posts", username=user.username)

        return redirect(next_url)

    return render_template("openid/signup.html", form=form)
Beispiel #5
0
def login():

    form = LoginForm(login=request.args.get('login', None),
                     next=request.args.get('next', None))

    if form.validate_on_submit():

        user, authenticated = User.query.authenticate(form.login.data,
                                                      form.password.data)

        if user and authenticated:
            session.permanent = form.remember.data

            identity_changed.send(current_app._get_current_object(),
                                  identity=Identity(user.id))

            flash(_("Welcome back, %(name)s", name=user.username), "success")

            next_url = form.next.data

            if not next_url or next_url == request.path:
                next_url = url_for('frontend.people', username=user.username)

            return redirect(next_url)

        else:

            flash(_("Sorry, invalid login"), "error")

    return render_template("account/login.html", form=form)
Beispiel #6
0
def signup():

    form = SignupForm(next=request.args.get('next', None))

    if form.validate_on_submit():

        code = UserCode.query.filter_by(code=form.code.data).first()

        if code:
            user = User(role=code.role)
            form.populate_obj(user)

            db.session.add(user)
            db.session.delete(code)
            db.session.commit()

            identity_changed.send(current_app._get_current_object(),
                                  identity=Identity(user.id))

            flash(_("Welcome, %(name)s", name=user.nickname), "success")

            next_url = form.next.data

            if not next_url or next_url == request.path:
                next_url = url_for('frontend.people', username=user.username)

            return redirect(next_url)
        else:
            form.code.errors.append(_("Code is not allowed"))

    return render_template("account/signup.html", form=form)
Beispiel #7
0
def login():
    form = LoginForm(login=request.args.get("login", None),
                     next=request.args.get("next", None))

    # TBD: ensure "next" field is passed properly
    if form.validate_on_submit():
        user, authenticated = \
            User.query.authenticate(form.login.data,
                                    form.password.data)

        if user and authenticated:
            # Flask-Login
            login_user(user, remember=form.remember.data)

            # change identity
            identity = Identity(user.username)
            identity_changed.send(current_app._get_current_object(),
                                  identity=identity)
            # next_url
            next_url = form.next.data
            if not next_url or next_url == 'home.index':
                next_url = url_for('user.posts', username=user.username)
                flash(u"登录成功", "successfully")
            return redirect(next_url)
        else:
            flash(u"账号或密码错误", "error")
    return render_template("account/login.html", form=form)
Beispiel #8
0
    def test_should_be_able_to_load_a_user_by_a_given_identity(self):
        with app.test_request_context():
            identity = Identity(self.user.mongo_id)
            user = User.query.from_identity(identity)

            assert_equals(self.user, identity.user)
            assert_equals(self.user, user)
Beispiel #9
0
def callback(source, app):

    verifier = request.args.get('oauth_verifier', '')
    #oauth_token = request.args.get('oauth_token','')

    if source == 'sina':
        try:
            api_key, api_secret, callback = sina_api[app]
        except:
            abort(404)
        auth = sina.OAuthHandler(api_key, api_secret, callback)
        token_string = sina.oauth.OAuthToken.from_string(
            session['oauth_token'])

    #elif source=='qq':
    #    api_key, api_secret, callback = qq_api
    #    auth = qq.OAuthHandler(api_key, api_secret, callback)
    #    token_string = qq.oauth.OAuthToken.from_string(session['oauth_token'])

    auth.set_req_token(token_string)
    token = auth.get_access_token(verifier)

    session['oauth_token'] = token.key
    session['oauth_token_secret'] = token.secret

    auth.setToken(token.key, token.secret)

    if source == 'sina':
        username = auth.get_username()
    #elif source=='qq':
    #    username = auth.get_username()
    else:
        username = ''

    session['source'] = source
    session['app'] = app
    session['username'] = username

    if not g.user:
        mapper = UserMapper.query.filter(db.and_(UserMapper.source==source,
                                                 UserMapper.app==app,
                                                 UserMapper.access_token==token.key))\
                                 .first()
        if mapper:
            # login
            identity_changed.send(current_app._get_current_object(),
                                  identity=Identity(mapper.user.id))
        else:
            return redirect(url_for('auth.register'))

    g.user.bind(source, app, token.key, token.secret)

    # update profile
    update_profile(source, g.user, auth)

    return redirect(url_for('%s.index' % app))
Beispiel #10
0
    def l():
        s = []
        if not admin_or_editor:
            s.append("not admin")

        i = Identity('ali')
        identity_changed.send(app, identity=i)
        if admin_or_editor:
            s.append("now admin")
        return Response('\n'.join(s))
Beispiel #11
0
def register():

    if g.user:
        return 'is logined'

    source = session.get('source')
    app = session.get('app')
    username = session.get('username')

    if source and username and app:

        token = session['oauth_token']
        secret = session['oauth_token_secret']

        if source == 'sina':

            api_key, api_secret, callback = sina_api[app]
            auth = sina.OAuthHandler(api_key, api_secret, callback)
            auth.setToken(token, secret)

        #elif source=='qq':
        #    api_key, api_secret, callback = qq_api
        #    auth = qq.OAuthHandler(api_key, api_secret, callback)
        #    auth.setToken(token, secret)

        # 创建shorten
        while True:
            code = shorten(str(datetime.now()))
            if User.query.filter_by(shorten=code).count() == 0:
                break

        email = '*****@*****.**' % code

        user = User(nickname=username, email=email, shorten=code)

        user.password = email

        user.profile = UserProfile()

        update_profile(source, user, auth)

        db.session.add(user)
        db.session.commit()

        # login
        identity_changed.send(current_app._get_current_object(),
                              identity=Identity(user.id))

        user.bind(source, app, token, secret)

        return redirect(url_for('%s.post' % app))

    else:
        return redirect(url_for('frontend.login'))
Beispiel #12
0
def create_or_login(rsp):
    session['openid'] = rsp.identity_url

    user = User.query.filter_by(openid=rsp.identity_url).first()
    if user is not None:
        flash(_('Successfully signed in'), 'success')
        identity_changed.send(current_app._get_current_object(), identity=Identity(user.pk))
        return redirect(oid.get_next_url())

    return redirect(url_for('create_profile',
        next=oid.get_next_url(),
        nickname=rsp.nickname or rsp.fullname,
        email=rsp.email))
Beispiel #13
0
def login():
    form = LoginForm(request.form, next=request.args.get('next', ''))

    if form.validate_on_submit():
        user = User.query.filter(User.name == form.name.data).first()
        if user and user.check_password(form.password.data):
            identity_changed.send(current_app._get_current_object(),
                                  identity=Identity(user.pk))
            flash(u"登录成功")
            return redirect(request.args.get("next") or url_for("index"))

        flash(u"登录失败, 请重新登录")
    return render_template("admin/login.html", form=form)
Beispiel #14
0
def create_or_login(response):

    openid = response.identity_url

    user, authenticated = \
        User.query.authenticate_openid(response.email, openid)

    next_url = session.pop('next', None)

    if user is None:
        session['openid'] = openid

        username = response.fullname or response.nickname
        if username:
            username = slugify(username.replace("-", "_"))

        return redirect(
            url_for("openid.signup",
                    next=next_url,
                    name=username,
                    email=response.email))

    if authenticated:

        session.permanent = True

        identity_changed.send(current_app._get_current_object(),
                              identity=Identity(user.id))

        flash(_("Welcome back, %%s") % user.username, "success")

        if next_url is None:
            next_url = url_for('user.posts', username=user.username)

        return redirect(next_url)

    # user already exists, so login and attach openid
    session['openid'] = openid

    flash(
        _("You already have an account with us. "
          "Please login with your email address so your "
          "OpenID can be attached to your user account"), "success")

    return redirect(url_for('account.login', login=response.email))
Beispiel #15
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        if password == username + "_secret":
            identity = Identity(username)
            identity_changed.send(app, identity=identity)
            return redirect(session['redirected_from'])
        else:
            return abort(401)
    else:
        return Response('''
        <form action="" method="post">
            <p><input type=text name=username>
            <p><input type=password name=password>
            <p><input type=submit value=Login>
        </form>
        ''')
Beispiel #16
0
def signup():
    form = SignupForm(next=request.args.get("next"))

    if form.validate_on_submit():
        user = User()
        form.populate_obj(user)
        user.save()
        # Flask-Login
        login_user(user)

        # Flask-principal
        identity_changed.send(current_app._get_current_object(),
                              identity=Identity(user.username))

        flash(u"欢迎, %s" % user.username, "successfully")
        next_url = form.next.data
        if not next_url or next_url == request.path:
            next_url = url_for('user.posts', username=user.username)

        return redirect(next_url)
    return render_template("account/signup.html", form=form)
Beispiel #17
0
def login():
    if request.method == 'GET':
        return Response('''
                        <form name="login" action="" method="post">
                            username:<input name="username" type="text"/><br/>
                            password:<input name="password" type="password"/><br/>
                            <input type="submit" value="login"/>
                        ''')
    else:
        username = request.form['username']
        #password = request.form['password']
        #用户认证
        #认证成功后发信号通知pincipal
        identity = Identity(username)
        identity_changed.send(app, identity=identity)
        redirected_from = session.get('redirected_from')
        print redirected_from, url_for('login')
        if redirected_from and not redirected_from.endswith(url_for('login')):
            #如果有记录来源页且来源页非登录页,则转向到来源页
            return redirect(redirected_from)
        #否则转向到首页
        return redirect(url_for('index'))
Beispiel #18
0
def create_profile():
    if g.user is not None or 'openid' not in session:
        return redirect(url_for('home.index'))

    form = SignupForm(
            next=oid.get_next_url,
            nickname=request.values.get('nickname', None),
            email=request.values.get('email', None),
            )

    if form.validate_on_submit():
        user = User(openid=session['openid'])
        user.init_optional()
        form.populate_obj(user)

        user.save()
        identity_changed.send(current_app._get_current_object(), identity=Identity(user.pk))

        flash(_('Profile successfully created'), 'success')

        return redirect(oid.get_next_url())

    return render_template('account/create_profile.html',
            form=form)
Beispiel #19
0
def mkadmin():
    i = Identity('ali')
    return i
Beispiel #20
0
 def j():
     i = Identity('james')
     identity_changed.send(app, identity=i)
     with admin_permission.require(403):
         with editor_permission.require(403):
             pass
Beispiel #21
0
def login_view(req):
    username = req.form.get('username')

    identity_changed.send(current_app._get_current_object(),
                          identity=Identity(username))
Beispiel #22
0
def check_user():
    g.identity = Identity(
        User.query.filter_by(username=session.get('username')))