Ejemplo n.º 1
0
def _linkUser(self, user):
    """Set the UID of the current Contact in the User properties and update
    all relevant own properties.
    """
    logger.warn("MONKEY PATCHING __linkUser")
    KEY = "linked_contact_uid"

    username = user.getId()
    contact = self.getContactByUsername(username)

    # User is linked to another contact (fix in UI)
    if contact and contact.UID() != self.UID():
        raise ValueError("User '{}' is already linked to Contact '{}'".format(
            username, contact.Title()))

    # User is linked to multiple other contacts (fix in Data)
    if isinstance(contact, list):
        raise ValueError(
            "User '{}' is linked to multiple Contacts: '{}'".format(
                username, ",".join(map(lambda x: x.Title(), contact))))

    # XXX: Does it make sense to "remember" the UID as a User property?
    tool = user.getTool()
    try:
        user.getProperty(KEY)
    except ValueError:
        logger.info("Adding User property {}".format(KEY))
        tool.manage_addProperty(KEY, "", "string")

    # Set the UID as a User Property
    uid = self.UID()
    user.setMemberProperties({KEY: uid})
    logger.info("Linked Contact UID {} to User {}".format(
        user.getProperty(KEY), username))

    # Set the Username
    self.setUsername(user.getId())

    # Update the Email address from the user
    self.setEmailAddress(user.getProperty("email"))

    # somehow the `getUsername` index gets out of sync
    self.reindexObject()

    # N.B. Local owner role and client group applies only to client
    #      contacts, but not lab contacts.
    if IClient.providedBy(self.aq_parent):
        # Grant local Owner role
        self._addLocalOwnerRole(username)
        # Add user to "Clients" group
        self._addUserToGroup(username, group="Clients")

        # SENAITE.HEALTH-specific!
        # Add user to "InternalClients" group
        if is_internal_client(self.aq_parent):
            self._addUserToGroup(username, group="InternalClients")

    return True
Ejemplo n.º 2
0
    def is_visible(self):
        """Returns whether the viewlet must be visible or not
        """
        logged_client = api.get_current_client()
        if not logged_client:
            # Current user is from Lab, display always
            return True

        # Only display the viewlet if the current user does belong to an
        # Internal Client. It does not make sense to display this viewlet if
        # the user is from an external client, cause in such case, there is no
        # need to warn him/her. He/She expects that their own Patients, Cases
        # and Doctors are private already
        return is_internal_client(logged_client)
Ejemplo n.º 3
0
    def guard(self, action):
        """Returns True if the object does belong to an internal client
        """
        if action not in ["share", "unshare"]:
            return True

        # Do not allow to "unshare" individual items for now
        if action == "unshare":
            return False

        client = self.context.getClient()
        if not client:
            return False

        internal = is_internal_client(client)
        return internal
Ejemplo n.º 4
0
def try_share_unshare(obj):
    """Tries to share or unshare the object based on the type of its client
    """
    client = resolve_client(obj)
    if not IClient.providedBy(client):
        # This object does not (yet) have a client assigned, do nothing
        return

    if is_internal_client(client):
        # Try to share the obj. Note this transition will only take place if
        # the guard for "share" evaluates to True.
        doActionFor(obj, "share")
    else:
        # Try to unshare the obj. Note this transition will only take place
        # if the guard for "share" evaluates to True.
        doActionFor(obj, "unshare")
Ejemplo n.º 5
0
    def is_visible(self):
        """Returns whether the viewlet must be visible or not
        """
        if self.context.isTemporary():
            # Temporary object, not yet created
            return False

        logged_client = api.get_current_client()
        if not logged_client:
            # Current user is from Lab, display always
            return True

        # Only display the viewlet if the current user does belong to an
        # Internal Client. It does not make sense to display this viewlet if
        # the user is from an external client, cause in such case, he/she will
        # only be able to see the its own patients
        return is_internal_client(logged_client)
Ejemplo n.º 6
0
    def __call__(self):
        client = self.context.getClient()
        if not client:
            # Patient from the laboratory (no client assigned)
            base_folder = api.get_portal().analysisrequests
        elif is_internal_client(client):
            # Patient from an internal client, shared
            base_folder = api.get_portal().analysisrequests
        else:
            # Patient from an external client, private
            base_folder = client

        url = "{}/{}".format(api.get_url(base_folder), "ar_add")
        url = "{}?Patient={}".format(url, api.get_uid(self.context))
        qs = self.request.getHeader("query_string")
        if qs:
            url = "{}&{}".format(url, qs)
        return self.request.response.redirect(url)
Ejemplo n.º 7
0
def _unlinkUser(self):
    """Remove the UID of the current Contact in the User properties and
    update all relevant own properties.
    """
    logger.warn("MONKEY PATCHING __unlinkUser")
    KEY = "linked_contact_uid"

    # Nothing to do if no user is linked
    if not self.hasUser():
        return False

    user = self.getUser()
    username = user.getId()

    # Unset the UID from the User Property
    user.setMemberProperties({KEY: ""})
    logger.info("Unlinked Contact UID from User {}".format(
        user.getProperty(KEY, "")))

    # Unset the Username
    self.setUsername(None)

    # Unset the Email
    self.setEmailAddress(None)

    # somehow the `getUsername` index gets out of sync
    self.reindexObject()

    # N.B. Local owner role and client group applies only to client
    #      contacts, but not lab contacts.
    if IClient.providedBy(self.aq_parent):
        # Revoke local Owner role
        self._delLocalOwnerRole(username)
        # Remove user from "Clients" group
        self._delUserFromGroup(username, group="Clients")

        # SENAITE.HEALTH-specific!
        # Remove user from "InternalClients" group
        if is_internal_client(self.aq_parent):
            self._delUserFromGroup(username, group="InternalClients")

    return True
Ejemplo n.º 8
0
 def is_internal(self):
     """Returns whether the current client is internal or not
     """
     return is_internal_client(self.context)
Ejemplo n.º 9
0
 def is_shared_patient(self):
     """Returns whether the current Patient is shared among Internal Clients
     """
     client = self.context.getClient()
     return not client or is_internal_client(client)