o
    ˷e3                     @   sf   d Z ddlZddlZddlZddlZdZ	 dZ	 dZ	 dZdZ	G dd	 d	e
ZG d
d dejjZdS )a6  Non-API-specific IAM policy definitions

For allowed roles / permissions, see:
https://cloud.google.com/iam/docs/understanding-roles

Example usage:

.. code-block:: python

   # ``get_iam_policy`` returns a :class:'~google.api_core.iam.Policy`.
   policy = resource.get_iam_policy(requested_policy_version=3)

   phred = "user:phred@example.com"
   admin_group = "group:admins@groups.example.com"
   account = "serviceAccount:account-1234@accounts.example.com"

   policy.version = 3
   policy.bindings = [
       {
           "role": "roles/owner",
           "members": {phred, admin_group, account}
       },
       {
           "role": "roles/editor",
           "members": {"allAuthenticatedUsers"}
       },
       {
           "role": "roles/viewer",
           "members": {"allUsers"}
           "condition": {
               "title": "request_time",
               "description": "Requests made before 2021-01-01T00:00:00Z",
               "expression": "request.time < timestamp("2021-01-01T00:00:00Z")"
           }
       }
   ]

   resource.set_iam_policy(policy)
    Nzroles/ownerzroles/editorzroles/viewerz_Assigning to '{}' is deprecated. Use the `policy.bindings` property to modify bindings instead.zWDict access is not supported on policies with version > 1 or with conditional bindings.c                   @   s   e Zd ZdZdS )InvalidOperationExceptionz1Raised when trying to use Policy class as a dict.N)__name__
__module____qualname____doc__ r   r   J/var/www/ideatree/venv/lib/python3.10/site-packages/google/api_core/iam.pyr   M   s    r   c                   @   s.  e Zd ZdZefZ	 efZ	 efZ		 d/ddZ
dd Zdd Zd	d
 Zdd Zdd Zdd Zdd Zedd Zejdd Zedd Zejdd Zedd Zejdd Zedd Zejdd Zedd  Zed!d" Zed#d$ Zed%d& Zed'd( Zed)d* Zed+d, Z d-d. Z!dS )0Policya1  IAM Policy

    Args:
        etag (Optional[str]): ETag used to identify a unique of the policy
        version (Optional[int]): The syntax schema version of the policy.

    Note:
        Using conditions in bindings requires the policy's version to be set
        to `3` or greater, depending on the versions that are currently supported.

        Accessing the policy using dict operations will raise InvalidOperationException
        when the policy's version is set to 3.

        Use the policy.bindings getter/setter to retrieve and modify the policy's bindings.

    See:
        IAM Policy https://cloud.google.com/iam/reference/rest/v1/Policy
        Policy versions https://cloud.google.com/iam/docs/policies#versions
        Conditions overview https://cloud.google.com/iam/docs/conditions-overview.
    Nc                 C   s   || _ || _g | _d S N)etagversion	_bindings)selfr   r   r   r   r   __init__r   s   
zPolicy.__init__c                 C   s   |    dd | jD S )Nc                 s   s     | ]}|d  r|d V  qdS )membersroleNr   ).0bindingr   r   r   	<genexpr>z   s    z"Policy.__iter__.<locals>.<genexpr>)__check_version__r   r   r   r   r   __iter__w      zPolicy.__iter__c                 C   s   |    tt|  S r
   )r   lenlistr   r   r   r   r   __len__|   r   zPolicy.__len__c                 C   sL   |    | jD ]}|d |kr|d   S q|t d}| j| |d S Nr   r   r   r   )r   r   setappend)r   keybnew_bindingr   r   r   __getitem__   s   
zPolicy.__getitem__c                 C   sL   |    t|}| jD ]}|d |kr||d<  d S q| j||d d S r   )r   r   r   r   )r   r    valuer   r   r   r   __setitem__   s   
zPolicy.__setitem__c                 C   s:   |    | jD ]}|d |kr| j|  d S qt|)Nr   )r   r   removeKeyError)r   r    r!   r   r   r   __delitem__   s   
zPolicy.__delitem__c                 C   s,   | j duo	| j dk}|s|  rttdS )z[Raise InvalidOperationException if version is greater than 1 or policy contains conditions.N   )r   _contains_conditionsr   _DICT_ACCESS_MSG)r   raise_versionr   r   r   r      s   zPolicy.__check_version__c                 C   s$   | j D ]}|dd ur dS qdS )N	conditionTF)r   get)r   r!   r   r   r   r*      s
   
zPolicy._contains_conditionsc                 C   s   | j S )aE  The policy's list of bindings.

        A binding is specified by a dictionary with keys:

        * role (str): Role that is assigned to `members`.

        * members (:obj:`set` of str): Specifies the identities associated to this binding.

        * condition (:obj:`dict` of str:str): Specifies a condition under which this binding will apply.

          * title (str): Title for the condition.

          * description (:obj:str, optional): Description of the condition.

          * expression: A CEL expression.

        Type:
           :obj:`list` of :obj:`dict`

        See:
           Policy versions https://cloud.google.com/iam/docs/policies#versions
           Conditions overview https://cloud.google.com/iam/docs/conditions-overview.

        Example:

        .. code-block:: python

           USER = "user:phred@example.com"
           ADMIN_GROUP = "group:admins@groups.example.com"
           SERVICE_ACCOUNT = "serviceAccount:account-1234@accounts.example.com"
           CONDITION = {
               "title": "request_time",
               "description": "Requests made before 2021-01-01T00:00:00Z", # Optional
               "expression": "request.time < timestamp("2021-01-01T00:00:00Z")"
           }

           # Set policy's version to 3 before setting bindings containing conditions.
           policy.version = 3

           policy.bindings = [
               {
                   "role": "roles/viewer",
                   "members": {USER, ADMIN_GROUP, SERVICE_ACCOUNT},
                   "condition": CONDITION
               },
               ...
           ]
        r   r   r   r   r   bindings   s   2zPolicy.bindingsc                 C   s
   || _ d S r
   r/   )r   r0   r   r   r   r0      s   
c                 C   6   t  }| jD ]}| |dD ]}|| qqt|S )zLegacy access to owner role.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to access bindings instead.
        r   )r   _OWNER_ROLESr.   add	frozensetr   resultr   memberr   r   r   owners      
zPolicy.ownersc                 C       t tdtt || t< dS )zUpdate owners.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to access bindings instead.
        r8   N)warningswarn_ASSIGNMENT_DEPRECATED_MSGformat
OWNER_ROLEDeprecationWarningr   r$   r   r   r   r8      s   c                 C   r1   )zLegacy access to editor role.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to access bindings instead.
        r   )r   _EDITOR_ROLESr.   r3   r4   r5   r   r   r   editors   r9   zPolicy.editorsc                 C   r:   )zUpdate editors.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to modify bindings instead.
        rC   N)r;   r<   r=   r>   EDITOR_ROLEr@   rA   r   r   r   rC     
   
c                 C   r1   )zLegacy access to viewer role.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to modify bindings instead.
        r   )r   _VIEWER_ROLESr.   r3   r4   r5   r   r   r   viewers  r9   zPolicy.viewersc                 C   r:   )zUpdate viewers.

        Raise InvalidOperationException if version is greater than 1 or policy contains conditions.

        DEPRECATED:  use `policy.bindings` to modify bindings instead.
        rG   N)r;   r<   r=   r>   VIEWER_ROLEr@   rA   r   r   r   rG   (  rE   c                 C   
   d| f S )zFactory method for a user member.

        Args:
            email (str): E-mail for this particular user.

        Returns:
            str: A member string corresponding to the given user.
        zuser:%sr   emailr   r   r   user6     

zPolicy.userc                 C   rI   )zFactory method for a service account member.

        Args:
            email (str): E-mail for this particular service account.

        Returns:
            str: A member string corresponding to the given service account.

        zserviceAccount:%sr   rJ   r   r   r   service_accountB  s   
zPolicy.service_accountc                 C   rI   )zFactory method for a group member.

        Args:
            email (str): An id or e-mail for this particular group.

        Returns:
            str: A member string corresponding to the given group.
        zgroup:%sr   rJ   r   r   r   groupO  rM   zPolicy.groupc                 C   rI   )zFactory method for a domain member.

        Args:
            domain (str): The domain for this member.

        Returns:
            str: A member string corresponding to the given domain.
        z	domain:%sr   )domainr   r   r   rP   [  rM   zPolicy.domainc                   C      dS )zFactory method for a member representing all users.

        Returns:
            str: A member string representing all users.
        allUsersr   r   r   r   r   	all_usersg     zPolicy.all_usersc                   C   rQ   )zFactory method for a member representing all authenticated users.

        Returns:
            str: A member string representing all authenticated users.
        allAuthenticatedUsersr   r   r   r   r   authenticated_usersp  rT   zPolicy.authenticated_usersc                 C   sP   | d}| d}| ||}| dg |_|jD ]}t| dd|d< q|S )zFactory: create a policy from a JSON resource.

        Args:
            resource (dict): policy resource returned by ``getIamPolicy`` API.

        Returns:
            :class:`Policy`: the parsed policy
        r   r   r0   r   r   )r.   r0   r   )clsresourcer   r   policyr   r   r   r   from_api_repry  s   




zPolicy.from_api_reprc                 C   s   i }| j dur| j |d< | jdur| j|d< | jrWt| jdkrWg }| jD ]"}|d}|rG|d t|d}|d}|rB||d< || q%|rWtd}t||d	|d
< |S )zRender a JSON policy resource.

        Returns:
            dict: a resource to be passed to the ``setIamPolicy`` API.
        Nr   r   r   r   r   r   r-   )r    r0   )	r   r   r   r   r.   sortedr   operator
itemgetter)r   rX   r0   r   r   r"   r-   r    r   r   r   to_api_repr  s(   








zPolicy.to_api_repr)NN)"r   r   r   r   r?   r2   rD   rB   rH   rF   r   r   r   r#   r%   r(   r   r*   propertyr0   setterr8   rC   rG   staticmethodrL   rN   rO   rP   rS   rV   classmethodrZ   r^   r   r   r   r   r	   S   s^    
	
3













r	   )r   collectionscollections.abcr\   r;   r?   rD   rH   r=   r+   	Exceptionr   abcMutableMappingr	   r   r   r   r   <module>   s   (