summaryrefslogtreecommitdiffstats
path: root/nova/context.py
blob: f5d3fed08cf46372efdc2e9b47aadb3d63b8b570 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""
RequestContext: context for requests that persist through all of nova.
"""

import datetime
import random

from nova import exception
from nova import utils

class RequestContext(object):
    def __init__(self, user, project, is_admin=None, read_deleted=False,
                 remote_address=None, timestamp=None, request_id=None):
        if hasattr(user, 'id'):
            self._user = user
            self.user_id = user.id
        else:
            self._user = None
            self.user_id = user
        if hasattr(project, 'id'):
            self._project = project
            self.project_id = project.id
        else:
            self._project = None
            self.project_id = project
        if is_admin is None:
            if self.user_id and self.user:
                self.is_admin = self.user.is_admin()
            else:
                self.is_admin = False
        else:
            self.is_admin = is_admin
        self.read_deleted = read_deleted
        self.remote_address = remote_address
        if not timestamp:
            timestamp = datetime.datetime.utcnow()
        if isinstance(timestamp, str) or isinstance(timestamp, unicode):
            timestamp = utils.parse_isotime(timestamp)
        self.timestamp = timestamp
        if not request_id:
            request_id = ''.join(
                    [random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-')
                     for x in xrange(20)]
                    )
        self.request_id = request_id

    @property
    def user(self):
        # NOTE(vish): Delay import of manager, so that we can import this
        #             file from manager.
        from nova.auth import manager
        if not self._user:
            try:
                self._user = manager.AuthManager().get_user(self.user_id)
            except exception.NotFound:
                pass
        return self._user

    @property
    def project(self):
        # NOTE(vish): Delay import of manager, so that we can import this
        #             file from manager.
        from nova.auth import manager
        if not self._project:
            try:
                self._project = manager.AuthManager().get_project(self.project_id)
            except exception.NotFound:
                pass
        return self._project

    def to_dict(self):
        return {'user': self.user_id,
                'project': self.project_id,
                'is_admin': self.is_admin,
                'read_deleted': self.read_deleted,
                'remote_address': self.remote_address,
                'timestamp': utils.isotime(self.timestamp),
                'request_id': self.request_id}

    @classmethod
    def from_dict(cls, values):
        return cls(**values)

    def elevated(self, read_deleted=False):
        """Return a version of this context with admin flag set"""
        return RequestContext(self.user_id,
                              self.project_id,
                              True,
                              read_deleted,
                              self.remote_address,
                              self.timestamp,
                              self.request_id)


def get_admin_context(read_deleted=False):
    return RequestContext(None, None, True, read_deleted)