Add Move user's libraries action for solving migration issues

This commit is contained in:
HugoPoi 2019-11-05 19:47:16 +01:00
parent 28760cfe57
commit f86b6b6ee1
4 changed files with 73 additions and 0 deletions

20
actions.json Normal file
View file

@ -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"
}
]
}]

View file

@ -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() {

29
sources/move_library.py Normal file
View file

@ -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)

22
sources/seafile_api_loader.sh Executable file
View file

@ -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 $*