def mergeRole(self, roleId): ''' @see: IRbacService.mergeRole ''' sql = self.session().query(RoleNode).filter(RoleNode.role == roleId) if sql.count() > 0: return None # on rbac roles add a row in order to identify from rbac the correspondent role # this will help to have the same query for both user&role rbac rbacRole = RbacRole() rbacRole.rbac = roleId rbacRole.role = roleId roleNode = RoleNode() roleNode.role = roleId rootId = self._rootId() rootNode = self.session().query(RoleNode).get(rootId) roleNode.left = rootNode.right roleNode.right = roleNode.left + 1 rootNode.right += 2 self.session().add(rbacRole) self.session().add(roleNode) self.session().add(rootNode)
def _rootId(self): ''' Return the root id, that has the lower left value ''' if self._idRoot is None: sql = self.session().query(RoleNode) sql = sql.order_by(RoleNode.left) rootNode = sql.first() if not rootNode: rootNode = RoleNode() rootNode.left = 1 rootNode.right = 2 self.session().add(rootNode) self.session().flush((rootNode, )) self._idRoot = rootNode.id return self._idRoot