Hook implementation + post_user_create hook

This commit is contained in:
Kload 2013-12-09 19:37:16 +01:00
parent c553877d4c
commit 4fcc1642d6
3 changed files with 49 additions and 32 deletions

View file

@ -826,6 +826,10 @@ hook:
arguments: arguments:
action: action:
help: Action name help: Action name
-a:
full: --args
help: Ordered list of arguments to pass to the script
nargs: '*'
### hook_check() ### hook_check()
check: check:

View file

@ -29,14 +29,14 @@ import re
import json import json
from yunohost import YunoHostError, YunoHostLDAP, win_msg, colorize from yunohost import YunoHostError, YunoHostLDAP, win_msg, colorize
hook_folder = '/user/share/yunohost/hooks/' hook_folder = '/usr/share/yunohost/hooks/'
def hook_add(action, file): def hook_add(action, file):
""" """
Store hook script to filsystem Store hook script to filsystem
Keyword argument: Keyword argument:
file -- Script to check file -- Script to add
action -- Action folder to store into action -- Action folder to store into
""" """
@ -46,20 +46,28 @@ def hook_add(action, file):
os.system('cp '+ file +' '+ hook_folder + action) os.system('cp '+ file +' '+ hook_folder + action)
def hook_callback(action): def hook_callback(action, args=None):
""" """
Execute all scripts binded to an action Execute all scripts binded to an action
Keyword argument: Keyword argument:
action -- Action name action -- Action name
args -- Ordered list of arguments to pass to the script
""" """
with YunoHostLDAP() as yldap: with YunoHostLDAP() as yldap:
try: os.listdir(hook_folder + action) try: os.listdir(hook_folder + action)
except OSError: os.makedirs(hook_folder + action) except OSError: pass
else:
if args is None:
args = []
elif not isinstance(args, list):
args = [args]
for hook in os.listdir(hook_folder + action): for hook in os.listdir(hook_folder + action):
hook_exec(file=hook_folder + action +'/'+ hook) try:
hook_exec(file=hook_folder + action +'/'+ hook, args=args)
except: pass
def hook_check(file): def hook_check(file):
@ -93,34 +101,37 @@ def hook_exec(file, args=None):
""" """
with YunoHostLDAP() as yldap: with YunoHostLDAP() as yldap:
required_args = hook_check(file) if isinstance(args, list):
if args is None: arg_list = args
args = {} else:
required_args = hook_check(file)
if args is None:
args = {}
arg_list = [] arg_list = []
for arg in required_args: for arg in required_args:
if arg['name'] in args: if arg['name'] in args:
if 'choices' in arg and args[arg['name']] not in arg['choices']: if 'choices' in arg and args[arg['name']] not in arg['choices']:
raise YunoHostError(22, _("Invalid choice") + ': ' + args[arg['name']]) raise YunoHostError(22, _("Invalid choice") + ': ' + args[arg['name']])
arg_list.append(args[arg['name']]) arg_list.append(args[arg['name']])
else:
if os.isatty(1) and 'ask' in arg:
ask_string = arg['ask']['en'] #TODO: I18n
if 'choices' in arg:
ask_string = ask_string +' ('+ '|'.join(arg['choices']) +')'
if 'default' in arg:
ask_string = ask_string +' (default: '+ arg['default'] +')'
input_string = raw_input(colorize(ask_string + ': ', 'cyan'))
if input_string == '' and 'default' in arg:
input_string = arg['default']
arg_list.append(input_string)
elif 'default' in arg:
arg_list.append(arg['default'])
else: else:
raise YunoHostError(22, _("Missing arguments") + ': ' + arg['name']) if os.isatty(1) and 'ask' in arg:
ask_string = arg['ask']['en'] #TODO: I18n
if 'choices' in arg:
ask_string = ask_string +' ('+ '|'.join(arg['choices']) +')'
if 'default' in arg:
ask_string = ask_string +' (default: '+ arg['default'] +')'
input_string = raw_input(colorize(ask_string + ': ', 'cyan'))
if input_string == '' and 'default' in arg:
input_string = arg['default']
arg_list.append(input_string)
elif 'default' in arg:
arg_list.append(arg['default'])
else:
raise YunoHostError(22, _("Missing arguments") + ': ' + arg['name'])
file_path = "./" file_path = "./"
if "/" in file and file[0:2] != file_path: if "/" in file and file[0:2] != file_path:

View file

@ -32,6 +32,7 @@ import string
import getpass import getpass
from yunohost import YunoHostError, YunoHostLDAP, win_msg, colorize, validate, get_required_args from yunohost import YunoHostError, YunoHostLDAP, win_msg, colorize, validate, get_required_args
from yunohost_domain import domain_list from yunohost_domain import domain_list
from yunohost_hook import hook_callback
def user_list(fields=None, filter=None, limit=None, offset=None): def user_list(fields=None, filter=None, limit=None, offset=None):
""" """
@ -116,6 +117,7 @@ def user_create(username, firstname, lastname, mail, password):
if user_added == pwd_changed == 0: if user_added == pwd_changed == 0:
os.system('yunohost app ssowatconf > /dev/null 2>&1') os.system('yunohost app ssowatconf > /dev/null 2>&1')
hook_callback('post_user_create', [username, mail, password, firstname, lastname])
#TODO: Send a welcome mail to user #TODO: Send a welcome mail to user
win_msg(_("User successfully created")) win_msg(_("User successfully created"))
return { _("Fullname") : firstname +' '+ lastname, _("Username") : username, _("Mail") : mail } return { _("Fullname") : firstname +' '+ lastname, _("Username") : username, _("Mail") : mail }