def get_unencry_rw_repos_by_user(request): """Get all unencrypted repos a logged-in user can read and write. """ username = request.user.username if not username: return [] def has_repo(repos, repo): for r in repos: if repo.id == r.id: return True return False org_id = request.user.org.org_id if is_org_context(request) else None owned_repos, shared_repos, groups_repos, public_repos = get_user_repos( username, org_id=org_id) accessible_repos = [] for r in owned_repos: if not has_repo(accessible_repos, r) and not r.encrypted: accessible_repos.append(r) for r in shared_repos + groups_repos + public_repos: if not has_repo(accessible_repos, r) and not r.encrypted: if check_folder_permission(request, r.id, '/') == 'rw': accessible_repos.append(r) return accessible_repos
def remove_repo_passwds(self): """ Remove all repo decryption passwords stored on server. """ owned_repos, shared_repos, groups_repos = get_user_repos(self) def has_repo(repos, repo): for r in repos: if repo.id == r.id: return True return False passwd_setted_repos = [] for r in owned_repos + groups_repos: if not has_repo(passwd_setted_repos, r) and r.encrypted and \ is_passwd_set(r.id, self.email): passwd_setted_repos.append(r) for r in shared_repos: # For compatibility with diffrent fields names in Repo and # SharedRepo objects. r.id = r.repo_id r.name = r.repo_name r.desc = r.repo_desc if not has_repo(passwd_setted_repos, r) and r.encrypted and \ is_passwd_set(r.id, self.email): passwd_setted_repos.append(r) for r in passwd_setted_repos: unset_repo_passwd(r.id, self.email)
def remove_repo_passwds(self): """ Remove all repo decryption passwords stored on server. """ from seahub.utils import get_user_repos owned_repos, shared_repos, groups_repos, public_repos = get_user_repos( self.email) def has_repo(repos, repo): for r in repos: if repo.id == r.id: return True return False passwd_setted_repos = [] for r in owned_repos + groups_repos: if not has_repo(passwd_setted_repos, r) and r.encrypted and \ is_passwd_set(r.id, self.email): passwd_setted_repos.append(r) for r in shared_repos + public_repos: # For compatibility with diffrent fields names in Repo and # SharedRepo objects. r.id = r.repo_id r.name = r.repo_name r.desc = r.repo_desc if not has_repo(passwd_setted_repos, r) and r.encrypted and \ is_passwd_set(r.id, self.email): passwd_setted_repos.append(r) for r in passwd_setted_repos: unset_repo_passwd(r.id, self.email)
def get_search_repos_map(search_repo, username, org_id, shared_from, not_shared_from): # for getting repo type map def get_repo_type_map(repo_list, repo_type): repo_type_map = {} for repo in repo_list: repo_type_map[repo.id] = repo_type return repo_type_map repo_id_map = {} repo_type_map = {} if search_repo == 'mine': repo_list = get_owned_repos(username, org_id=org_id) repo_type_map = get_repo_type_map(repo_list, search_repo) elif search_repo == 'shared': repo_list = get_shared_repos(username, org_id=org_id) if shared_from: repo_list = [r for r in repo_list if r.user == shared_from] if not_shared_from: repo_list = [r for r in repo_list if r.user != not_shared_from] repo_type_map = get_repo_type_map(repo_list, search_repo) elif search_repo == 'group': repo_list = get_group_repos(username, org_id=org_id) repo_type_map = get_repo_type_map(repo_list, search_repo) elif search_repo == 'public': repo_list = get_public_repos(username, org_id=org_id) repo_type_map = get_repo_type_map(repo_list, search_repo) else: owned_repos, shared_repos, group_repos, public_repos = get_user_repos( username, org_id=org_id) repo_list = owned_repos + shared_repos + group_repos + public_repos # priority is group > public > mine(or shared) repo_type_map.update(get_repo_type_map(owned_repos, 'mine')) repo_type_map.update(get_repo_type_map(shared_repos, 'shared')) repo_type_map.update(get_repo_type_map(public_repos, 'public')) repo_type_map.update(get_repo_type_map(group_repos, 'group')) for repo in repo_list: subrepo_tag = False search_repo_id = repo.id if repo.origin_repo_id: search_repo_id = repo.origin_repo_id subrepo_tag = True # search priority: repo > subrepo if search_repo_id not in repo_id_map or subrepo_tag is False: repo_id_map[search_repo_id] = repo return repo_id_map, repo_type_map
def get_search_repos_map(search_repo, username, org_id): def map_repo(repo_list, val): repo_map = {} for repo in repo_list: repo_map[repo.id] = val return repo_map # Get repos owned_repos, shared_repos, groups_repos, public_repos = get_user_repos( username, org_id=org_id) if search_repo == 'mine': repo_list = owned_repos elif search_repo == 'shared': repo_list = shared_repos elif search_repo == 'group': repo_list = groups_repos elif search_repo == 'public': repo_list = public_repos else: repo_list = owned_repos + shared_repos + groups_repos + public_repos # Create repo_id_map repo_id_map = {} for repo in repo_list: has_origin = False search_repo_id = repo.id if repo.origin_repo_id: search_repo_id = repo.origin_repo_id has_origin = True if search_repo_id not in repo_id_map or not has_origin: repo_id_map[search_repo_id] = repo # Create repo_type_map if search_repo in ('mine', 'shared', 'group', 'public'): repo_type_map = map_repo(repo_list, search_repo) else: repo_type_map = {} repo_type_map.update(map_repo(owned_repos, 'mine')) repo_type_map.update(map_repo(shared_repos, 'shared')) repo_type_map.update(map_repo(public_repos, 'public')) repo_type_map.update(map_repo(groups_repos, 'group')) return repo_id_map, repo_type_map
def get(self, request, format=None): """List all wikis. """ # parse request params filter_by = { 'mine': False, 'shared': False, 'group': False, 'org': False, } rtype = request.GET.get('type', "") if not rtype: # set all to True, no filter applied filter_by = filter_by.fromkeys(filter_by.iterkeys(), True) for f in rtype.split(','): f = f.strip() filter_by[f] = True username = request.user.username org_id = request.user.org.org_id if is_org_context(request) else None (owned, shared, groups, public) = get_user_repos(username, org_id) filter_repo_ids = [] if filter_by['mine']: filter_repo_ids += ([r.id for r in owned]) if filter_by['shared']: filter_repo_ids += ([r.id for r in shared]) if filter_by['group']: filter_repo_ids += ([r.id for r in groups]) if filter_by['org']: filter_repo_ids += ([r.id for r in public]) filter_repo_ids = list(set(filter_repo_ids)) ret = [ x.to_dict() for x in Wiki.objects.filter(repo_id__in=filter_repo_ids) ] return Response({'data': ret})
def remove_org_repo_passwds(self, org_id): """ Remove all org repo decryption passwords stored on server. """ from seahub.utils import get_user_repos owned_repos, shared_repos, groups_repos, public_repos = get_user_repos(self.email, org_id=org_id) def has_repo(repos, repo): for r in repos: if repo.id == r.id: return True return False passwd_setted_repos = [] for r in owned_repos + shared_repos + groups_repos + public_repos: if not has_repo(passwd_setted_repos, r) and r.encrypted and \ is_passwd_set(r.id, self.email): passwd_setted_repos.append(r) for r in passwd_setted_repos: unset_repo_passwd(r.id, self.email)
def get(self, request, name=None): username = request.user.username org_id = request.user.org.org_id if is_org_context(request) else None owned_repos, shared_repos, groups_repos, public_repos = get_user_repos( username, org_id=org_id) repo_id_list =\ [repo.id for repo in owned_repos] + \ [repo.id for repo in shared_repos] + \ [repo.id for repo in groups_repos] + \ [repo.id for repo in public_repos] if name is None: def with_quotes(s): return "'" + s + "'" # sql = """ # SELECT DISTINCT t.* FROM tags_tags t # LEFT JOIN tags_filetag f ON t.id = f.tag_id # LEFT JOIN tags_fileuuidmap m ON f.uuid_id = m.uuid # WHERE m.repo_id IN (%(repo_id_list)s) # """ # tag_list = Tags.objects.raw(sql, {'repo_id_list': repo_id_list}) repo_id_text = ', '.join(map(with_quotes, repo_id_list)) sql = """ SELECT DISTINCT t.* FROM tags_tags t LEFT JOIN tags_filetag f ON t.id = f.tag_id LEFT JOIN tags_fileuuidmap m ON f.uuid_id = m.uuid WHERE m.repo_id IN ({}) """.format(repo_id_text) tag_list = Tags.objects.raw(sql) tag_list = [tag.to_dict() for tag in tag_list] return Response(tag_list, status=status.HTTP_200_OK) else: tag = get_object_or_404(Tags, name=name) fileuuidmap_list = tag.fileuuidmap_set.all() repo = None dir_list = [] file_list = [] for fileuuidmap in fileuuidmap_list: if repo is None or repo.id != fileuuidmap.repo_id: repo = seafile_api.get_repo(fileuuidmap.repo_id) fullpath = posixpath.join(fileuuidmap.parent_path, fileuuidmap.filename) dirent = seafile_api.get_dirent_by_path( fileuuidmap.repo_id, fullpath) dirent.repo_id = repo.id dirent.parent_path = fileuuidmap.parent_path dirent.fullpath = fullpath dirent.last_modified = dirent.mtime if stat.S_ISDIR(dirent.mode): dir_list.append(dirent) else: if repo.version == 0: file_size = seafile_api.get_file_size( repo.store_id, repo.version, dirent.obj_id) else: file_size = dirent.size dirent.file_size = file_size if file_size else 0 can_preview, err_msg = can_preview_file( dirent.obj_name, file_size, repo) dirent.can_preview = can_preview file_list.append(dirent) dirent_list = [] for d in dir_list: d_ = { 'is_dir': True, 'obj_name': d.obj_name, 'last_modified': d.last_modified, 'last_update': translate_seahub_time(d.last_modified), 'p_dpath': d.fullpath, 'perm': d.permission, 'repo_id': d.repo_id, 'parent_path': d.parent_path, } dirent_list.append(d_) size = int( request.GET.get('thumbnail_size', THUMBNAIL_DEFAULT_SIZE)) for f in file_list: f_ = { 'is_file': True, 'obj_name': f.obj_name, 'last_modified': f.last_modified, 'last_update': translate_seahub_time(f.last_modified), 'file_size': filesizeformat(f.file_size), 'obj_id': f.obj_id, 'perm': f.permission, 'can_preview': f.can_preview, 'repo_id': f.repo_id, 'parent_path': f.parent_path, } if not repo.encrypted and ENABLE_THUMBNAIL: file_ext = os.path.splitext(f.obj_name)[1][1:].lower() file_type = FILEEXT_TYPE_MAP.get(file_ext) if file_type == IMAGE: f_['is_img'] = True if file_type == VIDEO and ENABLE_VIDEO_THUMBNAIL: f_['is_video'] = True if file_type == IMAGE or file_type == VIDEO and ENABLE_VIDEO_THUMBNAIL: thumbnail_file_path = os.path.join( THUMBNAIL_ROOT, str(size), f.obj_id) thumbnail_exist = os.path.exists(thumbnail_file_path) if thumbnail_exist: src = get_thumbnail_src(f.repo_id, size, f.fullpath) f_['encoded_thumbnail_src'] = urlquote(src) dirent_list.append(f_) return Response(dirent_list, status=status.HTTP_200_OK)
except Exception, e: logger.error(e) return False else: for dirent in dirs: if stat.S_ISDIR(dirent.props.mode): return True return False def has_repo(repos, repo): for r in repos: if repo.id == r.id: return True return False owned_repos, shared_repos, groups_repos, public_repos = get_user_repos( username) accessible_repos = [] for r in owned_repos: if not has_repo(accessible_repos, r) and not r.encrypted: r.has_subdir = check_has_subdir(r) accessible_repos.append(r) for r in shared_repos + public_repos: # For compatibility with diffrent fields names in Repo and # SharedRepo objects. r.id = r.repo_id r.name = r.repo_name r.desc = r.repo_desc