workflows: add date support for idp profile change (#22586)

This commit is contained in:
Frédéric Péters 2018-03-20 09:04:04 +01:00
parent 5c37cdf39a
commit 3e7c60d4d4
3 changed files with 41 additions and 6 deletions

View File

@ -2867,10 +2867,31 @@ def test_profile(two_pubs):
assert User.get(user.id).form_data.get('3') == 'Plop'
assert not User.get(user.id).form_data.get('4')
item.fields = [{'field_id': 'bar', 'value': '20/03/2018'}]
item.perform(formdata)
assert User.get(user.id).form_data.get('3') == 'Plop'
assert User.get(user.id).form_data.get('4').tm_year == 2018
# check transmission to IdP
get_publisher().cfg['sp'] = {'idp-manage-user-attributes': True}
get_publisher().cfg['idp'] = {'xxx': {'metadata_url': 'http://idp.example.net/idp/saml2/metadata'}}
get_publisher().write_cfg()
user = User.get(user.id)
user.name_identifiers = ['xyz']
user.store()
for date_value in (
'20/03/2018',
'=utils.make_date("20/03/2018")',
'=utils.make_date("20/03/2018").timetuple()'):
# check local value
item.fields = [{'field_id': 'bar', 'value': date_value}]
item.perform(formdata)
assert User.get(user.id).form_data.get('3') == 'Plop'
assert User.get(user.id).form_data.get('4').tm_year == 2018
with mock.patch('wcs.wf.profile.http_patch_request') as http_patch_request:
http_patch_request.return_value = (None, 200, '', None)
get_response().process_after_jobs()
assert http_patch_request.call_count == 1
assert http_patch_request.call_args[0][1] == '{"bar": "2018-03-20"}'
def test_set_backoffice_field(http_requests, two_pubs):
Workflow.wipe()

View File

@ -465,6 +465,9 @@ class JSONEncoder(json.JSONEncoder):
if isinstance(obj, datetime.datetime):
return obj.isoformat()
if isinstance(obj, datetime.date):
return obj.strftime('%Y-%m-%d')
if isinstance(obj, decimal.Decimal):
return str(obj)

View File

@ -14,7 +14,9 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
import datetime
import json
import time
import urlparse
import xml.etree.ElementTree as ET
@ -24,7 +26,7 @@ from qommon import _
from qommon.form import (CompositeWidget, SingleSelectWidget,
WidgetListAsTable, ComputedExpressionWidget)
from qommon.ident.idp import is_idp_managing_user_attributes
from qommon.misc import http_patch_request
from qommon.misc import http_patch_request, JSONEncoder
from qommon.publisher import get_cfg, get_logger
from wcs.api_utils import sign_url, get_secret_and_orig, MissingSecret
@ -156,6 +158,10 @@ class UpdateUserProfileStatusItem(WorkflowStatusItem):
if field and field_value and field.convert_value_from_anything:
field_value = field.convert_value_from_anything(field_value)
new_user_data[field.id] = field_value
# also change initial value to the converted one, as the
# initial dictionary is used when sending the profile changes
# to the identity provider.
new_data[field.varname] = field_value
if '__name' in new_data:
user.name = new_data.get('__name')
@ -181,10 +187,15 @@ class UpdateUserProfileStatusItem(WorkflowStatusItem):
return
payload = new_data.copy()
for k, v in payload.items():
# fix date fields to be datetime.date
if isinstance(v, time.struct_time):
payload[k] = datetime.date(*v[:3])
if '__email' in new_data:
payload['email'] = new_data.get('__email')
payload = json.dumps(payload)
payload = json.dumps(payload, cls=JSONEncoder)
def after_job(job):
response, status, data, auth_header = http_patch_request(url,