hobo: update profile fields (#7187)

This commit is contained in:
Frédéric Péters 2015-05-08 15:18:57 +02:00
parent 4cc5c26f4f
commit 94868cd068
2 changed files with 61 additions and 7 deletions

View File

@ -159,9 +159,12 @@ class UserFieldsFormDef(FormDef):
'''Class to handle custom user fields, it loads and saves from/to
an XML string stored in the configuration (at users/formdef)'''
def __init__(self):
def __init__(self, publisher=None):
if publisher is None:
publisher = get_publisher()
self.publisher = publisher
self.name = _('Custom User Fields')
users_cfg = get_cfg('users', {})
users_cfg = publisher.cfg.get('users', {})
xml_import = users_cfg.get('formdef')
self.fields = [] # make sure fields is a list
self.id = None # required for XML import/export
@ -176,7 +179,7 @@ class UserFieldsFormDef(FormDef):
self.max_field_id = obj.max_field_id
else:
# compatibility with older location
filename = os.path.join(get_publisher().app_dir, 'config', 'user')
filename = os.path.join(publisher.app_dir, 'config', 'user')
if os.path.exists(filename):
try:
formdef = FormDef.get_filename(filename)
@ -190,11 +193,11 @@ class UserFieldsFormDef(FormDef):
def store(self):
xml_export = self.export_to_xml(include_id=True)
users_cfg = get_cfg('users', {})
users_cfg = self.publisher.cfg.get('users', {})
users_cfg['formdef'] = ET.tostring(xml_export)
get_publisher().cfg['users'] = users_cfg
get_publisher().write_cfg()
if get_publisher().is_using_postgresql():
self.publisher.cfg['users'] = users_cfg
self.publisher.write_cfg()
if self.publisher.is_using_postgresql():
import sql
sql.do_user_table()

View File

@ -25,6 +25,9 @@ import urllib2
from qommon.ctl import Command, make_option
from qommon.storage import atomic_write
from wcs.admin.settings import UserFieldsFormDef
from wcs.fields import StringField, EmailField
class NoChange(Exception):
pass
@ -92,6 +95,8 @@ class CmdCheckHobos(Command):
if new_site:
self.configure_sql(service, pub)
self.update_profile(self.all_services.get('profile', {}), pub)
def update_configuration(self, service, pub):
if not pub.cfg.get('misc'):
pub.cfg['misc'] = {}
@ -99,6 +104,52 @@ class CmdCheckHobos(Command):
pub.cfg['misc']['frontoffice-url'] = service.get('base_url').encode('utf-8')
pub.write_cfg()
def update_profile(self, profile, pub):
formdef = UserFieldsFormDef(publisher=pub)
profile_fields = {}
profile_field_names = [x['name'] for x in profile.get('fields', [])]
for field in formdef.fields:
if field.varname in profile_field_names:
profile_fields[field.varname] = field
# create or update profile fields
for attribute in profile.get('fields', []):
if not attribute['name'] in profile_fields:
field_class, field_typename = StringField, 'string'
if attribute['kind'] == 'email':
field_class, field_typename = EmailField, 'email'
new_field = field_class(label=attribute['label'].encode('utf-8'),
type=field_typename,
varname=attribute['name'])
new_field.id = '_' + attribute['name']
profile_fields[new_field.varname] = new_field
else:
# remove it for the moment
formdef.fields.remove(profile_fields[attribute['name']])
profile_fields[attribute['name']].label = attribute['label'].encode('utf-8')
profile_fields[attribute['name']].hint = attribute['description'].encode('utf-8')
profile_fields[attribute['name']].required = attribute['required']
if attribute['disabled']:
profile_field_names.remove(attribute['name'])
# insert profile fields at the beginning
formdef.fields = [profile_fields[x] for x in profile_field_names] + formdef.fields
formdef.store()
pub.cfg['users']['field_email'] = '_email'
pub.cfg['users']['field_name'] = ['_first_name', '_last_name']
pub.write_cfg()
# add mapping for SAML provisioning
for idp in pub.cfg.get('idp', {}).values():
if not 'attribute-mapping' in idp:
idp['attribute-mapping'] = {}
for field_name in profile_field_names:
idp['attribute-mapping'][str(field_name)] = '_' + str(field_name)
pub.write_cfg()
def configure_authentication_methods(self, service, pub):
# look for an identity provider
idps = [x for x in self.all_services.get('services', []) if x.get('service-id') == 'authentic']