2020-08-22 12:02:39 +00:00
|
|
|
from ..database import db
|
|
|
|
from sqlalchemy.orm.collections import attribute_mapped_collection
|
|
|
|
from flask import current_app
|
|
|
|
from werkzeug.local import LocalProxy
|
2020-09-01 23:32:55 +00:00
|
|
|
|
2020-08-22 12:02:39 +00:00
|
|
|
logger = LocalProxy(lambda: current_app.logger)
|
|
|
|
|
|
|
|
association_table = db.Table('user_group',
|
2020-09-01 23:32:55 +00:00
|
|
|
db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
|
|
|
|
db.Column('group_id', db.Integer, db.ForeignKey('group.id'))
|
|
|
|
)
|
2020-08-22 12:02:39 +00:00
|
|
|
|
2020-09-01 23:09:24 +00:00
|
|
|
|
2020-08-22 12:02:39 +00:00
|
|
|
class User(db.Model):
|
|
|
|
""" Database Object for User
|
|
|
|
|
2020-09-01 23:32:55 +00:00
|
|
|
Table for all saved User
|
2020-08-22 12:02:39 +00:00
|
|
|
|
|
|
|
Attributes:
|
|
|
|
id: Id in Database as Primary Key.
|
2020-09-01 19:36:25 +00:00
|
|
|
uid: User ID used by authentication provider
|
2020-09-01 23:32:55 +00:00
|
|
|
display_name: Name to show
|
2020-08-22 12:02:39 +00:00
|
|
|
firstname: Firstname of the User
|
|
|
|
lastname: Lastname of the User
|
|
|
|
mail: mail address of the User
|
|
|
|
"""
|
|
|
|
__tablename__ = 'user'
|
2020-09-01 23:32:55 +00:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
uid = db.Column(db.String(30))
|
|
|
|
display_name = db.Column(db.String(30))
|
|
|
|
firstname = db.Column(db.String(30))
|
|
|
|
lastname = db.Column(db.String(30))
|
|
|
|
mail = db.Column(db.String(30))
|
|
|
|
groups = db.relationship("Group", secondary=association_table)
|
|
|
|
sessions = db.relationship("AccessToken", back_populates="user")
|
|
|
|
attributes = db.relationship("UserAttribute", collection_class=attribute_mapped_collection('name'),
|
|
|
|
cascade="all, delete")
|
|
|
|
|
|
|
|
def set_attribute(self, name, value):
|
2020-08-25 02:34:57 +00:00
|
|
|
if name in self.attributes:
|
|
|
|
self.attributes[name].value = value
|
|
|
|
else:
|
|
|
|
self.attributes[name] = UserAttribute(name=name, value=value)
|
|
|
|
|
2020-09-01 23:32:55 +00:00
|
|
|
def add_group(self, name):
|
2020-09-01 19:36:25 +00:00
|
|
|
r = Group.query.filter_by(name=name).first()
|
2020-08-25 02:34:57 +00:00
|
|
|
if not r:
|
2020-09-01 19:36:25 +00:00
|
|
|
r = Group(name=name)
|
2020-08-25 02:34:57 +00:00
|
|
|
self.groups.append(r)
|
2020-09-01 23:32:55 +00:00
|
|
|
|
|
|
|
def update_data(self, data):
|
2020-08-22 12:02:39 +00:00
|
|
|
logger.debug("update data of user")
|
|
|
|
if 'uid' in data:
|
|
|
|
self.uid = data['uid']
|
|
|
|
if 'firstname' in data:
|
|
|
|
self.firstname = data['firstname']
|
|
|
|
if 'lastname' in data:
|
|
|
|
self.lastname = data['lastname']
|
|
|
|
if 'mail' in data:
|
|
|
|
self.mail = data['mail']
|
2020-09-01 23:32:55 +00:00
|
|
|
if 'display_name' in data:
|
|
|
|
self.display_name = data['display_name']
|
2020-08-22 12:02:39 +00:00
|
|
|
|
2020-09-02 11:07:21 +00:00
|
|
|
def default(self):
|
2020-08-22 14:47:56 +00:00
|
|
|
return {
|
2020-08-25 02:34:57 +00:00
|
|
|
# TODO: username should be UID?
|
|
|
|
"username": self.uid,
|
2020-09-01 23:32:55 +00:00
|
|
|
"display_name": self.display_name,
|
2020-08-22 14:47:56 +00:00
|
|
|
"firstname": self.firstname,
|
|
|
|
"lastname": self.lastname,
|
|
|
|
"mail": self.mail,
|
2020-08-25 02:34:57 +00:00
|
|
|
"groups": ["user"] + [g.name for g in self.groups]
|
2020-08-22 14:47:56 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 12:02:39 +00:00
|
|
|
|
|
|
|
class UserAttribute(db.Model):
|
|
|
|
__tablename__ = 'userAttribute'
|
2020-09-01 23:32:55 +00:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
user = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
|
|
|
name = db.Column(db.String(30))
|
2020-08-22 12:02:39 +00:00
|
|
|
value = db.Column(db.String(192))
|
|
|
|
|
2020-09-01 23:32:55 +00:00
|
|
|
|
2020-09-01 19:36:25 +00:00
|
|
|
group_permission_association_table = db.Table('group_permission',
|
2020-09-01 23:32:55 +00:00
|
|
|
db.Column('group_id', db.Integer, db.ForeignKey('group.id')),
|
|
|
|
db.Column('permission_id', db.Integer, db.ForeignKey('permission.id'))
|
|
|
|
)
|
2020-09-01 19:36:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Group(db.Model):
|
2020-08-22 12:02:39 +00:00
|
|
|
__tablename__ = 'group'
|
2020-09-01 23:32:55 +00:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2020-08-22 12:02:39 +00:00
|
|
|
name = db.Column(db.String(30))
|
2020-09-01 19:36:25 +00:00
|
|
|
permissions = db.relationship("Permission", secondary=group_permission_association_table)
|
2020-08-22 12:02:39 +00:00
|
|
|
|
2020-08-25 02:34:57 +00:00
|
|
|
def toJSON(self):
|
|
|
|
return {
|
|
|
|
'name': self.name
|
|
|
|
}
|
|
|
|
|
2020-09-01 19:36:25 +00:00
|
|
|
|
|
|
|
class Permission(db.Model):
|
|
|
|
__tablename__ = 'permission'
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
name = db.Column(db.String(30))
|