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

« back to all changes in this revision

Viewing changes to .pc/git-2012.1~e2~20111201.1077.patch/horizon/horizon/users.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2011-12-09 16:18:55 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20111209161855-nguyenpghx2o2lqy
Tags: 2012.1~e2~20111209.1104-0ubuntu1
* New upstream release.
* Refreshed patches.
* debian/docs: Removed README

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright 2011 United States Government as represented by the
4
 
# Administrator of the National Aeronautics and Space Administration.
5
 
# All Rights Reserved.
6
 
#
7
 
# Copyright 2011 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
 
 
25
 
def get_user_from_request(request):
26
 
    """ Checks the current session and returns a :class:`~horizon.users.User`.
27
 
 
28
 
    If the session contains user data the User will be treated as
29
 
    authenticated and the :class:`~horizon.users.User` will have all
30
 
    its attributes set.
31
 
 
32
 
    If not, the :class:`~horizon.users.User` will have no attributes set.
33
 
 
34
 
    If the session contains invalid data,
35
 
    :exc:`~horizon.exceptions.NotAuthorized` will be raised.
36
 
    """
37
 
    if 'user' not in request.session:
38
 
        return User()
39
 
    try:
40
 
        return User(token=request.session['token'],
41
 
                    user=request.session['user'],
42
 
                    tenant_id=request.session['tenant_id'],
43
 
                    tenant_name=request.session['tenant'],
44
 
                    service_catalog=request.session['serviceCatalog'],
45
 
                    roles=request.session['roles'])
46
 
    except KeyError:
47
 
        # If any of those keys are missing from the session it is
48
 
        # overwhelmingly likely that we're dealing with an outdated session.
49
 
        request.session.clear()
50
 
        raise exceptions.NotAuthorized(_("Your session has expired. "
51
 
                                         "Please log in again."))
52
 
 
53
 
 
54
 
class LazyUser(object):
55
 
    def __get__(self, request, obj_type=None):
56
 
        if not hasattr(request, '_cached_user'):
57
 
            request._cached_user = get_user_from_request(request)
58
 
        return request._cached_user
59
 
 
60
 
 
61
 
class User(object):
62
 
    """ The main user class which Horizon expects.
63
 
 
64
 
    .. attribute:: token
65
 
 
66
 
        The id of the Keystone token associated with the current user/tenant.
67
 
 
68
 
    .. attribute:: username
69
 
 
70
 
        The name of the current user.
71
 
 
72
 
    .. attribute:: tenant_id
73
 
 
74
 
        The id of the Keystone tenant for the current user/token.
75
 
 
76
 
    .. attribute:: tenant_name
77
 
 
78
 
        The name of the Keystone tenant for the current user/token.
79
 
 
80
 
    .. attribute:: service_catalog
81
 
 
82
 
        The ``ServiceCatalog`` data returned by Keystone.
83
 
 
84
 
    .. attribute:: roles
85
 
 
86
 
        A list of dictionaries containing role names and ids as returned
87
 
        by Keystone.
88
 
 
89
 
    .. attribute:: admin
90
 
 
91
 
        Boolean value indicating whether or not this user has admin
92
 
        privileges. Internally mapped to :meth:`horizon.users.User.is_admin`.
93
 
    """
94
 
    def __init__(self, token=None, user=None, tenant_id=None,
95
 
                    service_catalog=None, tenant_name=None, roles=None):
96
 
        self.token = token
97
 
        self.username = user
98
 
        self.tenant_id = tenant_id
99
 
        self.tenant_name = tenant_name
100
 
        self.service_catalog = service_catalog
101
 
        self.roles = roles or []
102
 
 
103
 
    def is_authenticated(self):
104
 
        """
105
 
        Evaluates whether this :class:`.User` instance has been authenticated.
106
 
        Returns ``True`` or ``False``.
107
 
        """
108
 
        # TODO: deal with token expiration
109
 
        return self.token
110
 
 
111
 
    @property
112
 
    def admin(self):
113
 
        return self.is_admin()
114
 
 
115
 
    def is_admin(self):
116
 
        """
117
 
        Evaluates whether this user has admin privileges. Returns
118
 
        ``True`` or ``False``.
119
 
        """
120
 
        for role in self.roles:
121
 
            if role['name'].lower() == 'admin':
122
 
                return True
123
 
        return False