I want to authenticate if the current_user object have 'Admin' in its role set to restrict taps from appearing in Jinja2.
However, if I only used {% if 'Admin' in current_user.format()['roles'] %} most of the time it will give me error
current_user is Non-type object
So, I used the following syntax and it worked for me locally.
{% if current_user.is_authenticated and ('Admin' in current_user.format()['roles']) %}
...
...
...
{% endif %}
The only issue I am not sure of the order of AND operation for Jinja2 and I am afraid the second condition after the AND operator is going to run first, and this will cause me trouble if I proceed with this code in the production server.
So my question:
Is there documentation about the order of operation in Jinja2, or any other reliable reference? Or if someone know whether the sequence of operation will always start from the left or not?
The model below is the class which current_user inherits from:
class Authorization(db.Model, UserMixin):
__tablename__ = 'authorizations'
id = db.Column(db.Integer, primary_key=True)
active = db.Column('is_active', db.Boolean(), nullable=False, server_default='0')
# User authentication information. The collation='NOCASE' is required
# to search case insensitively when USER_IFIND_MODE is 'nocase_collation'.
username = db.Column(db.BigInteger, nullable=False, unique=True)
password = db.Column(db.String(255), nullable=False, server_default='')
# Define the relationship to Role via UserRoles
roles = db.relationship('Role', secondary='user_roles')
def format(self):
roles = [role.name for role in self.roles]
print(roles)
return {
'id': self.id,
'active': self.active,
'username': self.username,
'roles': roles,
}
# Define the Role data-model
class Role(db.Model):
__tablename__ = 'roles'
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(50), unique=True)
# Define the UserRoles association table
class UserRoles(db.Model):
__tablename__ = 'user_roles'
id = db.Column(db.Integer(), primary_key=True)
user_id = db.Column(db.Integer(), db.ForeignKey('authorizations.id', ondelete='CASCADE'))
role_id = db.Column(db.Integer(), db.ForeignKey('roles.id', ondelete='CASCADE'))