2017-03-27 21:37:45 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
2013-09-01 17:31:50 -05:00
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Rattail -- Retail Software Framework
|
2021-10-14 09:39:54 -05:00
|
|
|
# Copyright © 2010-2021 Lance Edgar
|
2013-09-01 17:31:50 -05:00
|
|
|
#
|
|
|
|
# This file is part of Rattail.
|
|
|
|
#
|
|
|
|
# Rattail is free software: you can redistribute it and/or modify it under the
|
2017-07-06 23:47:56 -05:00
|
|
|
# terms of the GNU General Public License as published by the Free Software
|
|
|
|
# Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
2013-09-01 17:31:50 -05:00
|
|
|
#
|
|
|
|
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
2017-07-06 23:47:56 -05:00
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
2013-09-01 17:31:50 -05:00
|
|
|
#
|
2017-07-06 23:47:56 -05:00
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# Rattail. If not, see <http://www.gnu.org/licenses/>.
|
2013-09-01 17:31:50 -05:00
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
"""
|
|
|
|
Authentication & Authorization
|
|
|
|
"""
|
|
|
|
|
2016-10-09 21:12:13 -05:00
|
|
|
from __future__ import unicode_literals, absolute_import
|
2015-08-11 17:26:04 -05:00
|
|
|
|
2017-02-11 17:08:27 -06:00
|
|
|
import logging
|
|
|
|
|
2017-08-04 16:48:33 -05:00
|
|
|
from rattail import enum
|
2017-02-13 19:23:24 -06:00
|
|
|
from rattail.util import prettify, NOTSET
|
2015-08-11 17:26:04 -05:00
|
|
|
|
2013-09-01 17:31:50 -05:00
|
|
|
from zope.interface import implementer
|
|
|
|
from pyramid.interfaces import IAuthorizationPolicy
|
2017-03-27 21:37:45 -05:00
|
|
|
from pyramid.security import remember, forget, Everyone, Authenticated
|
2013-09-01 17:31:50 -05:00
|
|
|
|
2015-08-11 17:26:04 -05:00
|
|
|
from tailbone.db import Session
|
2013-09-01 17:31:50 -05:00
|
|
|
|
|
|
|
|
2017-02-11 17:08:27 -06:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-02-21 13:12:23 -06:00
|
|
|
def login_user(request, user, timeout=NOTSET):
|
2017-02-11 17:08:27 -06:00
|
|
|
"""
|
|
|
|
Perform the steps necessary to login the given user. Note that this
|
|
|
|
returns a ``headers`` dict which you should pass to the redirect.
|
|
|
|
"""
|
2017-08-04 16:48:33 -05:00
|
|
|
user.record_event(enum.USER_EVENT_LOGIN)
|
2017-02-11 17:08:27 -06:00
|
|
|
headers = remember(request, user.uuid)
|
2017-02-13 19:23:24 -06:00
|
|
|
if timeout is NOTSET:
|
2017-02-21 13:12:23 -06:00
|
|
|
timeout = session_timeout_for_user(user)
|
2017-02-11 17:08:27 -06:00
|
|
|
log.debug("setting session timeout for '{}' to {}".format(user.username, timeout))
|
|
|
|
set_session_timeout(request, timeout)
|
|
|
|
return headers
|
|
|
|
|
|
|
|
|
2017-03-27 21:37:45 -05:00
|
|
|
def logout_user(request):
|
|
|
|
"""
|
|
|
|
Perform the logout action for the given request. Note that this returns a
|
|
|
|
``headers`` dict which you should pass to the redirect.
|
|
|
|
"""
|
2017-08-04 16:48:33 -05:00
|
|
|
user = request.user
|
|
|
|
if user:
|
|
|
|
user.record_event(enum.USER_EVENT_LOGOUT)
|
2017-03-27 21:37:45 -05:00
|
|
|
request.session.delete()
|
|
|
|
request.session.invalidate()
|
|
|
|
headers = forget(request)
|
|
|
|
return headers
|
|
|
|
|
|
|
|
|
2017-02-21 13:12:23 -06:00
|
|
|
def session_timeout_for_user(user):
|
2017-02-11 17:08:27 -06:00
|
|
|
"""
|
2017-02-21 13:12:23 -06:00
|
|
|
Returns the "max" session timeout for the user, according to roles
|
2017-02-11 17:08:27 -06:00
|
|
|
"""
|
2017-05-26 12:44:06 -05:00
|
|
|
from rattail.db.auth import authenticated_role
|
|
|
|
|
2017-02-21 13:12:23 -06:00
|
|
|
roles = user.roles + [authenticated_role(Session())]
|
|
|
|
timeouts = [role.session_timeout for role in roles
|
|
|
|
if role.session_timeout is not None]
|
|
|
|
if timeouts and 0 not in timeouts:
|
|
|
|
return max(timeouts)
|
2017-02-11 17:08:27 -06:00
|
|
|
|
|
|
|
|
|
|
|
def set_session_timeout(request, timeout):
|
|
|
|
"""
|
|
|
|
Set the server-side session timeout to the given value.
|
|
|
|
"""
|
|
|
|
request.session['_timeout'] = timeout or None
|
|
|
|
|
|
|
|
|
2013-09-01 17:31:50 -05:00
|
|
|
@implementer(IAuthorizationPolicy)
|
|
|
|
class TailboneAuthorizationPolicy(object):
|
|
|
|
|
|
|
|
def permits(self, context, principals, permission):
|
2021-10-14 20:42:16 -05:00
|
|
|
config = context.request.rattail_config
|
|
|
|
model = config.get_model()
|
|
|
|
app = config.get_app()
|
|
|
|
auth = app.get_auth_handler()
|
|
|
|
|
2013-09-01 17:31:50 -05:00
|
|
|
for userid in principals:
|
|
|
|
if userid not in (Everyone, Authenticated):
|
2016-10-18 16:59:38 -05:00
|
|
|
if context.request.user and context.request.user.uuid == userid:
|
|
|
|
return context.request.has_perm(permission)
|
|
|
|
else:
|
2020-09-20 16:32:44 -05:00
|
|
|
# this is pretty rare, but can happen in dev after
|
|
|
|
# re-creating the database, which means new user uuids.
|
|
|
|
# TODO: the odds of this query returning a user in that
|
|
|
|
# case, are probably nil, and we should just skip this bit?
|
2016-10-18 16:59:38 -05:00
|
|
|
user = Session.query(model.User).get(userid)
|
|
|
|
if user:
|
2021-10-14 09:39:54 -05:00
|
|
|
if auth.has_permission(Session(), user, permission):
|
2016-10-18 16:59:38 -05:00
|
|
|
return True
|
2013-09-01 17:31:50 -05:00
|
|
|
if Everyone in principals:
|
2021-10-14 20:42:16 -05:00
|
|
|
return auth.has_permission(Session(), None, permission)
|
2013-09-01 17:31:50 -05:00
|
|
|
return False
|
|
|
|
|
|
|
|
def principals_allowed_by_permission(self, context, permission):
|
|
|
|
raise NotImplementedError
|
2015-08-11 17:26:04 -05:00
|
|
|
|
|
|
|
|
2016-01-19 17:29:19 -06:00
|
|
|
def add_permission_group(config, key, label=None, overwrite=True):
|
2015-08-11 17:26:04 -05:00
|
|
|
"""
|
|
|
|
Add a permission group to the app configuration.
|
|
|
|
"""
|
|
|
|
def action():
|
|
|
|
perms = config.get_settings().get('tailbone_permissions', {})
|
2016-01-19 17:29:19 -06:00
|
|
|
if key not in perms or overwrite:
|
|
|
|
group = perms.setdefault(key, {'key': key})
|
|
|
|
group['label'] = label or prettify(key)
|
2015-08-11 17:26:04 -05:00
|
|
|
config.add_settings({'tailbone_permissions': perms})
|
|
|
|
config.action(None, action)
|
|
|
|
|
|
|
|
|
|
|
|
def add_permission(config, groupkey, key, label=None):
|
|
|
|
"""
|
|
|
|
Add a permission to the app configuration.
|
|
|
|
"""
|
|
|
|
def action():
|
|
|
|
perms = config.get_settings().get('tailbone_permissions', {})
|
|
|
|
group = perms.setdefault(groupkey, {'key': groupkey})
|
|
|
|
group.setdefault('label', prettify(groupkey))
|
|
|
|
perm = group.setdefault('perms', {}).setdefault(key, {'key': key})
|
|
|
|
perm['label'] = label or prettify(key)
|
|
|
|
config.add_settings({'tailbone_permissions': perms})
|
|
|
|
config.action(None, action)
|