def get_permissions(self, user_id, filter=None): """ get user permissions :param user_id: user id :return: list """ user = self.find_by_id(user_id) if not user: return [] relate_team = Model.build_model('team_members').find( {'user_id': user_id}) relate_team = list(relate_team) team_ids = list(map(lambda i: i.get('team_id'), relate_team)) role_ids = [] menus = [] if team_ids: team_roles = Model.build_model('team_roles').find( {'team_id': { '$in': team_ids }}) for item in team_roles: role_ids.append(item.get('role_id')) roles = db.collection('user_roles').find({'user_id': user_id}) roles = list(roles) if roles: ids = map(lambda i: i['role_id'], roles) role_ids += list(ids) if role_ids: where = {'role_id': {'$in': role_ids}} records = db.collection('role_menus').find(where).sort('id', 1) for record in records: where = filter or {} where['_id'] = ObjectId(record['m_id']) item = Menu.find_one(where) if not item or item.get( 'mpid') == '-1' or item.get('status') < 1: continue item['actions'] = record.get('actions', ['get']) menus.append(item) roles = Role().find_by_ids(role_ids) return menus, roles
def test_property(self): collection_name = 'tests' model = Model.build_model(collection_name) assert model.name == collection_name assert isinstance(model.collection, Collection) assert model.collection.name == collection_name assert isinstance(model.db, Mongo)
def check_workspace(self, path=None, child=None): if not path: path = self.workspace # filename = workspace # else: filename = path if child: filename += '/' + child index = filename.replace(self.workspace, '') if not index: index = '/' if os.path.isfile(filename): record = Model.build_model('playbooks').find_one({'path': index}) if not record: os.remove(filename) return True files = os.listdir(filename) for file in files: # 遍历文件夹 self.check_workspace(filename, file) return True
def setUp(self): data = {'uuid': str(uuid.uuid4())} self.data = data self.model = Model.build_model('tests')
def load_book_from_db(self, name, roles=None, build_id=False): book = Book.find_one({'name': name}) if not book: return False files = Model.build_model('playbook').find({'book_id': str(book['_id'])})\ .sort([('is_edit', pymongo.ASCENDING), ('path', pymongo.ASCENDING)]) files = list(files) if not files: return False if build_id: bookspace = os.path.join(self.book, md5(str(build_id))) else: bookspace = self.get_book_space(name) def parse_register(record): register = record.get('register') if not register: return record c_ids = map(lambda i: ObjectId(i), register) cfg_records = Configuration.find({'_id': {'$in': list(c_ids)}}) if not cfg_records: return record try: variables = {} content = yaml.safe_load(record.get('content', '')) if not content: return record vault = Vault({'vault_pass': config.vault.get('secret')}) for cfg in cfg_records: config_vars = cfg.get('variables') if not config_vars: continue for k, v in config_vars.items(): key = '_'.join( ['ECLOGUE', 'CONFIG', cfg.get('name', ''), k]) is_encrypt = Vault.is_encrypted(v) value = v if is_encrypt: value = vault.decrypt_string(value) variables[key] = value content = dict(content) content.update(variables) record['content'] = yaml.safe_dump(content) except Exception as e: print(e) return record self.check_workspace(path=self._check_make(bookspace)) for item in files: item = parse_register(item) if roles and item.get('folder'): folder = item.get('folder') if folder and folder not in roles: continue filename = bookspace + item.get('path') if item['is_dir']: if os.path.isdir(filename): continue self.mkdir(filename) else: if os.path.isfile(filename): file_hash = file_md5(filename) if item.get('md5') and item['md5'] == file_hash: continue dirname = os.path.dirname(filename) if not os.path.exists(dirname): os.makedirs(dirname) if item['is_edit']: Model.build_model('playbooks').update_one( {'_id': item['_id']}, {'$set': { 'md5': md5(item['content']) }}) with open(filename, 'w') as stream: stream.write(item['content']) else: with open(filename, 'wb') as stream: db.fs_bucket().download_to_stream( item['file_id'], stream) return bookspace
def import_book_from_dir(self, home_path, book_id, exclude=None, links=False, prefix='/'): """ import dir file to db @todo """ book_id = str(book_id) exclude = exclude or ['*.retry'] bucket = [] cursor = 0 home_path = home_path.rstrip('/') parent = home_path book_record = Book.find_by_id(book_id) model = Model.build_model('playbook') playbooks = model.find({'book_id': book_id}) paths = map(lambda i: i['path'], playbooks) paths = list(paths) pattern = '|'.join(exclude).replace('*', '.*?') home_path = '/'.join([home_path, '']) for current, dirs, files in os.walk(home_path, topdown=True, followlinks=links): pathname = current.replace(home_path, '') if pathname != '/': pathname = os.path.join(prefix, pathname) if exclude: match = re.search(pattern, pathname) if match: continue if pathname in paths: index = paths.index(pathname) paths.pop(index) dir_record = { 'book_id': str(book_record.get('_id')), 'path': pathname, 'is_dir': True, 'is_edit': False, 'seq_no': cursor, 'parent': None, 'created_at': int(time.time()), } if not current == home_path: dir_record['parent'] = parent meta = get_meta(pathname) dir_record.update(meta) dir_record['additions'] = meta parent = pathname bucket.append(dir_record) for file in files: pathname = parent.rstrip('/') + '/' + file if exclude: match = re.match(pattern, pathname) if match: continue cursor += 1 filename = current + '/' + file can_edit = is_edit(filename) file_record = dir_record.copy() file_record['is_edit'] = can_edit file_record['path'] = pathname file_record['parent'] = parent file_record['is_dir'] = False file_record['seq_no'] = cursor if is_edit: with open(filename, 'r', encoding='utf-8') as fd: file_record['content'] = fd.read() file_record['md5'] = md5(file_record['content']) file_record['is_encrypt'] = Vault.is_encrypted( file_record['content']) meta = get_meta(file_record['path']) file_record['additions'] = meta file_record.update(meta) bucket.append(file_record) cursor += 1 is_entry = filter(lambda i: i.get('role') == 'entry', bucket) is_entry = list(is_entry) # if not entry set book status to disable if not is_entry: Book.update_one({'_id': ObjectId(book_id)}, {'$set': { 'status': 0 }}) for path in paths: model.delete_one({'book_id': book_id, 'path': path}) mapping = {} map(lambda i: {mapping['path']: i}, playbooks) for item in bucket: record = mapping.get(item['path']) if not record: model.insert_one(item) continue else: # inherit old additions if record['additions']: item['additions'].update(record['additions']) model.update_one({'_id': record['_id']}, {'$set': item}) return bucket