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

[Do not merge] ACL Database Models #794

Closed
wants to merge 1 commit into from
Closed
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
42 changes: 42 additions & 0 deletions migrations/versions/f79764fe536e_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""empty message

Revision ID: f79764fe536e
Revises: 00ea66754d06
Create Date: 2016-06-14 03:26:04.095775

"""

# revision identifiers, used by Alembic.
revision = 'f79764fe536e'
down_revision = '00ea66754d06'

from alembic import op
import sqlalchemy as sa
import sqlalchemy_utils


def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.create_table('user_roles',
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('role_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['role_id'], ['role.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], )
)
op.alter_column(u'role', 'name',
existing_type=sa.VARCHAR(length=128),
nullable=False)
op.create_unique_constraint(None, 'role', ['name'])
op.drop_column(u'user', 'role')
### end Alembic commands ###


def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.add_column(u'user', sa.Column('role', sa.VARCHAR(), autoincrement=False, nullable=True))
op.drop_constraint(None, 'role', type_='unique')
op.alter_column(u'role', 'name',
existing_type=sa.VARCHAR(length=128),
nullable=True)
op.drop_table('user_roles')
### end Alembic commands ###
17 changes: 0 additions & 17 deletions open_event/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,6 @@ def send_email_with_reset_password_hash(email, link):
headers=HEADERS)


def is_event_admin(event_id, users):
"""
:param event_id: Event id
:param users: User id
:return: is user admin
"""
is_admin = False
for user_obj in users:
if user_obj.user.id == login.current_user.id:
for ass in login.current_user.events_assocs:
if ass.event_id == int(event_id):
is_admin = ass.admin
if is_event_admin:
return is_admin
return is_admin


def get_serializer(secret_key=None):
return Serializer('secret_key')

Expand Down
21 changes: 20 additions & 1 deletion open_event/models/role.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
"""Copyright 2015 Rafal Kowalski"""
from . import db

user_roles = db.Table('user_roles',
db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
db.Column('role_id', db.Integer, db.ForeignKey('role.id'))
)


class Role(db.Model):
__tablename__ = 'role'

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128))
name = db.Column(db.String, unique=True, nullable=False)
users = db.relationship("User", secondary=user_roles, back_populates="roles")

def __init__(self, name=None):
self.name = name

def __repr__(self):
return '<Role %r>' % self.name

def __str__(self):
return unicode(self).encode('utf-8')

def __unicode__(self):
return self.name
15 changes: 2 additions & 13 deletions open_event/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from . import db
from user_detail import UserDetail
from .role import user_roles

SPEAKER = 'speaker'
ADMIN = 'admin'
Expand All @@ -16,10 +17,10 @@ class User(db.Model):
password = db.Column(db.String(128))
reset_password = db.Column(db.String(128))
salt = db.Column(db.String(128))
role = db.Column(db.String())
avatar = db.Column(db.String())
tokens = db.Column(db.Text)
user_detail = db.relationship("UserDetail", uselist=False, backref="user")
roles = db.relationship("Role", secondary=user_roles, back_populates="users")

# Flask-Login integration
def is_authenticated(self):
Expand All @@ -34,18 +35,6 @@ def is_anonymous(self):
def get_id(self):
return self.id

def is_super_admin(self):
return self.role == SUPERADMIN

def is_admin(self):
return self.role == ORGANIZER

def is_organizer(self):
return self.role == ADMIN

def is_speaker(self):
return self.role == SPEAKER

# Required for administrative interface
def __unicode__(self):
return self.username
Expand Down
13 changes: 10 additions & 3 deletions open_event/models/users_events_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@

class UsersEventsRoles(db.Model):
__tablename__ = 'users_events_roles'

id = db.Column(db.Integer, primary_key=True)
event_id = db.Column(db.Integer, db.ForeignKey('events.id'))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
user = db.relationship("User", backref="roles")
event_id = db.Column(db.Integer, db.ForeignKey('events.id'))
role_id = db.Column(db.Integer, db.ForeignKey('role.id'))
role = db.relationship("Role", backref="roles")
user = db.relationship("User")
role = db.relationship("Role")

def __init__(self, name=None):
self.name = name

def __repr__(self):
return '<UserEventRole %r>' % self.name