Ñò »Ic@s³dZddklZlZddddddd d d d d ddddgZdefd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZ defd„ƒYZ d efd„ƒYZ d efd„ƒYZ d e fd„ƒYZ d e fd„ƒYZdefd„ƒYZdefd„ƒYZd efd„ƒYZde fd„ƒYZde fd „ƒYZdefd!„ƒYZdefd"„ƒYZd#S($s¥ Built-in predicate checkers. This is module provides the predicate checkers that were present in the original "identity" framework of TurboGears 1, plus others. iÿÿÿÿ(tparse_formvarstparse_dict_querystringt PredicatetCompoundPredicatetAlltAnythas_all_permissionsthas_any_permissionthas_permissiont in_all_groupst in_any_grouptin_grouptis_usert is_anonymoust not_anonymoustPredicateErrortNotAuthorizedErrorcBs\eZdZd d„Zd„Zd„Zd„Zd d„Zd„Z d„Z d„Z RS( s– Generic predicate checker. This is the base predicate class. It won't do anything useful for you, unless you subclass it. cCs|o ||_ndS(sB Create a predicate and use ``msg`` as the error message if it fails. :param msg: The error message, if you want to override the default one defined by the predicate. :type msg: str You may use the ``msg`` keyword argument with any predicate. N(tmessage(tselftmsg((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyt__init__/s cCs|i|ƒdS(sò Raise an exception if the predicate is not met. :param environ: The WSGI environment. :type environ: dict :param credentials: The :mod:`repoze.what` ``credentials`` dictionary as a short-cut. :type credentials: dict :raise NotImplementedError: When the predicate doesn't define this method. :raises NotAuthorizedError: If the predicate is not met (use :meth:`unmet` to raise it). This is the method that must be overridden by any predicate checker. For example, if your predicate is "The current month is the specified one", you may define the following predicate checker:: from datetime import date from repoze.what.predicates import Predicate class is_month(Predicate): message = 'The current month must be %(right_month)s' def __init__(self, right_month, **kwargs): self.right_month = right_month super(is_month, self).__init__(**kwargs) def evaluate(self, environ, credentials): today = date.today() if today.month != self.right_month: # Raise an exception because the predicate is not met. self.unmet() .. versionadded:: 1.0.2 .. attention:: Do not evaluate predicates by yourself using this method. See :meth:`check_authorization` and :meth:`is_met`. .. warning:: To make your predicates thread-safe, keep in mind that they may be instantiated at module-level and then shared among many threads, so avoid predicates from being modified after their evaluation. This is, the ``evaluate()`` method should not add, modify or delete any attribute of the predicate. N(teval_with_environ(Rtenviront credentials((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pytevaluate=s2cCs t‚dS(s­ Check whether the predicate is met. :param environ: The WSGI environment. :type environ: dict :return: Whether the predicate is met or not. :rtype: bool :raise NotImplementedError: This must be defined by the predicate itself. .. deprecated:: 1.0.2 Only :meth:`evaluate` will be used as of :mod:`repoze.what` v2. N(tNotImplementedError(RR((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyt_eval_with_environqscCsKddkl}d}||tddƒ|i|ƒp|iƒndS(sç Make sure this predicate is met. :param environ: The WSGI environment. :raises NotAuthorizedError: If the predicate is not met. .. versionchanged:: 1.0.1 In :mod:`repoze.what`<1.0.1, this method returned a ``bool`` and set the ``error`` instance attribute of the predicate to the predicate message. .. deprecated:: 1.0.2 Define :meth:`evaluate` instead. iÿÿÿÿ(twarnsšPredicate._eval_with_environ(environ) is deprecated for forward compatibility with repoze.what v2; define Predicate.evaluate(environ, credentials) insteadt stackleveliN(twarningsRtDeprecationWarningRtunmet(RRRR((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR‚s cKsV|o |}n |i}t|ƒ}|iiƒ}|i|ƒt||ƒ‚dS(s  Raise an exception because this predicate is not met. :param msg: The error message to be used; overrides the predicate's default one. :type msg: str :raises NotAuthorizedError: If the predicate is not met. ``placeholders`` represent the placeholders for the predicate message. The predicate's attributes will also be taken into account while creating the message with its placeholders. For example, if you have a predicate that checks that the current month is the specified one, where the predicate message is defined with two placeholders as in:: The current month must be %(right_month)s and it is %(this_month)s and the predicate has an attribute called ``right_month`` which represents the expected month, then you can use this method as in:: self.unmet(this_month=this_month) Then :meth:`unmet` will build the message using the ``this_month`` keyword argument and the ``right_month`` attribute as the placeholders for ``this_month`` and ``right_month``, respectively. So, if ``this_month`` equals ``3`` and ``right_month`` equals ``5``, the message for the exception to be raised will be:: The current month must be 5 and it is 3 If you have a context-sensitive predicate checker and thus you want to change the error message on evaluation, you can call :meth:`unmet` as:: self.unmet('%(this_month)s is not a good month', this_month=this_month) The exception raised would contain the following message:: 3 is not a good month .. versionadded:: 1.0.2 .. versionchanged:: 1.0.4 Introduced the ``msg`` argument. .. attention:: This method should only be called from :meth:`evaluate`. N(Rtunicodet__dict__tcopytupdateR(RRt placeholdersRtall_placeholders((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRšs5    cCs€|idƒ}|idhƒ}y|i||ƒWn0tj o$}|o|id|ƒ‚nX|o |idƒdS(sÈ Evaluate the predicate and raise an exception if it's not met. :param environ: The WSGI environment. :raise NotAuthorizedError: If it the predicate is not met. Example:: >>> from repoze.what.predicates import is_user >>> environ = gimme_the_environ() >>> p = is_user('gustavo') >>> p.check_authorization(environ) # ... repoze.what.predicates.NotAuthorizedError: The current user must be "gustavo" .. versionadded:: 1.0.4 Backported from :mod:`repoze.what` v2; deprecates :func:`repoze.what.authorize.check_authorization`. srepoze.who.loggersrepoze.what.credentialsuAuthorization denied: %ssAuthorization grantedN(tgetRRtinfo(RRtloggerRterror((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pytcheck_authorizationÛscCsG|idhƒ}y|i||ƒtSWntj o }tSXdS(s  Find whether the predicate is met or not. :param environ: The WSGI environment. :return: Whether the predicate is met or not. :rtype: bool Example:: >>> from repoze.what.predicates import is_user >>> environ = gimme_the_environ() >>> p = is_user('gustavo') >>> p.is_met(environ) False .. versionadded:: 1.0.4 Backported from :mod:`repoze.what` v2. srepoze.what.credentialsN(R&RtTrueRtFalse(RRRR)((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pytis_metùs cCs©t|ƒph}yt|tƒph}Wntj o h}nX|idghfƒ}|dpd}|dph}h|d6|d6|d6|d6}|S( sj Return the GET and POST variables in the request, as well as ``wsgiorg.routing_args`` arguments. :param environ: The WSGI environ. :return: The GET and POST variables and ``wsgiorg.routing_args`` arguments. :rtype: dict This is a handy method for request-sensitive predicate checkers. It will return a dictionary for the POST and GET variables, as well as the `wsgiorg.routing_args `_'s ``positional_args`` and ``named_args`` arguments, in the ``post``, ``get``, ``positional_args`` and ``named_args`` items (respectively) of the returned dictionary. For example, if the user submits a form using the POST method to ``http://example.com/blog/hello-world/edit_post?wysiwyg_editor=Yes``, this method may return:: { 'post': {'new_post_contents': 'These are the new contents'}, 'get': {'wysiwyg_editor': 'Yes'}, 'named_args': {'post_slug': 'hello-world'}, 'positional_args': (), } But note that the ``named_args`` and ``positional_args`` items depend completely on how you configured the dispatcher. .. versionadded:: 1.0.4 swsgiorg.routing_argsiitpostR&tpositional_argst named_args((RRR,tKeyErrorR&(RRtget_varst post_varst routing_argsR/R0t variables((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pytparse_variabless$  N( t__name__t __module__t__doc__tNoneRRRRRR*R-R6(((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR&s  4   A  cBseZdZd„ZRS(s)A predicate composed of other predicates.cOs#tt|ƒi|||_dS(N(tsuperRRt predicates(RR<tkwargs((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRKs(R7R8R9R(((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRHstNotcBs&eZdZdZd„Zd„ZRS(sÇ Negate the specified predicate. :param predicate: The predicate to be negated. Example:: # The user *must* be anonymous: p = Not(not_anonymous()) uThe condition must not be metcKs#tt|ƒi|||_dS(N(R;R>Rt predicate(RR?R=((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR^scCs>y|ii||ƒWntj o }dSX|iƒdS(N(R?RRR(RRRR)((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRbs (R7R8R9RRR(((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR>Ps  cBseZdZd„ZRS(s< Check that all of the specified predicates are met. :param predicates: All of the predicates that must be met. Example:: # Grant access if the current month is July and the user belongs to # the human resources group. p = All(is_month(7), in_group('hr')) cCs(x!|iD]}|i||ƒq WdS(s Evaluate all the predicates it contains. :param environ: The WSGI environment. :param credentials: The :mod:`repoze.what` ``credentials``. :raises NotAuthorizedError: If one of the predicates is not met. N(R<R(RRRtp((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRxs (R7R8R9R(((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRjs cBseZdZdZd„ZRS(s4 Check that at least one of the specified predicates is met. :param predicates: Any of the predicates that must be met. Example:: # Grant access if the currest user is Richard Stallman or Linus # Torvalds. p = Any(is_user('rms'), is_user('linus')) uKAt least one of the following predicates must be met: %(failed_predicates)scCs€g}xT|iD]I}y|i||ƒdSWqtj o}|it|ƒƒqXqWdi|ƒ}|id|ƒdS(s Evaluate all the predicates it contains. :param environ: The WSGI environment. :param credentials: The :mod:`repoze.what` ``credentials``. :raises NotAuthorizedError: If none of the predicates is met. Ns, tfailed_predicates(R<RRtappendR tjoinR(RRRterrorsR@texcRA((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR•s  (R7R8R9RR(((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR…s cBs&eZdZdZd„Zd„ZRS(sÒ Check that the authenticated user's username is the specified one. :param user_name: The required user name. :type user_name: str Example:: p = is_user('linus') u(The current user must be "%(user_name)s"cKs#tt|ƒi|||_dS(N(R;R Rt user_name(RRFR=((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR¸scCs3|o|i|idƒjodS|iƒdS(Nsrepoze.what.userid(RFR&R(RRR((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR¼s(R7R8R9RRR(((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR ©s  cBs&eZdZdZd„Zd„ZRS(sç Check that the user belongs to the specified group. :param group_name: The name of the group to which the user must belong. :type group_name: str Example:: p = in_group('customers') u:The current user must belong to the group "%(group_name)s"cKs#tt|ƒi|||_dS(N(R;R Rt group_name(RRGR=((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRÒscCs3|o|i|idƒjodS|iƒdS(Ntgroups(RGR&R(RRR((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRÖs (R7R8R9RRR(((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR Ãs  cBseZdZd„ZRS(sã Check that the user belongs to all of the specified groups. :param groups: The name of all the groups the user must belong to. Example:: p = in_all_groups('developers', 'designers') cOsDg}|D]}|t|ƒq ~}tt|ƒi||ŽdS(N(R R;R R(RRHR=t_[1]tgtgroup_predicates((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRés'(R7R8R9R(((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR Üs cBseZdZdZd„ZRS(så Check that the user belongs to at least one of the specified groups. :param groups: The name of any of the groups the user may belong to. Example:: p = in_any_group('directors', 'hr') uNThe member must belong to at least one of the following groups: %(group_list)scOsVdi|ƒ|_g}|D]}|t|ƒq~}tt|ƒi||ŽdS(Ns, (RCt group_listR R;R R(RRHR=RIRJRK((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRýs'(R7R8R9RR(((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR îs cBseZdZdZd„ZRS(s² Check that the current user is anonymous. Example:: # The user must be anonymous! p = is_anonymous() .. versionadded:: 1.0.7 u"The current user must be anonymouscCs|o|iƒndS(N(R(RRR((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRs(R7R8R9RR(((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR s cBseZdZdZd„ZRS(s§ Check that the current user has been authenticated. Example:: # The user must have been authenticated! p = not_anonymous() u-The current user must have been authenticatedcCs|p|iƒndS(N(R(RRR((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR$s(R7R8R9RR(((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRs cBs&eZdZdZd„Zd„ZRS(sê Check that the current user has the specified permission. :param permission_name: The name of the permission that must be granted to the user. Example:: p = has_permission('hire') u7The user must have the "%(permission_name)s" permissioncKs#tt|ƒi|||_dS(N(R;RRtpermission_name(RRMR=((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR7scCs3|o|i|idƒjodS|iƒdS(Nt permissions(RMR&R(RRR((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR;s(R7R8R9RRR(((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR)s  cBseZdZd„ZRS(s Check that the current user has been granted all of the specified permissions. :param permissions: The names of all the permissions that must be granted to the user. Example:: p = has_all_permissions('view-users', 'edit-users') cOsDg}|D]}|t|ƒq ~}tt|ƒi||ŽdS(N(RR;RR(RRNR=RIR@tpermission_predicates((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRPs'(R7R8R9R(((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRBs cBseZdZdZd„ZRS(s Check that the user has at least one of the specified permissions. :param permissions: The names of any of the permissions that have to be granted to the user. Example:: p = has_any_permission('manage-users', 'edit-users') uQThe user must have at least one of the following permissions: %(permission_list)scOsVdi|ƒ|_g}|D]}|t|ƒq~}tt|ƒi||ŽdS(Ns, (RCtpermission_listRR;RR(RRNR=RIR@RO((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRfs'(R7R8R9RR(((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRVs cBs+eZdZeedƒp d„ZnRS(sè Former exception raised by a :class:`Predicate` if it's not met. .. deprecated:: 1.0.4 Deprecated in favor of :class:`NotAuthorizedError`, for forward compatibility with :mod:`repoze.what` v2. t __unicode__cCs"t|io|idpdƒS(Nit(R targs(R((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRQ|s(R7R8R9thasattrt ExceptionRQ(((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyRpscBseZdZRS(s. Exception raised by :meth:`Predicate.check_authorization` if the subject is not allowed to access the requested source. This exception deprecates :class:`PredicateError` as of v1.0.4, but extends it to avoid breaking backwards compatibility. .. versionchanged:: 1.0.4 This exception was defined at :mod:`repoze.what.authorize` until version 1.0.3, but is still imported into that module to keep backwards compatibility with v1.X releases -- but it won't work in :mod:`repoze.what` v2. (R7R8R9(((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyR€sN(R9t paste.requestRRt__all__tobjectRRR>RRR R R R R RRRRRURR(((s:/usr/lib/python2.6/site-packages/repoze/what/predicates.pyts.     ÿ#$