add escalator frame
[escalator.git] / api / escalator / context.py
1 # Copyright 2011-2014 OpenStack Foundation
2 # All Rights Reserved.
3 #
4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
5 #    not use this file except in compliance with the License. You may obtain
6 #    a copy of the License at
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #    Unless required by applicable law or agreed to in writing, software
11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 #    License for the specific language governing permissions and limitations
14 #    under the License.
15
16 from oslo_context import context
17
18 from escalator.api import policy
19
20
21 class RequestContext(context.RequestContext):
22     """Stores information about the security context.
23
24     Stores how the user accesses the system, as well as additional request
25     information.
26
27     """
28
29     def __init__(self, roles=None,
30                  owner_is_tenant=True, service_catalog=None,
31                  policy_enforcer=None, **kwargs):
32         super(RequestContext, self).__init__(**kwargs)
33         self.roles = roles or []
34         self.owner_is_tenant = owner_is_tenant
35         self.service_catalog = service_catalog
36         self.policy_enforcer = policy_enforcer or policy.Enforcer()
37         if not self.is_admin:
38             self.is_admin = self.policy_enforcer.check_is_admin(self)
39
40     def to_dict(self):
41         d = super(RequestContext, self).to_dict()
42         d.update({
43             'roles': self.roles,
44             'service_catalog': self.service_catalog,
45         })
46         return d
47
48     @classmethod
49     def from_dict(cls, values):
50         return cls(**values)
51
52     @property
53     def owner(self):
54         """Return the owner to correlate with an image."""
55         return self.tenant if self.owner_is_tenant else self.user
56
57     @property
58     def can_see_deleted(self):
59         """Admins can see deleted by default"""
60         return self.show_deleted or self.is_admin