Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.82 KB | None | 0 0
  1. class User(UserMixin, SurrogatePK, Model):
  2.     """A user of the app."""
  3.  
  4.     __tablename__ = 'users'
  5.     username = Column(db.String(80), unique=True, nullable=False)
  6.     first_name = Column(db.String(80))
  7.     last_name = Column(db.String(80))
  8.     mobile_number = Column(db.String(80))
  9.     created_on = Column(db.DateTime, default=dt.datetime.now())
  10.     last_update = Column(db.DateTime, onupdate=dt.datetime.now())
  11.     is_deleted = Column(db.Boolean, default=False)
  12.     email_verified = Column(db.Boolean, default=False)
  13.     email = Column(db.String(80), unique=True, nullable=False)
  14.     #: The hashed password
  15.     password = Column(db.Binary(128), nullable=True)
  16.     roles = relationship("Role",
  17.                          secondary=rel_users_roles,
  18.                          backref="users")
  19.  
  20.  
  21.     def __init__(self, password=None, **kwargs):
  22.         """Create instance."""
  23.         db.Model.__init__(self, **kwargs)
  24.         if password:
  25.             self.set_password(password)
  26.         else:
  27.             self.password = None
  28.  
  29.         # internal flag
  30.         self.permission_types = []
  31.  
  32.     def traverse_up_tree_from_node(self, starting_node):
  33.         for role in starting_node.roles:
  34.             for user in role.users:
  35.                 if user == self and role.name in self.permission_types:
  36.                     print(user, 'has permission')
  37.                     return True
  38.         if starting_node.parent:
  39.             self.traverse_up_tree_from_node(starting_node.parent)
  40.         else:
  41.             return False
  42.  
  43.     @property
  44.     def can(self):
  45.         self.permission_types = []
  46.         return self
  47.  
  48.     @property
  49.     def view(self):
  50.         self.permission_types = ['READ', 'READ-WRITE', 'ADMIN']
  51.         return self
  52.  
  53.     @property
  54.     def modify(self):
  55.         self.permission_types = ['READ-WRITE', 'ADMIN']
  56.         return self
  57.  
  58.     @property
  59.     def delete(self):
  60.         self.permission_types = ['ADMIN']
  61.         return self
  62.  
  63.     def user_id(self, _user_id):
  64.         """If a user has no roles for any scope they are 'free-floating' users
  65.        and anyone should be able to edit them or view them. If a user has at least
  66.        one role then we can look at all of their roles and check to see that we have
  67.        permission in at least one of those roles to do the requested action."""
  68.         user = self.query.filter_by(_user_id).first()
  69.         roles_with_permission = []
  70.         for role in user.roles:
  71.             if self.traverse_up_tree_from_node(role.scope):
  72.                 roles_with_permission.append(role.scope)
  73.         return True if roles_with_permission else False
  74.  
  75.     def role_id(self, _role_id):
  76.         """Start with the role to be modified, and use the attached scope as
  77.        the starting scope for determining permission to modify the role."""
  78.         role = Role.query.filter_by(id=_role_id).first()
  79.         return self.traverse_up_tree_from_node(role.scope)
  80.  
  81.     def scope_id(self, _scope_id):
  82.         """Start with the scope to be read/modified/deleted and go up the tree
  83.        looking for permissions on the target scope or any parent."""
  84.         scope = Scope.query.filter_by(id=_scope_id).first()
  85.         return self.traverse_up_tree_from_node(scope)
  86.  
  87.     def category_id(self, _category_id):
  88.         """The approach here is to find every scope that is being used by the
  89.        category in question. The user would have to have the right permission
  90.        on every scope attached to the category in orer to modify/read/delete
  91.        the category."""
  92.         category = Category.query.filter_by(id=_category_id).first()
  93.         scopes_with_permission = []
  94.         for scope in category.scopes:
  95.             if self.traverse_up_tree_from_node(scope):
  96.                 scopes_with_permission.append(scope)
  97.         return len(scopes_with_permission == len(category.scopes))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement