def add(self, wiki_name, username, permission='private', repo_id=None, org_id=-1): if not permission: permission = 'private' from .utils import slugfy_wiki_name slug = slugfy_wiki_name(wiki_name) if self.filter(slug=slug).count() > 0: raise DuplicateWikiNameError now = timezone.now() if repo_id is None: # create new repo to store the wiki pages if org_id > 0: repo_id = seafile_api.create_org_repo(wiki_name, '', username, passwd=None, org_id=org_id) else: repo_id = seafile_api.create_repo(wiki_name, '', username, passwd=None) repo = seafile_api.get_repo(repo_id) assert repo is not None wiki = self.model(username=username, name=wiki_name, slug=slug, repo_id=repo.id, permission=permission, created_at=now) wiki.save(using=self._db) return wiki
def _create_repo_common(request, repo_name, repo_desc, encryption, uuid, magic_str, encrypted_file_key): """Common logic for creating repo. Returns: newly created repo id. Or ``None`` if error raised. """ username = request.user.username try: if not encryption: if is_org_context(request): org_id = request.user.org.org_id repo_id = seafile_api.create_org_repo(repo_name, repo_desc, username, None, org_id) else: repo_id = seafile_api.create_repo(repo_name, repo_desc, username, None) else: if is_org_context(request): org_id = request.user.org.org_id repo_id = seafile_api.create_org_enc_repo( uuid, repo_name, repo_desc, username, magic_str, encrypted_file_key, enc_version=2, org_id=org_id) else: repo_id = seafile_api.create_enc_repo( uuid, repo_name, repo_desc, username, magic_str, encrypted_file_key, enc_version=2) except SearpcError as e: logger.error(e) repo_id = None return repo_id
def add(self, wiki_name, username, permission='private', repo_id=None, org_id=-1): if not permission: permission = 'private' from .utils import slugfy_wiki_name slug = slugfy_wiki_name(wiki_name) if self.filter(slug=slug).count() > 0: raise DuplicateWikiNameError now = timezone.now() if repo_id is None: # create new repo to store the wiki pages if org_id > 0: repo_id = seafile_api.create_org_repo(wiki_name, '', username, passwd=None, org_id=org_id) else: repo_id = seafile_api.create_repo(wiki_name, '', username, passwd=None) repo = seafile_api.get_repo(repo_id) assert repo is not None wiki = self.model(username=username, name=wiki_name, slug=slug, repo_id=repo.id, permission=permission, created_at=now) wiki.save(using=self._db) return wiki
def create_user_draft_repo(username, org_id=-1): repo_name = 'Drafts' if org_id > 0: repo_id = seafile_api.create_org_repo(repo_name, '', username, org_id) else: repo_id = seafile_api.create_repo(repo_name, '', username) return repo_id
def create_group_repo(request, group_id): """Create a repo and share it to current group""" content_type = 'application/json; charset=utf-8' def json_error(err_msg): result = {'error': err_msg} return HttpResponseBadRequest(json.dumps(result), content_type=content_type) group_id = int(group_id) group = get_group(group_id) if not group: return json_error(_(u'Failed to create: the group does not exist.')) # Check whether user belongs to the group. username = request.user.username if not is_group_user(group_id, username): return json_error(_(u'Failed to create: you are not in the group.')) form = SharedRepoCreateForm(request.POST) if not form.is_valid(): return json_error(str(form.errors.values()[0])) # Form is valid, create group repo repo_name = form.cleaned_data['repo_name'] repo_desc = form.cleaned_data['repo_desc'] permission = form.cleaned_data['permission'] encryption = int(form.cleaned_data['encryption']) uuid = form.cleaned_data['uuid'] magic_str = form.cleaned_data['magic_str'] encrypted_file_key = form.cleaned_data['encrypted_file_key'] if is_org_context(request): org_id = request.user.org.org_id try: if encryption: repo_id = seafile_api.create_org_enc_repo(uuid, repo_name, repo_desc, username, magic_str, encrypted_file_key, enc_version=2, org_id=org_id) else: repo_id = seafile_api.create_org_repo(repo_name, repo_desc, username, None, org_id) except SearpcError, e: logger.error(e) return json_error(_(u'Failed to create')) try: seafile_api.add_org_group_repo(repo_id, org_id, group.id, username, permission) except SearpcError, e: logger.error(e) return json_error(_(u'Failed to create: internal error.'))
def create_user_draft_repo(username, org_id=-1): repo_name = 'Drafts' if org_id > 0: repo_id = seafile_api.create_org_repo(repo_name, '', username, passwd=None, org_id=org_id) else: repo_id = seafile_api.create_repo(repo_name, '', username, passwd=None) return repo_id
def create_group_repo(request, group_id): """Create a repo and share it to current group""" content_type = 'application/json; charset=utf-8' def json_error(err_msg): result = {'error': err_msg} return HttpResponseBadRequest(json.dumps(result), content_type=content_type) group_id = int(group_id) group = get_group(group_id) if not group: return json_error(_(u'Failed to create: the group does not exist.')) # Check whether user belongs to the group. username = request.user.username if not is_group_user(group_id, username): return json_error(_(u'Failed to create: you are not in the group.')) form = SharedRepoCreateForm(request.POST) if not form.is_valid(): return json_error(str(form.errors.values()[0])) # Form is valid, create group repo repo_name = form.cleaned_data['repo_name'] repo_desc = form.cleaned_data['repo_desc'] permission = form.cleaned_data['permission'] encryption = int(form.cleaned_data['encryption']) uuid = form.cleaned_data['uuid'] magic_str = form.cleaned_data['magic_str'] encrypted_file_key = form.cleaned_data['encrypted_file_key'] if is_org_context(request): org_id = request.user.org.org_id try: if encryption: repo_id = seafile_api.create_org_enc_repo( uuid, repo_name, repo_desc, username, magic_str, encrypted_file_key, enc_version=2, org_id=org_id) else: repo_id = seafile_api.create_org_repo(repo_name, repo_desc, username, None, org_id) except SearpcError, e: logger.error(e) return json_error(_(u'Failed to create')) try: seafile_api.add_org_group_repo(repo_id, org_id, group.id, username, permission) except SearpcError, e: logger.error(e) return json_error(_(u'Failed to create: internal error.'))
def create_default_library(request): """Create a default library for user. Arguments: - `username`: """ username = request.user.username # Disable user guide no matter user permission error or creation error, # so that the guide popup only show once. UserOptions.objects.disable_user_guide(username) if not request.user.permissions.can_add_repo(): return if is_org_context(request): org_id = request.user.org.org_id default_repo = seafile_api.create_org_repo(name=_("My Library"), desc=_("My Library"), username=username, passwd=None, org_id=org_id) else: default_repo = seafile_api.create_repo(name=_("My Library"), desc=_("My Library"), username=username, passwd=None) sys_repo_id = get_system_default_repo_id() if sys_repo_id is None: return try: dirents = seafile_api.list_dir_by_path(sys_repo_id, '/') for e in dirents: obj_name = e.obj_name seafile_api.copy_file(sys_repo_id, '/', obj_name, default_repo, '/', obj_name, username, 0) except SearpcError as e: logger.error(e) return UserOptions.objects.set_default_repo(username, default_repo) return default_repo
def create_default_library(request): """Create a default library for user. Arguments: - `username`: """ username = request.user.username # Disable user guide no matter user permission error or creation error, # so that the guide popup only show once. UserOptions.objects.disable_user_guide(username) if not request.user.permissions.can_add_repo(): return if is_org_context(request): org_id = request.user.org.org_id default_repo = seafile_api.create_org_repo(name=_("My Library"), desc=_("My Library"), username=username, passwd=None, org_id=org_id) else: default_repo = seafile_api.create_repo(name=_("My Library"), desc=_("My Library"), username=username, passwd=None) sys_repo_id = get_system_default_repo_id() if sys_repo_id is None: return try: dirents = seafile_api.list_dir_by_path(sys_repo_id, '/') for e in dirents: obj_name = e.obj_name seafile_api.copy_file(sys_repo_id, '/', obj_name, default_repo, '/', obj_name, username, 0) except SearpcError as e: logger.error(e) return UserOptions.objects.set_default_repo(username, default_repo) return default_repo
def post(self, request, group_id): """ Add a group library. Permission checking: 1. role permission, can_add_repo; 1. is group member; """ # argument check repo_name = request.data.get("repo_name", None) if not repo_name or \ not is_valid_dirent_name(repo_name): error_msg = "repo_name invalid." return api_error(status.HTTP_400_BAD_REQUEST, error_msg) password = request.data.get("password", None) if password and not config.ENABLE_ENCRYPTED_LIBRARY: error_msg = 'NOT allow to create encrypted library.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) permission = request.data.get('permission', PERMISSION_READ) if permission not in get_available_repo_perms(): error_msg = 'permission invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # permission check if not request.user.permissions.can_add_repo(): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) if not is_group_member(group_id, request.user.username): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) # create group repo org_id = -1 group_id = int(group_id) username = request.user.username is_org = False if is_org_context(request): is_org = True org_id = request.user.org.org_id repo_id = seafile_api.create_org_repo(repo_name, '', username, password, org_id) else: repo_id = seafile_api.create_repo(repo_name, '', username, password) repo = seafile_api.get_repo(repo_id) share_dir_to_group(repo, '/', username, username, group_id, permission, org_id if is_org else None) # for activities library_template = request.data.get("library_template", '') repo_created.send(sender=None, org_id=org_id, creator=username, repo_id=repo_id, repo_name=repo_name, library_template=library_template) # for notification share_repo_to_group_successful.send(sender=None, from_user=username, group_id=group_id, repo=repo, path='/', org_id=org_id) # for perm audit send_perm_audit_msg('add-repo-perm', username, group_id, repo_id, '/', permission) group_repo = seafile_api.get_group_shared_repo_by_path( repo_id, None, group_id, is_org) group_repo_info = get_group_repo_info(request, group_repo) group_repo_info['owner_email'] = username group_repo_info['owner_name'] = email2nickname(username) group_repo_info['owner_contact_email'] = email2contact_email(username) modifier = group_repo.last_modifier group_repo_info['modifier_email'] = modifier group_repo_info['modifier_name'] = email2nickname(modifier) group_repo_info['modifier_contact_email'] = email2contact_email( modifier) return Response(group_repo_info)
def create_org_repo(self, **kwargs): repo_id = seafile_api.create_org_repo(**kwargs) return repo_id
def create_org_repo(self, **kwargs): repo_id = seafile_api.create_org_repo(**kwargs) return repo_id
def get(self, request): """get all workspaces """ username = request.user.username org_id = -1 if is_org_context(request): org_id = request.user.org.org_id if org_id > 0: groups = ccnet_api.get_org_groups_by_user(org_id, username) else: groups = ccnet_api.get_groups(username, return_ancestors=True) owner_list = list() owner_list.append(username) for group in groups: group_user = '******' % group.id owner_list.append(group_user) workspace_list = list() for owner in owner_list: try: workspace = Workspaces.objects.get_workspace_by_owner(owner) except Exception as e: logger.error(e) error_msg = 'Internal Server Error.' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) if not workspace: if '@seafile_group' in owner: continue # permission check if not request.user.permissions.can_add_repo(): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) try: if org_id > 0: repo_id = seafile_api.create_org_repo( _("My Workspace"), _("My Workspace"), "dtable@seafile", org_id) else: repo_id = seafile_api.create_repo( _("My Workspace"), _("My Workspace"), "dtable@seafile") except Exception as e: logger.error(e) error_msg = 'Internal Server Error.' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) workspace = Workspaces.objects.create_workspace(owner, repo_id) # resource check repo_id = workspace.repo_id repo = seafile_api.get_repo(repo_id) if not repo: logger.warning('Library %s not found.' % repo_id) continue res = workspace.to_dict() table_list = DTables.objects.get_dtable_by_workspace(workspace) res["table_list"] = table_list if '@seafile_group' in owner: group_id = owner.split('@')[0] res["owner_name"] = group_id_to_name(group_id) res["owner_type"] = "Group" else: res["owner_name"] = email2nickname(owner) res["owner_type"] = "Personal" workspace_list.append(res) return Response({"workspace_list": workspace_list}, status=status.HTTP_200_OK)
def post(self, request): """create a table file Permission: 1. owner 2. group member """ # argument check table_owner = request.POST.get('owner') if not table_owner: error_msg = 'owner invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) table_name = request.POST.get('name') if not table_name: error_msg = 'name invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) table_file_name = table_name + FILE_TYPE if not is_valid_dirent_name(table_file_name): error_msg = 'name invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # resource check workspace = Workspaces.objects.get_workspace_by_owner(table_owner) if not workspace: if not request.user.permissions.can_add_repo(): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) org_id = -1 if is_org_context(request): org_id = request.user.org.org_id try: if org_id > 0: repo_id = seafile_api.create_org_repo( _("My Workspace"), _("My Workspace"), "dtable@seafile", org_id) else: repo_id = seafile_api.create_repo(_("My Workspace"), _("My Workspace"), "dtable@seafile") except Exception as e: logger.error(e) error_msg = 'Internal Server Error.' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) try: workspace = Workspaces.objects.create_workspace( table_owner, repo_id) except Exception as e: logger.error(e) error_msg = 'Internal Server Error.' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) repo_id = workspace.repo_id repo = seafile_api.get_repo(repo_id) if not repo: error_msg = 'Library %s not found.' % repo_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission check username = request.user.username if not check_dtable_permission(username, table_owner): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) # repo status check repo_status = repo.status if repo_status != 0: error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) # create new empty table table_file_name = check_filename_with_rename(repo_id, '/', table_file_name) try: seafile_api.post_empty_file(repo_id, '/', table_file_name, username) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) try: dtable = DTables.objects.create_dtable(username, workspace, table_name) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({"table": dtable.to_dict()}, status=status.HTTP_201_CREATED)
def post(self, request, group_id): """ Add a group library. Permission checking: 1. role permission, can_add_repo; 1. is group member; """ # argument check repo_name = request.data.get("repo_name", None) if not repo_name or \ not is_valid_dirent_name(repo_name): error_msg = "repo_name invalid." return api_error(status.HTTP_400_BAD_REQUEST, error_msg) password = request.data.get("password", None) if password and not config.ENABLE_ENCRYPTED_LIBRARY: error_msg = 'NOT allow to create encrypted library.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) permission = request.data.get('permission', PERMISSION_READ) if permission not in get_available_repo_perms(): error_msg = 'permission invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # permission check if not request.user.permissions.can_add_repo(): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) if not is_group_member(group_id, request.user.username): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) # create group repo org_id = -1 group_id = int(group_id) username = request.user.username is_org = False if is_org_context(request): is_org = True org_id = request.user.org.org_id repo_id = seafile_api.create_org_repo(repo_name, '', username, password, org_id) else: repo_id = seafile_api.create_repo(repo_name, '', username, password) repo = seafile_api.get_repo(repo_id) share_dir_to_group(repo, '/', username, username, group_id, permission, org_id if is_org else None) # for activities library_template = request.data.get("library_template", '') repo_created.send(sender=None, org_id=org_id, creator=username, repo_id=repo_id, repo_name=repo_name, library_template=library_template) # for notification share_repo_to_group_successful.send(sender=None, from_user=username, group_id=group_id, repo=repo, path='/', org_id=org_id) # for perm audit send_perm_audit_msg('add-repo-perm', username, group_id, repo_id, '/', permission) group_repo = seafile_api.get_group_shared_repo_by_path(repo_id, None, group_id, is_org) group_repo_info = get_group_repo_info(request, group_repo) group_repo_info['owner_email'] = username group_repo_info['owner_name'] = email2nickname(username) group_repo_info['owner_contact_email'] = email2contact_email(username) modifier = group_repo.last_modifier group_repo_info['modifier_email'] = modifier group_repo_info['modifier_name'] = email2nickname(modifier) group_repo_info['modifier_contact_email'] = email2contact_email(modifier) return Response(group_repo_info)