Flask Security: Unterschied zwischen den Versionen

Aus Froggis Wissenssammlung
Wechseln zu:Navigation, Suche
(Die Seite wurde neu angelegt: „=== 1. User Role models for Flask-SQLAlchemy === old... <pre> class Role(db.Model, RoleMixin): __tablename__ = table_prefix + "role" id = db.Column(d…“)
 
 
(4 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
 
=== 1. User Role models for Flask-SQLAlchemy ===
 
=== 1. User Role models for Flask-SQLAlchemy ===
  
old...
+
old example with UserMixin and RoleMixin from Flask-Login:
  
 
<pre>
 
<pre>
Zeile 33: Zeile 33:
 
new...? UserMixin and RoleMixin from Flask-Security with minimal requirements included (see also: [https://pythonhosted.org/Flask-Security/models.html Flask Security (Models)]
 
new...? UserMixin and RoleMixin from Flask-Security with minimal requirements included (see also: [https://pythonhosted.org/Flask-Security/models.html Flask Security (Models)]
  
 +
'''Minimal columns required'''
 +
 +
For the Usermodel:
 +
    id
 +
    email
 +
    password
 +
    active
 +
 +
For the Rolemodel:
 +
    id
 +
    name
 +
    description
 +
 +
Gives this result as an example (with MySQL as database):
 
<pre>
 
<pre>
 
class Role(db.Model, RoleMixin):
 
class Role(db.Model, RoleMixin):

Aktuelle Version vom 5. August 2019, 12:35 Uhr

1. User Role models for Flask-SQLAlchemy

old example with UserMixin and RoleMixin from Flask-Login:

class Role(db.Model, RoleMixin):
    __tablename__ = table_prefix + "role"
    id = db.Column(db.Integer(), primary_key=True)
    role = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))
    users = db.relationship(
        'User',
        secondary=table_prefix + 'user_roles',
        back_populates='roles',
        lazy='dynamic')


class User(UserMixin, db.Model):
    __tablename__ = table_prefix + "user"
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), index=True, unique=True)
    password_hash = db.Column(db.String(128))
    email = db.Column(db.String(100), index=True, unique=True)
    language = db.Column(db.String(10), default="en_UK")
    roles = db.relationship(
        'Role',
        secondary=table_prefix + 'user_roles',
        back_populates='users',
        cascade='all, delete',
        lazy='dynamic')

new...? UserMixin and RoleMixin from Flask-Security with minimal requirements included (see also: Flask Security (Models)

Minimal columns required

For the Usermodel:

   id
   email
   password
   active

For the Rolemodel:

   id
   name
   description

Gives this result as an example (with MySQL as database):

class Role(db.Model, RoleMixin):
    __tablename__ = table_prefix + "role"
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))


class User(db.Model, UserMixin):
    __tablename__ = table_prefix + "user"
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), index=True, unique=True)
    password_hash = db.Column(db.String(128))
    email = db.Column(db.String(100), index=True, unique=True)
    active = db.Column(db.Boolean(), default=False)
    confirmed_at = db.Column(db.DateTime())
    language = db.Column(db.String(10), default="en_UK")
    roles = db.relationship(
        'Role',
        secondary=table_prefix + 'userrole',
        backref=db.backref('users', lazy='dynamic'),
        passive_deletes=True)

    def __repr__(self):
        return '<User {}>'.format(self.username)

class UserRole(db.Model):
    __tablename__ = table_prefix + 'userrole'
    user_id = db.Column(
                        db.Integer(),
                        db.ForeignKey(table_prefix
                                      + 'user.id',
                                      ondelete='CASCADE')
                        )
    role_id = db.Column(
                        db.Integer(),
                        db.ForeignKey(table_prefix + 'role.id')
                        )
    __table_args__ = (
        PrimaryKeyConstraint(
            user_id,
            role_id),
        {})