|
| 1 | +from collective.techevent import _ |
| 2 | +from collective.techevent.users.utils import validate_unique_email |
| 3 | +from plone import api |
| 4 | +from plone.autoform import directives |
| 5 | +from plone.dexterity.content import Container |
| 6 | +from plone.schema import Email |
| 7 | +from plone.supermodel import model |
| 8 | +from Products.membrane.interfaces import IMembraneUserObject |
| 9 | +from z3c.form.interfaces import IEditForm |
| 10 | +from zope import schema |
| 11 | +from zope.interface import implementer |
| 12 | +from zope.interface import Interface |
| 13 | +from zope.interface import Invalid |
| 14 | +from zope.interface import invariant |
| 15 | + |
| 16 | + |
| 17 | +class IBaseUser(Interface): |
| 18 | + id = schema.ASCIILine(title=_("User ID"), required=True) |
| 19 | + directives.order_before(id="*") |
| 20 | + |
| 21 | + title = schema.TextLine(readonly=True) |
| 22 | + first_name = schema.TextLine( |
| 23 | + title=_("First Name"), |
| 24 | + required=True, |
| 25 | + ) |
| 26 | + last_name = schema.TextLine( |
| 27 | + title=_("Last Name"), |
| 28 | + required=True, |
| 29 | + ) |
| 30 | + email = Email( |
| 31 | + title=_("E-mail Address"), |
| 32 | + required=True, |
| 33 | + ) |
| 34 | + otp_seed = schema.ASCIILine( |
| 35 | + title=_("OTP Shared Secret"), |
| 36 | + required=False, |
| 37 | + ) |
| 38 | + model.fieldset("credentials", label=_("Credentials"), fields=["email", "otp_seed"]) |
| 39 | + |
| 40 | + directives.read_permission( |
| 41 | + id="zope2.View", |
| 42 | + email="zope2.View", |
| 43 | + text="zope2.View", |
| 44 | + otp_seed="zope2.ManageUsers", |
| 45 | + ) |
| 46 | + directives.write_permission( |
| 47 | + id="cmf.ReviewPortalContent", |
| 48 | + email="cmf.ReviewPortalContent", |
| 49 | + text="cmf.ModifyPortalContent", |
| 50 | + otp_seed="zope2.ManageUsers", |
| 51 | + ) |
| 52 | + directives.omitted("otp_seed") |
| 53 | + directives.no_omit(IEditForm, "otp_seed") |
| 54 | + |
| 55 | + @invariant |
| 56 | + def email_unique(data): |
| 57 | + """The email must be unique, as it is the login name (user name). |
| 58 | +
|
| 59 | + The tricky thing is to make sure editing a user and keeping |
| 60 | + his email the same actually works. |
| 61 | + """ |
| 62 | + user = data.__context__ |
| 63 | + if user is not None: |
| 64 | + if getattr(user, "email", None) and user.email == data.email: |
| 65 | + # No change, fine. |
| 66 | + return |
| 67 | + error = validate_unique_email(data.email) |
| 68 | + if error: |
| 69 | + raise Invalid(error) |
| 70 | + |
| 71 | + exclude_from_nav = schema.Bool( |
| 72 | + default=True, |
| 73 | + required=False, |
| 74 | + ) |
| 75 | + directives.omitted("exclude_from_nav") |
| 76 | + |
| 77 | + |
| 78 | +@implementer(IBaseUser, IMembraneUserObject) |
| 79 | +class BaseUser(Container): |
| 80 | + """A Membrane user.""" |
| 81 | + |
| 82 | + @property |
| 83 | + def title(self): |
| 84 | + return f"{self.first_name} {self.last_name}" |
| 85 | + |
| 86 | + @title.setter |
| 87 | + def title(self, value): |
| 88 | + # title is not writable |
| 89 | + pass |
| 90 | + |
| 91 | + def getUserId(self): |
| 92 | + return self.id |
| 93 | + |
| 94 | + def getUserName(self): |
| 95 | + return self.email |
| 96 | + |
| 97 | + def get_full_name(self): |
| 98 | + return self.title |
| 99 | + |
| 100 | + @property |
| 101 | + def user(self): |
| 102 | + user = api.user.get(userid=self.id) |
| 103 | + if user: |
| 104 | + return user |
0 commit comments