# Path: repocribro/models.py
# class UserAccount(db.Model, UserMixin, SearchableMixin):
#     """UserAccount in the repocribro app"""
#     __tablename__ = 'UserAccount'
# 
#     default_rolename = 'user'
# 
#     #: Unique identifier of the user account
#     id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
#     #: Timestamp where account was created
#     created_at = sqlalchemy.Column(
#         sqlalchemy.DateTime, default=datetime.datetime.now()
#     )
#     #: Flag if the account is active or banned
#     active = sqlalchemy.Column(sqlalchemy.Boolean, default=True)
#     #: Relation to the GitHub user connected to account
#     github_user = sqlalchemy.orm.relationship(
#         'User', back_populates='user_account',
#         uselist=False, cascade='all, delete-orphan'
#     )
#     #: Roles assigned to the user account
#     roles = sqlalchemy.orm.relationship(
#         'Role', back_populates='user_accounts',
#         secondary=roles_users,
#     )
# 
#     @property
#     def login(self):
#         """Get login name for user account from related GH user
# 
#         :return: Login name
#         :rtype: str
#         """
#         if self.github_user is None:
#             return '<unknown>'
#         return self.github_user.login
# 
#     def __repr__(self):
#         """Standard string representation of DB object
# 
#         :return: Unique string representation
#         :rtype: str
#         """
#         return '<UserAccount (#{})>'.format(self.id)
# 
# class Anonymous(flask_login.AnonymousUserMixin, UserMixin):
#     """Anonymous (not logged) user representation"""
# 
#     rolename = 'anonymous'
#     _roles = []
# 
#     @property
#     def roles(self):
#         return type(self)._roles
# 
#     @classmethod
#     def set_role(cls, role):
#         cls._roles = [role]
# 
#     @property
#     def is_active(self):
#         """Check whether is user active
# 
#         :return: False, anonymous is not active
#         :rtype: bool
#         """
#         return False
# 
#     def has_role(self, role):
#         """Check whether has the role
# 
#         :param role: Role to be checked
#         :type role: ``repocribro.models.RoleMixin``
#         :return: False, anonymous has no roles
#         :rtype: bool
#         """
#         return role == self.rolename
# 
#     @property
#     def rolenames(self):
#         """Get names of all roles of that user
# 
#         :return: Empty list, anonymous has no roles
#         :rtype: list of str
#         """
#         return [self.rolename]
# 
#     def owns_repo(self, repo):
#         """Check if user owns the repository
# 
#         :param repo: Repository which shoudl be tested
#         :type repo: ``repocribro.models.Repository``
#         :return: False, anonymous can not own repository
#         :rtype: bool
#         """
#         return False
# 
#     def sees_repo(self, repo, has_secret=False):
#         """Check if user is allowed to see the repo
# 
#         Anonymous can see only public repos
# 
#         :param repo: Repository which user want to see
#         :type repo: ``repocribro.models.Repository``
#         :param has_secret: If current user knows the secret URL
#         :type has_secret: bool
#         :return: If user can see repo
#         :rtype: bool
#         """
#         visible = repo.is_public or (has_secret and repo.is_hidden)
#         return visible
# 
# class Role(db.Model, RoleMixin):
#     """User account role in the application"""
#     __tablename__ = 'Role'
# 
#     #: Unique identifier of the role
#     id = sqlalchemy.Column(sqlalchemy.Integer(), primary_key=True)
#     #: Unique name of the role
#     name = sqlalchemy.Column(sqlalchemy.String(80), unique=True)
#     #: Description (purpose, notes, ...) of the role
#     description = sqlalchemy.Column(sqlalchemy.UnicodeText)
#     #: Serialized list of privileges
#     privileges = sqlalchemy.Column(sqlalchemy.Text)
#     #: User accounts assigned to the role
#     user_accounts = sqlalchemy.orm.relationship(
#         'UserAccount', back_populates='roles', secondary=roles_users
#     )
# 
#     def __init__(self, name, privileges, description):
#         self.name = name
#         self.privileges = privileges
#         self.description = description
# 
#     def __repr__(self):
#         """Standard string representation of DB object
# 
#         :return: Unique string representation
#         :rtype: str
#         """
#         return '<Role {} (#{})>'.format(
#             self.name, self.id
#         )


import flask
import flask_login
import flask_principal
from .models import UserAccount, Anonymous, Role

    :param app: Current flask application
    :type app: ``repocribro.repocribro.Repocribro``
    :param db: Database connection
    :type db: ``flask_sqlalchemy.SQLAlchemy``
    """
    with app.app_context():
        anonymous_role = None
        try:
            anonymous_role = db.session.query(Role).filter_by(
                name=Anonymous.rolename
            ).first()
        except:
            pass
        if anonymous_role is not None:
            Anonymous.set_role(anonymous_role)


def get_default_user_role(app, db):
    """Get special default role for registered users

    :param app: Current flask application
    :type app: ``repocribro.repocribro.Repocribro``
    :param db: Database connection
    :type db: ``flask_sqlalchemy.SQLAlchemy``
    """
    user_role = None
    with app.app_context():
        try:
            user_role = db.session.query(Role).filter_by(

# Path: repocribro/models.py
# class UserAccount(db.Model, UserMixin, SearchableMixin):
#     """UserAccount in the repocribro app"""
#     __tablename__ = 'UserAccount'
# 
#     default_rolename = 'user'
# 
#     #: Unique identifier of the user account
#     id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
#     #: Timestamp where account was created
#     created_at = sqlalchemy.Column(
#         sqlalchemy.DateTime, default=datetime.datetime.now()
#     )
#     #: Flag if the account is active or banned
#     active = sqlalchemy.Column(sqlalchemy.Boolean, default=True)
#     #: Relation to the GitHub user connected to account
#     github_user = sqlalchemy.orm.relationship(
#         'User', back_populates='user_account',
#         uselist=False, cascade='all, delete-orphan'
#     )
#     #: Roles assigned to the user account
#     roles = sqlalchemy.orm.relationship(
#         'Role', back_populates='user_accounts',
#         secondary=roles_users,
#     )
# 
#     @property
#     def login(self):
#         """Get login name for user account from related GH user
# 
#         :return: Login name
#         :rtype: str
#         """
#         if self.github_user is None:
#             return '<unknown>'
#         return self.github_user.login
# 
#     def __repr__(self):
#         """Standard string representation of DB object
# 
#         :return: Unique string representation
#         :rtype: str
#         """
#         return '<UserAccount (#{})>'.format(self.id)
# 
# class Anonymous(flask_login.AnonymousUserMixin, UserMixin):
#     """Anonymous (not logged) user representation"""
# 
#     rolename = 'anonymous'
#     _roles = []
# 
#     @property
#     def roles(self):
#         return type(self)._roles
# 
#     @classmethod
#     def set_role(cls, role):
#         cls._roles = [role]
# 
#     @property
#     def is_active(self):
#         """Check whether is user active
# 
#         :return: False, anonymous is not active
#         :rtype: bool
#         """
#         return False
# 
#     def has_role(self, role):
#         """Check whether has the role
# 
#         :param role: Role to be checked
#         :type role: ``repocribro.models.RoleMixin``
#         :return: False, anonymous has no roles
#         :rtype: bool
#         """
#         return role == self.rolename
# 
#     @property
#     def rolenames(self):
#         """Get names of all roles of that user
# 
#         :return: Empty list, anonymous has no roles
#         :rtype: list of str
#         """
#         return [self.rolename]
# 
#     def owns_repo(self, repo):
#         """Check if user owns the repository
# 
#         :param repo: Repository which shoudl be tested
#         :type repo: ``repocribro.models.Repository``
#         :return: False, anonymous can not own repository
#         :rtype: bool
#         """
#         return False
# 
#     def sees_repo(self, repo, has_secret=False):
#         """Check if user is allowed to see the repo
# 
#         Anonymous can see only public repos
# 
#         :param repo: Repository which user want to see
#         :type repo: ``repocribro.models.Repository``
#         :param has_secret: If current user knows the secret URL
#         :type has_secret: bool
#         :return: If user can see repo
#         :rtype: bool
#         """
#         visible = repo.is_public or (has_secret and repo.is_hidden)
#         return visible
# 
# class Role(db.Model, RoleMixin):
#     """User account role in the application"""
#     __tablename__ = 'Role'
# 
#     #: Unique identifier of the role
#     id = sqlalchemy.Column(sqlalchemy.Integer(), primary_key=True)
#     #: Unique name of the role
#     name = sqlalchemy.Column(sqlalchemy.String(80), unique=True)
#     #: Description (purpose, notes, ...) of the role
#     description = sqlalchemy.Column(sqlalchemy.UnicodeText)
#     #: Serialized list of privileges
#     privileges = sqlalchemy.Column(sqlalchemy.Text)
#     #: User accounts assigned to the role
#     user_accounts = sqlalchemy.orm.relationship(
#         'UserAccount', back_populates='roles', secondary=roles_users
#     )
# 
#     def __init__(self, name, privileges, description):
#         self.name = name
#         self.privileges = privileges
#         self.description = description
# 
#     def __repr__(self):
#         """Standard string representation of DB object
# 
#         :return: Unique string representation
#         :rtype: str
#         """
#         return '<Role {} (#{})>'.format(
#             self.name, self.id
#         )


# Path: repocribro/security.py
import flask
import flask_login
import flask_principal
from .models import UserAccount, Anonymous, Role


    :param app: Current flask application
    :type app: ``repocribro.repocribro.Repocribro``
    :param db: Database connection
    :type db: ``flask_sqlalchemy.SQLAlchemy``
    """
    with app.app_context():
        anonymous_role = None
        try:
            anonymous_role = db.session.query(Role).filter_by(
                name=Anonymous.rolename
            ).first()
        except:
            pass
        if anonymous_role is not None:
            Anonymous.set_role(anonymous_role)


def get_default_user_role(app, db):
    """Get special default role for registered users

    :param app: Current flask application
    :type app: ``repocribro.repocribro.Repocribro``
    :param db: Database connection
    :type db: ``flask_sqlalchemy.SQLAlchemy``
    """
    user_role = None
    with app.app_context():
        try:
            user_role = db.session.query(Role).filter_by(

                name=UserAccount.default_rolename