B
    `UA                 @   sj  d dl mZ d dlmZmZ d dlmZmZmZm	Z	m
Z
mZ d dlmZ dZdd Zdd	 Zd
d Zdd Zedd d.ddZG dd deZG dd deZG dd deZG dd dZG dd dZee	G dd dZdZdZd Zd!ZG d"d# d#Ze Z eee fZ!G d$d% d%eZ"G d&d' d'e"eZ#G d(d) d)e"eZ$x d*D ]Z%ee%d+j&e%d, qJW d-S )/    )
deprecated)implementer
providedBy)IAuthenticationPolicyIAuthorizationPolicyISecuredViewISecurityPolicyIViewIViewClassifier)get_current_registryZ__no_permission_required__c             C   s   | j tS )N)registryqueryUtilityr   )request r   X/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid/security.py_get_security_policy   s    r   c             K   s$   t | }|dkrg S |j| |f|S )a  
    Returns a sequence of header tuples (e.g. ``[('Set-Cookie', 'foo=abc')]``)
    on this request's response.
    These headers are suitable for 'remembering' a set of credentials
    implied by the data passed as ``userid`` and ``*kw`` using the
    current :term:`security policy`.  Common usage might look
    like so within the body of a view function (``response`` is
    assumed to be a :term:`WebOb` -style :term:`response` object
    computed previously by the view code):

    .. code-block:: python

       from pyramid.security import remember
       headers = remember(request, 'chrism', password='123', max_age='86400')
       response = request.response
       response.headerlist.extend(headers)
       return response

    If no :term:`security policy` is in use, this function will
    always return an empty sequence. If used, the composition and
    meaning of ``**kw`` must be agreed upon by the calling code and
    the effective security policy.

    .. versionchanged:: 1.6
        Deprecated the ``principal`` argument in favor of ``userid`` to clarify
        its relationship to the security policy.

    .. versionchanged:: 1.10
        Removed the deprecated ``principal`` argument.
    N)r   remember)r   useridkwpolicyr   r   r   r      s    r   c             K   s"   t | }|dkrg S |j| f|S )a  
    Return a sequence of header tuples (e.g. ``[('Set-Cookie',
    'foo=abc')]``) suitable for 'forgetting' the set of credentials
    possessed by the currently authenticated user.  A common usage
    might look like so within the body of a view function
    (``response`` is assumed to be an :term:`WebOb` -style
    :term:`response` object computed previously by the view code):

    .. code-block:: python

       from pyramid.security import forget
       headers = forget(request)
       response.headerlist.extend(headers)
       return response

    If no :term:`security policy` is in use, this function will
    always return an empty sequence.
    N)r   forget)r   r   r   r   r   r   r   :   s    r   c             C   s6   t  }|t}|dkr*ddlm} |gS || |S )a  
    .. deprecated:: 2.0

        The new security policy has removed the concept of principals.  See
        :ref:`upgrading_auth_20` for more information.

    Provided a ``context`` (a resource object), and a ``permission``
    string, if an :term:`authorization policy` is
    in effect, return a sequence of :term:`principal` ids that possess
    the permission in the ``context``.  If no authorization policy is
    in effect, this will return a sequence with the single value
    :mod:`pyramid.authorization.Everyone` (the special principal
    identifier representing all principals).

    .. note::

       Even if an :term:`authorization policy` is in effect,
       some (exotic) authorization policies may not implement the
       required machinery for this function; those will cause a
       :exc:`NotImplementedError` exception to be raised when this
       function is invoked.

    Nr   )Everyone)r   r   r   pyramid.authorizationr    principals_allowed_by_permission)context
permissionregr   r   r   r   r   r   S   s    
r   zThe new security policy has removed the concept of principals.  See "Upgrading Authentication/Authorization" in "What's New in Pyramid 2.0" of the documentation for more information. c             C   sv   |j }tgdd || fD  }|jj|t|d}|dkrj|jj|t|d}|dkrZtdtd|| f S || |S )a  If the view specified by ``context`` and ``name`` is protected
    by a :term:`permission`, check the permission associated with the
    view using the effective authentication/authorization policies and
    the ``request``.  Return a boolean result.  If no
    :term:`security policy` is in effect, or if the view is not
    protected by a permission, return ``True``. If no view can view found,
    an exception will be raised.

    .. versionchanged:: 1.4a4
       An exception is raised if no view is found.

    c             S   s   g | ]}t |qS r   )r   ).0xr   r   r   
<listcomp>   s    z,view_execution_permitted.<locals>.<listcomp>)nameNzyNo registered view satisfies the constraints. It would not make sense to claim that this view "is" or "is not" permitted.z;Allowed: view name %r in context %r (no permission defined))	r   r
   adapterslookupr   r	   	TypeErrorAllowedZ__permitted__)r   r   r!   r   providesviewr   r   r   view_execution_permitted|   s    r(   c               @   s0   e Zd Zdd Zedd Zdd Zdd Zd	S )
PermitsResultc             G   s   t | | j}||_||_|S )z
        Create a new instance.

        :param fmt: A format string explaining the reason for denial.
        :param args: Arguments are stored and used with the format string
                      to generate the ``msg``.

        )int__new__boolvalsargs)clsr-   r.   instr   r   r   r+      s    	zPermitsResult.__new__c             C   s   | j | j S )z2 A string indicating why the result was generated.)r-   r.   )selfr   r   r   msg   s    zPermitsResult.msgc             C   s   | j S )N)r2   )r1   r   r   r   __str__   s    zPermitsResult.__str__c             C   s   d| j jt| | jf S )Nz<%s instance at %s with msg %r>)	__class____name__idr2   )r1   r   r   r   __repr__   s    zPermitsResult.__repr__N)r5   
__module____qualname__r+   propertyr2   r3   r7   r   r   r   r   r)      s   r)   c               @   s   e Zd ZdZdZdS )Denieda#  
    An instance of ``Denied`` is returned when a security-related
    API or other :app:`Pyramid` code denies an action unrelated to
    an ACL check.  It evaluates equal to all boolean false types.  It
    has an attribute named ``msg`` describing the circumstances for
    the deny.

    r   N)r5   r8   r9   __doc__r,   r   r   r   r   r;      s   r;   c               @   s   e Zd ZdZdZdS )r%   a$  
    An instance of ``Allowed`` is returned when a security-related
    API or other :app:`Pyramid` code allows an action unrelated to
    an ACL check.  It evaluates equal to all boolean true types.  It
    has an attribute named ``msg`` describing the circumstances for
    the allow.

       N)r5   r8   r9   r<   r,   r   r   r   r   r%      s   r%   c               @   s>   e Zd ZdZedd Zedd Zedd Zdd	d
ZdS )SecurityAPIMixinz< Mixin for Request class providing auth-related properties. c             C   s   t | }|dkrdS || S )z
        Return an opaque object identifying the current user or ``None`` if no
        user is authenticated or there is no :term:`security policy` in effect.

        N)r   identity)r1   r   r   r   r   r?      s    zSecurityAPIMixin.identityc             C   s   t | }|dkrdS || S )av  
        Return the :term:`userid` of the currently authenticated user or
        ``None`` if there is no :term:`security policy` in effect or there is
        no currently authenticated user.

        .. versionchanged:: 2.0

           This property delegates to the effective :term:`security policy`,
           ignoring old-style :term:`authentication policy`.

        N)r   authenticated_userid)r1   r   r   r   r   r@      s    z%SecurityAPIMixin.authenticated_useridc             C   s
   | j dk	S )z<Return ``True`` if a user is authenticated for this request.N)r@   )r1   r   r   r   is_authenticated   s    z!SecurityAPIMixin.is_authenticatedNc             C   s4   |dkr| j }t| }|dkr&tdS || ||S )a  Given a permission and an optional context, returns an instance of
        :data:`pyramid.security.Allowed` if the permission is granted to this
        request with the provided context, or the context already associated
        with the request.  Otherwise, returns an instance of
        :data:`pyramid.security.Denied`.  This method delegates to the current
        security policy.  Returns
        :data:`pyramid.security.Allowed` unconditionally if no security
        policy has been registered for this request.  If ``context`` is not
        supplied or is supplied as ``None``, the context used is the
        ``request.context`` attribute.

        :param permission: Does this request have the given permission?
        :type permission: str
        :param context: A resource object or ``None``
        :type context: object
        :returns: Either :class:`pyramid.security.Allowed` or
                  :class:`pyramid.security.Denied`.

        NzNo security policy in use.)r   r   r%   permits)r1   r   r   r   r   r   r   has_permission   s    zSecurityAPIMixin.has_permission)N)	r5   r8   r9   r<   r:   r?   r@   rA   rC   r   r   r   r   r>      s
   r>   c               @   s<   e Zd ZdZedd ZeedZedd ZeedZdS )	AuthenticationAPIMixinz= Mixin for Request class providing compatibility properties. c             C   s<   t | }|dkrdS t|tr2|| }|| S || S )a  
        .. deprecated:: 2.0

            ``unauthenticated_userid`` does not have an equivalent in the new
            security system. Use :attr:`.authenticated_userid` or
            :attr:`.identity` instead. See :ref:`upgrading_auth_20` for more
            information.

        Return an object which represents the *claimed* (not verified) user
        id of the credentials present in the request. ``None`` if there is no
        :term:`authentication policy` in effect or there is no user data
        associated with the current request.  This differs from
        :attr:`~pyramid.request.Request.authenticated_userid`, because the
        effective authentication policy will not ensure that a record
        associated with the userid exists in persistent storage.

        N)r   
isinstanceLegacySecurityPolicy_get_authn_policyunauthenticated_useridr@   )r1   securityauthnr   r   r   rH     s    


z-AuthenticationAPIMixin.unauthenticated_useridzThe new security policy has deprecated unauthenticated_userid. See "Upgrading Authentication/Authorization" in "What's New in Pyramid 2.0" of the documentation for more information.c             C   s@   ddl m} t| }|dk	r:t|tr:|| }|| S |gS )a  
        .. deprecated:: 2.0

            The new security policy has removed the concept of principals.  See
            :ref:`upgrading_auth_20` for more information.

        Return the list of 'effective' :term:`principal` identifiers
        for the ``request``. If no :term:`authentication policy` is in effect,
        this will return a one-element list containing the
        :data:`pyramid.authorization.Everyone` principal.

        r   )r   N)r   r   r   rE   rF   rG   effective_principals)r1   r   rI   rJ   r   r   r   rK   ?  s    

z+AuthenticationAPIMixin.effective_principalszThe new security policy has deprecated effective_principals. See "Upgrading Authentication/Authorization" in "What's New in Pyramid 2.0" of the documentation for more information.N)r5   r8   r9   r<   r:   rH   r   rK   r   r   r   r   rD     s   rD   c               @   sH   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dS )rF   z
    A :term:`security policy` which provides a backwards compatibility shim for
    the :term:`authentication policy` and the :term:`authorization policy`.

    c             C   s   |j tS )N)r   
getUtilityr   )r1   r   r   r   r   rG   g  s    z&LegacySecurityPolicy._get_authn_policyc             C   s   |j tS )N)r   rL   r   )r1   r   r   r   r   _get_authz_policyj  s    z&LegacySecurityPolicy._get_authz_policyc             C   s
   |  |S )N)r@   )r1   r   r   r   r   r?   m  s    zLegacySecurityPolicy.identityc             C   s   |  |}||S )N)rG   r@   )r1   r   rJ   r   r   r   r@   p  s    
z)LegacySecurityPolicy.authenticated_useridc             K   s   |  |}|j||f|S )N)rG   r   )r1   r   r   r   rJ   r   r   r   r   t  s    
zLegacySecurityPolicy.rememberc             K   s    |rt d| |}||S )NzLLegacy authentication policies do not support keyword arguments for `forget`)
ValueErrorrG   r   )r1   r   r   rJ   r   r   r   r   x  s
    
zLegacySecurityPolicy.forgetc             C   s,   |  |}| |}||}||||S )N)rG   rM   rK   rB   )r1   r   r   r   rJ   Zauthz
principalsr   r   r   rB     s    


zLegacySecurityPolicy.permitsN)r5   r8   r9   r<   rG   rM   r?   r@   r   r   rB   r   r   r   r   rF   _  s   	rF   zsystem.Everyonezsystem.AuthenticatedAllowDenyc               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	AllPermissionsListz9 Stand in 'permission list' to represent all permissions c             C   s   t dS )Nr   )iter)r1   r   r   r   __iter__  s    zAllPermissionsList.__iter__c             C   s   dS )NTr   )r1   otherr   r   r   __contains__  s    zAllPermissionsList.__contains__c             C   s   t || jS )N)rE   r4   )r1   rU   r   r   r   __eq__  s    zAllPermissionsList.__eq__N)r5   r8   r9   r<   rT   rV   rW   r   r   r   r   rR     s   rR   c               @   s   e Zd Zdd ZdS )ACLPermitsResultc          
   C   s@   d}t | || j|||||}||_||_||_||_||_|S )a  
        Create a new instance.

        :param ace: The :term:`ACE` that matched, triggering the result.
        :param acl: The :term:`ACL` containing ``ace``.
        :param permission: The required :term:`permission`.
        :param principals: The list of :term:`principals <principal>` provided.
        :param context: The :term:`context` providing the :term:`lineage`
                        searched.

        zE%s permission %r via ACE %r in ACL %r on context %r for principals %r)r)   r+   r5   r   aceaclrO   r   )r/   rY   rZ   r   rO   r   fmtr0   r   r   r   r+     s    zACLPermitsResult.__new__N)r5   r8   r9   r+   r   r   r   r   rX     s   rX   c               @   s   e Zd ZdZdS )	ACLDenieda8  
    An instance of ``ACLDenied`` is a specialization of
    :class:`pyramid.security.Denied` that represents that a security check
    made explicitly against ACL was denied.  It evaluates equal to all
    boolean false types.  It also has the following attributes: ``acl``,
    ``ace``, ``permission``, ``principals``, and ``context``.  These
    attributes indicate the security values involved in the request.  Its
    ``__str__`` method prints a summary of these attributes for debugging
    purposes. The same summary is available as the ``msg`` attribute.

    N)r5   r8   r9   r<   r   r   r   r   r\     s   r\   c               @   s   e Zd ZdZdS )
ACLAlloweda:  
    An instance of ``ACLAllowed`` is a specialization of
    :class:`pyramid.security.Allowed` that represents that a security check
    made explicitly against ACL was allowed.  It evaluates equal to all
    boolean true types.  It also has the following attributes: ``acl``,
    ``ace``, ``permission``, ``principals``, and ``context``.  These
    attributes indicate the security values involved in the request.  Its
    ``__str__`` method prints a summary of these attributes for debugging
    purposes. The same summary is available as the ``msg`` attribute.

    N)r5   r8   r9   r<   r   r   r   r   r]     s   r]   )	ALL_PERMISSIONSDENY_ALLr]   r\   rR   rP   AuthenticatedrQ   r   zl"pyramid.security.{attr}" is deprecated in Pyramid 2.0. Adjust your import to "pyramid.authorization.{attr}")attrN)r   )'Zzope.deprecationr   Zzope.interfacer   r   Zpyramid.interfacesr   r   r   r   r	   r
   Zpyramid.threadlocalr   ZNO_PERMISSION_REQUIREDr   r   r   r   r(   r*   r)   r;   r%   r>   rD   rF   r   r`   rP   rQ   rR   r^   r_   rX   r\   r]   ra   formatr   r   r   r   <module>   sD    %!
!BG)

