def filterFields(context, form_fields): omit_names = [] if IAlchemistContent.providedBy(context): md = utils.get_descriptor(context.__class__) for field in form_fields: # field:zope.formlib.form.FormField try: can_write = security.canWrite(context, field.__name__) can_read = security.canAccess(context, field.__name__) except AttributeError: log.warn('filterFields: item [%s] has no field named "%s"', context, field.__name__) can_write = can_read = False if can_write: continue if can_read: field.for_display = True field.custom_widget = md.get(field.__name__).view_widget else: omit_names.append(field.__name__) elif not IAlchemistContainer.providedBy(context): ctx = getattr(context, "context", None) if ctx: filterFields(ctx, form_fields) else: raise NotImplementedError return form_fields.omit(*omit_names)
def check(*args, **kwargs): component = lookup(*args, **kwargs) if component is not None: if canAccess(component, '__call__'): return removeSecurityProxy(component) else: interaction = getInteraction() principal = interaction.participations[0].principal if principal is unauthenticated_principal: raise exceptions.HTTPUnauthorized(component) else: raise exceptions.HTTPForbidden(component) return None
def available(self): """See zope.app.publisher.interfaces.browser.IBrowserMenuItem""" # Make sure we have the permission needed to access the menu's action if self.permission is not None: # If we have an explicit permission, check that we # can access it. if not checkPermission(self.permission, self.context): return False elif self.action != u'': # Otherwise, test access by attempting access path = self.action l = self.action.find('?') if l >= 0: path = self.action[:l] traverser = PublicationTraverser() try: view = traverser.traverseRelativeURL(self.request, self.context, path) except (Unauthorized, Forbidden, LookupError): return False else: # we're assuming that view pages are callable # this is a pretty sound assumption if not canAccess(view, '__call__'): return False # Make sure that we really want to see this menu item if self.filter is not None: try: include = self.filter( Engine.getContext( context=self.context, nothing=None, request=self.request, modules=sys.modules, )) except Unauthorized: return False else: if not include: return False return True
def available(self): """See zope.app.publisher.interfaces.browser.IBrowserMenuItem""" # Make sure we have the permission needed to access the menu's action if self.permission is not None: # If we have an explicit permission, check that we # can access it. if not checkPermission(self.permission, self.context): return False elif self.action != u'': # Otherwise, test access by attempting access path = self.action l = self.action.find('?') if l >= 0: path = self.action[:l] traverser = PublicationTraverser() try: view = traverser.traverseRelativeURL( self.request, self.context, path) except (Unauthorized, Forbidden, LookupError): return False else: # we're assuming that view pages are callable # this is a pretty sound assumption if not canAccess(view, '__call__'): return False # Make sure that we really want to see this menu item if self.filter is not None: try: include = self.filter(Engine.getContext( context = self.context, nothing = None, request = self.request, modules = sys.modules, )) except Unauthorized: return False else: if not include: return False return True
def getContextObjList(self, preList=None, postList=None): """ get an Object list of all interesting objects in the context """ retList = [] if preList is not None: retList.extend(preList) try: parentObj = zapi.getParent(self.context) if parentObj is not None and canAccess(parentObj, '__len__'): retList.append((None, None, parentObj)) except Exception: print "111e" import traceback print traceback.format_exc() if postList is not None: retList.extend(postList) return retList
def _canAccessSiteManager(self): try: # the ++etc++ namespace is public this means we get the sitemanager # without permissions. But this does not mean we can access it # Right now we check the __getitem__ method on the sitemamanger # but this means we don't show the ++etc++site link if we have # registered views on the sitemanager which have other permission # then the __getitem__ method form the interface IReadContainer # in the LocalSiteManager. # If this will be a problem in the future, we can add a # attribute to the SiteManager which we can give individual # permissions and check it via canAccess. sitemanager = self.context.getSiteManager() authorized = canAccess(sitemanager, '__getitem__') return bool(authorized) except zope.interface.interfaces.ComponentLookupError: return False except TypeError: # pragma: no cover # we can't check unproxied objects, but unproxied objects # are public. return True
def filterFields(context, form_fields): omit_names = [] if IAlchemistContent.providedBy(context): md = queryModelDescriptor(context.__class__) for field in form_fields: try: can_write = security.canWrite( context, field.__name__) can_read = security.canAccess( context, field.__name__) except AttributeError: can_write = can_read = False if can_write: continue if can_read: field.for_display = True field.custom_widget = md.get(field.__name__).view_widget else: omit_names.append(field.__name__) elif not IAlchemistContainer.providedBy(context): ctx=getattr(context, 'context', None) if ctx: filterFields(ctx, form_fields) else: raise NotImplementedError return form_fields.omit(*omit_names)
def filterFields(context, form_fields): omit_names = [] if IAlchemistContent.providedBy(context): md = queryModelDescriptor(context.__class__) for field in form_fields: try: can_write = security.canWrite(context, field.__name__) can_read = security.canAccess(context, field.__name__) except AttributeError: can_write = can_read = False if can_write: continue if can_read: field.for_display = True field.custom_widget = md.get(field.__name__).view_widget else: omit_names.append(field.__name__) elif not IAlchemistContainer.providedBy(context): ctx = getattr(context, "context", None) if ctx: filterFields(ctx, form_fields) else: raise NotImplementedError return form_fields.omit(*omit_names)
def _canAccessSiteManager(self): try: # the ++etc++ namespace is public this means we get the sitemanager # without permissions. But this does not mean we can access it # Right now we check the __getitem__ method on the sitemamanger # but this means we don't show the ++etc++site link if we have # registred views on the sitemanager which have other permission # then the __getitem__ method form the interface IReadContainer # in the LocalSiteManager. # If this will be a problem in the future, we can add a # attribute to the SiteManager which we can give individual # permissions and check it via canAccess. sitemanager = self.context.getSiteManager() authorized = canAccess(sitemanager, '__getitem__') if authorized: return True else: return False except ComponentLookupError: return False except TypeError: # we can't check unproxied objects, but unproxied objects # are public. return True
def setUpEditWidgets(view, schema, source=None, prefix=None, ignoreStickyValues=False, names=None, context=None, degradeInput=False, degradeDisplay=False): """Sets up widgets to collect input on a view. See `setUpWidgets` for details on `view`, `schema`, `prefix`, `ignoreStickyValues`, `names`, and `context`. `source`, if specified, is an object from which initial widget values are read. If source is not specified, the view context is used as the source. `degradeInput` is a flag that changes the behavior when a user does not have permission to edit a field in the names. By default, the function raises Unauthorized. If degradeInput is True, the field is changed to an IDisplayWidget. `degradeDisplay` is a flag that changes the behavior when a user does not have permission to access a field in the names. By default, the function raises Unauthorized. If degradeDisplay is True, the field is removed from the form. Returns a list of names, equal to or a subset of the names that were supposed to be drawn, with uninitialized undrawn fields missing. """ if context is None: context = view.context if source is None: source = view.context security_proxied = isProxy(source, Proxy) res_names = [] for name, field in _fieldlist(names, schema): try: value = field.get(source) except ForbiddenAttribute: raise except AttributeError: value = no_value except Unauthorized: if degradeDisplay: continue else: raise if field.readonly: viewType = IDisplayWidget else: if security_proxied: is_accessor = IMethod.providedBy(field) if is_accessor: set_name = field.writer.__name__ authorized = security.canAccess(source, set_name) else: set_name = name authorized = security.canWrite(source, name) if not authorized: if degradeInput: viewType = IDisplayWidget else: raise Unauthorized(set_name) else: viewType = IInputWidget else: # if object is not security proxied, might be a standard # adapter without a registered checker. If the feature of # paying attention to the users ability to actually set a # field is decided to be a must-have for the form machinery, # then we ought to change this case to have a deprecation # warning. viewType = IInputWidget setUpWidget(view, name, field, viewType, value, prefix, ignoreStickyValues, context) res_names.append(name) return res_names
def update(self): pagelets = getAdapters((self.context, self.request), IControlPagelet) self.pagelets = [v for k, v in pagelets if canAccess(v, '__call__')] self.pagelets.sort(key=lambda x: x.weight)
def can_access(self): """See pyams_utils.interfaces.form.IDataManager""" context = self.adapted_context if isinstance(context, Proxy): return canAccess(context, self.field.__name__) return True
def update(self): pagelets = getAdapters((self.context, self.request), IControlPagelet) self.pagelets = [v for k,v in pagelets if canAccess(v, '__call__')] self.pagelets.sort(key = lambda x: x.weight)
raise except AttributeError, v: value = no_value except Unauthorized: if degradeDisplay: continue else: raise if field.readonly: viewType = IDisplayWidget else: if security_proxied: is_accessor = IMethod.providedBy(field) if is_accessor: set_name = field.writer.__name__ authorized = security.canAccess(source, set_name) else: set_name = name authorized = security.canWrite(source, name) if not authorized: if degradeInput: viewType = IDisplayWidget else: raise Unauthorized(set_name) else: viewType = IInputWidget else: # if object is not security proxied, might be a standard # adapter without a registered checker. If the feature of # paying attention to the users ability to actually set a # field is decided to be a must-have for the form machinery,