Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for check if adding a portal type is allowed #400

Open
jensens opened this issue Mar 20, 2018 · 0 comments
Open

Support for check if adding a portal type is allowed #400

jensens opened this issue Mar 20, 2018 · 0 comments

Comments

@jensens
Copy link
Member

jensens commented Mar 20, 2018

I really often have the use case to check if the creation of a portal_type is allowed for a specific context.

Just checking for Add portal content is not enough. First allowedContentTypes (taking constraintypes into account) need to be checked and also checks for custom add permissions (stored at the FTI) are needed. The FTI has a method for this.

A sample code snippet doing this looks like so (I am sure it needs some polishing):

    def can_create(self, context, portal_type=None):
        """check if creation/construction is allowed. 
        if no type is given check for general add permission
        """
        allowed_ftis_iterator = getattr(context, 'allowedContentTypes', None)
        if not allowed_ftis_iterator:
            # context is not a valid container
            return False
        if portal_type is None:
            # generic if no explicit type was given
            return api.user.has_permission('Add portal content', obj=context)

        allowed_types = [fti.getId() for fti in allowed_ftis_iterator()]
        if portal_type not in allowed_types:
            return False

        types_tool = api.portal.get_tool('portal_types')
        type_info = types_tool.getTypeInfo(portal_type)
        return bool(type_info.isConstructionAllowed(context))

I propose to add such a check either to plone.api.user or plone.api.content and enhance the function to take a user or username argument.

If there are enough thumbs up for this I will implement and test it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant