1 ### -*- coding: utf-8 -*- #################################################### |
|
2 ############################################################################## |
|
3 # |
|
4 # Copyright (c) 2008 Thierry Florac <tflorac AT ulthar.net> |
|
5 # All Rights Reserved. |
|
6 # |
|
7 # This software is subject to the provisions of the Zope Public License, |
|
8 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
9 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
10 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
11 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
12 # FOR A PARTICULAR PURPOSE. |
|
13 # |
|
14 ############################################################################## |
|
15 |
|
16 |
|
17 # import standard packages |
|
18 from persistent.list import PersistentList |
|
19 from persistent.dict import PersistentDict |
|
20 |
|
21 # import Zope3 interfaces |
|
22 from zope.authentication.interfaces import IAuthentication |
|
23 from zope.pluggableauth.interfaces import IPrincipalInfo |
|
24 |
|
25 # import local interfaces |
|
26 |
|
27 # import Zope3 packages |
|
28 from zc.set import Set |
|
29 from zope.component import getUtility |
|
30 from zope.deprecation.deprecation import deprecate |
|
31 from zope.i18n import translate |
|
32 from zope.interface import implements |
|
33 from zope.security.proxy import removeSecurityProxy |
|
34 |
|
35 # import local packages |
|
36 from ztfy.utils.request import getRequest |
|
37 |
|
38 from ztfy.utils import _ |
|
39 |
|
40 |
|
41 def unproxied(value): |
|
42 """Remove security proxies from given value ; if value is a list or dict, all elements are unproxied""" |
|
43 if isinstance(value, (list, PersistentList)): |
|
44 result = [] |
|
45 for v in value: |
|
46 result.append(unproxied(v)) |
|
47 elif isinstance(value, (dict, PersistentDict)): |
|
48 result = value.copy() |
|
49 for v in value: |
|
50 result[v] = unproxied(value[v]) |
|
51 elif isinstance(value, (set, Set)): |
|
52 result = [] |
|
53 for v in value: |
|
54 result.append(unproxied(v)) |
|
55 else: |
|
56 result = removeSecurityProxy(value) |
|
57 return result |
|
58 |
|
59 |
|
60 @deprecate("ztfy.utils.security.MissingPrincipal is deprecated. Use ztfy.security.search.MissingPrincipal class instead.") |
|
61 class MissingPrincipal(object): |
|
62 |
|
63 implements(IPrincipalInfo) |
|
64 |
|
65 def __init__(self, id): |
|
66 self.id = id |
|
67 self.request = getRequest() |
|
68 |
|
69 @property |
|
70 def title(self): |
|
71 return translate(_("< missing principal %s >"), context=self.request) % self.id |
|
72 |
|
73 @property |
|
74 def description(self): |
|
75 return translate(_("This principal can't be found in any authentication utility..."), context=self.request) |
|
76 |
|
77 |
|
78 @deprecate("ztfy.utils.security.getPrincipal is deprecated. Use ztfy.security.search.getPrincipal function instead.") |
|
79 def getPrincipal(uid): |
|
80 principals = getUtility(IAuthentication) |
|
81 try: |
|
82 return principals.getPrincipal(uid) |
|
83 except: |
|
84 return MissingPrincipal(uid) |
|