Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

roles: changed need creation by role name to id #54

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Core

Protecting Views
----------------
.. autofunction:: flask_security.decorators.auth_required

.. autofunction:: flask_security.decorators.roles_required

.. autofunction:: flask_security.decorators.roles_accepted
Expand Down
2 changes: 1 addition & 1 deletion flask_security/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def _on_identity_loaded(sender, identity):
identity.provides.add(UserNeed(current_user.id))

for role in getattr(current_user, 'roles', []):
identity.provides.add(RoleNeed(role.name))
identity.provides.add(RoleNeed(role.id))

identity.user = current_user

Expand Down
2 changes: 1 addition & 1 deletion flask_security/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _get_unauthorized_view():

def auth_required(*auth_methods):
"""
Decorator that protects enpoints through multiple mechanisms
Decorator that protects endpoints through multiple mechanisms
Example::

@app.route('/dashboard')
Expand Down
12 changes: 11 additions & 1 deletion run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,18 @@ set -o errexit
# Quit on unbound symbols
set -o nounset

# Check for arguments
pytest_args=()
for arg in $@; do
case ${arg} in
*)
pytest_args+=( ${arg} )
;;
esac
done

python -m check_manifest --ignore ".*-requirements.txt"
python -m sphinx.cmd.build -qnN docs docs/_build/html
python -m pytest
python -m pytest ${pytest_args[@]+"${pytest_args[@]}"}
tests_exit_code=$?
exit "$tests_exit_code"
15 changes: 11 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import os
import tempfile
import uuid
from json import JSONEncoder as BaseEncoder

import pytest
Expand Down Expand Up @@ -161,14 +162,20 @@ def sqlalchemy_datastore(request, app, tmpdir):
roles_users = db.Table(
"roles_users",
db.Column("user_id", db.Integer(), db.ForeignKey("user.id")),
db.Column("role_id", db.Integer(), db.ForeignKey("role.id")),
db.Column("role_id", db.String(80), db.ForeignKey("role.id")),
)

class Role(db.Model, RoleMixin):
id = db.Column(db.Integer(), primary_key=True)
id = db.Column(db.String(80), primary_key=True, default=lambda x: str(uuid.uuid4()))
name = db.Column(db.String(80), unique=True)
description = db.Column(db.String(255))

def __init__(self, **kwargs):
if kwargs.get("name"):
kwargs.setdefault("id", kwargs["name"])
super().__init__(**kwargs)


class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True)
Expand Down Expand Up @@ -219,11 +226,11 @@ class RolesUsers(Base):
__tablename__ = "roles_users"
id = Column(Integer(), primary_key=True)
user_id = Column("user_id", Integer(), ForeignKey("user.id"))
role_id = Column("role_id", Integer(), ForeignKey("role.id"))
role_id = Column("role_id", String(80), ForeignKey("role.id"))

class Role(Base, RoleMixin):
__tablename__ = "role"
id = Column(Integer(), primary_key=True)
id = Column(String(80), primary_key=True, default=lambda x: str(uuid.uuid4()))
name = Column(String(80), unique=True)
description = Column(String(255))

Expand Down