diff --git a/actions.json b/actions.json new file mode 100644 index 0000000..4190bcb --- /dev/null +++ b/actions.json @@ -0,0 +1,20 @@ +[{ + "id": "move_all_libraries", + "name": "Move user's libraries", + "command": "/opt/yunohost/seafile/seafile_api_loader.sh /opt/yunohost/seafile/move_library.py $YNH_ACTION_USER_TO_MIGRATE", + "user": "root", + "accepted_return_codes": [0], + "description": { + "en": "WARNING: Only use this feature if you know what you do.\n This action is only needed if you have installed Seafile before the version 7 AND your Seafile instance was installed under other domain than the user accounts. Move all user's libraries to the correct account, this action exists only for handling old created account with the old SSO behavior.\n See https://github.com/YunoHost-Apps/seafile_ynh/issues/44 for more information." + }, + "arguments": [ + { + "name": "user_to_migrate", + "type": "user", + "ask": { + "en": "User account to be migrate from the old to the new behavior" + }, + "example": "bob@ynh.local" + } + ] +}] diff --git a/scripts/_common.sh b/scripts/_common.sh index 74b35c3..bdaa500 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -17,6 +17,8 @@ install_source() { ynh_die --message "Error : this architecture is no longer supported by the upstream. Please create en issue here : https://github.com/YunoHost-Apps/seafile_ynh/issues to ask to discuss about a support of this architecture" fi ynh_setup_source "$final_path/seafile-server-$seafile_version" "$architecture" + cp ../sources/seafile_api_loader.sh $final_path/ + cp ../sources/move_library.py $final_path/ } install_dependance() { diff --git a/sources/move_library.py b/sources/move_library.py new file mode 100644 index 0000000..b2fa7ae --- /dev/null +++ b/sources/move_library.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +import sys +import os +from seaserv import seafile_api, ccnet_api + +seafile_local_emails = map(lambda user: user.email, ccnet_api.get_emailusers('DB', start=-1, limit=-1, is_active=None)) +active_user_emails = map(lambda user: user.email, ccnet_api.get_emailusers(source='LDAPImport', start=-1, limit=-1, is_active=True)) +ldap_user_emails = map(lambda user: user.email, ccnet_api.get_emailusers(source='LDAP', start=-1, limit=-1, is_active=None)) +non_active_user_emails = list(set(ldap_user_emails) - set(active_user_emails)) + +seafile_domain = os.environ['SEAFILE_CURRENT_DOMAIN'] +source_user = sys.argv[1] +source_user_email = source_user + '@' + seafile_domain +assert (source_user_email in seafile_local_emails), 'The user must be in local seafile users list' + +# TODO we might want to use non_active_user_emails in place of ldap_user_emails here +filtered_potencial_target_users = list(filter(lambda email: email.startswith(source_user + '@'), ldap_user_emails)) +assert (not len(filtered_potencial_target_users) > 1), 'This user have multiple target possible can\'t continue' +assert (len(filtered_potencial_target_users) == 1), 'This user doesn\'t have any possible match in Yunohost LDAP' + +target_user_email = filtered_potencial_target_users[0] + +ccnet_api.get_emailuser_with_import(target_user_email) + +source_repositories = seafile_api.get_owned_repo_list(source_user_email); + +for repo in source_repositories: + seafile_api.set_repo_owner(repo.id, target_user_email) diff --git a/sources/seafile_api_loader.sh b/sources/seafile_api_loader.sh new file mode 100755 index 0000000..d9a8847 --- /dev/null +++ b/sources/seafile_api_loader.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# Source YunoHost helpers +source /usr/share/yunohost/helpers + +# Stop script if errors +#ynh_abort_if_errors + +# Import common cmd +#source ./experimental_helper.sh +#source ./_common.sh + +final_path=$(ynh_app_setting_get --app $YNH_APP_INSTANCE_NAME --key final_path) + +export SEAFILE_CURRENT_DOMAIN=$(ynh_app_setting_get --app $YNH_APP_INSTANCE_NAME --key domain) +export CCNET_CONF_DIR=$final_path/ccnet +export SEAFILE_CONF_DIR=$final_path/conf +export SEAFILE_CENTRAL_CONF_DIR=$final_path/conf + +export PYTHONPATH=$final_path/seafile-server-latest/seafile/lib/python2.7/site-packages:$final_path/seafile-server-latest/seafile/lib64/python2.7/site-packages:$PYTHONPATH + +python $*