~ubuntu-branches/ubuntu/precise/horizon/precise-updates

« back to all changes in this revision

Viewing changes to horizon/horizon/users.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-03-02 12:11:59 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20120302121159-65b88lcl4slve26i
Tags: 2012.1~e4-0ubuntu1
* New upstream version.
* debian/rules: Update due to upstream build changes.
* debian/control: Update standards-version.
* debian/patches/openstack-config-settings.patch: Dropped
* debian/patches/fix-dashboard-django-wsgi.patch: Refreshed
* debian/patches/fix-dashboard-manage.patch: Refreshed
* debian/openstack-dashboard.install: Update due to upstream build changes.
* debian/dashboard: Update to upstream build changes.
* debian/pydist-overrides: Dont try to install python-django-nose-selenium.
* debian/openstack-dashboard.install: Add missing config files.
* debian/rules: Fix broken settings.py
* debian/patches/pkg-setup.patch: Copy missing templates, shameously
  taken from debian
* debian/patches/fix-broken-tarbll.patch: Add missing files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright 2012 United States Government as represented by the
4
 
# Administrator of the National Aeronautics and Space Administration.
5
 
# All Rights Reserved.
6
 
#
7
 
# Copyright 2012 Nebula, Inc.
8
 
#
9
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
10
 
#    not use this file except in compliance with the License. You may obtain
11
 
#    a copy of the License at
12
 
#
13
 
#         http://www.apache.org/licenses/LICENSE-2.0
14
 
#
15
 
#    Unless required by applicable law or agreed to in writing, software
16
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18
 
#    License for the specific language governing permissions and limitations
19
 
#    under the License.
20
 
"""
21
 
Classes and methods related to user handling in Horizon.
22
 
"""
23
 
 
24
 
import logging
25
 
 
26
 
from django.utils.translation import ugettext as _
27
 
 
28
 
from horizon import exceptions
29
 
 
30
 
 
31
 
LOG = logging.getLogger(__name__)
32
 
 
33
 
 
34
 
def get_user_from_request(request):
35
 
    """ Checks the current session and returns a :class:`~horizon.users.User`.
36
 
 
37
 
    If the session contains user data the User will be treated as
38
 
    authenticated and the :class:`~horizon.users.User` will have all
39
 
    its attributes set.
40
 
 
41
 
    If not, the :class:`~horizon.users.User` will have no attributes set.
42
 
 
43
 
    If the session contains invalid data,
44
 
    :exc:`~horizon.exceptions.NotAuthorized` will be raised.
45
 
    """
46
 
    if 'user_id' not in request.session:
47
 
        return User()
48
 
    try:
49
 
        return User(id=request.session['user_id'],
50
 
                    token=request.session['token'],
51
 
                    user=request.session['user_name'],
52
 
                    tenant_id=request.session['tenant_id'],
53
 
                    tenant_name=request.session['tenant'],
54
 
                    service_catalog=request.session['serviceCatalog'],
55
 
                    roles=request.session['roles'])
56
 
    except KeyError:
57
 
        # If any of those keys are missing from the session it is
58
 
        # overwhelmingly likely that we're dealing with an outdated session.
59
 
        LOG.exception("Error while creating User from session.")
60
 
        request.session.clear()
61
 
        raise exceptions.NotAuthorized(_("Your session has expired. "
62
 
                                         "Please log in again."))
63
 
 
64
 
 
65
 
class LazyUser(object):
66
 
    def __get__(self, request, obj_type=None):
67
 
        if not hasattr(request, '_cached_user'):
68
 
            request._cached_user = get_user_from_request(request)
69
 
        return request._cached_user
70
 
 
71
 
 
72
 
class User(object):
73
 
    """ The main user class which Horizon expects.
74
 
 
75
 
    .. attribute:: token
76
 
 
77
 
        The id of the Keystone token associated with the current user/tenant.
78
 
 
79
 
    .. attribute:: username
80
 
 
81
 
        The name of the current user.
82
 
 
83
 
    .. attribute:: tenant_id
84
 
 
85
 
        The id of the Keystone tenant for the current user/token.
86
 
 
87
 
    .. attribute:: tenant_name
88
 
 
89
 
        The name of the Keystone tenant for the current user/token.
90
 
 
91
 
    .. attribute:: service_catalog
92
 
 
93
 
        The ``ServiceCatalog`` data returned by Keystone.
94
 
 
95
 
    .. attribute:: roles
96
 
 
97
 
        A list of dictionaries containing role names and ids as returned
98
 
        by Keystone.
99
 
 
100
 
    .. attribute:: admin
101
 
 
102
 
        Boolean value indicating whether or not this user has admin
103
 
        privileges. Internally mapped to :meth:`horizon.users.User.is_admin`.
104
 
    """
105
 
    def __init__(self, id=None, token=None, user=None, tenant_id=None,
106
 
                    service_catalog=None, tenant_name=None, roles=None,
107
 
                    authorized_tenants=None):
108
 
        self.id = id
109
 
        self.token = token
110
 
        self.username = user
111
 
        self.tenant_id = tenant_id
112
 
        self.tenant_name = tenant_name
113
 
        self.service_catalog = service_catalog
114
 
        self.roles = roles or []
115
 
        self.authorized_tenants = authorized_tenants
116
 
 
117
 
    def is_authenticated(self):
118
 
        """
119
 
        Evaluates whether this :class:`.User` instance has been authenticated.
120
 
        Returns ``True`` or ``False``.
121
 
        """
122
 
        # TODO: deal with token expiration
123
 
        return self.token
124
 
 
125
 
    @property
126
 
    def admin(self):
127
 
        return self.is_admin()
128
 
 
129
 
    def is_admin(self):
130
 
        """
131
 
        Evaluates whether this user has admin privileges. Returns
132
 
        ``True`` or ``False``.
133
 
        """
134
 
        for role in self.roles:
135
 
            if role['name'].lower() == 'admin':
136
 
                return True
137
 
        return False
138
 
 
139
 
    def get_and_delete_messages(self):
140
 
        """
141
 
        Placeholder function for parity with
142
 
        ``django.contrib.auth.models.User``.
143
 
        """
144
 
        return []