class PermissionManager(object): def __init__(self, session): self.manager = Manager() self.db = self.manager.db('appinit') self.session = session # self.settings = settings def get_route_uids(self, route, permission): route = PermissionsRoute(self.db, route) return route.get_uids(permission) def list_user_permissions(self): user = PermissionsUser(self.db, self.session.uid) return user.list_permissions() def get_route(self, route=None): permissions = self.list_user_permissions() if route == None: return permissions else: if route in permissions: return permissions[route] else: return []
def logging(session_mgr, request, response, module_path, result, **kwargs): manager = Manager() session = session_mgr.get() # get list of parent module chains not including the full path of this one parent_modules = [] module_chain = module_path.split('.') temp_parent_chain = module_chain[0] for module_name in module_chain[1:]: parent_modules.append(temp_parent_chain) temp_parent_chain += '.' + module_name # do not log if the API call is part of the logging API (search, etc) if 'logging' in parent_modules: return # convert(request.headers) log = { 'timestamp': Manager.get_current_time(), 'path': module_path, 'parent_modules': parent_modules, 'uid': session.uid, 'source_ip': request.remote_addr, 'method': request.method, # don't yet know whether a module exists to get an action from 'action': None, # permissions is represented as a set, so convert it to a list # else the database can't encode it 'permissions': list(session_mgr.get_permissions()), 'request': { 'headers': dict(request.headers), 'data': { 'form': request.form, 'args': request.args, 'data': request.data, }, 'kwargs': json.dumps(kwargs, default=convert, ensure_ascii=False).encode("utf-8"), 'cookies': request.cookies, }, 'response': { 'status': response.status, 'headers': dict(response.headers), 'data': response.get_data() } } if result['module'] is not None: log['action'] = getattr(result['module'], 'action', None) if result['error'] is not None: # the actual exception, if present, can't and shouldn't be encoded into mongo # create a copy of result[error] with exception field explicitly left out log['failure'] = { key: result['error'][key] for key in result['error'] if key != 'exception' } # set up db db = manager.db("logging") db.logs.insert_one(log)