Add User.is_admin()
convenience method
This commit is contained in:
parent
997145200b
commit
76d333a346
|
@ -2,7 +2,7 @@
|
|||
################################################################################
|
||||
#
|
||||
# Rattail -- Retail Software Framework
|
||||
# Copyright © 2010-2015 Lance Edgar
|
||||
# Copyright © 2010-2017 Lance Edgar
|
||||
#
|
||||
# This file is part of Rattail.
|
||||
#
|
||||
|
@ -24,14 +24,13 @@
|
|||
Data Models for Users & Permissions
|
||||
"""
|
||||
|
||||
from __future__ import unicode_literals
|
||||
from __future__ import unicode_literals, absolute_import
|
||||
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy import orm
|
||||
from sqlalchemy.ext.associationproxy import association_proxy
|
||||
|
||||
from .core import Base, uuid_column, getset_factory
|
||||
from .people import Person
|
||||
from rattail.db.model import Base, uuid_column, getset_factory, Person
|
||||
|
||||
|
||||
class Role(Base):
|
||||
|
@ -67,7 +66,7 @@ class Permission(Base):
|
|||
return unicode(self.permission or '')
|
||||
|
||||
|
||||
Role._permissions = relationship(
|
||||
Role._permissions = orm.relationship(
|
||||
Permission, backref='role',
|
||||
cascade='save-update, merge, delete, delete-orphan')
|
||||
|
||||
|
@ -171,13 +170,23 @@ class User(Base):
|
|||
"""
|
||||
return self.get_email_address()
|
||||
|
||||
def is_admin(self):
|
||||
"""
|
||||
Convenience method to determine if current user is a member of the
|
||||
Administrator role.
|
||||
"""
|
||||
from rattail.db.auth import administrator_role
|
||||
|
||||
User.person = relationship(
|
||||
session = orm.object_session(self)
|
||||
return administrator_role(session) in self.roles
|
||||
|
||||
|
||||
User.person = orm.relationship(
|
||||
Person,
|
||||
back_populates='user',
|
||||
uselist=False)
|
||||
|
||||
Person.user = relationship(
|
||||
Person.user = orm.relationship(
|
||||
User,
|
||||
back_populates='person',
|
||||
uselist=False)
|
||||
|
@ -198,7 +207,7 @@ class UserRole(Base):
|
|||
role_uuid = sa.Column(sa.String(length=32), nullable=False)
|
||||
|
||||
|
||||
Role._users = relationship(
|
||||
Role._users = orm.relationship(
|
||||
UserRole, backref='role',
|
||||
cascade='all, delete-orphan')
|
||||
|
||||
|
@ -207,7 +216,7 @@ Role.users = association_proxy(
|
|||
creator=lambda u: UserRole(user=u),
|
||||
getset_factory=getset_factory)
|
||||
|
||||
User._roles = relationship(
|
||||
User._roles = orm.relationship(
|
||||
UserRole, backref='user',
|
||||
cascade='all, delete-orphan')
|
||||
|
||||
|
|
Loading…
Reference in a new issue