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:
action:
help: Action name
-a:
full: --args
help: Ordered list of arguments to pass to the script
nargs: '*'
### hook_check()
check:

View file

@ -29,14 +29,14 @@ import re
import json
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):
"""
Store hook script to filsystem
Keyword argument:
file -- Script to check
file -- Script to add
action -- Action folder to store into
"""
@ -46,20 +46,28 @@ def hook_add(action, file):
os.system('cp '+ file +' '+ hook_folder + action)
def hook_callback(action):
def hook_callback(action, args=None):
"""
Execute all scripts binded to an action
Keyword argument:
action -- Action name
args -- Ordered list of arguments to pass to the script
"""
with YunoHostLDAP() as yldap:
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):
hook_exec(file=hook_folder + action +'/'+ hook)
try:
hook_exec(file=hook_folder + action +'/'+ hook, args=args)
except: pass
def hook_check(file):
@ -93,6 +101,9 @@ def hook_exec(file, args=None):
"""
with YunoHostLDAP() as yldap:
if isinstance(args, list):
arg_list = args
else:
required_args = hook_check(file)
if args is None:
args = {}

View file

@ -32,6 +32,7 @@ import string
import getpass
from yunohost import YunoHostError, YunoHostLDAP, win_msg, colorize, validate, get_required_args
from yunohost_domain import domain_list
from yunohost_hook import hook_callback
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:
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
win_msg(_("User successfully created"))
return { _("Fullname") : firstname +' '+ lastname, _("Username") : username, _("Mail") : mail }