Ejemplo n.º 1
0
class UserModel(db.Model, UserMixin):
    __tablename__ = dbtableprefix + 'user'
    id = db.Column(db.Integer, primary_key=True, unique=True)
    social_id = db.Column(db.String(255), nullable=False, unique=True)
    nickname = db.Column(db.String(255), nullable=False)
    email = db.Column(db.String(255), nullable=True, unique=True, index=True)
    confirmed_at = db.Column(db.DateTime())
    active = db.Column('active', db.Boolean(), nullable=False, server_default='0')
    first_name = db.Column(db.String(255), nullable=False, server_default='')
    last_name = db.Column(db.String(255), nullable=False, server_default='')
    country = db.Column(db.String(3))
    subdivisionfirst = db.Column(db.String(255))
    subdivisionsecond = db.Column(db.String(255))
    subdivisionthird = db.Column(db.String(255))
    organization = db.Column(db.String(255))
    timezone = db.Column(db.String(64))
    language = db.Column(db.String(64))
    user_auth = db.relationship('UserAuthModel', uselist=False, primaryjoin="UserAuthModel.user_id==UserModel.id", back_populates="user")
    roles = db.relationship('Role', secondary=dbtableprefix + 'user_roles', backref=db.backref(dbtableprefix + 'user', lazy='dynamic'))
    password = db.Column(db.String(255), nullable=False, server_default='') # work around a bug
    otp_secret = db.Column(db.String(255), nullable=True)
    pypi_username = db.Column(db.String(255), nullable=True)
    pypi_password = db.Column(db.String(255), nullable=True)
    modified_at = db.Column(db.DateTime())
    last_login = db.Column(db.DateTime())
    #email_is_phone_number = db.Column(db.Boolean(), nullable=True, server_default='0')
    limited_api = False
    def same_as(self, user_id):
        if self.limited_api:
            return False
        return self.id == user_id
    def has_role_or_permission(self, *specified_role_names, permissions=None):
        if self.limited_api:
            if isinstance(permissions, list):
                for task in permissions:
                    if self.can_do(task):
                        return True
            return False
        role_result = super().has_role(*specified_role_names)
        if not role_result and isinstance(permissions, list):
            for task in permissions:
                if self.can_do(task):
                    return True
        return role_result
    def can_do(self, task):
        if self.is_anonymous:
            return False
        if self.limited_api:
            return bool(task in self.limits)
        if hasattr(self, 'roles'):
            roles = self.roles
        else:
            if hasattr(self, 'user_profile') and hasattr(self.user_profile, 'roles'):
                roles = self.user_profile.roles
            else:
                roles = None
        for role in roles:
            if role.name in allowed and task in allowed[role.name]:
                return True
        return False
Ejemplo n.º 2
0
class UserModel(db.Model, UserMixin):
    __tablename__ = dbtableprefix + 'user'
    id = db.Column(db.Integer, primary_key=True)
    social_id = db.Column(db.String(255), nullable=False, unique=True)
    nickname = db.Column(db.String(255), nullable=False)
    email = db.Column(db.String(255), nullable=True, unique=True)
    confirmed_at = db.Column(db.DateTime())
    active = db.Column('active',
                       db.Boolean(),
                       nullable=False,
                       server_default='0')
    first_name = db.Column(db.String(255), nullable=False, server_default='')
    last_name = db.Column(db.String(255), nullable=False, server_default='')
    country = db.Column(db.String(3))
    subdivisionfirst = db.Column(db.String(255))
    subdivisionsecond = db.Column(db.String(255))
    subdivisionthird = db.Column(db.String(255))
    organization = db.Column(db.String(255))
    timezone = db.Column(db.String(64))
    language = db.Column(db.String(64))
    user_auth = db.relationship(
        'UserAuthModel',
        uselist=False,
        primaryjoin="UserAuthModel.user_id==UserModel.id")
    roles = db.relationship('Role',
                            secondary=dbtableprefix + 'user_roles',
                            backref=db.backref(dbtableprefix + 'user',
                                               lazy='dynamic'))
    password = db.Column(db.String(255), nullable=False,
                         server_default='')  # work around a bug
    otp_secret = db.Column(db.String(255), nullable=True)
    pypi_username = db.Column(db.String(255), nullable=True)
    pypi_password = db.Column(db.String(255), nullable=True)
    modified_at = db.Column(db.DateTime())