pyramid.security
¶
Authentication API Functions¶
-
authenticated_userid
(request)[source]¶ A function that returns the value of the property
pyramid.request.Request.authenticated_userid
.Deprecated since version 1.5: Use
pyramid.request.Request.authenticated_userid
instead.
-
unauthenticated_userid
(request)[source]¶ A function that returns the value of the property
pyramid.request.Request.unauthenticated_userid
.Deprecated since version 1.5: Use
pyramid.request.Request.unauthenticated_userid
instead.
-
effective_principals
(request)[source]¶ A function that returns the value of the property
pyramid.request.Request.effective_principals
.Deprecated since version 1.5: Use
pyramid.request.Request.effective_principals
instead.
-
forget
(request)[source]¶ 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 WebOb -style response object computed previously by the view code):from pyramid.security import forget headers = forget(request) response.headerlist.extend(headers) return response
If no authentication policy is in use, this function will always return an empty sequence.
-
remember
(request, userid, **kwargs)[source]¶ 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 asuserid
and*kw
using the current authentication policy. Common usage might look like so within the body of a view function (response
is assumed to be a WebOb -style response object computed previously by the view code):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 authentication 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 authentication policy.Deprecated since version 1.6: Renamed the
principal
argument touserid
to clarify its purpose.
Authorization API Functions¶
-
has_permission
(permission, context, request)[source]¶ A function that calls
pyramid.request.Request.has_permission()
and returns its result.Deprecated since version 1.5: Use
pyramid.request.Request.has_permission()
instead.Changed in version 1.5a3: If context is None, then attempt to use the context attribute of self; if not set, then the AttributeError is propagated.
-
principals_allowed_by_permission
(context, permission)[source]¶ Provided a
context
(a resource object), and apermission
(a string or unicode object), if a authorization policy is in effect, return a sequence of principal ids that possess the permission in thecontext
. If no authorization policy is in effect, this will return a sequence with the single valuepyramid.security.Everyone
(the special principal identifier representing all principals).Note
even if an authorization policy is in effect, some (exotic) authorization policies may not implement the required machinery for this function; those will cause a
NotImplementedError
exception to be raised when this function is invoked.
-
view_execution_permitted
(context, request, name='')[source]¶ If the view specified by
context
andname
is protected by a permission, check the permission associated with the view using the effective authentication/authorization policies and therequest
. Return a boolean result. If no authorization policy is in effect, or if the view is not protected by a permission, returnTrue
. If no view can view found, an exception will be raised.Changed in version 1.4a4: An exception is raised if no view is found.
Constants¶
-
Everyone
¶ The special principal id named 'Everyone'. This principal id is granted to all requests. Its actual value is the string 'system.Everyone'.
-
Authenticated
¶ The special principal id named 'Authenticated'. This principal id is granted to all requests which contain any other non-Everyone principal id (according to the authentication policy). Its actual value is the string 'system.Authenticated'.
-
ALL_PERMISSIONS
¶ An object that can be used as the
permission
member of an ACE which matches all permissions unconditionally. For example, an ACE that usesALL_PERMISSIONS
might be composed like so:('Deny', 'system.Everyone', ALL_PERMISSIONS)
.
-
DENY_ALL
¶ A convenience shorthand ACE that defines
('Deny', 'system.Everyone', ALL_PERMISSIONS)
. This is often used as the last ACE in an ACL in systems that use an "inheriting" security policy, representing the concept "don't inherit any other ACEs".
-
NO_PERMISSION_REQUIRED
¶ A special permission which indicates that the view should always be executable by entirely anonymous users, regardless of the default permission, bypassing any authorization policy that may be in effect. Its actual value is the string '__no_permission_required__'.
Return Values¶
-
Allow
¶ The ACE "action" (the first element in an ACE e.g.
(Allow, Everyone, 'read')
that means allow access. A sequence of ACEs makes up an ACL. It is a string, and its actual value is "Allow".
-
Deny
¶ The ACE "action" (the first element in an ACE e.g.
(Deny, 'george', 'read')
that means deny access. A sequence of ACEs makes up an ACL. It is a string, and its actual value is "Deny".
-
class
ACLDenied
[source]¶ An instance of
ACLDenied
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
, andcontext
. 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 themsg
attribute.
-
class
ACLAllowed
[source]¶ An instance of
ACLAllowed
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
, andcontext
. 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 themsg
attribute.