6

Pythonのユーザー/グループ管理パッケージを探していたところ(ユーザーグループの作成とそのグループへのメンバーの追加/削除)、flask_dashedを見つけました。

     https://github.com/jeanphix/Flask-Dashed/ 

それは多かれ少なかれ私が探していたものです。ただし、グループの追加/削除をサポートするのは 1 人のユーザーのみです。python-flask の世界で利用可能な他の同様のパッケージが何であるかを知っている人はいますか?

4

1 に答える 1

10

私は文字通りこれを昨日自分でやった. の組み合わせでやりました。

Flask-Login
Flask-Principal
Flask-SQLAlchemy

基本的にはこんな感じです。Flask-Login は、ユーザー認証に使用されます。ログインに成功すると、許可を追加するために傍受/使用できる信号が送信されます

@identity_loaded.connect_via(app)
def on_identity_loaded(sender, identity):
    # Set the identity user object
    current_user = session.get('user', False)
    if not current_user:
        return False
    identity.user = current_user

    # Add the UserNeed to the identity
    if hasattr(current_user, 'id'):
        identity.provides.add(UserNeed(current_user.id))

    # Assuming the User model has a list of groups, update the
    # identity with the groups that the user provides
    if hasattr(current_user, 'groups'):
        groups = user.Group.query.filter(user.Group.users.any(id=current_user.id)).all()
        for group in groups:
            identity.provides.add(RoleNeed(group.name))

ユーザーとグループのモデルは次のようになります。

groups = db.Table('groups',
    db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
    db.Column('group_id', db.Integer, db.ForeignKey('group.id'))
)

group_to_group = db.Table('group_to_group',
    db.Column('parent_id', db.Integer, db.ForeignKey('group.id'), primary_key=True),
    db.Column('child_id', db.Integer, db.ForeignKey('group.id'), primary_key=True)
)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    email = db.Column(db.String(120), unique=True)
    display_name = db.Column(db.String(120))
    created_at = db.Column(db.DateTime)
    last_login = db.Column(db.DateTime, default=db.func.now())

    def __init__(self, name, email, display_name):
        self.name = name
        self.email = email
        self.display_name = display_name
        self.created_at = datetime.datetime.now()
        self.order_by = User.display_name

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return unicode(self.id)

    def __repr__(self):
        return self.display_name


class Group(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64))
    users = db.relationship('User', 
                            secondary=groups,
                            backref=db.backref('groups',
                                               lazy='dynamic',
                                               order_by=name
                            )
    )
    parents = db.relationship('Group',
                              secondary=group_to_group, 
                              primaryjoin=id==group_to_group.c.parent_id,  
                              secondaryjoin=id==group_to_group.c.child_id, 
                              backref="children",
                              remote_side=[group_to_group.c.parent_id])

    def __repr__(self):
        return self.name

次に、グループの管理を処理するためにいくつかの CRUD ページを展開するだけです。

次に、グループ名に基づいてプリンシパル ロールを作成するロジックを設定し、新しい各グループが利用できるようにします。

何かへのアクセスを制限したいときは、次のようにします...

@NameOfYourRole.require(http_exception=403)
@route("/something/special/people/can/do")
def super_cool(...):
于 2013-05-01T06:15:54.477 に答える