o
    Ch                     @   sr   d dl mZ d dlmZmZ d dlmZmZ G dd deZ	dddZ
d	d
 Zdd Zdd Zdd Zdd ZdS )    )unicode_literals)RolePermissionScopeExceptionCheckerNotRegistered)get_user_rolesget_or_create_permissionc                   @   s4   e Zd Zi Zedd Zedd Zedd ZdS )PermissionsManagerc                 C   s   || j |< d S N	_checkers)clsnamefunction r   m/var/www/html/myvaluetrips/my_value_trip_new/venv/lib/python3.10/site-packages/rolepermissions/permissions.pyregister_checker   s   z#PermissionsManager.register_checkerc                 C   s   | j S r   r	   )r   r   r   r   get_checkers   s   zPermissionsManager.get_checkersc                 C   s    || j v r
| j | S td| )Nz'Checker with name %s was not registered)r
   r   )r   checker_namer   r   r   retrieve_checker   s   

z#PermissionsManager.retrieve_checkerN)__name__
__module____qualname__r
   classmethodr   r   r   r   r   r   r   r      s    

r   Nc                    s    fdd}|S )Nc                    s    r n| j }t||  | S r   )r   r   r   )funcr   r   r   r   fuction_decorator   s   z2register_object_checker.<locals>.fuction_decoratorr   )r   r   r   r   r   register_object_checker   s   r   c                 C   s   t | \}}|S )z/Get a Permission object from a permission name.)r   )permission_name
permissioncreatedr   r   r   get_permission#   s   r   c                 C   sH   t | }i }t| jjddd}|D ]}| D ]}||v ||< qq|S )zb
    Get a boolean map of the permissions available to a user
    based on that user's roles.
    codenameT)flat)r   setuser_permissionsvalues_listpermission_names_list)userrolespermission_hashuser_permission_namesroler   r   r   r   available_perm_status*   s   r+   c                    s:   t | }tdd |D  |r fdd| j D S g S )a  
    Return a list of permissions codenames available to a user, based on that user's roles.
      i.e., keys for all "True" permissions from available_perm_status(user):
       Assert: set(available_perm_names(user)) == set(perm for perm,has_perm in available_perm_status(user) if has_perm)
       Query efficient; especially when prefetch_related('group', 'user_permissions') on user object.
       No side-effects; permissions are not created in DB as side-effect.
    c                 s   s"    | ]}|  D ]}|V  qqd S r   )r%   ).0r*   pr   r   r   	<genexpr>D   s     z'available_perm_names.<locals>.<genexpr>c                    s   g | ]
}|j  v r|j qS r   )r    )r,   r-   
perm_namesr   r   
<listcomp>E   s    z(available_perm_names.<locals>.<listcomp>)r   r"   r#   all)r&   r'   r   r/   r   available_perm_names;   s   r3   c                 C   @   t | }|D ]}|| v rt|}| j|  dS qtd)z
    Grant a user a specified permission.

    Permissions are only granted if they are in the scope any of the
    user's roles. If the permission is out of scope,
    a RolePermissionScopeException is raised.
    N?This permission isn't in the scope of any of this user's roles.)r   r%   r   r#   addr   r&   r   r'   r*   r   r   r   r   grant_permissionI      r8   c                 C   r4   )z
    Revoke a specified permission from a user.

    Permissions are only revoked if they are in the scope any of the user's
    roles. If the permission is out of scope, a RolePermissionScopeException
    is raised.
    Nr5   )r   r%   r   r#   remover   r7   r   r   r   revoke_permission^   r9   r;   r   )
__future__r   rolepermissions.exceptionsr   r   rolepermissions.rolesr   r   objectr   r   r   r+   r3   r8   r;   r   r   r   r   <module>   s    
