Python flask_login.current_user.can() Examples

The following are 17 code examples of flask_login.current_user.can(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module flask_login.current_user , or try the search function .
Example #1
Source File: views.py    From circleci-demo-python-flask with MIT License 6 votes vote down vote up
def index():
    form = PostForm()
    if current_user.can(Permission.WRITE_ARTICLES) and \
            form.validate_on_submit():
        post = Post(body=form.body.data,
                    author=current_user._get_current_object())
        db.session.add(post)
        return redirect(url_for('.index'))
    page = request.args.get('page', 1, type=int)
    show_followed = False
    if current_user.is_authenticated:
        show_followed = bool(request.cookies.get('show_followed', ''))
    if show_followed:
        query = current_user.followed_posts
    else:
        query = Post.query
    pagination = query.order_by(Post.timestamp.desc()).paginate(
        page, per_page=current_app.config['CIRCULATE_POSTS_PER_PAGE'],
        error_out=False)
    posts = pagination.items
    return render_template('index.html', form=form, posts=posts,
                           show_followed=show_followed, pagination=pagination) 
Example #2
Source File: views.py    From flasky-first-edition with MIT License 6 votes vote down vote up
def index():
    form = PostForm()
    if current_user.can(Permission.WRITE_ARTICLES) and \
            form.validate_on_submit():
        post = Post(body=form.body.data,
                    author=current_user._get_current_object())
        db.session.add(post)
        return redirect(url_for('.index'))
    page = request.args.get('page', 1, type=int)
    show_followed = False
    if current_user.is_authenticated:
        show_followed = bool(request.cookies.get('show_followed', ''))
    if show_followed:
        query = current_user.followed_posts
    else:
        query = Post.query
    pagination = query.order_by(Post.timestamp.desc()).paginate(
        page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'],
        error_out=False)
    posts = pagination.items
    return render_template('index.html', form=form, posts=posts,
                           show_followed=show_followed, pagination=pagination) 
Example #3
Source File: decorators.py    From flask-blog with MIT License 5 votes vote down vote up
def permission_required(permission):
    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            if not current_user.can(permission):
                abort(403)
            return f(*args, **kwargs)

        return decorated_function

    return decorator 
Example #4
Source File: decorators.py    From circleci-demo-python-flask with MIT License 5 votes vote down vote up
def permission_required(permission):
    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            if not current_user.can(permission):
                abort(403)
            return f(*args, **kwargs)
        return decorated_function
    return decorator 
Example #5
Source File: decorators.py    From flask-base with MIT License 5 votes vote down vote up
def permission_required(permission):
    """Restrict a view to users with the given permission."""

    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            if not current_user.can(permission):
                abort(403)
            return f(*args, **kwargs)

        return decorated_function

    return decorator 
Example #6
Source File: views.py    From flasky-first-edition with MIT License 5 votes vote down vote up
def edit(id):
    post = Post.query.get_or_404(id)
    if current_user != post.author and \
            not current_user.can(Permission.ADMINISTER):
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post.body = form.body.data
        db.session.add(post)
        flash('The post has been updated.')
        return redirect(url_for('.post', id=post.id))
    form.body.data = post.body
    return render_template('edit_post.html', form=form) 
Example #7
Source File: decorators.py    From flasky-first-edition with MIT License 5 votes vote down vote up
def permission_required(permission):
    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            if not current_user.can(permission):
                abort(403)
            return f(*args, **kwargs)
        return decorated_function
    return decorator 
Example #8
Source File: decorators.py    From penn-club-ratings with MIT License 5 votes vote down vote up
def permission_required(permission):
    """Restrict a view to users with the given permission."""

    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            if not current_user.can(permission):
                abort(403)
            return f(*args, **kwargs)

        return decorated_function

    return decorator 
Example #9
Source File: decorators.py    From BhagavadGita with GNU General Public License v3.0 5 votes vote down vote up
def permission_required(permission):
    """Restrict a view to users with the given permission."""

    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            if not current_user.can(permission):
                abort(403)
            return f(*args, **kwargs)

        return decorated_function

    return decorator 
Example #10
Source File: utils.py    From flask-shop with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def permission_required(permission):
    def decorator(f):
        @wraps(f)
        def _deco(*args, **kwargs):
            if current_user.is_authenticated and current_user.can(permission):
                return f(*args, **kwargs)
            abort(403)

        return _deco

    return decorator 
Example #11
Source File: app_security.py    From web_develop with GNU General Public License v3.0 5 votes vote down vote up
def admin():
    return 'Only administrators can see this!' 
Example #12
Source File: app_security.py    From web_develop with GNU General Public License v3.0 5 votes vote down vote up
def permission_required(permission):
    def decorator(f):
        @wraps(f)
        def _deco(*args, **kwargs):
            if not current_user.can(permission):
                abort(403)
            return f(*args, **kwargs)
        return _deco
    return decorator 
Example #13
Source File: app_security.py    From web_develop with GNU General Public License v3.0 5 votes vote down vote up
def can_admin(self):
        return self.can(Permission.ADMINISTER) 
Example #14
Source File: app_security.py    From web_develop with GNU General Public License v3.0 5 votes vote down vote up
def can(self, permissions):
        if self.roles is None:
            return False
        all_perms = reduce(or_, map(lambda x: x.permissions, self.roles))
        return all_perms & permissions == permissions 
Example #15
Source File: utils.py    From openvpn-admin-ui with Apache License 2.0 5 votes vote down vote up
def permission_required(permission):
    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            if not current_user.can(permission):
                abort(403)
            return f(*args, **kwargs)
        return decorated_function
    return decorator 
Example #16
Source File: views.py    From openvpn-admin-ui with Apache License 2.0 5 votes vote down vote up
def app_permission():
    def can_admin(permission):
        return current_user.can(permission)
    return dict(can_admin=can_admin, permission=Permission) 
Example #17
Source File: views.py    From circleci-demo-python-flask with MIT License 5 votes vote down vote up
def edit(id):
    post = Post.query.get_or_404(id)
    if current_user != post.author and \
            not current_user.can(Permission.ADMINISTER):
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post.body = form.body.data
        db.session.add(post)
        flash('The post has been updated.')
        return redirect(url_for('.post', id=post.id))
    form.body.data = post.body
    return render_template('edit_post.html', form=form)