Users can now edit user_attributes on profile page
Message-ID: <50cb330d.WPvpES/EZxwBWKEE%lm(a)mathias-kettner.de>
User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Module: check_mk
Branch: master
Commit: d9ebd1d404a8855fcf3fd7ee942c39e674dde13d
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=d9ebd1d404a885…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Fri Dec 14 15:08:35 2012 +0100
userdb: Bugfix after code cleanups; Users can now edit user_attributes on profile page
---
web/htdocs/default_permissions.py | 7 +++++
web/htdocs/wato.py | 45 ++++++++++++++++++++++++++----------
web/plugins/userdb/hook_auth.py | 3 +-
3 files changed, 41 insertions(+), 14 deletions(-)
diff --git a/web/htdocs/default_permissions.py b/web/htdocs/default_permissions.py
index d2c3a4c..c4a49b2 100644
--- a/web/htdocs/default_permissions.py
+++ b/web/htdocs/default_permissions.py
@@ -118,6 +118,13 @@ def load():
[ 'admin', 'user' ]
)
+ config.declare_permission('general.edit_user_attributes',
+ _('Edit personal user attributes'),
+ _('This allows a user to edit his personal user attributes. You also need the
permission '
+ '<i>Edit the user profile</i> in order to do this.'),
+ [ 'admin', 'user' ]
+ )
+
config.declare_permission('general.change_password',
_('Edit the user password'),
_('Permits the user to change the password.'),
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index df9b101..8d4d97d 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -7504,9 +7504,10 @@ def delete_distributed_wato_file():
# | Mode for managing users and contacts. |
# '----------------------------------------------------------------------'
-def declare_user_attribute(name, vs):
+def declare_user_attribute(name, vs, user_editable = True):
userdb.user_attributes[name] = {
- 'valuespec': vs,
+ 'valuespec': vs,
+ 'user_editable': user_editable,
}
def load_notification_scripts_from(adir):
@@ -7977,9 +7978,9 @@ def mode_edit_user(phase):
new_user["notification_method"] = value
# Custom attributes
- for name, vs in userdb.get_user_attributes():
- value = vs.from_html_vars('ua_' + name)
- vs.validate_value(value, "ua_" + name)
+ for name, attr in userdb.get_user_attributes():
+ value = attr['valuespec'].from_html_vars('ua_' + name)
+ attr['valuespec'].validate_value(value, "ua_" + name)
new_user[name] = value
# Saving
@@ -8201,10 +8202,12 @@ def mode_edit_user(phase):
forms.header(_("Personal Settings"), isopen = False)
select_language(user.get('language', ''))
- for name, vs in userdb.get_user_attributes():
- forms.section(vs.title())
- vs.render_input("ua_" + name, user.get(name, vs.default_value()))
- html.help(vs.help())
+ for name, attr in userdb.get_user_attributes():
+ if attr['user_editable']:
+ vs = attr['valuespec']
+ forms.section(vs.title())
+ vs.render_input("ua_" + name, user.get(name, vs.default_value()))
+ html.help(vs.help())
# TODO: Later we could add custom macros here, which
# then could be used for notifications. On the other hand,
@@ -10814,10 +10817,19 @@ def page_user_profile():
# load the new language
load_language(config.get_language())
- user = users.get(config.user_id)
- if config.may('general.edit_notifications') and
user.get("notifications_enabled"):
- value = forms.get_input(vs_notification_method,
"notification_method")
- users[config.user_id]["notification_method"] = value
+ user = users.get(config.user_id)
+ if config.may('general.edit_notifications') and
user.get("notifications_enabled"):
+ value = forms.get_input(vs_notification_method,
"notification_method")
+ users[config.user_id]["notification_method"] = value
+
+ # Custom attributes
+ if config.may('general.edit_user_attributes'):
+ for name, attr in userdb.get_user_attributes():
+ if attr['user_editable']:
+ vs = attr['valuespec']
+ value = vs.from_html_vars('ua_' + name)
+ vs.validate_value(value, "ua_" + name)
+ users[config.user_id][name] = value
# Change the password if requested
if config.may('general.change_password'):
@@ -10894,6 +10906,13 @@ def page_user_profile():
vs_notification_method.render_input("notification_method",
user.get("notification_method"))
# forms.input(vs_notification_method, "notification_method",
user.get("notification_method"))
+ if config.may('general.edit_user_attributes'):
+ for name, attr in userdb.get_user_attributes():
+ vs = attr['valuespec']
+ forms.section(vs.title())
+ vs.render_input("ua_" + name, user.get(name,
vs.default_value()))
+ html.help(vs.help())
+
# Save button
forms.end()
html.button("_save", _("Save"))
diff --git a/web/plugins/userdb/hook_auth.py b/web/plugins/userdb/hook_auth.py
index c4aaaaf..04e3576 100644
--- a/web/plugins/userdb/hook_auth.py
+++ b/web/plugins/userdb/hook_auth.py
@@ -185,7 +185,8 @@ function may($username, $need_permission) {
def create_auth_file(users):
make_nagios_directory(g_auth_base_dir)
- create_php_file(users, config.get_role_permissions(),
get_folder_permissions_of_users(users))
+ import wato # HACK: cleanup!
+ create_php_file(users, config.get_role_permissions(),
wato.get_folder_permissions_of_users(users))
hooks.register('users-saved', create_auth_file)
hooks.register('roles-saved', lambda x: create_auth_file(load_users()))