|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | from collective.contact.plonegroup import _ as _ccp |
| 3 | +from collective.contact.plonegroup.behaviors import IPlonegroupUserLink |
3 | 4 | from collective.z3cform.datagridfield import DataGridFieldFactory |
4 | 5 | from collective.z3cform.datagridfield.registry import DictRow |
5 | 6 | from dexterity.localrolesfield.field import LocalRoleField |
|
10 | 11 | from imio.dms.mail.utils import vocabularyname_to_terms |
11 | 12 | from imio.helpers.content import uuidToObject |
12 | 13 | from operator import itemgetter |
| 14 | +from plone import api |
13 | 15 | from plone.autoform import directives as form |
14 | 16 | from plone.autoform.interfaces import IFormFieldProvider |
15 | 17 | from plone.supermodel import directives |
@@ -208,3 +210,50 @@ def validate_signing(data): |
208 | 210 | raise Invalid(_(u"You cannot have empty and defined approvings at the same time !")) |
209 | 211 | if data.seal and not data.esign: |
210 | 212 | raise Invalid(_(u"You cannot have a seal without electronic signature !")) |
| 213 | + |
| 214 | + |
| 215 | +@provider(IFormFieldProvider) |
| 216 | +class IImioPlonegroupUserLink(IPlonegroupUserLink): |
| 217 | + @invariant |
| 218 | + def validate_userid(data): |
| 219 | + context = data.__context__ |
| 220 | + if not hasattr(context, 'userid') or getattr(context, 'userid', None) is None: |
| 221 | + return |
| 222 | + |
| 223 | + # Raise if trying to remove an existing userid |
| 224 | + if data.userid is None: |
| 225 | + raise Invalid(_(u"You cannot remove a userid once it is set.")) |
| 226 | + |
| 227 | + # Raise if changing userid but user has pending esign approvals |
| 228 | + if data.userid != context.userid: |
| 229 | + catalog = api.portal.get_tool('portal_catalog') |
| 230 | + |
| 231 | + # Get person(s) with approving held positions for the user |
| 232 | + hps = catalog.unrestrictedSearchResults( |
| 233 | + portal_type='held_position', |
| 234 | + userid=context.userid, |
| 235 | + ) |
| 236 | + approving_persons = set() |
| 237 | + for b in hps: |
| 238 | + hp = b.getObject() |
| 239 | + if 'signer'in hp.usages or 'approving' in hp.usages: |
| 240 | + approving_persons.add(hp.get_person().UID()) |
| 241 | + |
| 242 | + # Get all persons with pending esign approvals |
| 243 | + mails = catalog.unrestrictedSearchResults( |
| 244 | + portal_type='dmsoutgoingmail', |
| 245 | + review_state='to_approve', |
| 246 | + ) |
| 247 | + pending_approvings = set() |
| 248 | + for b in mails: |
| 249 | + mail = b.getObject() |
| 250 | + signers = mail.signers |
| 251 | + for signer in signers: |
| 252 | + for approving in signer.get('approvings', []): |
| 253 | + if approving == "_themself_": |
| 254 | + pending_approvings.add(uuidToObject(signer.get('signer'), unrestricted=True).get_person().UID()) |
| 255 | + else: |
| 256 | + pending_approvings.add(approving) |
| 257 | + |
| 258 | + if approving_persons.intersection(pending_approvings): |
| 259 | + raise Invalid(_(u"You cannot change the userid because the user has pending esign approvals.")) |
0 commit comments